<?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: SpecialoffersController.php, v 1.0 2019/05/17 
//
class OfferController extends Controller {
    public function indexAction() {
        $ObjOffer    	= 	new OfferModel();         
        $languageIDs	=	$this->ObjView->languageIDs;
        $offergroupID    =	$this->getRequestValue("id");
        $ObjEmailtemplate       =       new EmailtemplateModel();
        
        if($offergroupID>0) {                 
                $this->showAction();
                exit;
        }
        else{
                $isAjax =   isset($_POST['isAjax']) && $_POST['isAjax']=='true' ? 'yes' : 'no';
                $sortBy = '';
                if($sortBy=='')
                    $sortBy='sort_priority';
                else
                     $sortBy=$sortBy;
				if($this->ObjCommon->_platform=='mobile'  && $this->ObjCommon->_amp_section=='amp'){
					$params =array(
							'languageIDs'=>$languageIDs,
							'status'=>'A',
							'sortby'=>$sortBy,
							'orderby'=>'asc',
                                                        'frontend'=>'Yes',
							'offerTypeID'   => '',
							'offerGroupID'   => '',
                                                        'brandID'=>$this->ObjCommon->brandID
					);
                                        
					$listOffer  =  $ObjOffer->listOffer($params);	
						
				} else {
					list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page', $rows = 12); 
					$params =array(
							'limitFrom'=>$limitFrom,
							'limitTo'=>$limitTo,
							'languageIDs'=>$languageIDs,
							'status'=>'A',
							'sortby'=>$sortBy,
							'orderby'=>'asc',
							'offerTypeID'   => '',
							'offerGroupID'   => '',
                                                        'frontend'=>'Yes',
                                                        'brandID'=>$this->ObjCommon->brandID
					);
					$listOffer    = $ObjOffer->listOffer($params);	
					$resPagesCount = $this->setPagesCount($key = 'page');
				}
                $this->ObjView->assign('offerPageCount', $resPagesCount);
                for($v=0;$v<count($listOffer);$v++){
                     $sefoffer_idArray[] = $listOffer[$v]['offerID'];
                }
                $this->ObjCommon->setCommonSefPath(array('offer'),$sefoffer_idArray);
                   
		$this->ObjView->assign('isAjax', $isAjax);
		$this->ObjView->assign('listOffer', $listOffer);	                
		
        }        
        $this->ObjView->display('Specialoffers.tpl');
    }
    public function detailsAction() {
      
        $this->ObjView->display('SpecialofferDetails.tpl');
    }
    public function showAction(){
        $ObjOffer               = 	new OfferModel();        
        $ShowroomsModel         =       new ShowroomsModel();
        $languageIDs		=	$this->ObjView->languageIDs;
        $ObjEmailtemplate       =       new EmailtemplateModel();
        $ObjOtpCountry                       = new OtpCountryModel();
        $ObjUrl                 =       new UrlrewriteModel();
        $languageIDs['Content'] =       $languageIDs['Current'];
        $offerID                =	$this->getRequestValue("id");
        $userID			= 	$this->ObjSession->getSession("REGISTERED_USER_ID");
        
        $settingsDateFormat     	= 	$this->getSettingValue('site_settings', 'date_format');
        $settingsTimeFormat    		= 	$this->getSettingValue('site_settings', 'time_format');
        
        $params=array(
                'offerID'=>$offerID,
                'languageIDs'=>$languageIDs,
                'brandID'=>$this->ObjCommon->brandID
        );
        $getOffer	= $ObjOffer->getOffer($params);        
        if(empty($getOffer) || empty($offerID)){
            $this->setRedirect('/');
            exit;
        }
        $urlparams      =   array();
        $urlparams['language_ids']   = $languageIDs;
        $urlparams['objectID']      = $getOffer['offerID'];
        $urlparams['type']          = 'offer';
        $urldet         =   $ObjUrl->getRewriteUrl($urlparams); 
		/* if($languageIDs['Current']=='3'){
			$findusArr[0][0] = 'خدمة الرسائل القصيرة (SMS)';
			$findusArr[0][1] = 'خدمة الرسائل القصيرة (SMS)';
			$findusArr[1][0] = 'البريد الإلكتروني';
			$findusArr[1][1] = 'البريد الإلكتروني';
			$findusArr[2][0] = 'الفعاليات';
			$findusArr[2][1] = 'الفعاليات';
			$findusArr[3][0] = 'مجلات أو جرائد';
			$findusArr[3][1] = 'مجلات أو جرائد';
			$findusArr[4][0] = 'إعلانات خارجية';
			$findusArr[4][1] = 'إعلانات خارجية';
			$findusArr[5][0] = 'راديو';
			$findusArr[5][1] = 'راديو';
			$findusArr[6][0] = 'محرك البحث';
			$findusArr[6][1] = 'محرك البحث';
			$findusArr[7][0] = 'وسائل التواصل الاجتماعي';
			$findusArr[7][1] = 'وسائل التواصل الاجتماعي';
			$findusArr[8][0] = 'موقع الكتروني';
			$findusArr[8][1] = 'موقع الكتروني';
			$findusArr[9][0] = 'شفهياً';
			$findusArr[9][1] = 'شفهياً';
			$findusArr[10][0] = 'وسيلة أخرى';
			$findusArr[10][1] = 'وسيلة أخرى';
        }
		else{
			$findusArr[0][0] = 'SMS';
			$findusArr[0][1] = 'SMS';
			$findusArr[1][0] = 'Email';
			$findusArr[1][1] = 'Email';
			$findusArr[2][0] = 'Event';
			$findusArr[2][1] = 'Event';
			$findusArr[3][0] = 'Magazine or Newspaper';
			$findusArr[3][1] = 'Magazine or Newspaper';
			$findusArr[4][0] = 'Outdoor Advert';
			$findusArr[4][1] = 'Outdoor Advert';
			$findusArr[5][0] = 'Radio';
			$findusArr[5][1] = 'Radio';
			$findusArr[6][0] = 'Search Engine';
			$findusArr[6][1] = 'Search Engine';
			$findusArr[7][0] = 'Social Media';
			$findusArr[7][1] = 'Social Media';
			$findusArr[8][0] = 'Website';
			$findusArr[8][1] = 'Website';
			$findusArr[9][0] = 'Word of Mouth';
			$findusArr[9][1] = 'Word of Mouth';
			$findusArr[10][0] = 'Other';
			$findusArr[10][1] = 'Other';
		} */
		if($languageIDs['Current']=='3'){
			$findusArr[0][0] = 'فيسبوك ';
			$findusArr[0][1] = 'فيسبوك ';
			$findusArr[1][0] = ' انستغرام';
            $findusArr[1][1] = ' انستغرام ';
			$findusArr[2][0] = ' تويتر ';
			$findusArr[2][1] = ' تويتر';
			$findusArr[3][0] = ' سناب شات ';
			$findusArr[3][1] = ' سناب شات ';
			$findusArr[4][0] = ' يوتيوب ';
			$findusArr[4][1] = ' يوتيوب ';
			$findusArr[5][0] = ' لينكد إن ';
			$findusArr[5][1] = ' لينكد إن ';
			$findusArr[6][0] = ' لافتات على الإنترنت ';
			$findusArr[6][1] = ' لافتات على الإنترنت ';
			$findusArr[7][0] = ' البحث عبر الإنترنت ';
			$findusArr[7][1] = ' البحث عبر الإنترنت ';
			$findusArr[8][0] = ' البريد الإلكتروني ';
			$findusArr[8][1] = ' البريد الإلكتروني ';
			$findusArr[9][0] = ' رسالة قصيرة ';
			$findusArr[9][1] = ' رسالة قصيرة ';
			$findusArr[10][0] = ' راديو ';
			$findusArr[10][1] = ' راديو ';
			$findusArr[11][0]= ' وسيلة أخرى ';
			$findusArr[11][1]= ' وسيلة أخرى ';
        }
		else{
            $findusArr[0][0] = 'Facebook';
            $findusArr[0][1] = 'Facebook';
            $findusArr[1][0] = 'Instagram';
            $findusArr[1][1] = 'Instagram';
            $findusArr[2][0] = 'Twitter';
            $findusArr[2][1] = 'Twitter';
            $findusArr[3][0] = 'Snapchat';
            $findusArr[3][1] = 'Snapchat';
			$findusArr[4][0] = 'Youtube';
            $findusArr[4][1] = 'Youtube';
            $findusArr[5][0] = 'LinkedIn';
            $findusArr[5][1] = 'LinkedIn';
            $findusArr[6][0] = 'Online Banners';
            $findusArr[6][1] = 'Online Banners';
            $findusArr[7][0] = 'Online Search';
            $findusArr[7][1] = 'Online Search';
            $findusArr[8][0] = 'Email';
            $findusArr[8][1] = 'Email';
            $findusArr[9][0] = 'SMS';
            $findusArr[9][1] = 'SMS';
            $findusArr[10][0] = 'Radio';
            $findusArr[10][1] = 'Radio';
            $findusArr[11][0]= 'Other';
            $findusArr[11][1]= 'Other';
		}
         if($this->ObjForm->isValid('formOffer','submitForm')){
            $_POST['offerID'] =  $getOffer['offerID'];
            $_POST['brandID'] = $this->ObjCommon->brandID;
            $insert =$ObjOffer->saveSubmission($languageIDs,$_POST);
            if($insert){
                
                $languageIDs['Content']=$languageIDs['Current'];
                $params       = array(
                   'showroomID' => $_POST['showroomID'],
                   'languageIDs' => $languageIDs,
                    'brandID'=>$this->ObjCommon->brandID
                   );
                 $getShowroom = $ShowroomsModel->getShowroom($params);
                 $emailbody              = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'offer-notification-email-user');
                    $from                   = array(
                        $emailbody['emailFromEmail'],
                        $emailbody['emailFromName']
                    );
                    $subject                = $emailbody['emailSubject'];
                    $userName               = $_POST['name'];
                    //email body
                    $bodytag                = str_replace('{$username}', $userName, $emailbody['emailHtml']);
                    $bodytag                = str_replace('{$offerTitle}', $getOffer['offerTitle'], $bodytag);
                     $bodytag                = str_replace('{$showroom}', $getShowroom['name'], $bodytag);
                     $bodytag                = str_replace('{$showroomAddress}', $getShowroom['address'], $bodytag);
                    //$bodytag                = str_replace('{$showroomPhone}', $getShowroom['phone'], $bodytag);
					$phone 					 = stripslashes(preg_replace('/\s+/', '', $getShowroom['phone']));
					$bodytag                = str_replace('{$showroomPhone}', '<a href="tel:'.$phone.'">'.$phone.'</a>', $bodytag);
                    $bodytag                = str_replace('{$showroomEmail}', $getShowroom['email'], $bodytag);
                    // $bodytag                = str_replace('{$showroomLatitude}', $getShowroom['latitude'], $bodytag);
                    // $bodytag                = str_replace('{$showroomLongitude}', $getShowroom['longitude'], $bodytag);
					$bodytag                = str_replace('{$mapLink}', $getShowroom['mapLink'], $bodytag);
                    $this->ObjView->assign('emailbody', $bodytag);
                    $body                          = $this->ObjCommon->fetch("email_templates/Layout.tpl", $languageIDs);
                    $mailSettings                  = $this->getSettingValue('mail_settings');
                    $mailSettings['smtp_password'] = $this->encryptDecrypt("decrypt", $mailSettings['smtp_password']);
                    $ObjMail                       = new Mail($mailSettings);
                    $to        =  trim(addslashes($_POST['emailID']));
                    $ObjMail->clearAddresses();
                    $ObjMail->IsHTML();
                    $ObjMail->sendMail($from, $to, $subject, $body);
                    if ($ObjMail->Send()) {
                    }
                
                    $emailbody              = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'offer-notification-email-admin');
                    $from                   = array(
                        $emailbody['emailFromEmail'],
                        $emailbody['emailFromName']
                    );
                    $subject                = $emailbody['emailSubject'];
                    $userName               = $_POST['name']; 
                    
                    $emailID                = $this->getSettingValue('site_settings', 'global_notification_email');
                    //email body
                    $bodytag                = str_replace('{$offerTitle}', $getOffer['offerTitle'], $emailbody['emailHtml']);
                    $bodytag                = str_replace('{$showroom}', $getShowroom['name'], $bodytag);
                     $bodytag                = str_replace('{$showroomAddress}', $getShowroom['address'], $bodytag);
                    $bodytag                = str_replace('{$showroomPhone}', $getShowroom['phone'], $bodytag);
                    $bodytag                = str_replace('{$showroomEmail}', $getShowroom['email'], $bodytag);
                    $bodytag                = str_replace('{$showroomLatitude}', $getShowroom['latitude'], $bodytag);
                    $bodytag                = str_replace('{$showroomLongitude}', $getShowroom['longitude'], $bodytag);
                    $bodytag                = str_replace('{$userName}', $userName, $bodytag);                    
                    $bodytag                = str_replace('{$userPhone}', "+".$_POST['countrycode']." ".$_POST['phone'], $bodytag);
                    $bodytag                = str_replace('{$userEmail}', $_POST['emailID'], $bodytag);                    
                    $bodytag                = str_replace('{$findus}', $_POST['findus'], $bodytag);
                   
                    $this->ObjView->assign('emailbody', $bodytag);
                    $body                          = $this->ObjCommon->fetch("email_templates/Layout.tpl", $languageIDs);
                    $mailSettings                  = $this->getSettingValue('mail_settings');
                    $mailSettings['smtp_password'] = $this->encryptDecrypt("decrypt", $mailSettings['smtp_password']);
                    $ObjMail                       = new Mail($mailSettings);
                    $emailID        =  trim(addslashes($emailID));
                    $ObjMail->clearAddresses();
                    $ObjMail->IsHTML();
                    $ObjMail->sendMail($from, $emailID, $subject, $body);
                    if ($ObjMail->Send()) {
                    }
                    //$this->setMessage("language.lblThankYouForOfferSubmission", "success");
                    //$redurl = (!empty($urldet['requestPath']))  ?$urldet['requestPath']:'/offer/';
                    $this->setRedirect('/offer-thank-you.html');
                    exit;
            }
        }
        $sefoffer_idArray[] = $getOffer['offerID'];
        $paramsdealer        = array(
            'limitFrom' => 0,
            'limitTo' => '',
            'languageIDs' => $languageIDs,
            'searchName' => '',
            'searchEmail' => '',
			'brandID'=>$this->ObjCommon->brandID,
            'status' => 'A'
        );
       
            
        $this->ObjCommon->setCommonSefPath(array('offer'),$sefoffer_idArray);
        $listShowrooms = $ShowroomsModel->testdriveShowrooms($paramsdealer);
        $this->ObjView->assign('resOfferDetails', $getOffer);	
        $this->ObjView->assign('findusArr', $findusArr);
        $this->ObjView->assign('listShowrooms', $listShowrooms);
        $this->ObjView->assign('settingsDateFormat',$settingsDateFormat);
        $this->ObjView->assign('settingsTimeFormat',$settingsTimeFormat); 
        //otpcountry list
        $languageIDs['Content']=$languageIDs['Current'];
        $resOTPCountry  =   $ObjOtpCountry->listOtpCountryOptions(array('status'=>'A','languageIDs'=>$languageIDs));
        $this->ObjView->assign('resCountry', $resOTPCountry);
        $this->ObjView->display('SpecialofferDetails.tpl');
    }
     public function ajaxgetofferslistAction(){
            $ObjOffer               = 	new OfferModel();           
            $languageIDs            =	$this->ObjView->languageIDs;
            $languageIDs['Content'] =   $languageIDs['Current'];            
            $sortBy                 =	$this->getRequestValue("sort");
            
            $page                                 =	$this->getRequestValue("page");
            $sortBy='sort_priority';
            $orderBy='asc';
            
            list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page', $rows = 12); 
            $params =array(
                        'limitFrom'=>$limitFrom,
                        'limitTo'=>$limitTo,
                        'languageIDs'=>$languageIDs,
                        'status'=>'A',
                        'sortby'=>$sortBy,
                        'orderby'=>$orderBy,
                        'offerTypeID'   => '',
                        'offerGroupID'   => '',
                        'frontend'=>'Yes',
                        'brandID'=>$this->ObjCommon->brandID
                );
                $listOffer    = $ObjOffer->listOffer($params);	
                $resPagesCount = $this->setPagesCount($key = 'page');
                $this->ObjView->assign('offerPageCount', $resPagesCount);
                for($v=0;$v<count($listOffer);$v++){
                         $sefoffer_idArray[] = $listOffer[$v]['offerID'];
                }
                $this->ObjCommon->setCommonSefPath(array('offer'),$sefoffer_idArray);
                $this->ObjView->assign('offerPageCount', $resPagesCount);
                $this->ObjView->assign('pageID', $page);
                $this->ObjView->assign('listOffer',$listOffer);         
            
                $settingsDateFormat     	= 	$this->getSettingValue('site_settings', 'date_format');
                $settingsTimeFormat    		= 	$this->getSettingValue('site_settings', 'time_format');
                $this->ObjView->assign('settingsDateFormat', $settingsDateFormat);
                $this->ObjView->assign('settingsTimeFormat', $settingsTimeFormat);
                $this->ObjView->display('OfferListing.tpl');
            
        }
}