<?php
/*
Project Name: Jnannys
File Name: expquotes_controller.php. Controller File
Description: The file handles all admin side common pages function requests.
*/

require_once './lib/simplemvcwf/view_data.php';
require_once './lib/simplemvcwf/controller_base.php';
require_once './model/expquotes_model.php';
require_once './utility/date_time_utility.php';

class ExpquotesController Extends ControllerBase {
    function index($actionValues=array()) {		
		// Gateway function for common pages actions in Admin side. All the actions will go through this function in Admin side.
		if($this->checkLogin()){			// Check for admin login. The functions only can be accessed by logged in admin user.
			if(count($actionValues) > 0){	// Look for any action called. Call action related function.
				$ctrlAction=$actionValues[0];
				if(!empty($ctrlAction))
				$this->$ctrlAction();
			}
		}else{
			// Render the action with template
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}
		
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	        
				$expquotes = ExpquotesModel::create()->selectPages($start,PAGING_LIMIT);		// List Common Pages.
				$this->viewData['expquotes'] = $expquotes;
				$totalpages = ExpquotesModel::create()->selectCount();					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('expquotes/index', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}
    }    
	function msgs(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	        
				$Msgs = ExpquotesModel::create()->selectPages_msgs($start,PAGING_LIMIT,$_POST['expquote_id']);		// List Common Pages.
				$this->viewData['Msgs'] = $Msgs;
				$totalpages = ExpquotesModel::create()->selectCount_msgs($_POST['expquote_id']);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('expquotes/msgs', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}exit;
	}
	// Check for admin login 
	function checkLogin(){
		if(isset($_SESSION['user_admin_login']) && $_SESSION['user_admin_login'] != ""){
			return true;
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}
	}
	// Function to add common pages. Function being called form admin side.
	function Add(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
				if(ExpquotesModel::create()->checkExpquote($_POST["name"])){
					$action_msg = ExpquotesModel::create()->insertRecord($_POST,$_FILES);
					$this->viewData['error_msg'] = $action_msg;
				}else{
					$Expquotes = new stdClass();
					foreach ($_POST as $key => $value){
							$Expquotes->$key = $value;
						}
					$this->viewData['expquotes'] =$Expquotes ;
					$this->viewData['error_msg'] = CommonModel::create()->getSettings('EXPQUOTE_NAME_EXISTS');;
				}
			}
			$totalpages = ExpquotesModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('expquotes/add', 'admin_base_template');
		}
	}
	function AddMsg(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
					$action_msg = ExpquotesModel::create()->insertRecord_msg($_POST,$_FILES);
					$this->viewData['error_msg'] = $action_msg;
				
			}
			$totalpages = DownloadsModel::create()->selectCount_cat();
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('expquotes/addmsg', 'admin_base_template');
		}
	}
	// Function to edit common pages. Function being called form admin side.
	function Edit(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit" && $_POST['expquote_id'] != ""){
				if(ExpquotesModel::create()->checkExpquote($_POST["name"],$_POST["expquote_id"])){
					$action_msg = ExpquotesModel::create()->editRecord($_POST,$_FILES);
					$this->viewData['error_msg'] = $action_msg;
				}else{
					$userDetails = new stdClass();
					foreach ($_POST as $key => $value){
							$userDetails->$key = $value;
						}
					$this->viewData['error_msg'] = CommonModel::create()->getSettings('EXPQUOTE_NAME_EXISTS');
				}
				
			}
			$id = $_REQUEST['expquote_id'];
			$userDetails = ExpquotesModel::create()->selectPagesByPageId($id);	
			$totalpages = ExpquotesModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('expquotes/add', 'admin_base_template');
		}
	}
	function EditMsg(){		
		if($this->checkLogin()){
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit" && $_POST['expquote_id'] != ""){
					$action_msg = ExpquotesModel::create()->editRecord_msg($_POST,$_FILES);
					$this->viewData['error_msg'] = $action_msg;
				
				
			}
			$id = $_REQUEST['msg_id'];
			$userDetails = ExpquotesModel::create()->selectPagesByPageId_msg($id);	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('expquotes/addmsg', 'admin_base_template');
		}
	}
	// Function to delete the common page from admin panel.
	function Delete(){		
		if($this->checkLogin()){			
			$recId = $_POST['expquote_id'];		
			$this->viewData['error_msg'] = ExpquotesModel::create()->delete($recId);	
		}
	}
	function DeleteMsg(){		
		if($this->checkLogin()){			
			$recId = $_POST['msg_id'];		
			$this->viewData['error_msg'] = ExpquotesModel::create()->deletemsg($recId);	
		}
		$Msgs = ExpquotesModel::create()->selectPages_msgs($start,PAGING_LIMIT,$_POST['expquote_id']);		// List Common Pages.
				$this->viewData['Msgs'] = $Msgs;
				$totalpages = ExpquotesModel::create()->selectCount_msgs($_POST['expquote_id']);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('expquotes/msgs', 'admin_base_template');
				exit;
	}
	// Function to change page display status 
	function ChangeStatus(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = ExpquotesModel::create()->changeStatus($_POST);		
		}
	}
	function ChangeStatus_msg(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = ExpquotesModel::create()->changeStatus_msg($_POST);		
		}
		$Msgs = ExpquotesModel::create()->selectPages_msgs($start,PAGING_LIMIT,$_POST['expquote_id']);		// List Common Pages.
				$this->viewData['Msgs'] = $Msgs;
				$totalpages = ExpquotesModel::create()->selectCount_msgs($_POST['expquote_id']);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('expquotes/msgs', 'admin_base_template');
	exit;
	}	
	
}
?>