<?php
/*
Project Name: Jnannys
File Name: commonpages_model.php. Model File
Description: The file contains common pages related functions.
*/
require_once './lib/simplemvcwf/model_base.php';
require_once './utility/guid_utility.php';
// Encapsulates all common pages database table access. This method of encapsulation uses the singleton design pattern.
class HomebannersModel extends ModelBase {
    private static $HomebannersModel = NULL;
    
    private function __construct() {
    }
    
    // The create function is necessary to ensure there is only one instance of common pages table being used at any one time (eg. singleton)
    static function create() {
        if (NULL == self::$HomebannersModel) {
           self::$HomebannersModel = new HomebannersModel();
        }
        return self::$HomebannersModel;
    }
    
    // Retrieves a common page row by its id    
    public function selectPagesByPageId($pageId) {
		
		global $MyPdodb;		        
		$pageId = (int) $pageId;
		$sql = "SELECT * FROM hi_homebanners WHERE id = :id  LIMIT 1";        		
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			$stm->bindParam(':id', $pageId, PDO::PARAM_INT);
			$stm->execute();
			$results = $stm->fetchAll(PDO::FETCH_CLASS);			
			return $results[0];		
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
    }
	
	 // Retrieves a common page row by its pagename    
    public function selectPagesByPageName($pageName) {
		
		global $MyPdodb;		        		
		$sql = "SELECT * FROM hi_homebanners WHERE url = :url  LIMIT 1";        		
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			$stm->bindParam(':url', $pageName, PDO::PARAM_STR);
			$stm->execute();
			$results = $stm->fetchAll(PDO::FETCH_CLASS);			
			return $results[0];				
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
    }
    
