<?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: CurrencyController.php, v 1.0 2011/06/14 20:52:11
//
class EmailtemplateController extends Controller {
    public function indexAction() {
        $languageIDs            = $this->ObjView->languageIDs;
        $ObjEmailtemplate       = new EmailtemplateModel();
        $languageIDs['Current'] = $languageIDs['Content'];
        $currentAction          = $this->getRequestValue('action');
        $emailTempID            = $this->getRequestValue('id');
        $brandID            	= $this->getRequestValue('brandID');
        if ($this->ObjForm->isValid('formEmailTemplate', 'submitEmailTemplate','',false)) {
            $resUpdate = $ObjEmailtemplate->saveEmailTemplate($languageIDs, $_POST, $emailTempID);
            if ($resUpdate) {
                $this->setMessage("language.msgEmailTempUpdated", 3);
                $this->setRedirect("/emailtemplate/index/action/preview/id/$emailTempID/brandID/$brandID/");
            }
        }
		
		$ObjBrand       		= new BrandModel();	
		   $brandParams         = array(
            'languageIDs' 		=> $languageIDs
        );       
        $listBrand      		= $ObjBrand->listBrand($brandParams);  
        $this->ObjView->assign('listBrand', $listBrand);
		$brandID = (!empty($brandID )) ? $brandID : $listBrand[0]['brandID'];
		foreach ($listBrand as $key => $brands) {
			if($brandID!=$brands['brandID'])
			$copylistBrands[] = $brands;
		}
		$this->ObjView->assign('brandID', $brandID);
		$this->ObjView->assign('copylistBrands', $copylistBrands);
		
        $emailTemplates = $ObjEmailtemplate->listEmailTemplates($brandID);
        if ($emailTempID == '' || $emailTempID < 1) {
            //Default : Redirect to 1st Template
            $this->reDirectTemplate();
        }
        $templateArray = array();
        foreach ($emailTemplates as $key => $templates) {
            $templateArray[$templates['templateGroupID']][] = $templates;
        }
		
        $emailTemplateDetails = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, $emailTempID,'',$brandID);
        if (!empty($emailTemplateDetails) && ($currentAction == 'preview' || $currentAction == 'edit')) {
            $this->ObjView->assign('emailTempID', $emailTempID);
            if ($currentAction == 'preview') {
                $companyLogo  = $this->getSettingValue('company_information', 'company_logo');
                $siteName     = $this->getSettingValue('site_settings', 'site_name');
                $siteUrl      = $this->getSettingValue('site_settings', 'global_http_url');
                $contactEmail = $this->getSettingValue('site_settings', 'contact_email');
                if ($emailTemplateDetails['defaultLayout'] == 'Yes') {
                    $this->ObjView->assign('emailbody', $emailTemplateDetails['emailHtml']);
                    $this->ObjView->assign('emailTemplateDetails', $emailTemplateDetails);
                    $this->ObjView->assign('companyLogo', $companyLogo);
                    $this->ObjView->assign('siteName', $siteName);
                    $this->ObjView->assign('siteUrl', $siteUrl);
                    $email_body = $this->ObjCommon->fetch("email_templates/Layout.tpl", $languageIDs,$brandID);
                    $this->ObjView->assign('email_body', $email_body);
                } else {
                    $emailTemplateDetails['emailHtml'] = str_replace('{$companyLogo}', $companyLogo, $emailTemplateDetails['emailHtml']);
                    $emailTemplateDetails['emailHtml'] = str_replace('{$siteName}', $siteName, $emailTemplateDetails['emailHtml']);
                    $emailTemplateDetails['emailHtml'] = str_replace('{$siteURL}', $siteUrl, $emailTemplateDetails['emailHtml']);
                    $emailTemplateDetails['emailHtml'] = str_replace('{$siteUrl}', $siteUrl, $emailTemplateDetails['emailHtml']);
                    $resCommonBottom                   = $ObjEmailtemplate->getEmailTempSection($languageIDs, 'email_bottom','*',$brandID);
                    $emailBottom                       = str_replace('{$siteName}', $siteName, $resCommonBottom['sectionContent']);
                    $emailBottom                       = str_replace('{$siteURL}', $siteUrl, $emailBottom);
                    $emailBottom                       = str_replace('{$contactEmailAdress}', $contactEmail, $emailBottom);
                    $emailTemplateDetails['emailHtml'] = str_replace('{$emailBottom}', $emailBottom, $emailTemplateDetails['emailHtml']);
                    $this->ObjView->assign('email_body', $emailTemplateDetails['emailHtml']);
                    $this->ObjView->assign('emailTemplateDetails', $emailTemplateDetails);
                }
            }
            $this->ObjView->assign('emailTemplateDetails', $emailTemplateDetails);
            $this->ObjView->assign('showDetailSection', "1");
        } else {
            $this->ObjView->assign('showDetailSection', "0");
        }
		
