<?php
/*
File Name: pages_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/pages_model.php';
require_once './utility/date_time_utility.php';

class PagesController 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;
				}	 
				$this->viewData['section_name'] = SectionsModel::create()->selectSectionName($_REQUEST["section_id"]);
				$this->viewData['subsection_name'] = SubsectionsModel::create()->selectSubsectionName($_REQUEST["subsection_id"]);
				$this->viewData['section_id'] = $_REQUEST["section_id"];      
				$this->viewData['subsection_id'] = $_REQUEST["subsection_id"];      
				$this->viewData['pages'] = PagesModel::create()->selectPages($start,PAGING_LIMIT,$_REQUEST["section_id"],$_REQUEST["subsection_id"]);		// List Common Pages.
				$totalpages = PagesModel::create()->selectCount($_REQUEST["section_id"],$_REQUEST["subsection_id"]);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('pages/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(PagesModel::create()->checkPage($_POST["name"],$_REQUEST["section_id"],$_POST["subsection_id"])){
					$action_msg = PagesModel::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('PAGE_NAME_EXISTS');;
				}
			}
			$sectionName = SectionsModel::create()->getSectionName($_REQUEST["section_id"]);
			$this->viewData['sectionName'] = $sectionName->name;
			$subsectionName = SubsectionsModel::create()->getSubSectionName($_REQUEST["subsection_id"]);
			$this->viewData['subsectionName'] = $subsectionName->name;
			$totalpages = PagesModel::create()->selectCount($_REQUEST["section_id"],$_POST["subsection_id"]);
			$this->viewData['nume'] = $totalpages->cnt;	
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('pages/add', '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['subsection_id'] != ""){
				if(PagesModel::create()->checkPage($_POST["name"],$_REQUEST["section_id"],$_POST["subsection_id"],$_REQUEST["page_id"])){
					$action_msg = PagesModel::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('PAGE_NAME_EXISTS');
				}
				
			}
			$id = $_REQUEST['page_id'];
			$sectionName = SectionsModel::create()->getSectionName($_REQUEST["section_id"]);
			$this->viewData['sectionName'] = $sectionName->name;
			$subsectionName = SubsectionsModel::create()->getSubSectionName($_REQUEST["subsection_id"]);
			$this->viewData['subsectionName'] = $subsectionName->name;
			$userDetails = PagesModel::create()->selectPagesByPageId($id);	
			$totalpages = PagesModel::create()->selectCount($_REQUEST["section_id"],$_POST["subsection_id"]);
			$this->viewData['nume'] = $totalpages->cnt;	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('pages/add', 'admin_base_template');
		}
	}
	
	// Function to delete the common page from admin panel.
	function Delete(){		
		if($this->checkLogin()){			
			$recId = $_POST['page_id'];		
			$this->viewData['error_msg'] = PagesModel::create()->delete($recId);	
		}
	}
	
	// Function to change page display status 
	function ChangeStatus(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = PagesModel::create()->changeStatus($_POST);		
		}
	}
		
}
?>