<?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: EmailtemplateModel.php, v 1.0 2011/06/14 20:52:11
//
class SmstemplateModel extends Model {
    //FUNCTION TO LIST EMAIL TEMPLATES
		public function saveSmsTemplate($languageID,$smsTempID=0)
     {
	 	  
		

	if($smsTempID>0){	
        $sql = " UPDATE " . DB_PREFIX . "sms_template SET
		  											updatedDate   =now(),
                                          	 		smsStatus     ='" . $this->escape($_POST['smsStatus']) . "',
                                                                    brandID     ='" . $this->escape($_POST['brandID']) . "'
                                          	  WHERE smsTempID  =" . $smsTempID . "";
        $this->ObjDatabase->setQuery($sql);
        $sqlCheckDetailsExists = "SELECT 
		   									     COUNT(*)
									  	   FROM " . DB_PREFIX . "sms_template_details
                                           WHERE smsTempID = '" . $smsTempID . "'
                                            	 AND languageID = '" . $languageID . "'";
        $sqlCheckDetailsExists = $this->ObjDatabase->readValue($sqlCheckDetailsExists);
		
        if ($sqlCheckDetailsExists[0] > 0)
        {
            $sql = " UPDATE " . DB_PREFIX . "sms_template_details SET
														smsTemplateName   ='" . $this->escape($_POST['smsTemplateName']) . "',
														smsTemplateContent='" . $this->escape($_POST['smsTemplateContent'], FALSE) . "'
												  WHERE smsTempID     =" . $smsTempID . " AND languageID=" . $languageID . "";
            $this->ObjDatabase->setQuery($sql);
        }
        else
        {
            $sqlInsertEmailTempDetails = "INSERT INTO " . DB_PREFIX . "sms_template_details(
                                             smsTempID,
                                             smsTemplateName,
                                             smsTemplateContent,
                                             languageID)
                                             VALUES(
                                             " . $smsTempID . ",
                                             '" . $this->escape($_POST['smsTemplateName']) . "',
                                             '" . $this->escape($_POST['smsTemplateContent'], FALSE) . "',
                                             '" . $languageID . "'
                                             )";
            $resInsertEmailTempDetails = $this->ObjDatabase->setQuery($sqlInsertEmailTempDetails);
        }
		}else{
             $sql = "INSERT INTO " . DB_PREFIX . "sms_template(
                                             sms_key,
                                             updatedDate,
                                             smsStatus,brandID)
                                             VALUES(
                                            '" . $this->escape($_POST['sms_key']) . "',
                                             NOW(),
                                             '" . $this->escape($_POST['smsStatus']) . "','".$_POST['brandID']."'
                                             )";
            $this->ObjDatabase->setQuery($sql);	
			$smsTempID	=	$this->ObjDatabase->getInsertId();
            $sqlInsertEmailTempDetails = "INSERT INTO " . DB_PREFIX . "sms_template_details(
                                             smsTempID,
                                             smsTemplateName,
                                             smsTemplateContent,
                                             languageID)
                                             VALUES(
                                             " . $smsTempID . ",
                                             '" . $this->escape($_POST['smsTemplateName']) . "',
                                             '" . $this->escape($_POST['smsTemplateContent'], FALSE) . "',
                                             '" . $languageID . "'
                                             )";
            $resInsertEmailTempDetails = $this->ObjDatabase->setQuery($sqlInsertEmailTempDetails);	
			
		}
    }
   
    public function getSmsTempLists($languageIDs, $limitFrom = 0, $limitTo = "", $smsTemplateName = "", $smsStatus = "",$brandID='')
    {
        $currentLanguageID = $languageIDs;
        $sql               = "SELECT ";
        if ($limitTo)
        {
            $sql .= " SQL_CALC_FOUND_ROWS ";
        }
        $sql .= " *,brandDet.brandName FROM
									  	  		" . DB_PREFIX . "sms_template AS ns
										   LEFT JOIN  " . DB_PREFIX . "sms_template_details AS nsd
                                                 ON(ns.smsTempID = nsd.smsTempID)  LEFT JOIN 
						" . DB_PREFIX . "brands AS brand
							ON	(
								brand.brandID = ns.brandID
							)                     
                                        LEFT JOIN 
						" . DB_PREFIX . "brand_details AS brandDet
							ON	(
								brandDet.brandID = brand.brandID
								AND
								brandDet.languageID = '" . $currentLanguageID . "'
							)   
										   WHERE ns.smsStatus!='Delete' AND nsd.languageID='" . $currentLanguageID . "'";
        if ($smsTemplateName != '')
        {
            $sql .= " AND nsd.smsTemplateName LIKE '%" . $this->escape($smsTemplateName) . "%' ";
        }
         if ($brandID != '')
        {
            $sql .= " AND ns.brandID ='" . $brandID . "'";
        }
        if ($smsStatus != '')
        {
            $sql .= " AND ns.smsStatus ='" . $this->escape($smsStatus) . "'";
        }
	
        if ($limitTo)
        {
            $sql .= " LIMIT  " . $limitFrom . "," . $limitTo . "";
        }
        $resSelectSmsTempList = $this->ObjDatabase->readValues($sql, MYSQLI_ASSOC);
        return $resSelectSmsTempList;
    }
    public function getSmsTempDetails($smsTempID, $languageIDs, $smsKey = "")
    {
        $sql    = "SELECT smsTmp.*,smsTmpDet.*
										   FROM
									  	      " . DB_PREFIX . "sms_template AS smsTmp
										   LEFT JOIN  " . DB_PREFIX . "sms_template_details AS smsTmpDet
                                                 ON(smsTmp.smsTempID = smsTmpDet.smsTempID
												 AND smsTmpDet.languageID='" . $languageIDs['Current'] . "')
										   WHERE 
										   	  1 ";
        if ($smsTempID) {
            $sql .= "
						AND smsTmp.smsTempID='" . $smsTempID . "'";
        }											  
        if ($smsKey) {
            $sql .= "
						AND smsTmp.sms_key='" . $smsKey . "'";
        }
        //echo $sql;exit;
        $result = $this->ObjDatabase->readValue($sql);
        return $result;
    }
     public function changeTemplateStatus($smsTempID=0,$changeStatus="") {
       

			 $sql = " UPDATE " . DB_PREFIX . "sms_template SET
		  											
                                          	 		smsStatus     ='" . $this->escape($changeStatus) . "'
                                          	  WHERE smsTempID  =" . $smsTempID . "";
        $this->ObjDatabase->setQuery($sql);

    }    
   }