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

class GamsController 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;
				}	
			
				$Gams = GamsModel::create()->selectPages($start,PAGING_LIMIT);		// List Common Pages.
				$this->viewData['Gams'] = $Gams;
				$totalpages = GamsModel::create()->selectCount();					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('gams/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(GamsModel::create()->checkCity($_POST["name"])){
					$action_msg = GamsModel::create()->insertRecord($_POST,$_FILES);
					$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('GAM_NAME_EXISTS');;
				}
			}
			$totalpages = GamsModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('gams/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['city_id'] != ""){
				if(GamsModel::create()->checkCity($_POST["name"],$_POST["city_id"])){
					$action_msg = GamsModel::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('GAM_NAME_EXISTS');
				}
				
			}
			$id = $_REQUEST['city_id'];
			$userDetails = GamsModel::create()->selectPagesByPageId($id);	
			$totalpages = GamsModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('gams/add', 'admin_base_template');
		}
	}
	
	// Function to delete the common page from admin panel.
	function Delete(){		
		if($this->checkLogin()){			
			$recId = $_POST['city_id'];		
			$this->viewData['error_msg'] = GamsModel::create()->delete($recId);	
		}
	}
	
	// Function to change page display status 
	function ChangeStatus(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = GamsModel::create()->changeStatus($_POST);		
		}
	}
		
}
?>