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

class BannersController 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;
				}	        
				$Banners = BannersModel::create()->selectPages($start,PAGING_LIMIT,$_REQUEST["etype"]);		// List Common Pages.
				$this->viewData['Banners'] = $Banners;
				$totalpages = BannersModel::create()->selectCount();					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('banners/index', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}
    }    
	function top(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	        
				$Banners = BannersModel::create()->selectPages($start,PAGING_LIMIT,1);		// List Common Pages.
				$this->viewData['Banners'] = $Banners;
				$totalpages = BannersModel::create()->selectCount(1);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->viewData['btype'] = 1;
				$this->renderWithTemplate('banners/index', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}exit;
	}
	function left(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	        
				$Banners = BannersModel::create()->selectPages($start,PAGING_LIMIT,2);		// List Common Pages.
				$this->viewData['Banners'] = $Banners;
				$totalpages = BannersModel::create()->selectCount(2);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->viewData['btype'] = 2;
				$this->renderWithTemplate('banners/index', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}exit;
	}
	function right(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	        
				$Banners = BannersModel::create()->selectPages($start,PAGING_LIMIT,3);		// List Common Pages.
				$this->viewData['Banners'] = $Banners;
				$totalpages = BannersModel::create()->selectCount(3);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->viewData['btype'] = 3;
				$this->renderWithTemplate('banners/index', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}exit;
	}
	function bottom(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	        
				$Banners = BannersModel::create()->selectPages($start,PAGING_LIMIT,4);		// List Common Pages.
				$this->viewData['Banners'] = $Banners;
				$totalpages = BannersModel::create()->selectCount(4);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->viewData['btype'] = 4;
				$this->renderWithTemplate('banners/index', '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()){
			$this->viewData['btype'] = $_REQUEST["btype"];
			$wh = BannersModel::create()->getSBannerprop($_REQUEST["btype"]);
			$this->viewData['bannerwidth'] = $wh[0]->bannerwidth;	
			$this->viewData['bannerheight'] = $wh[0]->bannerheight;	
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
				if(BannersModel::create()->checkBanner($_POST["title"])){
					if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_BANNERS)){
						if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfilesize($_FILES["simage"],$wh[0]->bannerwidth,$wh[0]->bannerheight,true)){
							$action_msg = BannersModel::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('BANNER_INVALID_FILE');;
						}
					}else{
						$userDetails = new stdClass();
						foreach ($_POST as $key => $value){
								$userDetails->$key = $value;
							}
						$this->viewData['userDetails'] =$userDetails ;
						$this->viewData['error_msg'] = CommonModel::create()->getSettings('BANNER_INVALID_FILE');;
					}
				}else{
					$userDetails = new stdClass();
					foreach ($_POST as $key => $value){
							$userDetails->$key = $value;
						}
					$this->viewData['userDetails'] =$userDetails ;
					$this->viewData['error_msg'] = CommonModel::create()->getSettings('BANNER_NAME_EXISTS');;
				}
				
			}
			$totalpages = BannersModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;	
			
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('banners/add', 'admin_base_template');
		}exit;
	}
	
	// Function to edit common pages. Function being called form admin side.
	function Edit(){		
		if($this->checkLogin()){
			$this->viewData['btype'] = $_REQUEST["btype"];	
			$wh = BannersModel::create()->getSBannerprop($_REQUEST["btype"]);
			$this->viewData['bannerwidth'] = $wh[0]->bannerwidth;	
			$this->viewData['bannerheight'] = $wh[0]->bannerheight;
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit" && $_POST['banner_id'] != ""){
				if(BannersModel::create()->checkBanner($_POST["title"],$_POST['banner_id'])){
					if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_BANNERS)){
						if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfilesize($_FILES["simage"],$wh[0]->bannerwidth,$wh[0]->bannerheight,true)){
							$action_msg = BannersModel::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('BANNER_INVALID_FILE');
						}
					}else{
						$userDetails = new stdClass();
						foreach ($_POST as $key => $value){
								$userDetails->$key = $value;
							}
						$this->viewData['error_msg'] = CommonModel::create()->getSettings('BANNER_INVALID_FILE');
					}
				}else{
					$userDetails = new stdClass();
					foreach ($_POST as $key => $value){
							$userDetails->$key = $value;
						}
					$this->viewData['error_msg'] = CommonModel::create()->getSettings('BANNER_NAME_EXISTS');
				}
				
			}
			$id = $_REQUEST['banner_id'];
			$userDetails = BannersModel::create()->selectPagesByPageId($id);	
			$totalpages = BannersModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('banners/add', 'admin_base_template');
		}
	}
	
	// Function to delete the common page from admin panel.
	function Delete(){		
		if($this->checkLogin()){			
			$recId = $_POST['banner_id'];		
			$this->viewData['error_msg'] = BannersModel::create()->delete($recId);	
			$Banners = BannersModel::create()->selectPages($start,PAGING_LIMIT,$_REQUEST["btype"]);		// List Common Pages.
				$this->viewData['Banners'] = $Banners;
				$totalpages = BannersModel::create()->selectCount(1);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->viewData['btype'] = $_REQUEST["btype"];
				$this->renderWithTemplate('banners/index', 'admin_base_template');	
		}exit;
	}
	
	// Function to change page display status 
	function ChangeStatus(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = BannersModel::create()->changeStatus($_POST);	
			$Banners = BannersModel::create()->selectPages($start,PAGING_LIMIT,$_REQUEST["btype"]);		// List Common Pages.
				$this->viewData['Banners'] = $Banners;
				$totalpages = BannersModel::create()->selectCount(1);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->viewData['btype'] = $_REQUEST["btype"];
				$this->renderWithTemplate('banners/index', 'admin_base_template');	
		}exit;
	}
	
	function BannerClicks(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	   
				$this->viewData['btype'] = $_REQUEST["btype"];     
				$this->viewData['bname'] = BannersModel::create()->getBannerName($_REQUEST["banner_id"]);     
				$Banners = BannersModel::create()->selectPages_bannersclick($start,PAGING_LIMIT,$_REQUEST["banner_id"]);		// List Common Pages.
				$this->viewData['Banners'] = $Banners;
				$totalpages = BannersModel::create()->selectCount_bannersclick();					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('banners/bannerclicks', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}exit;
	}
}
?>