<?php
/*
Project Name: Zalawadivisha
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 ExpquotesModel extends ModelBase {
    private static $expquotesModel = 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::$expquotesModel) {
           self::$expquotesModel = new ExpquotesModel();
        }
        return self::$expquotesModel;
    }
    
    // Retrieves a common page row by its id    
    public function selectPagesByPageId($pageId) {
		
		global $MyPdodb;		        
		$pageId = (int) $pageId;
		$sql = "SELECT * FROM hi_expquotes 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 selectMsgDetail($postvars) {
		
		global $MyPdodb;		        
		$pageId = $postvars['msg_id'];
		$sql = "SELECT * FROM hi_expmessages 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);
			$i=0;
			foreach($results as $result){
				//print_r($result);
				$sql = "select topic from hi_expquotes where id='".$result->expid."'";
				try{
					$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   				$res = $MyPdodb->queryFirst($sql);
					$topic = $res->topic;
					//array_push($arrres,$totalsubsec);
					$results[$i]->topic = $topic;
				}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
				}
				
				$i++;
			}	
			$i=0;
			foreach($results as $result){
				//print_r($result);
				if($result->memberid!=0){
					$sql = "select fullname as member, simage from hi_members where id='".$result->memberid."'";
					try{
						$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
						$res = $MyPdodb->queryFirst($sql);
						$member = $res->member;
						$photo = $res->simage;
						//array_push($arrres,$totalsubsec);
						$results[$i]->member = $member;
						$results[$i]->photo = $photo;
					}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
					}
				}else{
					$results[$i]->member = "Administrator";
					$results[$i]->photo = $_SESSION['adminphoto'];
				}
				$i++;
			}
			$i=0;		
			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_expquotes 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_expquotes where status!=2";  
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			$stm->execute();
			$results = $stm->fetchAll(PDO::FETCH_CLASS);
			$i=0;
			foreach($results as $result){
				//print_r($result);
				$sql = "select count(id) as cnt from hi_expmessages where status = 1 and expid='".$result->id."'";
				try{
					$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   				$res = $MyPdodb->queryFirst($sql);
					$totalsubsec = $res->cnt;
					//array_push($arrres,$totalsubsec);
					$results[$i]->msg_count = $totalsubsec;
				}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
				}
				
				$i++;
			}
			$i=0;
			foreach($results as $result){
				//print_r($result);
				$sql = "select count(id) as cnt from hi_expmessages where status = 0 and expid='".$result->id."'";
				try{
					$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   				$res = $MyPdodb->queryFirst($sql);
					$totalsubsec = $res->cnt;
					//array_push($arrres,$totalsubsec);
					$results[$i]->msg_appcount = $totalsubsec;
				}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
				}
				
				$i++;
			}
			
			return $results ;		
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
		
    }
	
	public function selectExpquotes($postvars) {
		
		global $MyPdodb;
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));	
		
		$sql_count = "SELECT count(id) as cnt FROM hi_expquotes where status=1 order by id desc ";
	    try {
				$stm  = $MyPdodb->prepare($sql_count);      		
				$stm->execute($BindArr);
				$results = $stm->fetchAll(PDO::FETCH_CLASS);
			}catch(PDOException $e){
				Logger::newMessage($e);
				logger::customErrorMsg();
		    }	
				
		$totalCounts = $results[0]->cnt;			
	    $_REQUEST["totalres"] = $totalCounts;
		$_REQUEST["nume"] = $totalCounts;	
		$sortby = $postvars['sortby'];
		if($sortby==''){
			$sortby_=' order by tdate desc ';
		}else{
			if($sortby=='tdate'){
				$sortby_=' order by tdate desc';
			}else{
				$sortby_=' order by '.$sortby.' asc ';
			}
			
		}
		$sql = "SELECT * FROM hi_expquotes where status=1 $sortby_ LIMIT ?, ?";  
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			$BindArr[] =(int) $postvars["eu"];
			$BindArr[] =(int) $postvars["limit"];	
			$stm->execute($BindArr);
			$results = $stm->fetchAll(PDO::FETCH_CLASS);
			$i=0;
			foreach($results as $result){
				//print_r($result);
				if($result->createdby_type==2){
					$sql = "select fullname as member, simage from hi_members where id='".$result->createdby."'";
					try{
						$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
						$res = $MyPdodb->queryFirst($sql);
						$member = $res->member;
						$photo = $res->simage;
						//array_push($arrres,$totalsubsec);
						$results[$i]->member = $member;
						$results[$i]->photo = $photo;
					}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
					}
				}else{
					$results[$i]->member = "Administrator";
					$results[$i]->photo = $_SESSION['adminphoto'];
				}
				$i++;
			}
			$i=0;
			foreach($results as $result){
				//print_r($result);
				$sql = "select count(id) as cnt from hi_expmessages where status=1 and expid='".$result->id."'";
				try{
					$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   				$res = $MyPdodb->queryFirst($sql);
					$totalsubsec = $res->cnt;
					//array_push($arrres,$totalsubsec);
					$results[$i]->msg_count = $totalsubsec;
				}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
				}
				
				$i++;
			}
			return $results ;		
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
		
    }	
	public function selectExpMessages($postvars) {
		
		global $MyPdodb;
		$expid =  $postvars['exp_id'];
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));	
		
		$sql_count = "SELECT count(id) as cnt FROM hi_expmessages where status=1 and expid=? order by id desc ";
	    try {
				$stm  = $MyPdodb->prepare($sql_count);  
				$BindArr[] =(int) $expid;    		
				$stm->execute($BindArr);
				$results = $stm->fetchAll(PDO::FETCH_CLASS);
			}catch(PDOException $e){
				Logger::newMessage($e);
				logger::customErrorMsg();
		    }	
			$BindArr='';
		$totalCounts = $results[0]->cnt;			
	    $_REQUEST["totalres"] = $totalCounts;
		$_REQUEST["nume"] = $totalCounts;	
		$sql = "SELECT * FROM hi_expmessages where status=1 and expid=? order by id desc LIMIT ?, ?";  
		try{
			$stm  = $MyPdodb->prepare($sql); 
			$BindArr[] =(int) $expid;    	     		
			$BindArr[] =(int) $postvars["eu"];
			$BindArr[] =(int) $postvars["limit"];	
			$stm->execute($BindArr);
			$results = $stm->fetchAll(PDO::FETCH_CLASS);
			$i=0;
			foreach($results as $result){
				//print_r($result);
				if($result->membertype==2){
					$sql = "select fullname as member, simage from hi_members where id='".$result->memberid."'";
					try{
						$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
						$res = $MyPdodb->queryFirst($sql);
						$member = $res->member;
						$photo = $res->simage;
						//array_push($arrres,$totalsubsec);
						$results[$i]->member = $member;
						$results[$i]->photo = $photo;
					}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
					}
				}else{
					$results[$i]->member = "Administrator";
					$results[$i]->photo = $_SESSION['adminphoto'];
				}
				$i++;
			}
			$i=0;
			foreach($results as $result){
				//print_r($result);
				$sql = "select topic,simage,simage2 from hi_expquotes where id='".$result->expid."'";
				try{
					$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   				$res = $MyPdodb->queryFirst($sql);
					$topic = $res->topic;
					$simage_e = $res->simage;
					$simage2_e = $res->simage2;
					//array_push($arrres,$totalsubsec);
					$results[$i]->topic = $topic;
					$results[$i]->simage_e = $simage_e;
					$results[$i]->simage2_e = $simage2_e;
				}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
				}
				
				$i++;
			}
			return $results ;		
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
		
    }	
	public function selectExpquotes_search($postvars) {
		
		global $MyPdodb;
		$active=1;
		$topic = $postvars["topic"];
		if($topic!=''){
			$subsql = " topic like ?";
			$BindArr[]="%".$topic."%";
		}
		$name = $postvars["name"];
		if($name!=''){
			if($subsql!='')
				$subsql .= " and ";
			$subsql .= " createdby in (select id from hi_members where fullname like ? and createdby_type=2)";
			$BindArr[]="%".$name."%";
		}
		$description = $postvars["description"];
		if($lname!=''){
			if($subsql!='')
				$subsql .= " and ";
			$subsql .= " description like ?";
			$BindArr[]="%".$description."%";
		}
		if($subsql!=''){
			$subsql .= " and ";
		}
			$subsql .= "status = ?";
			$BindArr[] =(int) $active;
		
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));	
		
		$sql_count = "SELECT count(id) as cnt FROM hi_expquotes where $subsql order by id desc ";
	    try {
				$stm  = $MyPdodb->prepare($sql_count);      		
				$stm->execute($BindArr);
				$results = $stm->fetchAll(PDO::FETCH_CLASS);
			}catch(PDOException $e){
				Logger::newMessage($e);
				logger::customErrorMsg();
		    }	
				
		$totalCounts = $results[0]->cnt;			
	    $_REQUEST["totalres"] = $totalCounts;
		$_REQUEST["nume"] = $totalCounts;	
		
		$sql = "SELECT * FROM hi_expquotes where $subsql order by id desc LIMIT ?, ?";  
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			$BindArr[] =(int) $postvars["eu"];
			$BindArr[] =(int) $postvars["limit"];	
			$stm->execute($BindArr);
			$results = $stm->fetchAll(PDO::FETCH_CLASS);
			$i=0;
			foreach($results as $result){
				//print_r($result);
				if($result->createdby_type==2){
					$sql = "select fullname as member, simage from hi_members where id='".$result->createdby."'";
					try{
						$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
						$res = $MyPdodb->queryFirst($sql);
						$member = $res->member;
						$photo = $res->simage;
						//array_push($arrres,$totalsubsec);
						$results[$i]->member = $member;
						$results[$i]->photo = $photo;
					}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
					}
				}else{
					$results[$i]->member = "Administrator";
					$results[$i]->photo = $_SESSION['adminphoto'];
				}
				$i++;
			}	
			return $results ;		
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
		
    }	
	
	public function selectPagesByPageId_msg($pageId) {
		
		global $MyPdodb;		        
		$pageId = (int) $pageId;
		$sql = "SELECT * FROM hi_expmessages 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 editRecord_msg($postvars,$filevars) {
       global $MyPdodb;
	  $title =$postvars['title'];
	  $description =$postvars['description'];
		$status =$postvars['status'];
		$tdate = date("Y-m-d");	   
	   	$udate = date("Y-m-d");
	   $id = $postvars['msg_id'];
	   $extlink = $postvars['extlink'];
	   $simage = $filevars['simage'];
		$simage2 = $filevars['simage2'];
		$thumbFolder = UPLOADDIR_EXPS;
		$validExtArr = explode(",",VALID_EXT_EXPS);
		$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;
				 $exp_name = "";
			}else{	
				$exp_name = "expquote_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$exp_name;		
				$copied = copy($filevars['simage']['tmp_name'], $new_file);
			}
	   }else{
		   $exp_name="";
	   }
	   $recId = rand(1000,10000);
		if(!empty($filevars['simage2']['name'])){
		   $file = explode('.',basename($filevars['simage2']['name']));
		   $ext = ".".$file[count($file)-1];
			if(!in_array($ext,$validExtArr))
			{
			 	 $parentMsg=INVALID_PHOTO_MSG;
				 $exp_name2 = "";
			}else{	
				$exp_name2 = "expquote_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$exp_name2;		
				$copied = copy($filevars['simage2']['tmp_name'], $new_file);
			}
	   }else{
		   $exp_name2="";
	   }
		 
				
		
		$sql = "UPDATE hi_expmessages SET ";
	   	$sql .= " title = :title, ";
		$sql .= " description = :description, ";
		$sql .= " status = :status, ";
		$sql .= " simage = :exp_name, ";
		$sql .= " simage2 = :exp_name2, ";
		$sql .= " extlink = :extlink, ";
		$sql .= " tdate = :tdate ";
		$sql .= " Where id= :id";
		
		try{
			$stm  = $MyPdodb->prepare($sql);				
			$stm->bindParam(":title", $title, PDO::PARAM_STR);
			$stm->bindParam(":description", $description, PDO::PARAM_STR);
			$stm->bindParam(":status", $status, PDO::PARAM_STR);
			$stm->bindParam(":exp_name", $exp_name, PDO::PARAM_STR);
			$stm->bindParam(":exp_name2", $exp_name2, PDO::PARAM_STR);
			$stm->bindParam(":extlink", $extlink, PDO::PARAM_STR);
			$stm->bindParam(":tdate", $tdate, PDO::PARAM_STR);
			$stm->bindParam(":id", $id, PDO::PARAM_INT);		
			if($stm->execute()){
				
						
				$msg =CommonModel::create()->getSettings('MESSAGE_EDIT_MSG');
			}else{
				$msg =CommonModel::create()->getSettings('MESSAGE_EDIT_ERROR_MSG');
			}			
			
			return $msg;
	    }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_expquotes";        	       	  
	   $MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   return $MyPdodb->queryFirst($sql);	   	  
    }
	public function needapprove() {
       global $MyPdodb;
       $sql = "SELECT count(id) as cnt FROM hi_expmessages where status=0";        	       	  
	   $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_expquotes where id = $secid";        	       	  
	   $MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   return $MyPdodb->queryFirst($sql);	   	  
    }
	public function selectSectionName($expquote_id) {
       global $MyPdodb;
       $sql = "SELECT name FROM hi_expquotes where id=:expquote_id";        	       	  
	   $stm  = $MyPdodb->prepare($sql);      		
		$stm->bindParam(':expquote_id', $expquote_id, PDO::PARAM_INT);
		$stm->execute();       
	  	$results = $stm->fetchAll(PDO::FETCH_CLASS);	
		$section_name = $results[0];		
		return $section_name->name;  
    }
	public function selectPages_msgs($start=0,$limit,$expquoteid) {
		
		global $MyPdodb;
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));		
		$start = (int) $start;
		$limit = (int) $limit;		
		$sql = "SELECT * FROM hi_expmessages where status!=2 and expid = '".$expquoteid."' limit ?, ?";  
		try{
			$stm  = $MyPdodb->prepare($sql);      		
			$stm->execute(array($start,$limit));
			$results = $stm->fetchAll(PDO::FETCH_CLASS);
			$i=0;
			foreach($results as $result){
				//print_r($result);
					$sql = "select fullname as member, simage from hi_members where id='".$result->memberid."'";
					try{
						$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
						$res = $MyPdodb->queryFirst($sql);
						$member = $res->member;
						$photo = $res->simage;
						//array_push($arrres,$totalsubsec);
						$results[$i]->member = $member;
						$results[$i]->photo = $photo;
					}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
					}
				
				$i++;
			}	
			return $results ;		
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
		
    }
	public function selectCount_msgs($expquoteid) {
       global $MyPdodb;
       $sql = "SELECT count(id) as cnt FROM hi_expmessages where status!=2 and id = '".$expquoteid."'";        	       	  
	   $MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
	   return $MyPdodb->queryFirst($sql);	   	  
    }
	// Change disaply Status
    public function changeStatus($postvars) {
        global $MyPdodb;
		   $recId = $postvars["expquote_id"];
		   $recStatus = $postvars["recStatus"];	   
		   if($recStatus == 1) { $recStatus=0; } else { $recStatus = 1; }	
			$sql = "UPDATE hi_expquotes 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('EXPQUOTE_STATUS_MSG');					
				}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
				}
			return $msg;
		
    }
	public function changeStatus_msg($postvars) {
        global $MyPdodb;
		   $recId = $postvars["msg_id"];
		   $recStatus = $postvars["recStatus"];	   
		   if($recStatus == 1) { $recStatus=0; } else { $recStatus = 1; }	
			$sql = "UPDATE hi_expmessages 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('MESSAGES_STATUS_MSG');					
				}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
				}
			return $msg;
		
    }
    
	// Remove a common page.
	public function delete($recId) {
       global $MyPdodb;		        
	    $sql = "update hi_expquotes set status=2 where  id = :id ";
		try{
			$stm  = $MyPdodb->prepare($sql);      			
			$stm->bindParam(':id', $recId, PDO::PARAM_INT);
			$stm->execute();       		
			$msg=CommonModel::create()->getSettings('EXPQUOTE_DELETE_MSG');
			$sql = "update hi_expmessages set status=2 where  expid = :id ";
			try{
				$stm  = $MyPdodb->prepare($sql);      			
				$stm->bindParam(':id', $recId, PDO::PARAM_INT);
				$stm->execute();       		
				$msg=CommonModel::create()->getSettings('MESSAGE_DELETE_MSG');
				return $msg;			
			}catch(PDOException $e){
				Logger::newMessage($e);
				logger::customErrorMsg();
			}
			return $msg;			
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
		
    }
	public function deletemsg($recId) {
       global $MyPdodb;		        
	    $sql = "update hi_expmessages set status=2 where  id = :id ";
		try{
			$stm  = $MyPdodb->prepare($sql);      			
			$stm->bindParam(':id', $recId, PDO::PARAM_INT);
			$stm->execute();       		
			$msg=CommonModel::create()->getSettings('MESSAGE_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;
	    if($_SESSION['user_admin_login_id']!='' and $_SESSION["member_id"]==''){
	   		$updateby = $_SESSION['user_admin_login_id'];
			$updateby_type=1;
	   }else{
	   		$updateby = $_SESSION["member_id"];
			$updateby_type=2;
	   }
	  $topic =$postvars['topic'];
		$description =$postvars['description'];
		$order =$postvars['ord'];
		$status =$postvars['status'];
		if($status==''){
			$status=0;
		}
		$tdate = date("Y-m-d");	   
		
	   $id = $postvars['expquote_id'];
	   $simage = $filevars['simage'];
		$simage2 = $filevars['simage2'];
		$thumbFolder = UPLOADDIR_EXPS;
		$validExtArr = explode(",",VALID_EXT_EXPS);
		$recId = rand(1000,10000);
		$exp_image_old = $postvars['exp_image_old'];
		$exp_image_old2 = $postvars['exp_image_old2'];
		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;
				 $exp_name = "";
			}else{	
				$exp_name = "expquote_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$exp_name;		
				$copied = copy($filevars['simage']['tmp_name'], $new_file);
				@unlink($thumbFolder."/".$exp_image_old);
			}
	   }else{
		   $exp_name=$exp_image_old;
	   }
	   $recId = rand(1000,10000);
	   if(!empty($filevars['simage2']['name'])){
		   $file = explode('.',basename($filevars['simage2']['name']));
		   $ext = ".".$file[count($file)-1];
			if(!in_array($ext,$validExtArr))
			{
			 	 $parentMsg=INVALID_PHOTO_MSG;
				 $exp_name2 = "";
			}else{	
				$exp_name2 = "expquote_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$exp_name2;		
				$copied = copy($filevars['simage2']['tmp_name'], $new_file);
				@unlink($thumbFolder."/".$exp_image_old2);
			}
	   }else{
		   $exp_name2=$exp_image_old2;
	   }
		if($order!=''){
				$sql = "Select ord from hi_expquotes where id=:id";
				try{
					$stm  = $MyPdodb->prepare($sql);      		
					$stm->bindParam(':id', $id, PDO::PARAM_INT);
					$stm->execute();
					$results = $stm->fetchAll(PDO::FETCH_CLASS);
					$old_disp_ord = $results[0]->ord;
					if($order > $old_disp_ord)								
							{
								$sql = "Update hi_expquotes SET ord =ord - 1 where ord >'$old_disp_ord' and ord <= '$order'";
								//$sql = "Update tbl_section SET ord =$old_disp_ord where  ord = '$ord'";
								$stm  = $MyPdodb->prepare($sql);      		
								$stm->execute();
							}
						else if($order < $old_disp_ord)								
							{
								$sql = "Update hi_expquotes SET ord =ord + 1 where ord < '$old_disp_ord' and ord >= '$order'";
								//$sql = "Update tbl_section SET ord =$old_disp_ord where  ord = '$ord'";
								$stm  = $MyPdodb->prepare($sql);      		
								$stm->execute();
							}	
						      		
				}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
				}
		}
		
		$sql = "UPDATE hi_expquotes SET ";
	   	$sql .= " topic = :topic, ";
		$sql .= " description = :description, ";
		$sql .= " createdby = :createdby, ";
		$sql .= " createdby_type = :createdby_type, ";
		$sql .= " simage = :simage, ";
		$sql .= " simage2 = :simage2, ";
		$sql .= " ord = :order, ";
		$sql .= " status = :status ";
		$sql .= " Where id= :id";
		
		try{
			$stm  = $MyPdodb->prepare($sql);				
			$stm->bindParam(":topic", $topic, PDO::PARAM_STR);
			$stm->bindParam(":description", $description, PDO::PARAM_STR);
			$stm->bindParam(":createdby", $updateby, PDO::PARAM_STR);
			$stm->bindParam(":createdby_type", $updateby_type, PDO::PARAM_STR);
			$stm->bindParam(":simage", $exp_name, PDO::PARAM_STR);
			$stm->bindParam(":simage2", $exp_name2, PDO::PARAM_STR);	
			$stm->bindParam(":order", $order, PDO::PARAM_STR);
			$stm->bindParam(":status", $status, PDO::PARAM_STR);
			$stm->bindParam(":id", $id, PDO::PARAM_INT);		
			if($stm->execute()){
				
						
				$msg =CommonModel::create()->getSettings('EXPQUOTE_EDIT_MSG');
			}else{
				$msg =CommonModel::create()->getSettings('EXPQUOTE_EDIT_ERROR_MSG');
			}			
			
			return $msg;
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
    }
	public function insertRecord_msg($postvars,$filevars){
		global $MyPdodb;
		
		$title =$postvars['title'];
		$description =$postvars['description'];
		$status =$postvars['status'];
		if($status==''){
			$status='0';
		}
		if($_SESSION["member_id"]!=''){
			$memberid = $_SESSION["member_id"];
		}else{
			
		}
		if($_SESSION['user_admin_login_id']!='' and $_SESSION["member_id"]==''){
	   		$memberid = $_SESSION['user_admin_login_id'];
			$membertype=1;
	   }else{
	   		$memberid = $_SESSION["member_id"];
			$membertype=2;
	   }
		$expid =$postvars['expquote_id'];
		$extlink =$postvars['extlink'];
		$tdate = date("Y-m-d");	   
	   	$udate = date("Y-m-d");
		$simage = $filevars['simage'];
		$simage2 = $filevars['simage2'];
		$thumbFolder = UPLOADDIR_EXPS;
		$validExtArr = explode(",",VALID_EXT_EXPS);
		$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;
				 $exp_name = "";
			}else{	
				$exp_name = "expquote_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$exp_name;		
				$copied = copy($filevars['simage']['tmp_name'], $new_file);
			}
	   }else{
		   $exp_name="";
	   }
	   $recId = rand(1000,10000);
		if(!empty($filevars['simage2']['name'])){
		   $file = explode('.',basename($filevars['simage2']['name']));
		   $ext = ".".$file[count($file)-1];
			if(!in_array($ext,$validExtArr))
			{
			 	 $parentMsg=INVALID_PHOTO_MSG;
				 $exp_name2 = "";
			}else{	
				$exp_name2 = "expquote_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$exp_name2;		
				$copied = copy($filevars['simage2']['tmp_name'], $new_file);
			}
	   }else{
		   $exp_name2="";
	   }
		
		
		
		$sql = "insert into hi_expmessages (title, description, status, tdate, expid, simage, simage2, extlink, memberid, membertype) values (?,?,?,?,?,?,?,?,?,?)";
		try{
			$stm  = $MyPdodb->prepare($sql);
					
			$stm->bindParam(1, $title, PDO::PARAM_STR);
			$stm->bindParam(2, $description, PDO::PARAM_STR);
			$stm->bindParam(3, $status, PDO::PARAM_INT);
			$stm->bindParam(4, $tdate, PDO::PARAM_STR);
			$stm->bindParam(5, $expid, PDO::PARAM_INT);
			$stm->bindParam(6, $exp_name, PDO::PARAM_STR);
			$stm->bindParam(7, $exp_name2, PDO::PARAM_STR);
			$stm->bindParam(8, $extlink, PDO::PARAM_STR);
			$stm->bindParam(9, $memberid, PDO::PARAM_STR);
			$stm->bindParam(10, $membertype, PDO::PARAM_STR);
			
				if($stm->execute()){
					$msg =CommonModel::create()->getSettings('MESSAGE_INSERT_MSG');
				}else{
					$msg =CommonModel::create()->getSettings('MESSAGE_INSERT_ERROR_MSG');
				}	
				return $msg;
		}catch(PDOException $e){
					Logger::newMessage($e);
					logger::customErrorMsg();
		}
		
	}
	public function insertRecord($postvars,$filevars){
		global $MyPdodb;
		if($_SESSION['user_admin_login_id']!='' and $_SESSION["member_id"]==''){
			$updateby = $_SESSION['user_admin_login_id'];
			$updateby_type=1;
		}else{
			$updateby = $_SESSION["member_id"];
			$updateby_type=2;
		}
		$topic =$postvars['topic'];
		$description =$postvars['description'];
		$order =$postvars['ord'];
		if($order==''){
			$order=0;
		}
		$status =$postvars['status'];
		if($status==''){
			$status=0;
		}
		$tdate = date("Y-m-d");	   
	   	$udate = date("Y-m-d");
		
		if($opt_out_marketing==''){
			$opt_out_marketing=0;
		}
		$simage = $filevars['simage'];
		$simage2 = $filevars['simage2'];
		$thumbFolder = UPLOADDIR_EXPS;
		$validExtArr = explode(",",VALID_EXT_EXPS);
		$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;
				 $exp_name = "";
			}else{	
				$exp_name = "expquote_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$exp_name;		
				$copied = copy($filevars['simage']['tmp_name'], $new_file);
			}
	   }else{
		   $exp_name="";
	   }
	   $recId = rand(1000,10000);
		if(!empty($filevars['simage2']['name'])){
		   $file = explode('.',basename($filevars['simage2']['name']));
		   $ext = ".".$file[count($file)-1];
			if(!in_array($ext,$validExtArr))
			{
			 	 $parentMsg=INVALID_PHOTO_MSG;
				 $exp_name2 = "";
			}else{	
				$exp_name2 = "expquote_".$recId.".".$file[count($file)-1];
				$new_file = $thumbFolder."/".$exp_name2;		
				$copied = copy($filevars['simage2']['tmp_name'], $new_file);
			}
	   }else{
		   $exp_name2="";
	   }
		$sql = "insert into hi_expquotes (topic,  description, createdby, createdby_type, tdate, ord, status, simage, simage2) values (?,?,?,?,?,?,?,?,?)";
		try{
			$stm  = $MyPdodb->prepare($sql);
					
			$stm->bindParam(1, $topic, PDO::PARAM_STR);
			$stm->bindParam(2, $description, PDO::PARAM_STR);
			$stm->bindParam(3, $updateby, PDO::PARAM_INT);
			$stm->bindParam(4, $updateby_type, PDO::PARAM_STR);
			$stm->bindParam(5, $tdate, PDO::PARAM_STR);
			$stm->bindParam(6, $order, PDO::PARAM_INT);
			$stm->bindParam(7, $status, PDO::PARAM_INT);
			$stm->bindParam(8, $exp_name, PDO::PARAM_STR);
			$stm->bindParam(9, $exp_name2, PDO::PARAM_STR);
			
				if($stm->execute()){
					$msg =CommonModel::create()->getSettings('EXPQUOTE_INSERT_MSG');
				}else{
					$msg =CommonModel::create()->getSettings('EXPQUOTE_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_expquotes 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 checkExpquote($name,$recId=0){
		global $MyPdodb;	
		 	
		 if(!empty($recId)){		
			 $idSql=" and id != :id";
		 }else{
			 $idSql="";
		 }
		 $sql = "SELECT count(id) as cnt FROM hi_expquotes where topic= :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 getQuotes(){
		global $MyPdodb;		   
		$sql = "SELECT * FROM hi_expmessages where status = 1 order by tdate desc limit 5";		
		try{
			$stm  = $MyPdodb->prepare($sql);    
			
			$stm->execute();
			$results = $stm->fetchAll(PDO::FETCH_CLASS);	
			$i=0;
			foreach($results as $result){
				//print_r($result);
					$sql = "select fullname as member, simage from hi_members where id='".$result->memberid."'";
					try{
						$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
						$res = $MyPdodb->queryFirst($sql);
						$member = $res->member;
						$photo = $res->simage;
						//array_push($arrres,$totalsubsec);
						$results[$i]->member = $member;
						$results[$i]->photo = $photo;
					}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
					}
				
				$i++;
			}	
			if(count($results)<1){
				$sql = "SELECT * FROM hi_expquotes where status = 1 order by tdate desc limit 5";		
				try{
						$stm  = $MyPdodb->prepare($sql);    
						
						$stm->execute();
						$results = $stm->fetchAll(PDO::FETCH_CLASS);	
						$i=0;
						foreach($results as $result){
							if($result->createdby_type==2){
								$sql = "select fullname as member, simage from hi_members where id='".$result->createdby."'";
								try{
									$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
									$res = $MyPdodb->queryFirst($sql);
									$member = $res->member;
									$photo = $res->simage;
									//array_push($arrres,$totalsubsec);
									$results[$i]->member = $member;
									$results[$i]->photo = $photo;
								}catch(PDOException $e){
									Logger::newMessage($e);
									logger::customErrorMsg();
								}
							}else{
								$results[$i]->member = "Administrator";
								$results[$i]->photo = $_SESSION['adminphoto'];
							}
							$i++;
						}	
					}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
					}
			}
			return $results;				       
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
	}	
	public function selectExpquoteDetail($id){
		global $MyPdodb;		   
		$sql = "SELECT * FROM hi_expquotes where id = :id";		
		try{
			$stm  = $MyPdodb->prepare($sql);    
			 $stm->bindParam(':id', $id, PDO::PARAM_INT);
			$stm->execute();
			$results = $stm->fetchAll(PDO::FETCH_CLASS);	
			$i=0;
			foreach($results as $result){
				//print_r($result);
				if($result->createdby_type==2){
					$sql = "select fullname as member, simage from hi_members where id='".$result->createdby."'";
					try{
						$MyPdodb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
						$res = $MyPdodb->queryFirst($sql);
						$member = $res->member;
						$photo = $res->simage;
						//array_push($arrres,$totalsubsec);
						$results[$i]->member = $member;
						$results[$i]->photo = $photo;
					}catch(PDOException $e){
						Logger::newMessage($e);
						logger::customErrorMsg();
					}
				}else{
					$results[$i]->member = "Administrator";
					$results[$i]->photo = $_SESSION['adminphoto'];
				}
				$i++;
			}
			return $results[0];				       
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
	}	
	
}
?>