<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/ //
//$Id: MessagesController.php, v 1.0 2011/06/14 20:52:11
//
class MessagesController extends Controller {
    public function indexAction() {
        $ObjBusiness        = new BusinessModel();
        $languageIDs        = $this->ObjView->languageIDs;
        $settingsDateFormat = $this->getSettingValue("site_settings", "date_format");
        $link               = 'messages/index/';
        $fromDate           = '';
        $toDate             = '';
        if (isset($_POST['submitSearch'])) {
            $dateSearch = $_POST['fromDate'];
            if ($dateSearch) {
                if (strpos($dateSearch, ' - '))
                    list($fromDate, $toDate) = explode(' - ', $dateSearch);
                else
                    list($fromDate, $toDate) = array(
                        $dateSearch,
                        ''
                    );
            }
            $link .= (($fromDate != "") ? "from/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/" : "");
            $link .= (($toDate != "") ? "to/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/" : "");
            if ($_POST['msgSrchtype']) {
                $link .= 'srchtype/' . $this->encodeUrlString(trim($_POST['msgSrchtype'])) . '/';
            }
            if (trim($_POST['busSrchName']) != '') {
                $link .= 'key/' . $this->encodeUrlString(trim($_POST['busSrchName'])) . '/';
            }
            $this->setRedirect($link);
        }
        $fromDate = $this->urlDate($this->getRequestValue("from"), 'get', $settingsDateFormat);
        $toDate   = $this->urlDate($this->getRequestValue("to"), 'get', $settingsDateFormat);
        if ($fromDate && $toDate == '') {
            $toDate = $fromDate;
        }
        $busSeachName = $this->decodeUrlString($this->getRequestValue('key'));
        if ($busSeachName != '') {
            $link .= 'key/' . $busSeachName . '/';
            $this->ObjView->assign('busSrchName', $busSeachName);
        }
        $msgSrchtype = $this->decodeUrlString($this->getRequestValue("srchtype"));
        if ($msgSrchtype != '') {
            $link .= 'srchtype/' . $msgSrchtype . '/';
            $this->ObjView->assign('msgSrchtype', $msgSrchtype);
        }
        /*assigning from and to values for search*/
        if ($fromDate && $toDate == '') {
            $this->ObjView->assign('fromDate', $fromDate);
        } else if ($fromDate && $toDate) {
            $this->ObjView->assign('fromDate', $fromDate . ' - ' . $toDate);
        } else {
            $this->ObjView->assign('fromDate', '');
        }
        /*assigning from and to values for search*/ //SORT LINK        
        if ($fromDate) {
            $link .= "from/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/";
        }
        if ($toDate) {
            $link .= "to/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/";
        }
        $sort = ($this->getRequestValue("sort")) ? $this->getRequestValue("sort") : 'DESC';
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        // echo "<pre>";
        $resMessages   = $ObjBusiness->listMyMessages($limitFrom, $limitTo, 0, $languageIDs, '', '', 0, $busSeachName, $msgSrchtype, $this->convertDateForSQL($fromDate, $settingsDateFormat), $this->convertDateForSQL($toDate, $settingsDateFormat), $sort);
        // print_r($resMessages);
        //exit;
        /*    if($this->ObjForm->isValid('formReplySubmit','submitReply')){                   
        $message	=	$ObjBusiness->saveMessage($_POST,$languageIDs['Current']);                   
        $this->setMessage("language.msgReplied","success");
        $this->setRedirect();// exit;
        }*/
        $resPagesCount = $this->setPagesCount($key = 'page');
        $this->ObjView->assign('link', $link);
        $this->ObjView->assign('sort', $sort);
        $this->ObjView->assign('resMessages', $resMessages);
        $this->ObjView->display('MerchantMessages.tpl');
    }
    public function showAjaxAction() {
        $id          = isset($_POST['ajax_request'][0]) ? $_POST['ajax_request'][0] : '';
        $originID    = isset($_POST['ajax_request'][1]) ? $_POST['ajax_request'][1] : '';
        $messageID   = ($originID > 0) ? $originID : $id;
        $limitFrom   = isset($limitFrom) ? $limitFrom : '';
        $limitTo     = isset($limitTo) ? $limitTo : '';
        $languageIDs = $this->ObjView->languageIDs;
        $ObjBusiness = new BusinessModel();
        $resMessages = $ObjBusiness->listMyMessages($limitFrom, $limitTo, 0, $languageIDs, 'view', $messageID, $id);
        //echo "<pre>"; print_r($resMessages);exit;
        $message     = $ObjBusiness->changeMessageStatus($id, 'Admin');
        $this->ObjView->assign('messageType', 'reply');
        $this->ObjView->assign('showid', $id);
        $this->ObjView->assign('resParents', $resMessages['parentIds']);
        $this->ObjView->assign('resMessages', $resMessages['mainArr']);
        $this->ObjView->display('MessageDetails.tpl');
    }
    public function userAction() {
        $languageIDs        = $this->ObjView->languageIDs;
        $ObjUser            = new UserModel();
        $link               = 'messages/user/';
        $fromDate           = '';
        $toDate             = '';
        $srchParamsArray    = array();
        $settingsDateFormat = $this->getSettingValue("site_settings", "date_format");
        if (isset($_POST['submitSearch'])) {
            $dateSearch = $_POST['fromDate'];
            if ($dateSearch) {
                if (strpos($dateSearch, ' - '))
                    list($fromDate, $toDate) = explode(' - ', $dateSearch);
                else
                    list($fromDate, $toDate) = array(
                        $dateSearch,
                        ''
                    );
            }
            $link .= (($fromDate != "") ? "from/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/" : "");
            $link .= (($toDate != "") ? "to/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/" : "");
            if ($_POST['msgSrchtype']) {
                $link .= 'srchtype/' . $this->encodeUrlString(trim($_POST['msgSrchtype'])) . '/';
            }
            if (trim($_POST['busSrchName']) != '') {
                $link .= 'key/' . $this->encodeUrlString(trim($_POST['busSrchName'])) . '/';
            }
            if (trim($_POST['busSrchProduct']) != '') {
                $link .= 'pdct/' . $this->encodeUrlString(trim($_POST['busSrchProduct'])) . '/';
            }
            if (trim($_POST['srchStatus']) != '') {
                $link .= 'status/' . (trim($_POST['srchStatus'])) . '/';
            }
            $this->setRedirect($link);
        }
        $fromDate = $this->urlDate($this->getRequestValue("from"), 'get', $settingsDateFormat);
        $toDate   = $this->urlDate($this->getRequestValue("to"), 'get', $settingsDateFormat);
        if ($fromDate && $toDate == '') {
            $toDate = $fromDate;
        }
        $srchParamsArray['busName'] = $busSeachName = $this->decodeUrlString($this->getRequestValue('key'));
        $this->ObjView->assign('busSrchName', $busSeachName);
        $srchParamsArray['prodName'] = $busSrchProduct = $this->decodeUrlString($this->getRequestValue('pdct'));
        $this->ObjView->assign('busSrchProduct', $busSrchProduct);
        $srchParamsArray['type'] = $msgSrchtype = $this->decodeUrlString($this->getRequestValue("srchtype"));
        $this->ObjView->assign('msgSrchtype', ucwords($msgSrchtype));
        $srchParamsArray['status'] = $srchStatus = $this->getRequestValue("status");
        $this->ObjView->assign('srchStatus', $srchStatus);
        /*assigning from and to values for search*/
        if ($fromDate && $toDate == '') {
            $this->ObjView->assign('fromDate', $fromDate);
        } else if ($fromDate && $toDate) {
            $this->ObjView->assign('fromDate', $fromDate . ' - ' . $toDate);
        } else {
            $this->ObjView->assign('fromDate', '');
        }
        /*assigning from and to values for search*/
        $srchParamsArray['fromDate'] = $this->convertDateForSQL($fromDate, $settingsDateFormat);
        $srchParamsArray['toDate']   = $this->convertDateForSQL($toDate, $settingsDateFormat);
        if ($busSeachName != '')
            $link .= 'key/' . $busSeachName . '/';
        if ($fromDate) {
            $link .= "from/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/";
        }
        if ($toDate) {
            $link .= "to/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/";
        }
        $sort = ($this->getRequestValue("sort")) ? $this->getRequestValue("sort") : 'DESC';
        if ($msgSrchtype != '')
            $link .= 'srchtype/' . $msgSrchtype . '/';
        if ($srchStatus != '')
            $link .= 'status/' . $srchStatus . '/';
        if ($busSrchProduct != '')
            $link .= 'pdct/' . $busSrchProduct . '/';
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        //  echo "<pre>";
        $resMessages   = $ObjUser->listUserMessages($languageIDs, $limitFrom, $limitTo, 'admin', 0, 0, '', 0, $srchParamsArray, $sort);
        // print_r($resMessages);
        // exit;
        $resPagesCount = $this->setPagesCount($key = 'page');
        if (isset($_POST['bulkSubmit'])) {
            $array_ids = (isset($_POST['bulkSubmit'])) ? $_POST['verify_msgarr'] : array();
            $message   = $ObjUser->verifyMessage($_POST, $array_ids);
            $this->setMessage("language.msgVerified", "success");
            $this->setRedirect(); //exit;
        }
        $this->ObjView->assign('link', $link);
        $this->ObjView->assign('sort', $sort);
        $this->ObjView->assign('resMessages', $resMessages);
        $this->ObjView->display('CustomerMessages.tpl');
    }
    public function verifyAjaxAction() {
        $id          = $_POST['ajax_request'][0];
        $originID    = $_POST['ajax_request'][1];
        $messageID   = ($originID > 0) ? $originID : $id;
        $limitFrom   = isset($limitFrom) ? $limitFrom : '';
        $limitTo     = isset($limitTo) ? $limitTo : '';
        $languageIDs = $this->ObjView->languageIDs;
        $ObjUser     = new UserModel();
        $ObjBusiness = new BusinessModel();
        $resMessages = $ObjUser->listUserMessages($languageIDs, $limitFrom, $limitTo, 'admin', 0, 0, 'list', $messageID, '', '', $id);
        $this->ObjView->assign('messageType', 'verify');
        $this->ObjView->assign('resMessages', $resMessages['mainArr']);
        $this->ObjView->assign('resParents', $resMessages['parentIds']);
        // echo "<pre>";print_r($resMessages);exit;
        $this->ObjView->display('MessageVerify.tpl');
    }
    public function ajaxVerifyUserMessageAction() {
        $_POST['messageID'] = $_POST['ajax_request'][0];
        $_POST['status']    = $_POST['ajax_request'][1];
        if ($_POST['messageID'] > 0) {
            $ObjUser = new UserModel();
            $message = $ObjUser->verifyMessage($_POST);
            echo $_POST['messageID'];
        } else {
            echo '0';
        }
    }
    public function ajaxReplyAction() {
        $ObjBusiness = new BusinessModel();
        $languageIDs = $this->ObjView->languageIDs;
        $message     = $ObjBusiness->saveMessage($_POST, $languageIDs['Current']);
        $this->setMessage("language.msgReplied", "success");
    }
}
