<?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 SinglepagesModel extends ModelBase {
    private static $singlepagesModel = 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::$singlepagesModel) {
           self::$singlepagesModel = new SinglepagesModel();
        }
        return self::$singlepagesModel;
    }
    
    // Retrieves a common page row by its id    
    public function selectPagesByPageId($pageId) {
		global $MyPdodb;		        
		$pageId = (int) $pageId;
		$sql = "SELECT * FROM hi_onepages 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();
		}

    }
	
	public function selectWelcomeText($pageId) {
		
		global $MyPdodb;		        
		$pageId = (int) $pageId;
		$sql = "SELECT * FROM hi_welcometext 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_onepages 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();
		}
		
    }
	
	public function getMenuSinglepages() {
		
		global $MyPdodb;		        		
		$sql = "SELECT * FROM hi_onepages WHERE status=1 order by ord ";        		
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			$stm->execute();
			$results = $stm->fetchAll(PDO::FETCH_CLASS);			
			return $results;				
	    }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_onepages";  
		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 selectSinglepages() {
		
		global $MyPdodb;
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));		
		$sql = "SELECT * FROM hi_onepages where status=1";  
		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 getWelcomeText() {
		
		global $MyPdodb;
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));		
		$sql = "SELECT * FROM hi_welcometext";  
		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_onepages";        	       	  
	   $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_onepages 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_onepages 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["id"];
		   $recStatus = $postvars["recStatus"];	   
		   if($recStatus == 1) { $recStatus=0; } else { $recStatus = 1; }	
			$sql = "UPDATE hi_onepages set active = :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('SINGLEPAGE_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_onepages where  id = :id ";
		try{
			$stm  = $MyPdodb->prepare($sql);      			
			$stm->bindParam(':id', $recId, PDO::PARAM_INT);
			$stm->execute();       		
			$msg=CommonModel::create()->getSettings('SINGLEPAGE_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) {
       global $MyPdodb;
	  $section =$postvars['section'];
		$description =$postvars['description'];
		echo $status =$postvars['status'];
		$tdate = date("Y-m-d");	   
	   	$udate = date("Y-m-d");
		
	   $id = $postvars['id'];
	   
							
								
								$sql = "UPDATE hi_onepages SET ";
								$sql .= " section = :section, ";
								$sql .= " description = :description, ";
								$sql .= " active = :status, ";
								
								
								$sql .= " udate = :udate ";
								$sql .= " Where id= :id";
								
								try{
									$stm  = $MyPdodb->prepare($sql);				
									$stm->bindParam(":section", $section, PDO::PARAM_STR);
									$stm->bindParam(":description", $description, PDO::PARAM_STR);
									$stm->bindParam(":status", $status, PDO::PARAM_STR);
									
									
									$stm->bindParam(":udate", $udate, PDO::PARAM_STR);
									$stm->bindParam(":id", $id, PDO::PARAM_INT);		
									if($stm->execute()){
										
												
										$msg =CommonModel::create()->getSettings('SINGLEPAGE_EDIT_MSG');
									}else{
										$msg =CommonModel::create()->getSettings('SINGLEPAGE_EDIT_ERROR_MSG');
									}			
									
									return $msg;
								}catch(PDOException $e){
									Logger::newMessage($e);
									logger::customErrorMsg();
								}
		
				
		
    }
	public function insertRecord($postvars){
		global $MyPdodb;
		$section =$postvars['section'];
		$description =$postvars['description'];
		$order =$postvars['ord'];
		$status =$postvars['status'];
		$tdate = date("Y-m-d");	   
	   	$udate = date("Y-m-d");
		
		if($opt_out_marketing==''){
			$opt_out_marketing=0;
		}
		
						
								$sql = "insert into hi_onepages (section, description, udate, tdate, active) values (?,?,?,?,?)";
								try{
									$stm  = $MyPdodb->prepare($sql);
											
									$stm->bindParam(1, $section, PDO::PARAM_INT);
									$stm->bindParam(2, $description, PDO::PARAM_STR);
									$stm->bindParam(3, $udate, PDO::PARAM_STR);
									$stm->bindParam(4, $tdate, PDO::PARAM_STR);
									$stm->bindParam(5, $status, PDO::PARAM_INT);
									
										if($stm->execute()){
											$msg =CommonModel::create()->getSettings('SINGLEPAGE_INSERT_MSG');
										}else{
											$msg =CommonModel::create()->getSettings('SINGLEPAGE_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_onepages 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 checkSection($name,$recId=0){
		global $MyPdodb;	
		 	
		 if(!empty($recId)){		
			 $idSql=" and id != :id";
		 }else{
			 $idSql="";
		 }
		 $sql = "SELECT count(id) as cnt FROM hi_onepages where section= :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();
			}
	}		
}
?>