    // Retrieves all pages
    public function selectPages($start=0,$limit) {
		
		global $MyPdodb;
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));		
		$sql = "SELECT * FROM hi_homebanners";  
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			$stm->execute();
			$results = $stm->fetchAll(PDO::FETCH_CLASS);
			
			return $results ;		
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
		
    }
	
	public function selectHomebanners() {
		
		global $MyPdodb;
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));		
		$sql = "SELECT * FROM hi_homebanners ORDER BY id where user_type!=1";  
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			$stm->execute();
			$results = $stm->fetchAll(PDO::FETCH_CLASS);
			return $results ;		
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
		
    }
	
	// Retrieves total count of common pages
    public function selectCount() {
       global $MyPdodb;
       $sql = "SELECT count(id) as cnt FROM hi_homebanners";        	       	  
	   $MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   return $MyPdodb->queryFirst($sql);	   	  
    }
	public function getSectionName($secid) {
       global $MyPdodb;
       $sql = "SELECT name FROM hi_homebanners where id = $secid";        	       	  
	   $MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   return $MyPdodb->queryFirst($sql);	   	  
    }
	public function selectSectionName($section_id) {
       global $MyPdodb;
       $sql = "SELECT name FROM hi_homebanners where id=:section_id";        	       	  
	   $stm  = $MyPdodb->prepare($sql);      		
		$stm->bindParam(':section_id', $section_id, PDO::PARAM_INT);
		$stm->execute();       
	  	$results = $stm->fetchAll(PDO::FETCH_CLASS);	
		$section_name = $results[0];		
		return $section_name->name;  
    }
	
	// Change disaply Status
    public function changeStatus($postvars) {
        global $MyPdodb;
		   $recId = $postvars["homebanner_id"];
		   $recStatus = $postvars["recStatus"];	   
		   if($recStatus == 1) { $recStatus=0; } else { $recStatus = 1; }	
			$sql = "UPDATE hi_homebanners set status = :status where id = :id ";
			try {
						$stm  = $MyPdodb->prepare($sql);      		
						$stm->bindParam(':status', $recStatus, PDO::PARAM_INT);		
						$stm->bindParam(':id', $recId, PDO::PARAM_INT);
						$stm->execute();       
						$msg=CommonModel::create()->getSettings('HOMEBANNER_STATUS_MSG');					
				}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
				}
			return $msg;
		
    }
    
	// Remove a common page.
	public function delete($recId) {
       global $MyPdodb;		        
	    $sql = "Delete from hi_homebanners where  id = :id ";
		try{
			$stm  = $MyPdodb->prepare($sql);      			
			$stm->bindParam(':id', $recId, PDO::PARAM_INT);
			$stm->execute();       		
			$msg=CommonModel::create()->getSettings('HOMEBANNER_DELETE_MSG');
			return $msg;			
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
    }
	
	
	
	// Update a common page row into the table and returns confirmation message
    public function editRecord($postvars,$filevars) {
       global $MyPdodb;
	   $title =$postvars['title'];
	   
	   //$title2 =$postvars['title2'];
	   
	   //$title3 =$postvars['title3'];
		$description =$postvars['description'];
		$status =$postvars['status'];
		$is_common =$postvars['is_common'];
		$tdate = date("Y-m-d");	   
	   	$udate = date("Y-m-d");
		$simage = $filevars['simage'];
		$thumbFolder = UPLOADDIR_HOMEBANNERS;
		$validExtArr = explode(",",VALID_EXT_HOMEBANNERS);
		$recId = rand(1000,10000);
		$homebanner_image_old = $postvars['homebanner_image_old'];
		
	   $id = $postvars['homebanner_id'];
	   
		 if(!empty($filevars['simage']['name'])){
		   $file = explode('.',basename($filevars['simage']['name']));
		   $ext = ".".$file[count($file)-1];
			if(!in_array($ext,$validExtArr))
			{
			 	 $parentMsg=INVALID_PHOTO_MSG;
				 $homebanner_name = "";
			}else{	
				$homebanner_name = "homebanner_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$homebanner_name;		
				$copied = copy($filevars['simage']['tmp_name'], $new_file);
				@unlink($thumbFolder."/".$homebanner_image_old);
			}
	   }else{
		   $homebanner_name=$homebanner_image_old;
	   }
		
		
		$sql = "UPDATE hi_homebanners SET ";
	   	$sql .= " title = :title, ";
		
		//$sql .= " title2 = :title2, ";
			
		//$sql .= " title3 = :title3, ";
		$sql .= " simage = :simage, ";
		$sql .= " updateby = :updateby, ";
		$sql .= " status = :status, ";
		$sql .= " is_common = :is_common, ";
		$sql .= " udate = :udate ";
		$sql .= " Where id= :id";
		
		try{
			$stm  = $MyPdodb->prepare($sql);				
			$stm->bindParam(":title", $title, PDO::PARAM_STR);
			
			//$stm->bindParam(":title2", $title2, PDO::PARAM_STR);
			
			//$stm->bindParam(":title3", $title3, PDO::PARAM_STR);
			$stm->bindParam(":simage", $homebanner_name, PDO::PARAM_STR);
			$stm->bindParam(":updateby", $_SESSION['user_admin_login_id'], PDO::PARAM_STR);
			$stm->bindParam(":status", $status, PDO::PARAM_STR);
			$stm->bindParam(":is_common", $is_common, PDO::PARAM_STR);
			$stm->bindParam(":udate", $udate, PDO::PARAM_STR);
			$stm->bindParam(":id", $id, PDO::PARAM_INT);		
			if($stm->execute()){
				$msg =CommonModel::create()->getSettings('HOMEBANNER_EDIT_MSG');
			}else{
				$msg =CommonModel::create()->getSettings('HOMEBANNER_EDIT_ERROR_MSG');
			}		
			
			
		
		 $secCheckBox = $postvars['secCheckBox'];
		 if($secCheckBox){
		 $section_ids = implode(",",$secCheckBox);
		 }else{
			 $section_ids='';
		 }
		 
		 $subSecCheckBox = $postvars['subSecCheckBox'];
		 if($subSecCheckBox){
		 $subsection_ids = implode(",",$subSecCheckBox);
		 }else{
			 $subsection_ids='';
		 }
		 
		 $pagesCheckBox = $postvars['pagesCheckBox'];
		 if($pagesCheckBox){
		 $pages_ids = implode(",",$pagesCheckBox);		
		 }else{
			 $pages_ids='';
		 }
			
			$sql = "SELECT  * FROM  `hi_dynamicbanner` where bannerid = '".$id."'";         		
			try{
				$stm  = $MyPdodb->prepare($sql);      		
				//$stm->bindParam(':id', $pageId, PDO::PARAM_INT);
				$stm->execute();
				$results = $stm->fetchAll(PDO::FETCH_CLASS);	
				$bannerid =  $results[0]->bannerid;
					
			}catch(PDOException $e){
				Logger::newMessage($e);
				logger::customErrorMsg();
			}
		
		if($bannerid){
			$sql = "UPDATE hi_dynamicbanner SET ";
		}else{
			$sql = "INSERT INTO hi_dynamicbanner SET ";
		}
		
	   	$sql .= " section_ids = :section_ids, ";		
		$sql .= " subsection_ids = :subsection_ids, ";			
		$sql .= " pages_ids = :pages_ids ";
		
		if($bannerid){
			$sql .= " Where bannerid= :bannerid";	
		}else{
			$sql .= ", bannerid = :bannerid ";
		}
			
			/*echo $sql;
		exit;*/
			
			try{
			$stm  = $MyPdodb->prepare($sql);				
			$stm->bindParam(":section_ids", $section_ids, PDO::PARAM_STR);			
			$stm->bindParam(":subsection_ids", $subsection_ids, PDO::PARAM_STR);			
			$stm->bindParam(":pages_ids", $pages_ids, PDO::PARAM_STR);
			$stm->bindParam(":bannerid", $id, PDO::PARAM_INT);	
			$stm->execute();
						
			
			}catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
			
			return $msg;
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
    }
	public function insertRecord($postvars,$filevars){
		global $MyPdodb;
		$title =$postvars['title'];
		
		//$title2 =$postvars['title2'];
		
		//$title3 =$postvars['title3'];
		$description =$postvars['description'];
		$status =$postvars['status'];
		$is_common =$postvars['is_common'];
		$tdate = date("Y-m-d");	   
	   	$udate = date("Y-m-d");
		$simage = $filevars['simage'];
		$thumbFolder = UPLOADDIR_HOMEBANNERS;
		$validExtArr = explode(",",VALID_EXT_HOMEBANNERS);
		$recId = rand(1000,10000);
		
		
		if(!empty($filevars['simage']['name'])){
		   $file = explode('.',basename($filevars['simage']['name']));
		   $ext = ".".$file[count($file)-1];
			if(!in_array($ext,$validExtArr))
			{
			 	 $parentMsg=INVALID_PHOTO_MSG;
				 $homebanner_name = "";
			}else{	
				$homebanner_name = "homebanner_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$homebanner_name;		
				$copied = copy($filevars['simage']['tmp_name'], $new_file);
			}
	   }else{
		   $homebanner_name="";
	   }
		
	
		 
		 
		 
		  $secCheckBox = $postvars['secCheckBox'];
		 if($secCheckBox){
		 $section_ids = implode(",",$secCheckBox);
		 }else{
			 $section_ids='';
		 }
		 
		 $subSecCheckBox = $postvars['subSecCheckBox'];
		 if($subSecCheckBox){
		 $subsection_ids = implode(",",$subSecCheckBox);
		 }else{
			 $subsection_ids='';
		 }
		 
		 $pagesCheckBox = $postvars['pagesCheckBox'];
		 if($pagesCheckBox){
		 $pages_ids = implode(",",$pagesCheckBox);		
		 }else{
			 $pages_ids='';
		 }
		 
		 
		 
		 
		 
		
		//exit;
		
		$sql = "insert into hi_homebanners (title, simage,  udate, tdate, updateby, status,is_common) values (?,?,?,?,?,?,?)";
		
		
		try{
			$stm  = $MyPdodb->prepare($sql);
			
		
					
			$stm->bindParam(1, $title, PDO::PARAM_STR);
			
			//$stm->bindParam(2, $title2, PDO::PARAM_STR);
			
			//$stm->bindParam(3, $title3, PDO::PARAM_STR);
			$stm->bindParam(2, $homebanner_name, PDO::PARAM_STR);
			$stm->bindParam(3, $udate, PDO::PARAM_STR);
			$stm->bindParam(4, $tdate, PDO::PARAM_STR);
			$stm->bindParam(5, $_SESSION['user_admin_login_id'], PDO::PARAM_STR);
			$stm->bindParam(6, $status, PDO::PARAM_INT);
			$stm->bindParam(7, $is_common, PDO::PARAM_INT);
			$stm->execute();
			
		
		
			$sql = "SELECT  `id` FROM  `hi_homebanners` ORDER BY id DESC LIMIT 1";         		
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			//$stm->bindParam(':id', $pageId, PDO::PARAM_INT);
			$stm->execute();
			$results = $stm->fetchAll(PDO::FETCH_CLASS);	
			//return $results[0];	
			
			
			$bannerid =  $results[0]->id;
				
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
			
			$query = "insert into hi_dynamicbanner (bannerid, section_ids, subsection_ids, pages_ids) values (?,?,?,?)";
			
			try{
			$stm  = $MyPdodb->prepare($query);
			
			$stm->bindParam(1, $bannerid, PDO::PARAM_STR);
			
			$stm->bindParam(2, $section_ids, PDO::PARAM_STR);
			
			$stm->bindParam(3, $subsection_ids, PDO::PARAM_STR);
			$stm->bindParam(4, $pages_ids, PDO::PARAM_STR);
			
			//$stm->execute();
			
			}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
			}
			
				if($stm->execute()){
					$msg =CommonModel::create()->getSettings('HOMEBANNER_INSERT_MSG');
				}else{
					$msg =CommonModel::create()->getSettings('HOMEBANNER_INSERT_ERROR_MSG');
				}	
				return $msg;
		}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
		}
		
	}
	
	
	
	
	public function checkUserEmail($emailAddress,$recId=0){
		global $MyPdodb;	
		 	
		 if(!empty($recId)){		
			 $idSql=" and user_id != :id";
		 }else{
			 $idSql="";
		 }
		 $sql = "SELECT count(user_id) as cnt FROM hi_homebanners where email_address= :email ".$idSql;   
		 try{
				 $stm  = $MyPdodb->prepare($sql);      		  
				 $stm->bindParam(':email', $emailAddress, PDO::PARAM_STR);
				 if(!empty($recId)){
					 $stm->bindParam(':id', $recId, PDO::PARAM_STR);
				 }
				$stm->execute();
				$results = $stm->fetchAll(PDO::FETCH_CLASS);		
				$totalCounts = $results[0];		
				$totalCounts->cnt;
				 if($totalCounts->cnt > 0){
					 return false;
				 }else{
				 	return true;
				 }
			}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
			}
	}	
	
	public function checkHomebanner($name,$recId=0){
		global $MyPdodb;	
		 if(!empty($recId)){		
			 $idSql=" and id != :id";
		 }else{
			 $idSql="";
		 }
		 $sql = "SELECT count(id) as cnt FROM hi_homebanners where title= :name".$idSql ;   
		 try{
				 $stm  = $MyPdodb->prepare($sql);      		  
				 $stm->bindParam(':name', $name, PDO::PARAM_STR);
				 if(!empty($recId)){
				
					 $stm->bindParam(':id', $recId, PDO::PARAM_STR);
				 }
				$stm->execute();
				$results = $stm->fetchAll(PDO::FETCH_CLASS);
				$totalCounts = $results[0];		
				$totalCounts->cnt;
				 if($totalCounts->cnt > 0){
					 return false;
				 }else{
				 		
					 return true;
				 }
						
			}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
			}
	}
	
	
public function getSectionids($id){
	
	global $MyPdodb;
		
	$sql = "SELECT  * FROM  `hi_dynamicbanner` where bannerid = '".$id."'";         		
			try{
				$stm  = $MyPdodb->prepare($sql);      		
				//$stm->bindParam(':id', $pageId, PDO::PARAM_INT);
				$stm->execute();
				$results = $stm->fetchAll(PDO::FETCH_CLASS);	
				return $results;
					
			}catch(PDOException $e){
				Logger::newMessage($e);
				logger::customErrorMsg();
			}
	
	}		
}
?>