<?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: UserController.php, v 1.0 2011/06/14 20:52:11
//
class UserController extends Controller {
    //UserController index action		
    public function indexAction() {
        $businessID = $this->ObjSession->getSession("BUSINESS_USER_ID");
        if (!$businessID) {
            $this->setRedirect('/login/'); 
            //To redirect 
        }
        $languageIDs      = $this->ObjView->languageIDs;
        $ObjCountry       = new CountryModel();
        $ObjBusiness      = new BusinessModel();
        $ObjEmailtemplate = new EmailtemplateModel();
        $ObjSign          = new SignatureModel();
        $resLocs          = $ObjBusiness->listBusinessLocations('', '', '', $businessID, $languageIDs, '', '', 'Yes');
        $this->ObjView->assign('resLocs', $resLocs);
        $resBusiness = $ObjBusiness->getBusinessDetails($businessID, $languageIDs);
        $this->ObjView->assign('businessProfile1', $resBusiness['businessProfile']);
        // Signature details
        $resAgreement = $ObjSign->getSignature($businessID, 'business');
        $this->ObjView->assign('resAgreement', $resAgreement);
        $this->ObjView->assignValues($resBusiness);
        $this->ObjView->display('MyAccount.tpl');
    }
    //Function for business Account action
    public function editAction() {
        $businessID     = $this->ObjSession->getSession("BUSINESS_USER_ID");
        $businessUserID = $this->ObjSession->getSession("BUSINESS_SUB_USER_ID");
        if (!$businessID) {
            $this->setRedirect('/login/');
            //To redirect 
        }
        $ObjLanguage      = new LanguageModel();
        $ObjFilter        = new Filter();
        $languageIDs      = $this->ObjView->languageIDs;
        $ObjCountry       = new CountryModel();
        $ObjBusiness      = new BusinessModel();
        $ObjEmailtemplate = new EmailtemplateModel();
        $ObjBusinessType  = new BusinessTypeModel();
        $ObjImage         = new ImageModel();
	$ObjUser          = new UserModel(); 
          $resCountryCode= $ObjCountry->getAllCountryCode($languageIDs);
        $this->ObjView->assign('resCountryCode', $resCountryCode);
        if ($businessUserID) {
            $businessUser   = $ObjBusiness->getBusinessUser($businessUserID);
            $arrayLocations = $ObjBusiness->listBusinessLocations('', '', 'Active', $this->ObjSession->getSession("BUSINESS_USER_ID"), $languageIDs, '', '', 'Yes');
            $resLocs        = $ObjBusiness->listBusinessUserLocations($languageIDs, $businessUserID, 'Active', 'Yes');
            $this->ObjView->assign("userLocs", $resLocs);
            foreach ($arrayLocations as $location) {
                $locations[] = array(
                    $location['businessLocID'],
                    $location['location'] . '<br/>'
                );
            }
            $this->ObjView->assign("businessLocations", $locations);
            $arrLocations = explode(",", $businessUser['locName']);
            $this->ObjView->assign("arrLocations", $arrLocations);
            if ($this->ObjForm->isValid('formBusUserAdd', 'submitBusinessUser')) {
                $_POST['userProfile'] = 'Yes';
                $businessLocations    = $ObjBusiness->saveBusinessUser($_POST);
                $this->setMessage("language.lblUserUpdated", "success");
                $this->setRedirect('user/index/');
            }
            $this->ObjView->assign("businessUserID", $businessUser['businessUserID']);
            $this->ObjView->assign("userProfile", 1);
            $this->ObjView->assign("businessUserFirstName", $businessUser['businessUserFirstName']);
            $this->ObjView->assign("businessUserLastName", $businessUser['businessUserLastName']);
            $this->ObjView->assign("businessUserEmail", $businessUser['businessUserEmail']);
            $this->ObjView->assign("businessUserUserName", $businessUser['businessUserUserName']);
            $this->ObjView->assign("businessUserStatus", $businessUser['businessUserStatus']);
            $this->ObjView->assign("business_user_in", $this->ObjSession->getSession("BUSINESS_SUB_USER_ID"));
            $this->ObjView->display('BusinessUserAdd.tpl');
        } else {
            $resBusiness = $ObjBusiness->getBusinessDetails($businessID, $languageIDs);
           if(count($resBusiness)>0){
            $phone=explode("-",$resBusiness['businessPhone']);
            $userCountrycode=$phone[0];
            $resBusiness['businessPhone']=$phone[1];
        }

         $this->ObjView->assign("userCountrycode", isset($userCountrycode)?$userCountrycode:'');
            $this->ObjView->assignValues($resBusiness);
            $this->ObjView->assign('businessLocID', $resBusiness['businessLocID']);
            if ($resBusiness['locCity'] != '') {
                $cityValue = $resBusiness['locCity'];
            } elseif ($resBusiness['locProvince'] != '') {
                $cityValue = $resBusiness['locProvince'];
            }
            if ($resBusiness['locStateID'] > 0)
            {
                $stateValue = $resBusiness['locStateID'];
            }
            elseif ($resBusiness['locProvince'] != '')
            {
                $stateValue = $resBusiness['locProvince'];
            }
            
            $defaultOption = ($resBusiness['businessTypeID'] != '') ? $resBusiness['businessTypeID'] : '';
            $resListBusinessType   = $ObjBusinessType->getBusinessTypeOption($languageIDs, '', $defaultOption);
            $this->ObjView->assign('resListBusinessType', $resListBusinessType);
            
            /* $businessLocList =   $ObjBusiness->listBusinessLocations('','','',$businessID,$languageIDs,'','','Yes');*/
            $resListCountry = $ObjCountry->listCountries($languageIDs, "Yes");
            $businessLocID  = $this->getRequestValue('locid');
            $global_url     = $this->getSettingValue('site_settings', 'global_http_url');
            /* list($limitFrom,$limitTo)   =   $this->getPageSettings($key='review-page',$rows=10); 
            $params['languageIDs']		=	$languageIDs;	
            $params['limitFrom']		=	$limitFrom;
            $params['limitTo']			=	$limitTo;
            $params['businessID']		=	$businessID;*/
            /* $resBusinessReviews			=   $ObjBusiness->listBusinessRatingsAndReviews($params);
            $resPagesCount   			=	$this->setPagesCount($key='review-page'); 
            $resBusinessRatings   		=   $ObjBusiness->selectUserRating($businessID);
            if($businessLocID) {
            $businessLocDetails  =   $ObjBusiness->getBusinessLocationDetails('',$businessLocID,$languageIDs);
            $this->ObjView->assignValues($businessLocDetails);
            $locationstateSelectedID=$businessLocDetails['locStateID'];
            $this->ObjView->assign('locationstateSelectedID',$locationstateSelectedID);
            $this->ObjView->assign('businessLocID',$businessLocID);
            }*/
            if ($this->ObjForm->isValid('formBusinessProfile', 'submitProfile')) {
                $newPassword   = trim($_POST['businessPassword']);
                $cnfrmPassword = trim($_POST['businessPasswordnew']);
                if ((trim($_POST['businessPassword']) != '') && (trim($_POST['businessPassword']) == trim($_POST['businessPasswordnew'])) && (trim($_POST['businessPasswordnew']) != '')) {
                    $_POST['businessPassword'] = $this->generateHash(trim($_POST['businessPasswordnew']));
                } else {
                    $_POST['businessPassword'] = $resBusiness['businessPassword'];
                }
                //validating form
                $_POST['languageIDs']     = $languageIDs;
                $logoImageWidth           = $this->getSettingValue('product_cart_settings', 'logo_image_width');
                $logoImageHeight          = $this->getSettingValue('product_cart_settings', 'logo_image_height');
                //to get settings value
                $_POST['logoImageWidth']  = $logoImageWidth;
                $_POST['logoImageHeight'] = $logoImageHeight;
                /*unlink old image*/
                if (!empty($_POST["business_logo_temp"])) {
                    $imagePath = __UPLOADS_FOLDER . 'business/' . trim($resBusiness['businessLogo']);
                    $ObjImage->unlinkImage($imagePath);
                    $imagePath = __UPLOADS_FOLDER . 'business/thumb/' . trim($resBusiness['businessLogo']);
                    $ObjImage->unlinkImage($imagePath);
                }
                /*unlink old image*/
                $businessIDRet     = $ObjBusiness->saveBusiness($_POST);
                $resbusinessLangID = $ObjBusiness->getBusinessLanguageID($businessID);
                $ObjCookie         = $ObjFilter->getCookieInstance();
                $ObjCookie->setCookie($ObjLanguage->getSection() . '_CurrentLanguageID', $_POST['businessDefaultLanguageID']);
                //send mail to business user				
                if ($businessIDRet > 0) {
                    $this->ObjSession->setSession("BUSINESS_NAME", $_POST['businessName']);
                    $businessUserName = $_POST['businessCFirstName'] . '&nbsp;' . $_POST['businessCLastName'];
                    $this->ObjSession->setSession("BUSINESS_USER_NAME", $businessUserName);
                    $this->ObjSession->setSession("BUSINESS_LANG_ID", $_POST['businessDefaultLanguageID']);
                    if ($newPassword != "" && $cnfrmPassword != '') {
                        $languageIDs['Current'] = $resbusinessLangID;
                        $emailbody              = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'user-password-changed');
                        $from                   = array(
                            $emailbody['emailFromEmail'],
                            $emailbody['emailFromName']
                        );
                        $siteName               = $this->getSettingValue('site_settings', 'site_name');
                        $siteUrl                = $this->getSettingValue('site_settings', 'global_http_url');
                        $companyLogo            = $this->getSettingValue('company_information', 'company_logo');
                        $userName               = $_POST['businessCFirstName'] . " " . $_POST['businessCLastName'];
                        $subject                = $emailbody['emailSubject'];
                        $bodytag                = str_replace('{$siteName}', $siteName, $emailbody['emailHtml']);
                        $bodytag                = str_replace('{$userName}', $userName, $bodytag);
                        $bodytag                = str_replace('{$userPassword}', $newPassword, $bodytag);
                        $bodytag                = str_replace('{$companyLogo}', $companyLogo, $bodytag);
                        $bodytag                = str_replace('{url}', rtrim($siteUrl, "/") . '/business/', $bodytag);
                        $this->ObjView->assign('emailbody', $bodytag);
                        $this->ObjView->assign('siteUrl', $siteUrl);
                        $this->ObjView->assign('siteName', $siteName);
                        $this->ObjView->assign('userName', $userName);
                        $this->ObjView->assign('companyLogo', $companyLogo);
                        $body                           = $this->ObjCommon->fetch($this->ObjView, "email_templates/Layout.tpl");
                        $mail_settings                  = $this->getSettingValue('mail_settings');
                        $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
                        $ObjMail                        = new Mail($mail_settings);
                        $emailID                        = trim(addslashes($_POST['businessEmail']));
                        $ObjMail->IsHTML();
                        $ObjMail->sendMail($from, $emailID, $subject, $body);
                        if ($ObjMail->Send()) {
                        }
                    }
                    if ($newPassword != "" && $cnfrmPassword != '') {
                        $this->setMessage("language.msgBusinessUpdatePasswordCase", "message_success");
                    } else {
                        $this->setMessage("language.msgUpdateBusinessProfile", "message_success");
                    }
                    if($_POST['profileStatus']=='Level1')
                    {
                        $this->setRedirect("/user/edit/");
                    }
                    $this->setRedirect("/user/index/");
                }
            }
            if (isset($_POST['submitProfile'])) {
                $_POST['citySelectedID'] = $_POST['locCity'];
                $this->ObjView->assign($_POST['citySelectedID']);
            }
            //--------GOOGLE MAP-----------	
            $ObjCommon                 = new CommonModel();
            $settings                  = array();
            $settings['mapContainer']  = 'map_canvas';
            $settings['zoom']          = 14;
            $settings['latLng']        = array(
                $resBusiness['locLatitude'],
                $resBusiness['locLongitude']
            );
            $settings['latLngTextBox'] = array(
                'frmLat',
                'frmLon'
            );
            $settings['address']       = array(
                'address1' => 'locAddress1',
                'address2' => 'locAddress2',
                'city' => 'locCity',
                'state' => 'locStateID',
                'province' => 'locProvince',
                'country' => 'locCountryID'
            );
            $settings['changeTags']    = array(
                'locAddress1',
                'locAddress2',
                'locCity',
                'locProvince',
                'locCountryID'
            );
            $settings['isclickable']   = 'true';
            $settings['title']         = "";
            $settings['info']          = "";
            $ObjCommon->addMarker($settings);
            $this->ObjView->assign("map", $ObjCommon->showmap());
            //--------END OF GOOGLE MAP----
            $this->ObjView->assign('global_url', $global_url);
            if (isset($stateValue)) {
            $this->ObjView->assign('stateValue', isset($stateValue)?$stateValue:'');
        $this->ObjView->assign('statehidValue', isset($stateValue)?$stateValue:0);
        }
            $this->ObjView->assign('businessProfile', $resBusiness['businessProfile']);
            /* 				$this->ObjView->assign('resBusinessRatings', $resBusinessRatings);
            $this->ObjView->assign('resBusinessReviews', $resBusinessReviews);
            $this->ObjView->assign('businessLocDetails',$businessLocList); */
            $this->ObjView->assign('businessPassword', "");
            $this->ObjView->assign('businessID', $businessID);
            $this->ObjView->assign('resListCountry', $resListCountry);
            $this->ObjView->assign('tab', 'profile');
            $resListCountryCode        = $ObjUser->loadAllCountryCode($languageIDs); 
            $this->ObjView->assign('resListCountryCode', $resListCountryCode);	            
            $this->ObjView->display('MyAccountEdit.tpl');
        }
    }
    //End of function	
    //Function for logout action
    public function logoutAction() {
        $this->ObjSession->unsetSession("BUSINESS_USER_ID");
        $this->ObjSession->unsetSession("BUSINESS_SUB_USER_ID");
        $this->ObjSession->unsetSession("BUSINESS_RDEEM_LOCS");
        $this->ObjSession->unsetSession("BUSINESS_NAME");
        $this->ObjSession->unsetSession("BUSINESS_USER_NAME");
        $this->ObjSession->unsetSession("TIMEOUT");
        $this->ObjSession->unsetSession("BUSINESS_LANG_ID");
        $this->ObjSession->unsetSession($this->ObjView->_section . '_ContentLanguageID');
        $this->ObjCookie->setCookie($this->ObjView->_section . '_CurrentLanguageID', '');
        $this->setMessage("language.msglogout", "message_success", "User", 'logout_message');
        $this->setRedirect("login/");
    }
    //End of function
    public function businessuserAction() {
        $ObjBusiness = new BusinessModel();
        $languageIDs = $this->ObjView->languageIDs;
        //Start of Status Update
        if ($_POST['businessUserStatus']) {
            if (sizeof($_POST['checkBusinessUserID']) > 0) {
                $businessUsers = array();
                foreach ($_POST['checkBusinessUserID'] as $businessUserID) {
                    array_push($businessUsers, $businessUserID);
                }
                $updateBusinessUser = $ObjBusiness->updateBusinessUserStatus($businessUsers, $_POST['businessUserStatus']);
                if ($_POST['businessUserStatus'] == 'Deleted')
                    $this->setMessage("language.lblUserDeleted", "success");
                else
                    $this->setMessage("language.lblBusinessUserStatusChanged", "success");
            }
            $this->setRedirect('user/businessuser/');
        }
        //END of status UPDATE
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resBusinessUserDetails = $ObjBusiness->listBusinessUsers($languageIDs, $limitFrom, $limitTo, $this->ObjSession->getSession("BUSINESS_USER_ID"));
        $resPagesCount          = $this->setPagesCount($key = 'page');
        $this->ObjView->assign("resBusinessUserDetails", $resBusinessUserDetails);
        $this->ObjView->display('BusinessUsers.tpl');
    }
    //FUNCTION for Add New User in Business Interface
    public function newuserAction() {
        $ObjBusiness         = new BusinessModel();
        $languageIDs         = $this->ObjView->languageIDs;
        $ObjEmailtemplate    = new EmailtemplateModel();
        $resBusinessLocation = $ObjBusiness->listBusinessLocations('', '', 'Active', $this->ObjSession->getSession("BUSINESS_USER_ID"), $languageIDs, '', '', 'Yes');
        foreach ($resBusinessLocation as $location) {
            $locations[] = array(
                $location['businessLocID'],
                $location['location'] . '<br/>'
            );
        }
        $this->ObjView->assign("businessLocations", $locations);
        if ($this->ObjForm->isValid('formBusUserAdd', 'submitBusinessUser')) {
            $_POST['businessID']     = $this->ObjSession->getSession("BUSINESS_USER_ID");
            $_POST['businessUserID'] = 0;
            $businessLocations       = $ObjBusiness->saveBusinessUser($_POST);
            $resBusiness             = $ObjBusiness->getBusinessDetails($this->ObjSession->getSession("BUSINESS_USER_ID"), $languageIDs);
            /*mailing user*/
            $languageIDs['Current']  = $ObjBusiness->getBusinessLanguageID($_POST['businessID']);
            $emailbody               = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'business-user-added');
            $from                    = array(
                $emailbody['emailFromEmail'],
                $emailbody['emailFromName']
            );
            $siteName                = $this->getSettingValue('site_settings', 'site_name');
            $siteUrl                 = $this->getSettingValue('site_settings', 'global_http_url');
            $companyLogo             = $this->getSettingValue('company_information', 'company_logo');
            $userName                = $_POST['businessUserFirstName'] . " " . $_POST['businessUserLastName'];
            $subject                 = $emailbody['emailSubject'];
            $bodytag                 = $emailbody['emailHtml'];
            $bodytag                 = str_replace('{$siteName}', $siteName, $bodytag);
            $bodytag                 = str_replace('{$userName}', $userName, $bodytag);
            $bodytag                 = str_replace('{$businessLogin}', $_POST['businessUserUserName'], $bodytag);
            $bodytag                 = str_replace('{$businessPassword}', $_POST['businessUserPassword'], $bodytag);
            $bodytag                 = str_replace('{$businessName}', $resBusiness['businessName'], $bodytag);
            $bodytag                 = str_replace('{$companyLogo}', $companyLogo, $bodytag);
            $bodytag                 = str_replace('{url}', rtrim($siteUrl, "/") . '/business/', $bodytag);
            $this->ObjView->assign('emailbody', $bodytag);
            $this->ObjView->assign('siteUrl', $siteUrl);
            $this->ObjView->assign('siteName', $siteName);
            $this->ObjView->assign('userName', $userName);
            $this->ObjView->assign('companyLogo', $companyLogo);
            $body                           = $this->ObjCommon->fetch($this->ObjView, "email_templates/Layout.tpl", $languageIDs);
            $mail_settings                  = $this->getSettingValue('mail_settings');
            $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
            $ObjMail                        = new Mail($mail_settings);
            $emailID                        = trim(addslashes($_POST['businessUserEmail']));
            $ObjMail->IsHTML();
            $ObjMail->sendMail($from, $emailID, $subject, $body);
            if ($ObjMail->Send()) {
            }
            /*mailing user*/
            $this->setMessage("language.lblUserAdded", "success");
            $this->setRedirect('user/businessuser/');
        }
        $this->ObjView->assign("businessUserID", 0);
        $this->ObjView->assign("businessUserStatus", 'Active');
        $this->ObjView->display('BusinessUserAdd.tpl');
    }
    //END of function addUser
    //FUNCTION for edit the Business user
    public function edituserAction() {
        $ObjBusiness      = new BusinessModel();
        $languageIDs      = $this->ObjView->languageIDs;
        $ObjEmailtemplate = new EmailtemplateModel();
        $businessUser     = $ObjBusiness->getBusinessUser($this->getRequestValue('userid'));
        $dbPassword       = $this->decodeString($businessUser['businessUserPassword']);
        $arrayLocations   = $ObjBusiness->listBusinessLocations('', '', 'Active', $this->ObjSession->getSession("BUSINESS_USER_ID"), $languageIDs, '', '', 'Yes');
        foreach ($arrayLocations as $location) {
            $locations[] = array(
                $location['businessLocID'],
                $location['location'] . '<br/>'
            );
        }
        $this->ObjView->assign("businessLocations", $locations);
        $arrLocations = explode(",", $businessUser['locName']);
        $this->ObjView->assign("arrLocations", $arrLocations);
        if ($this->ObjForm->isValid('formBusUserAdd', 'submitBusinessUser')) {
            $businessLocations = $ObjBusiness->saveBusinessUser($_POST);
            /*mailing new password*/
            if (trim($dbPassword) != trim($_POST['userConfirmPassword']) && trim($_POST['userConfirmPassword']) != '') {
                $languageIDs['Current'] = $businessUser['businessDefaultLanguageID'];
                $emailbody              = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'user-password-changed');
                $from                   = array(
                    $emailbody['emailFromEmail'],
                    $emailbody['emailFromName']
                );
                $siteName               = $this->getSettingValue('site_settings', 'site_name');
                $siteUrl                = $this->getSettingValue('site_settings', 'global_http_url');
                $companyLogo            = $this->getSettingValue('company_information', 'company_logo');
                $userName               = $_POST['businessUserFirstName'] . " " . $_POST['businessUserLastName'];
                $subject                = $emailbody['emailSubject'];
                $bodytag                = str_replace('{$siteName}', $siteName, $emailbody['emailHtml']);
                $bodytag                = str_replace('{$userName}', $userName, $bodytag);
                $bodytag                = str_replace('{$userPassword}', $_POST['businessUserPassword'], $bodytag);
                $bodytag                = str_replace('{$companyLogo}', $companyLogo, $bodytag);
                $bodytag                = str_replace('{url}', rtrim($siteUrl, "/") . '/business/', $bodytag);
                $this->ObjView->assign('emailbody', $bodytag);
                $this->ObjView->assign('siteUrl', $siteUrl);
                $this->ObjView->assign('siteName', $siteName);
                $this->ObjView->assign('userName', $userName);
                $this->ObjView->assign('companyLogo', $companyLogo);
                $body                           = $this->ObjCommon->fetch($this->ObjView, "email_templates/Layout.tpl");
                $mail_settings                  = $this->getSettingValue('mail_settings');
                $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
                $ObjMail                        = new Mail($mail_settings);
                $emailID                        = trim(addslashes($_POST['businessUserEmail']));
                $ObjMail->IsHTML();
                $ObjMail->sendMail($from, $emailID, $subject, $body);
                if ($ObjMail->Send()) {
                }
            }
            /*mailing new password*/
            $this->setMessage("language.lblUserUpdated", "success");
            $this->setRedirect('user/businessuser/');
        }
        $this->ObjView->assign("businessUserID", $businessUser['businessUserID']);
        $this->ObjView->assign("businessUserFirstName", $businessUser['businessUserFirstName']);
        $this->ObjView->assign("businessUserLastName", $businessUser['businessUserLastName']);
        $this->ObjView->assign("businessUserEmail", $businessUser['businessUserEmail']);
        $this->ObjView->assign("businessUserUserName", $businessUser['businessUserUserName']);
        $this->ObjView->assign("businessUserStatus", $businessUser['businessUserStatus']);
        $this->ObjView->display('BusinessUserAdd.tpl');
    }
    //END of Edit Business User
    //FUNCTION to delete Business User
    public function destroyuserAction() {
        $ObjBusiness     = new BusinessModel();
        $userList        = array(
            $this->getRequestValue('userid')
        );
        $delBusinessUser = $ObjBusiness->updateBusinessUserStatus($userList, 'Deleted');
        $this->setMessage("language.lblUserDeleted", "success");
        $this->setRedirect('/user/businessuser/');
    }
    //END of function deleteBusinessUserAction
    //FUNCTION to change Business User status
    public function ajaxchangestatusAction() {
        $ObjBusiness     = new BusinessModel();
        $userList        = array(
            $_POST['userid']
        );
        $delBusinessUser = $ObjBusiness->updateBusinessUserStatus($userList, $_POST['status']);
        $this->setMessage("language.msgUserStatusUpdate", "success");
        if ($delBusinessUser)
            echo 1;
    }
    //END of function deleteBusinessUserAction
    public function showreviewsAction() {
        $businessID = $this->ObjSession->getSession("BUSINESS_USER_ID");
        if (!$businessID) {
            $this->setRedirect('/login/');
        }
        $ObjBusiness      = new BusinessModel();
        $ObjProductReview = new ProductreviewModel();
        $languageIDs      = $this->ObjView->languageIDs;
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'review-page');
        $params['languageIDs'] = $languageIDs;
        $params['limitFrom']   = $limitFrom;
        $params['limitTo']     = $limitTo;
        $params['businessID']  = $businessID;
        $resBusinessReviews    = $ObjBusiness->listBusinessRatingsAndReviews($params);
        $resPagesCount         = $this->setPagesCount($key = 'review-page');
        $resBusinessRatings    = $ObjBusiness->selectUserRating($businessID);
        $resProductReviewCount = $ObjProductReview->getAllProductReviewCount('', $businessID);
        $date_format           = $this->getSettingValue('site_settings', 'date_format');
        $time_format           = $this->getSettingValue('site_settings', 'time_format');
        $this->ObjView->assign('resBusinessRatings', $resBusinessRatings);
        $this->ObjView->assign('resBusinessReviews', $resBusinessReviews);
        $this->ObjView->assign('resProductReviewCount', $resProductReviewCount);
        $this->ObjView->assign('time_format', $time_format);
        $this->ObjView->assign('date_format', $date_format);
        $this->ObjView->display('BusinessReviews.tpl');
    }
    public function messagesAction() {  
        $businessID = $this->ObjSession->getSession("BUSINESS_USER_ID");
        if (!$businessID) {
            $this->setRedirect('/login/');
        }
        $ObjBusiness = new BusinessModel();
        $languageIDs = $this->ObjView->languageIDs;
        $link        = 'user/messages/';
        $sort        = ($this->getRequestValue("sort")) ? $this->getRequestValue("sort") : 'DESC';
        if(strtolower($sort)!='asc' && strtolower($sort)!='desc') {
            $this->setRedirect($link);
        }
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resMessages   = $ObjBusiness->listMyMessages($limitFrom, $limitTo, $businessID, $languageIDs, '', '', '', '', '', '', '', $sort);
        $resPagesCount = $this->setPagesCount($key = 'page');
        //  echo "<pre>";print_r($resMessages);exit;
        if ($this->ObjForm->isValid('formReplySubmit', 'submitReply')) {
            $message = $ObjBusiness->saveMessage($_POST, $languageIDs['Current'], $businessID);
            $this->setMessage("language.msgReplied", "success");
            $this->setRedirect();
        }
        $this->ObjView->assign('sort', $sort);
        $this->ObjView->assign('link', $link);
        $this->ObjView->assign('resMessages', $resMessages);
        $this->ObjView->display('MyMessages.tpl');
    }
    public function showAction() {
        $businessID  = $this->ObjSession->getSession("BUSINESS_USER_ID");
        $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;
        $languageIDs = $this->ObjView->languageIDs;
        $ObjBusiness = new BusinessModel();
        $limitFrom   = isset($limitFrom) ? $limitFrom : '';
        $limitTo     = isset($limitTo) ? $limitTo : '';
        $resMessages = $ObjBusiness->listMyMessages($limitFrom, $limitTo, $businessID, $languageIDs, 'view', $messageID, $id);
        //echo "<pre>"; print_r($resMessages);exit;
        $message     = $ObjBusiness->changeMessageStatus($id, 'Merchant');
        
        //echo "<pre>"; print_r($message);exit;
        $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 customermsgsAction() {
        $businessID = $this->ObjSession->getSession("BUSINESS_USER_ID");
        if (!$businessID) {
            $this->setRedirect('/login/');
        }
        $ObjUser     = new UserModel();
        $languageIDs = $this->ObjView->languageIDs;
        $link        = 'user/customermsgs/';
        $sort        = ($this->getRequestValue("sort")) ? $this->getRequestValue("sort") : 'DESC';
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resMessages           = $ObjUser->listUserMessages($languageIDs, $limitFrom, $limitTo, 'business', $businessID, 0, '', '', '', $sort);
        $resPagesCount         = $this->setPagesCount($key = 'page');
        $settingsMessageReview = $this->getSettingValue('multi_vendor_settings', 'review_message_before_publish');
        //  echo "<pre>";print_r($resMessages);exit;
        //reply submit
        if ($this->ObjForm->isValid('formSubmitMessage', 'submitMessage')) {
            $message = $ObjUser->saveUserMessage($_POST, $languageIDs['Current'], $businessID, $resMessages[0]['userID'], $settingsMessageReview);
            $this->setMessage("language.msgReplied", "success");
            $this->setRedirect();
        }
        $this->ObjView->assign('sort', $sort);
        $this->ObjView->assign('link', $link);
        $this->ObjView->assign('resMessages', $resMessages);
        $this->ObjView->display('CustomerMessages.tpl');
    }
    public function newmessageAction() {
        $businessID = $this->ObjSession->getSession("BUSINESS_USER_ID");
        if (!$businessID) {
            $this->setRedirect('/login/');
        }
        $ObjBusiness = new BusinessModel();
        if ($this->ObjForm->isValid('formSubmitMessage', 'submitMessage')) {
            $_POST['businessID']    = $this->ObjSession->getSession("BUSINESS_USER_ID");
            $languageIDs['Current'] = $ObjBusiness->getBusinessLanguageID($_POST['businessID']);
            $message                = $ObjBusiness->saveMessage($_POST, $languageIDs['Current']);
            echo '<script type="text/javascript">parent.location.reload();parent.$.colorbox.close(); </script>';
            $this->setMessage("language.msgSend", "success");
            exit;
        }
        $this->ObjView->assign('messageType', 'reply');
        $this->ObjView->display('Message.tpl');
    }
    public function customerreplyAction() {
        $ObjUser     = new UserModel();
        $languageIDs = $this->ObjView->languageIDs;
        $businessID  = $this->ObjSession->getSession("BUSINESS_USER_ID");
        $id          = $_POST['ajax_request'][0];
        $originID    = $_POST['ajax_request'][1];
        $messageID   = ($originID > 0) ? $originID : $id;
        if ($messageID > 0) {
            $resMessages = $ObjUser->listUserMessages($languageIDs, '', '', 'business', $businessID, 0, 'list', $messageID, '', '', $id);
        }
        $changetoRead = $ObjUser->changeMessageStatus($id, 'Merchant');
        $this->ObjView->assign('resMessages', $resMessages['mainArr']);
        $this->ObjView->assign('resParents', $resMessages['parentIds']);
        $this->ObjView->assign('showid', $id);
        $this->ObjView->display('MessageReply.tpl');
    }
    public function checkRegistrationNumExist($params)
    {
        $ObjBusiness        = new BusinessModel();
        $value = $params['value'];
        if($value){
            $res=  $ObjBusiness->checkRegistrationNumExist($value);
            if($res){
                    return false;
                }
                else{
                    return true;
                }
        }
    }
}