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

class DownloadsController 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;
				}	
				if($_REQUEST["List"]=="ChangeStatus"){
					$_POST["download_id"] = $_REQUEST["downcat_id"];
				}
				$Downloads = DownloadsModel::create()->selectPages($start,PAGING_LIMIT,$_POST["download_id"]);		// List Common Pages.
				$this->viewData['Downloads'] = $Downloads;
				$totalpages = DownloadsModel::create()->selectCount($_POST["download_id"]);		
				$this->viewData['downcat_id'] = $_POST["download_id"];			
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('downloads/index', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}
    }    
	function downcat(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	        
				$Downloads = DownloadsModel::create()->selectPages_cat($start,PAGING_LIMIT);		// List Common Pages.
				$this->viewData['Downloads'] = $Downloads;
				$totalpages = DownloadsModel::create()->selectCount_cat();					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('downloads/downcat', '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(CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_DOWNLOADS)){
					$action_msg = DownloadsModel::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('DOWNLOAD_INVALID_FILE');;
				}
				
			}
			$downcat = DownloadsModel::create()->getDownCatName($_REQUEST["downcat_id"]);
			$this->viewData['downcat'] = $downcat->catname;
			$totalpages = DownloadsModel::create()->selectCount($_REQUEST["downcat_id"]);
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('downloads/add', 'admin_base_template');
		}exit;
	}
	
	function AddCat(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
				if(DownloadsModel::create()->checkDowncat($_POST["title"])){
					$action_msg = DownloadsModel::create()->insertRecord_cat($_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('DOWNLOAD_NAME_EXISTS');;
				}
			}
			$totalpages = DownloadsModel::create()->selectCount_cat();
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('downloads/addcat', '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['download_id'] != ""){
				if(CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_DOWNLOADS)){
					$action_msg = DownloadsModel::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('DOWNLOAD_INVALID_FILE');;
				}
				
			}
			$id = $_REQUEST['download_id'];
			$downcat = DownloadsModel::create()->getDownCatName($_REQUEST["downcat_id"]);
			$this->viewData['downcat'] = $downcat->catname;
			$userDetails = DownloadsModel::create()->selectPagesByPageId($id);	
			$totalpages = DownloadsModel::create()->selectCount($_REQUEST["downcat_id"]);
			$this->viewData['nume'] = $totalpages->cnt;	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('downloads/add', 'admin_base_template');
		}
	}
	function EditCat(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit" && $_POST['download_id'] != ""){
				if(DownloadsModel::create()->checkDowncat($_POST["title"],$_POST["download_id"])){
					$action_msg = DownloadsModel::create()->editRecord_cat($_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('DOWNLOAD_NAME_EXISTS');
				}
				
			}
			$id = $_REQUEST['download_id'];
			$userDetails = DownloadsModel::create()->selectPagesByPageId_cat($id);	
			$totalpages = DownloadsModel::create()->selectCount_cat();
			$this->viewData['nume'] = $totalpages->cnt;	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('downloads/addcat', 'admin_base_template');
		}
	}
	
	// Function to delete the common page from admin panel.
	function Delete(){		
		if($this->checkLogin()){			
			$recId = $_POST['download_id'];		
			$this->viewData['error_msg'] = DownloadsModel::create()->delete($recId);	
		}
		$Downloads = DownloadsModel::create()->selectPages($start,PAGING_LIMIT,$_POST["downcat_id"]);		// List Common Pages.
				$this->viewData['Downloads'] = $Downloads;
				$totalpages = DownloadsModel::create()->selectCount($_POST["downcat_id"]);		
				$this->viewData['downcat_id'] = $_POST["downcat_id"];			
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('downloads/index', 'admin_base_template');exit;
	}
	function DeleteCat(){		
		if($this->checkLogin()){			
			$recId = $_POST['download_id'];		
			$this->viewData['error_msg'] = DownloadsModel::create()->deletecat($recId);	
		}
		$this->redirectto('downloads/downcat');
	}
	// Function to change page display status 
	function ChangeStatus(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = DownloadsModel::create()->changeStatus($_POST);		
		}
	}
	function ChangeStatus_cat(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = DownloadsModel::create()->changeStatus_cat($_POST);		
		}
		$this->redirectto('downloads/downcat');
	}
		
}
?>