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

class GalleriesController 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(!isset($_REQUEST['start_client'])) {                         
				$start_client = 0;
				}
				else
				{
				 $start_client = $_REQUEST['start_client'];
				}
				
				$eu = ($start_client - 0); 
				$limit = 15;                                 
				$th = $eu + $limit; 
				$back = $eu - $limit; 
				$next = $eu + $limit; 
				$this->viewData['start_client'] = $start_client;
				$this->viewData['eu'] = $eu;
				$this->viewData['limit'] = $limit;
				$this->viewData['th'] = $th;
				$this->viewData['back'] = $back;
				$this->viewData['next'] = $next;
				
				
				if($_REQUEST["List"]=="ChangeStatus"){
					$_POST["gallery_id"] = $_REQUEST["gallerycat_id"];
				}
				if(isset($_REQUEST["act"]) and $_REQUEST["act"]=='ADD'){
				$flg=true;
					for($i=1;$i<=($_POST["hdnLine"]);$i++)
					{
						if($_FILES["fileUpload".$i]["name"]!=''){
							if(CommonModel::create()->checkuploadfileext($_FILES["fileUpload".$i],VALID_EXT_GALLERY))
							{
								
							}else{
								$flg=false;
							}
						}
						
					}
					if($flg==true){
						$action_msg = GalleriesModel::create()->insertRecord($_POST);
						$this->viewData['error_msg'] = $action_msg;
					}else{
						$this->viewData['error_msg'] = CommonModel::create()->getSettings('GALLERY_INVALID_FILE');
					}
				}
				if(isset($_REQUEST["act"]) and $_REQUEST["act"]=='EDIT'){
					$action_msg = GalleriesModel::create()->editRecord($_POST);
					$this->viewData['error_msg'] = $action_msg;
				}
				if(isset($_REQUEST["act"]) and $_REQUEST["act"]=='DELETE'){
					$action_msg = GalleriesModel::create()->delete($_POST,$_FILES);
					$this->viewData['error_msg'] = $action_msg;
				}
				$Galleries = GalleriesModel::create()->selectPages($start_client,$limit,$_POST["gallery_id"]);		// List Common Pages.
				$this->viewData['Galleries'] = $Galleries;
				$totalpages = GalleriesModel::create()->selectCount($_POST["gallery_id"]);		
				$this->viewData['gallerycat_id'] = $_POST["gallery_id"];			
				$this->viewData['totalpages'] = $totalpages->cnt;
				$gallerycat = GalleriesModel::create()->getGalleryCatName($_REQUEST["gallery_id"]);
				$this->viewData['gallerycat'] = $gallerycat->gallerytitle;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('galleries/index', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}
    }    
	function gallerycat(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	        
				$Galleries = GalleriesModel::create()->selectPages_cat($start,PAGING_LIMIT);		// List Common Pages.
				$this->viewData['Galleries'] = $Galleries;
				$totalpages = GalleriesModel::create()->selectCount_cat();					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('galleries/gallerycat', '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"){
					$action_msg = GalleriesModel::create()->insertRecord($_POST,$_FILES);
					$this->viewData['error_msg'] = $action_msg;
				
			}
			$gallerycat = GalleriesModel::create()->getDownCatName($_REQUEST["gallerycat_id"]);
			$this->viewData['gallerycat'] = $gallerycat->catname;
			$totalpages = GalleriesModel::create()->selectCount($_REQUEST["gallerycat_id"]);
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('galleries/add', 'admin_base_template');
		}exit;
	}
	
	function AddGallery(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
				if(GalleriesModel::create()->checkGallerycat($_POST["gallerytitle"])){
					if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_GALLERY)){
						$width = CommonModel::create()->getSettings('GALLERY_UPLOAD_WIDTH');
						$height = CommonModel::create()->getSettings('GALLERY_UPLOAD_HEIGHT');
						//if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfilesize($_FILES["simage"],$width,$height,true)){
							$action_msg = GalleriesModel::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('GALLERY_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('GALLERY_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('GALLERY_NAME_EXISTS');;
				}
			}
			$totalpages = GalleriesModel::create()->selectCount_cat();
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('galleries/addgallery', '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['gallery_id'] != ""){
					$action_msg = GalleriesModel::create()->editRecord($_POST,$_FILES);
					$this->viewData['error_msg'] = $action_msg;
				
				
			}
			$id = $_REQUEST['gallery_id'];
			$gallerycat = GalleriesModel::create()->getDownCatName($_REQUEST["gallerycat_id"]);
			$this->viewData['gallerycat'] = $gallerycat->catname;
			$userDetails = GalleriesModel::create()->selectPagesByPageId($id);	
			$totalpages = GalleriesModel::create()->selectCount($_REQUEST["gallerycat_id"]);
			$this->viewData['nume'] = $totalpages->cnt;	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('galleries/add', 'admin_base_template');
		}
	}
	function EditGallery(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit" && $_POST['gallery_id'] != ""){
				if(GalleriesModel::create()->checkGallerycat($_POST["title"],$_POST["gallery_id"])){
					if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_GALLERY)){
						$width = CommonModel::create()->getSettings('GALLERY_UPLOAD_WIDTH');
						$height = CommonModel::create()->getSettings('GALLERY_UPLOAD_HEIGHT');
						//if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfilesize($_FILES["simage"],$width,$height,true)){
							$action_msg = GalleriesModel::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('GALLERY_INVALID_FILE');
						}*/
					}else{
						$userDetails = new stdClass();
						foreach ($_POST as $key => $value){
								$userDetails->$key = $value;
							}
						$this->viewData['error_msg'] = CommonModel::create()->getSettings('GALLERY_INVALID_FILE');
					}
				}else{
					$userDetails = new stdClass();
					foreach ($_POST as $key => $value){
							$userDetails->$key = $value;
						}
					$this->viewData['error_msg'] = CommonModel::create()->getSettings('GALLERY_NAME_EXISTS');
				}
				
			}
			$id = $_REQUEST['gallery_id'];
			$userDetails = GalleriesModel::create()->selectPagesByPageId_cat($id);	
			$totalpages = GalleriesModel::create()->selectCount_cat();
			$this->viewData['nume'] = $totalpages->cnt;	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('galleries/addgallery', 'admin_base_template');
		}
	}
	
	// Function to delete the common page from admin panel.
	function Delete(){		
		if($this->checkLogin()){			
			$imgid = $_POST['ImgId'];
			$Imgname = $_REQUEST['Imgname'];	
			$this->viewData['error_msg'] = GalleriesModel::create()->delete($imgid,$Imgname);	
		}
	}
	function Deletegallery(){		
		if($this->checkLogin()){			
			$recId = $_POST['gallery_id'];		
			$this->viewData['error_msg'] = GalleriesModel::create()->deletegallery($recId);	
		}
		$this->redirectto('galleries/gallerycat');
	}
	
	// Function to change page display status 
	function ChangeStatus(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = GalleriesModel::create()->changeStatus($_POST);		
		}
	}
	function ChangeStatus_cat(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = GalleriesModel::create()->changeStatus_cat($_POST);		
		}
		$this->redirectto('galleries/gallerycat');
	}
		
}
?>