<?php
require_once './lib/simplemvcwf/model_base.php';
require_once './utility/guid_utility.php';
// Encapsulates all Article database table access.
//   This method of encapsulation uses the singleton
//   design pattern.
class CommonModel extends ModelBase {
	
	private static $commonModel = NULL;
    
    private function __construct() {
    }
    
    // The create function is necessary to ensure there
    //   is only one instance of ArticleDbAccess being used
    //   at any one time (eg. singleton)
    static function create() {
        if (NULL == self::$commonModel) {
           self::$commonModel = new CommonModel();
        }
        return self::$commonModel;
    }
	
	
	
	
	public function sendEmail($to,$subject,$body,$isHTML=true){
		
		$fromEmail = $this->getSettings('MAIL_SEND_FROM_EMAIL');	
		$fromName = $this->getSettings('MAIL_SEND_FROM_NAME');	
		
		$header="MIME-Version: 1.0\r\n";
		if($isHTML)
			$header.="Content-type: text/html \r\n";
		else
			$header.="Content-type: text/plane \r\n";
		
		$header .= "From: $fromName <".$fromEmail."> \r\n";
		
		if(@mail($to,$subject,$body,$header)){
			return true;
		}else{
			return false;
		}
			
	}
	public function getCategoriesList(){
		global $MyPdodb;
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));				
		$sql ="Select * from hi_categories order by category_id";		
		$stm  = $MyPdodb->prepare($sql);      		
		$stm->execute(array(0,$start,$limit));
		$results = $stm->fetchAll(PDO::FETCH_CLASS);
		return $results ;	
		
	}	
	public function getSettings($option_name) {
		global $MyPdodb;		   
		if($option_name == "All"){
			$sql = "SELECT * FROM hi_settings";		
		}else{
			$sql = "SELECT * FROM hi_settings where option_name = :option_name";		
		}
		
		try{
			$stm  = $MyPdodb->prepare($sql);   
			if($option_name != "All"){
				$stm->bindParam(':option_name', $option_name, PDO::PARAM_INT);
				$stm->execute();
				$results = $stm->fetchAll(PDO::FETCH_CLASS);	
				return $results[0]->option_value;				  
			}else{				
				$stm->execute();
				$results = $stm->fetchAll(PDO::FETCH_OBJ);	
				return $results;				  
			}			
			     
	    }catch(PDOException $e){
			Logger::newMessage($e);
			logger::customErrorMsg();
		}
		
    }
	
	public function getPageRoutes($router){
		global $MyPdodb;		
		$MyPdodb->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MyPDOStatement'));				
		$sql ="Select * from hi_pages where status=1";		
		$stm  = $MyPdodb->prepare($sql);      		
		$stm->execute(array(0,$start,$limit));
		$results = $stm->fetchAll(PDO::FETCH_CLASS);
		$PagesDetails =  $results ;
		
		foreach($PagesDetails as $PagesDetail){
			$pageName = $PagesDetail->page_url;
			if(!empty($pageName)){
				$router->registerRoute(new Route($pageName, 'frontpages', 'showpage'));
			}else{
				$router->registerRoute(new Route($pageName, 'frontpages', 'index'));
			}
		}		
	}
	
	public function saveErrorinDb($errorDetails){
		global $MyPdodb;		
		
		$sql = "INSERT INTO tbl_errorlog (error_date,error_message,error_code,error_file,error_lineno,error_trace,tdate) VALUES (?, ?, ?, ?, ?, ?, ?)";
		try{
				$stm  = $MyPdodb->prepare($sql);						
				$stm->bindParam(1, $errorDetails['error_date'], PDO::PARAM_STR);
				$stm->bindParam(2, $errorDetails['error_message'], PDO::PARAM_STR);
				$stm->bindParam(3, $errorDetails['error_code'], PDO::PARAM_STR);
				$stm->bindParam(4, $errorDetails['error_file'], PDO::PARAM_STR);
				$stm->bindParam(5, $errorDetails['error_lineno'], PDO::PARAM_STR);
				$stm->bindParam(6, $errorDetails['error_trace'], PDO::PARAM_STR);
				$stm->bindParam(7, $errorDetails['tdate'], PDO::PARAM_STR);						
				
				if($stm->execute()){
					$flg= true;					
				}else{
					$flg= false;
				}
				
		}catch(PDOException $e){
				Logger::newMessage($e);
				logger::customErrorMsg();
		}		
		
	}// End Of function
	
	public function sendSubmittedEmail($project_id,$designer_id){
		
		$ProejctsDetail=ProjectsModel::create()->selectProjectById($project_id);
		$ClientsDetail=ClientsModel::create()->selectClientById($ProejctsDetail->client_id);						
		
		$adminEmail=$this->getSettings('MAIL_SEND_FROM_EMAIL');
		$EmailBody=$this->getSettings('ADMIN_PROJECT_ADD_EMAIL');
		$EmailBody = nl2br($EmailBody);		
		$clientName = $ClientsDetail->client_fname." ".$ClientsDetail->client_lname;
		$clientEmail = $ClientsDetail->client_email;
		$projectName = $ProejctsDetail->initiative_project;
		$projectDate = date("Y-m-d");
		$EmailBody = str_replace("[#PROJECT_DATE#]",$projectDate,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENT_NAME#]",$clientName,$EmailBody);
		
		$subject="A new project has been submitted :: DPM Admin";
		$to = $adminEmail;
		$isHTML=true;					
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);						
				
		
		return true;
			
	}
	
	public function sendRejectedEmail($project_id,$designer_id){
		
		$ProejctsDetail=ProjectsModel::create()->selectProjectById($project_id);
		$ClientsDetail=ClientsModel::create()->selectClientById($ProejctsDetail->client_id);						
		$adminEmail=$this->getSettings('MAIL_SEND_FROM_EMAIL');
		$EmailBody=$this->getSettings('ADMIN_PROJECT_REJECT_EMAIL');
		$EmailBody = nl2br($EmailBody);		
		$clientName = $ClientsDetail->client_fname." ".$ClientsDetail->client_lname;
		$clientEmail = $ClientsDetail->client_email;
		$projectName = $ProejctsDetail->initiative_project;
		$projectDate = date("Y-m-d");
		$EmailBody = str_replace("[#PROJECT_DATE#]",$projectDate,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENT_NAME#]",$clientName,$EmailBody);
		
		$subject="A project has been rejected :: DPM Admin";
		$to = $adminEmail;
		$isHTML=true;					
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);						
				
		$subject="A project has been rejected :: DPM Admin";
		$to = $clientEmail;
		$isHTML=true;					
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);	
		
		return true;
			
	}
	
	public function sendAcceptedEmail($project_id,$designer_id){
		
		$DesignerDetail=DesignersModel::create()->selectDesignerById($designer_id);						
		$ProejctsDetail=ProjectsModel::create()->selectProjectById($project_id);
		$ClientsDetail=ClientsModel::create()->selectClientById($ProejctsDetail->client_id);						
		
		$EmailBody=$this->getSettings('DESIGNER_PROJECT_ASSIGN_EMAIL');
		$EmailBody = nl2br($EmailBody);
		$designerName = $DesignerDetail->designer_fname." ".$DesignerDetail->designer_lname;
		$designerEmail = $DesignerDetail->designer_email;
		$clientName = $ClientsDetail->client_fname." ".$ClientsDetail->client_lname;
		$clientEmail = $ClientsDetail->client_email;
		$projectName = $ProejctsDetail->initiative_project;
		$EmailBody = str_replace("[#DESIGNER_NAME#]",$designerName,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENTNAME#]",$clientName,$EmailBody);
		
				
		$subject="You have assigned new project :: DPM Admin";
		$to = $designerEmail;
		$isHTML=true;					
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);						
		
		$EmailBody=$this->getSettings('CLIENT_PROJECT_ASSIGN_EMAIL');
		$EmailBody = nl2br($EmailBody);
		$EmailBody = str_replace("[#DESIGNER_NAME#]",$designerName,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENT_NAME#]",$clientName,$EmailBody);
		
		$subject="Your have has been assigned a designer :: DPM Admin";
		$to = $clientEmail;
		$isHTML=true;			
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);
		
		return true;
			
	}
	
	public function send1stroundEmail($project_id,$designer_id,$commentId){
		
		$DesignerDetail=DesignersModel::create()->selectDesignerById($designer_id);						
		$ProejctsDetail=ProjectsModel::create()->selectProjectById($project_id);
		$ClientsDetail=ClientsModel::create()->selectClientById($ProejctsDetail->client_id);
		$CommentsDetail=$this->selectCommentById($commentId);						
		
		$EmailBody=$this->getSettings('DESIGNER_SUBMIT_ASSETS_EMAIL_ADMIN');
		$EmailBody = nl2br($EmailBody);
		$designerName = $DesignerDetail->designer_fname." ".$DesignerDetail->designer_lname;
		$designerEmail = $DesignerDetail->designer_email;
		$clientName = $ClientsDetail->client_fname." ".$ClientsDetail->client_lname;
		$clientEmail = $ClientsDetail->client_email;
		$projectName = $ProejctsDetail->initiative_project;
		$EmailBody = str_replace("[#DESIGENRNAME#]",$designerName,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENT_NAME#]",$clientName,$EmailBody);
		$EmailBody = str_replace("[#COMMENTS#]",$CommentsDetail->comments,$EmailBody);
		$EmailBody = str_replace("[#HOURS#]",$CommentsDetail->hour,$EmailBody);
		$EmailBody = str_replace("[#SUBMITTED_DATE#]",$CommentsDetail->tdate,$EmailBody);
		
		$adminEmail=$this->getSettings('MAIL_SEND_FROM_EMAIL');
		$subject="New Files uploaded from designer :: DPM Admin";
		$to = $adminEmail;
		$isHTML=true;					
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);						
		
		$EmailBody=$this->getSettings('DESIGNER_SUBMIT_ASSETS_EMAIL_CLIENT');
		$EmailBody = nl2br($EmailBody);
		$EmailBody = str_replace("[#DESIGENRNAME#]",$designerName,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENT_NAME#]",$clientName,$EmailBody);
		$EmailBody = str_replace("[#COMMENTS#]",$CommentsDetail->comments,$EmailBody);
		$EmailBody = str_replace("[#HOURS#]",$CommentsDetail->hour,$EmailBody);
		$EmailBody = str_replace("[#SUBMITTED_DATE#]",$CommentsDetail->tdate,$EmailBody);
		
		$subject="New Files uploaded from designer :: DPM Admin";
		$to = $clientEmail;
		$isHTML=true;			
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);
		
		return true;
			
	}
	
	public function sendrevisionEmail($project_id,$designer_id,$commentId){
		
		$DesignerDetail=DesignersModel::create()->selectDesignerById($designer_id);						
		$ProejctsDetail=ProjectsModel::create()->selectProjectById($project_id);
		$ClientsDetail=ClientsModel::create()->selectClientById($ProejctsDetail->client_id);
		$CommentsDetail=$this->selectCommentById($commentId);						
		
		$EmailBody=$this->getSettings('CLIENT_SUBMIT_REVISION_EMAIL_ADMIN');
		$EmailBody = nl2br($EmailBody);
		$designerName = $DesignerDetail->designer_fname." ".$DesignerDetail->designer_lname;
		$designerEmail = $DesignerDetail->designer_email;
		$clientName = $ClientsDetail->client_fname." ".$ClientsDetail->client_lname;
		$clientEmail = $ClientsDetail->client_email;
		$projectName = $ProejctsDetail->initiative_project;
		$EmailBody = str_replace("[#DESIGENRNAME#]",$designerName,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENT_NAME#]",$clientName,$EmailBody);
		$EmailBody = str_replace("[#COMMENTS#]",$CommentsDetail->comments,$EmailBody);
		$EmailBody = str_replace("[#HOURS#]",$CommentsDetail->hour,$EmailBody);
		$EmailBody = str_replace("[#SUBMITTED_DATE#]",$CommentsDetail->tdate,$EmailBody);
		
		$adminEmail=$this->getSettings('MAIL_SEND_FROM_EMAIL');
		$subject="Project Revision by Client :: DPM Admin";
		$to = $adminEmail;
		$isHTML=true;					
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);						
		
		$EmailBody=$this->getSettings('CLIENT_SUBMIT_ASSETS_EMAIL_DESIGNER');
		$EmailBody = nl2br($EmailBody);
		$EmailBody = str_replace("[#DESIGENRNAME#]",$designerName,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENT_NAME#]",$clientName,$EmailBody);
		$EmailBody = str_replace("[#COMMENTS#]",$CommentsDetail->comments,$EmailBody);
		$EmailBody = str_replace("[#HOURS#]",$CommentsDetail->hour,$EmailBody);
		$EmailBody = str_replace("[#SUBMITTED_DATE#]",$CommentsDetail->tdate,$EmailBody);
		
		$subject="Project Revision by Client :: DPM Admin";
		$to = $clientEmail;
		$isHTML=true;			
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);
		
		return true;
			
	}
	
	public function sendfianlEmail($project_id,$designer_id,$commentId){
		
		$DesignerDetail=DesignersModel::create()->selectDesignerById($designer_id);						
		$ProejctsDetail=ProjectsModel::create()->selectProjectById($project_id);
		$ClientsDetail=ClientsModel::create()->selectClientById($ProejctsDetail->client_id);
		$CommentsDetail=$this->selectCommentById($commentId);						
		
		$EmailBody=$this->getSettings('CLIENT_SUBMIT_FINAL_EMAIL_ADMIN');
		$EmailBody = nl2br($EmailBody);
		$designerName = $DesignerDetail->designer_fname." ".$DesignerDetail->designer_lname;
		$designerEmail = $DesignerDetail->designer_email;
		$clientName = $ClientsDetail->client_fname." ".$ClientsDetail->client_lname;
		$clientEmail = $ClientsDetail->client_email;
		$projectName = $ProejctsDetail->initiative_project;
		$EmailBody = str_replace("[#DESIGENRNAME#]",$designerName,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENT_NAME#]",$clientName,$EmailBody);
		$EmailBody = str_replace("[#COMMENTS#]",$CommentsDetail->comments,$EmailBody);
		$EmailBody = str_replace("[#HOURS#]",$CommentsDetail->hour,$EmailBody);
		$EmailBody = str_replace("[#SUBMITTED_DATE#]",$CommentsDetail->tdate,$EmailBody);
		
		$adminEmail=$this->getSettings('MAIL_SEND_FROM_EMAIL');
		$subject="Project Finalized by Client :: DPM Admin";
		$to = $adminEmail;
		$isHTML=true;					
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);						
		
		$EmailBody=$this->getSettings('CLIENT_SUBMIT_FINAL_EMAIL_DESIGNER');
		$EmailBody = nl2br($EmailBody);
		$EmailBody = str_replace("[#DESIGENRNAME#]",$designerName,$EmailBody);
		$EmailBody = str_replace("[#PROJECTNAME#]",$projectName,$EmailBody);
		$EmailBody = str_replace("[#CLIENT_NAME#]",$clientName,$EmailBody);
		$EmailBody = str_replace("[#COMMENTS#]",$CommentsDetail->comments,$EmailBody);
		$EmailBody = str_replace("[#HOURS#]",$CommentsDetail->hour,$EmailBody);
		$EmailBody = str_replace("[#SUBMITTED_DATE#]",$CommentsDetail->tdate,$EmailBody);
		
		$subject="Project Finalized by Client :: DPM Admin";
		$to = $clientEmail;
		$isHTML=true;			
		
		$this->sendEmail($to,$subject,$EmailBody,$isHTML);
		
		return true;
			
	}
	public function checkuploadfileext($file, $exts){
		$validExtArr = explode(",",$exts);
		$filerealname = basename($file['name']);
		   $file = explode('.',basename($file['name']));
		   $ext = ".".$file[count($file)-1];
			if(!in_array($ext,$validExtArr)){
				return false;
			}
			else{
				return true;
			}
		
	}
	public function checkuploadfilesize($file, $width, $height, $flg){
		$test = getimagesize($file['tmp_name']);
		$width_x = $test[0];
		$height_x = $test[1];
		if($flg){
			if($width>=$width_x and $height>=$height_x){
				return true;
			}else{
				return false;
			}
		}else{
			if($width>=$width_x and $height>=$height_x){
				return true;
			}else{
				return false;
			}
		}
		exit;
		
	}
}
?>