<?php
/*
Project Name: Jnannys
File Name: singlepages_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/singlepages_model.php';
require_once './utility/date_time_utility.php';

class SinglepagesController 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;
				}	        
				$singlepages = SinglepagesModel::create()->selectPages($start,PAGING_LIMIT);		// List Common Pages.
				$this->viewData['singlepages'] = $singlepages;
				$totalpages = SinglepagesModel::create()->selectCount();					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('singlepages/index', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}
    }    
	
	// 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(SinglepagesModel::create()->checkSection($_POST["section"])){
					$action_msg = SinglepagesModel::create()->insertRecord($_POST);
					$this->viewData['error_msg'] = $action_msg;
				}else{
					$userDetails = new stdClass();
					foreach ($_POST as $key => $value){
							$userDetails->$key = $value;
						}
					$this->viewData['userDetails'] =$userDetails ;
					$this->viewData['error_msg'] = CommonModel::create()->getSettings('SECTION_NAME_EXISTS');;
				}
			}
			$totalpages = SinglepagesModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('singlepages/add', 'admin_base_template');
		}exit;
	}
	
	// Function to edit common pages. Function being called form admin side.
	function Edit(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit" && $_POST['id'] != ""){
				if(SinglepagesModel::create()->checkSection($_POST["section"],$_POST["id"])){
					$action_msg = SinglepagesModel::create()->editRecord($_POST);
					$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('SECTION_NAME_EXISTS');
				}
				
			}
			$id = $_REQUEST['id'];
			$userDetails = SinglepagesModel::create()->selectPagesByPageId($id);	
			$totalpages = SinglepagesModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('singlepages/add', 'admin_base_template');
		}
	}
	
	// Function to delete the common page from admin panel.
	function Delete(){		
		if($this->checkLogin()){			
			$recId = $_POST['id'];		
			$this->viewData['error_msg'] = SinglepagesModel::create()->delete($recId);	
		}
	}
	
	// Function to change page display status 
	function ChangeStatus(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = SinglepagesModel::create()->changeStatus($_POST);		
		}
	}
		
}
?>