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

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