<?php
QUnit_Global::includeClass('QUnit_UI_TemplatePage');
class Affiliate_Affiliates_Views_Communications extends QUnit_UI_TemplatePage {

function Affiliate_Affiliates_Views_Communications() {
    $this->navigationAddURL(L_G_HOME,'index.php?md=home');
    $this->navigationAddURL(L_G_SYSTOOLS, 'javascript:;');
    $this->navigationAddURL(L_G_COMMUNICATION,'index.php?md=Affiliate_Merchants_Views_Communications');
}

//--------------------------------------------------------------------------

function process(){
    $this->addContent('communications_style');
    $this->addContent('communications_js');
    if(isset($_GET['id']) && !empty($_GET['id'])) {
        $this->drawCommunicationsDetail();
    }else {
        $this->drawCommunicationsList();
    }
}

//--------------------------------------------------------------------------

function drawCommunicationsList() {
    $limit = (int) $_GET['limit'];
    $limit = empty($limit)?30:$limit;

    $query = 'select count(1) as "total" from `wd_g_messagestousers` where `userid` = "%s" or `fromuserid`="%s"';
    $query = sprintf($query,$GLOBALS['Auth']->getUserID(),$GLOBALS['Auth']->getUserID());
    $rs = $GLOBALS['db']->Query($query);
    $total = $rs->fields['total'];
    $totalpage = ceil($total/$limit);
    $totalpage = $totalpage<1?1:$totalpage;

    $page = (int) $_GET['page'];
    $page = $page<1?1:($page>$totalpage?$totalpage:$page);

    $start = ($page-1)*$limit;
    $this->assign('page',$page);
    $this->assign('totalpage',$totalpage);
    $this->assign('total',$total);
    $this->assign('limit',$limit);

    $query = 'select m.*,u.name as "touser",u2.name as "fromuser" from `wd_g_messages` m join `wd_g_messagestousers` m2u on m.messageid=m2u.messageid left join `wd_g_users` u on m2u.userid=u.userid left join `wd_g_users` u2 on m2u.fromuserid=u2.userid where m.`deleted`="0" and (m2u.`userid` = "'.$GLOBALS['Auth']->getUserID().'" or m2u.`fromuserid`="'.$GLOBALS['Auth']->getUserID().'") order by m.`dateinserted` desc limit '.$start.','.$limit;
    $rs = $GLOBALS['db']->Query($query);
    if(!$rs || $rs->EOF) $this->assign('listdata',array());
    $this->assign('listdata',$rs);
    $this->addContent('communications');
}

function drawCommunicationsDetail() {
    $query = 'select m.*,u.name as "touser",u2.name as "fromuser" from `wd_g_messages` m join `wd_g_messagestousers` m2u on m.messageid=m2u.messageid left join `wd_g_users` u on m2u.userid=u.userid left join `wd_g_users` u2 on m2u.fromuserid=u2.userid where m.messageid="'.$_GET['id'].'" and m.`deleted`="0" and (m2u.`userid` = "'.$GLOBALS['Auth']->getUserID().'" or m2u.`fromuserid`="'.$GLOBALS['Auth']->getUserID().'")';
    $rs = $GLOBALS['db']->Query($query);
    if(!$rs || $rs->EOF) $this->assign('listdata',array());
    $this->assign('listdata',$rs->fields);
    $this->addContent('communications_details');
}

}

?>