        $this->ObjView->assign('currentAction', $currentAction);
        $this->ObjView->assign('emailTemplates', $templateArray);
        $this->ObjView->display('EmailTemplates.tpl');
    }
    public function showAction() {
        $this->reDirectTemplate();
        $languageIDs            = $this->ObjView->languageIDs;
        $ObjEmailtemplate       = new EmailtemplateModel();
        $emailTempID            = $this->getRequestValue('id');
        $languageIDs['Current'] = $languageIDs['Content'];
        $emailTemplateDetails   = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, $emailTempID);
        $this->ObjView->assign('emailTemplateDetails', $emailTemplateDetails);
        $this->ObjView->display('EmailTemplateShow.tpl');
    }
    public function previewAction() {
        $this->reDirectTemplate();
        $languageIDs            = $this->ObjView->languageIDs;
        $ObjEmailtemplate       = new EmailtemplateModel();
        $emailTempID            = $this->getRequestValue('id');
        $languageIDs['Current'] = $languageIDs['Content'];
        $emailTemplateDetails   = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, $emailTempID);
        $companyLogo            = $this->getSettingValue('company_information', 'company_logo');
        $siteName               = $this->getSettingValue('site_settings', 'site_name');
        $siteUrl                = $this->getSettingValue('site_settings', 'global_http_url');
        $contactEmail           = $this->getSettingValue('site_settings', 'contact_email');
        if ($emailTemplateDetails['defaultLayout'] == 'Yes') {
            $this->ObjView->assign('emailbody', $emailTemplateDetails['emailHtml']);
            $this->ObjView->assign('emailTemplateDetails', $emailTemplateDetails);
            $this->ObjView->assign('companyLogo', $companyLogo);
            $this->ObjView->assign('siteName', $siteName);
            $this->ObjView->assign('siteUrl', $siteUrl);
            $email_body = $this->ObjCommon->fetch("email_templates/Layout.tpl");
            ;
            $this->ObjView->assign('email_body', $email_body);
        } else {
            $emailTemplateDetails['emailHtml'] = str_replace('{$companyLogo}', $companyLogo, $emailTemplateDetails['emailHtml']);
            $emailTemplateDetails['emailHtml'] = str_replace('{$siteName}', $siteName, $emailTemplateDetails['emailHtml']);
            $emailTemplateDetails['emailHtml'] = str_replace('{$siteURL}', $siteUrl, $emailTemplateDetails['emailHtml']);
            $emailTemplateDetails['emailHtml'] = str_replace('{$siteUrl}', $siteUrl, $emailTemplateDetails['emailHtml']);
            $resCommonBottom                   = $ObjEmailtemplate->getEmailTempSection($languageIDs, 'email_bottom');
            $emailBottom                       = str_replace('{$siteName}', $siteName, $resCommonBottom['sectionContent']);
            $emailBottom                       = str_replace('{$siteURL}', $siteUrl, $emailBottom);
            $emailBottom                       = str_replace('{$contactEmailAdress}', $contactEmail, $emailBottom);
            $emailTemplateDetails['emailHtml'] = str_replace('{$emailBottom}', $emailBottom, $emailTemplateDetails['emailHtml']);
            $this->ObjView->assign('email_body', $emailTemplateDetails['emailHtml']);
            $this->ObjView->assign('emailTemplateDetails', $emailTemplateDetails);
        }
        $this->ObjView->display('EmailTemplatePreview.tpl');
    }
    public function editAction() {
        $section = $this->getRequestValue('section');
        if (!$section)
            $this->reDirectTemplate();
        $this->ObjCommon->askPrivilege('emailtemplate/edit/section/bottom/');
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $ObjEmailtemplate       = new EmailtemplateModel();
        $emailTempID            = $this->getRequestValue('id');
		$brandID            	= $this->getRequestValue('brandID');
		
		$ObjBrand       		= new BrandModel();	
		$brandParams         	= array(
			'languageIDs' 		=> $languageIDs
		);       
		$listBrand      		= $ObjBrand->listBrand($brandParams);  
		$this->ObjView->assign('listBrand', $listBrand);
		$brandID = (!empty($brandID )) ? $brandID : $listBrand[0]['brandID'];
		foreach ($listBrand as $key => $brands) {
			if($brandID!=$brands['brandID'])
			$copylistBrands[] = $brands;
		}
		$this->ObjView->assign('brandID', $brandID);
		$this->ObjView->assign('copylistBrands', $copylistBrands);
		
        if ($this->ObjForm->isValid('formEmailTemplate', 'submitEmailTemplate','',false)) {
            $resUpdate = $ObjEmailtemplate->saveEmailTemplate($languageIDs, $_POST, $emailTempID);
            if ($resUpdate) {
                $this->setMessage("language.msgEmailTempUpdated", 3);
                $this->setRedirect("/emailtemplate/");
            }
        }
        if ($this->ObjForm->isValid('emailTemplateSection', 'submitSection')) {
            $resUpdate = $ObjEmailtemplate->saveEmailTempSections($languageIDs, $_POST, $section,$brandID);
            if ($resUpdate) {
                $this->setMessage("language.msgEmailTempUpdated", 3);
                $this->setRedirect("/emailtemplate/edit/section/bottom/brandID/".$brandID."/");
            }
        }
        if ($section != '') {
            $templateSection = 'email_' . $section;
            switch ($section) {
                case 'top':
                    $emailTempLabel = $this->getLanguageValue('lblEmailTemplateTop');
                    break;
                case 'bottom':
                    $emailTempLabel = $this->getLanguageValue('lblEmailTemplateBottom');
                    break;
                case 'unsubscribe':
                    $emailTempLabel = $this->getLanguageValue('lblEmailTemplateUnsubscribe');
                    break;
            }
			
            $emailTemplateDetails = $ObjEmailtemplate->getEmailTempSection($languageIDs, $templateSection,'*',$brandID);
            $this->ObjView->assign('emailTempLabel', (isset($emailTempLabel) ? $emailTempLabel : ''));
            $this->ObjView->assign('sectionContent', $emailTemplateDetails['sectionContent']);
            $this->ObjView->display('EmailTemplateSections.tpl');
        } elseif ($emailTempID) {
            $emailTemplateDetails = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, $emailTempID);
            $this->ObjView->assignValues($emailTemplateDetails);
            $this->ObjView->assign('emailHtml', $emailTemplateDetails['emailHtml']);
            $this->ObjView->display('EmailTemplateAdd.tpl');
        }
    }
    public function ajaxtestmailAction() {
        $languageIDs                    = $this->ObjView->languageIDs;
        $languageIDs['Current']         = $languageIDs['Content'];
        $mail_settings                  = $this->getSettingValue('mail_settings');
        $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
        $emailTempID                    = $_POST['emailTempID'];
        $brandID                    	= $_POST['brandID'];
        $ObjEmailtemplate               = new EmailtemplateModel();
        $emailTempDet                   = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, $emailTempID,'',$brandID);
        if ($emailTempDet) {
            $ObjMail = new Mail($mail_settings);
            $ObjMail->IsHTML();
            $fromEmailName = $emailTempDet['emailFromName'];
            $fromEmailId   = $emailTempDet['emailFromEmail'];
            $Subject       = $emailTempDet['emailSubject'];
            $Body          = $emailTempDet['emailHtml'];
            $toEmailID     = trim($_POST['testEmailId']);
            $companyLogo   = $this->getSettingValue('company_information', 'company_logo');
            $siteName      = $this->getSettingValue('site_settings', 'site_name');
            $siteUrl       = $this->getSettingValue('site_settings', 'global_http_url');
            $contactEmail  = $this->getSettingValue('site_settings', 'contact_email');
            if ($emailTempDet['defaultLayout'] == 'Yes') {
                $this->ObjView->assign('companyLogo', $companyLogo);
                $this->ObjView->assign('siteName', $siteName);
                $this->ObjView->assign('siteUrl', $siteUrl);
                $this->ObjView->assign('emailbody', $emailTempDet['emailHtml']);
                $email_body = $this->ObjCommon->fetch("email_templates/Layout.tpl","",$brandID);
            } else {
                $email_body      = $emailTempDet['emailHtml'];
                $email_body      = str_replace('{$companyLogo}', $companyLogo, $email_body);
                $email_body      = str_replace('{$siteName}', $siteName, $email_body);
                $email_body      = str_replace('{$siteURL}', $siteUrl, $email_body);
                $email_body      = str_replace('{$siteUrl}', $siteUrl, $email_body);
                $resCommonBottom = $ObjEmailtemplate->getEmailTempSection($languageIDs, 'email_bottom','*',$brandID);
                $emailBottom     = str_replace('{$siteName}', $siteName, $resCommonBottom['sectionContent']);
                $emailBottom     = str_replace('{$siteURL}', $siteUrl, $emailBottom);
                $emailBottom     = str_replace('{$contactEmailAdress}', $contactEmail, $emailBottom);
                $email_body      = str_replace('{$emailBottom}', $emailBottom, $email_body);
            }
            $ObjMail->sendMail(array(
                $fromEmailId,
                $fromEmailName
            ), $toEmailID, $Subject, $email_body);
            if ($ObjMail->Send()) {
                echo "success";
            } else {
                echo "failed";
            }
        } else {
            echo "invalid";
        }
        exit;
    }
    public function reDirectTemplate() {
        $ObjEmailtemplate = new EmailtemplateModel();
		
		$languageIDs        	= $this->ObjView->languageIDs;
		$ObjBrand       		= new BrandModel();	
		   $brandParams         = array(
            'languageIDs' 		=> $languageIDs
        );       
        $listBrand      		= $ObjBrand->listBrand($brandParams);  
		$brandID				= $listBrand[0]['brandID'];
        $emailTemplates   = $ObjEmailtemplate->listEmailTemplates($brandID);
        //Default : Redirect to 1st Template
        $redirectTo       = $emailTemplates[0]['emailTempID'];
        if ($redirectTo > 0) {
            $this->setRedirect("/emailtemplate/index/action/preview/id/$redirectTo/");
        }
    }
	public function ajaxCopyTemplatesAction() {
        $brandID 			= $this->getRequestValue('brandID');
        $copybrandID        = $this->getRequestValue('copybrandID');
        $section        = $this->getRequestValue('section');
        $ObjEmailtemplate 	= new EmailtemplateModel();
		$languageIDs        = $this->ObjView->languageIDs;
        $params       = array(
            'brandID' 		=> $brandID,
            'copybrandID' 	=> $copybrandID,
            'section' 		=> $section,
            'languageIDs' 	=> $languageIDs,
            
        );
        $ObjEmailtemplate->copyTemplates($params);
		if($section == "bottom") 
			$this->setMessage('language.msgEmailBottomCopiedSuccess', 'success');
		else	
        $this->setMessage('language.msgEmailTemplatesCopiedSuccess', 'success');
    }
	public function ajaxBrandChangeAction() {
		$brandID 				= $this->getRequestValue('brandID');
		$emailKey 				= $this->getRequestValue('emailKey');
		$ObjEmailtemplate 		= new EmailtemplateModel();
		
		$emailTempID 			= $ObjEmailtemplate->getTemplateIDFromKey($emailKey,$brandID);
		echo $emailTempID;
	}
}