<?php
/*
Project Name: Zalawadivisha
File Name: Members_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/members_model.php';
require_once './utility/date_time_utility.php';

class MembersController 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;
				}	        
				$Members = MembersModel::create()->selectPages($start,PAGING_LIMIT);		// List Common Pages.
				$this->viewData['Members'] = $Members;
				$totalpages = MembersModel::create()->selectCount();					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('members/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 import(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
					$action_msg = MembersModel::create()->importRecord($_POST,$_FILES);
					$this->viewData['error_msg'] = $action_msg;
				
			}
			
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('members/import', 'admin_base_template');
		}
	}
	function import_family(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
					$action_msg = MembersModel::create()->importRecord_family($_POST,$_FILES);
					$this->viewData['error_msg'] = $action_msg;
				
			}
			
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('members/importfamily', 'admin_base_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(MembersModel::create()->checkMember($_POST["email"])){
					if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_MEMBERS)){
						$width = CommonModel::create()->getSettings('MEMBER_UPLOAD_WIDTH');
						$height = CommonModel::create()->getSettings('MEMBER_UPLOAD_HEIGHT');
						//if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfilesize($_FILES["simage"],$width,$height,true)){
							$action_msg = MembersModel::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('MEMBER_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('MEMBER_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('MEMBER_EMAIL_EXISTS');
				}
			}
			$countries = CountriesModel::create()->getAllCountries();
			$this->viewData['countries'] = $countries;
			
			$gams = GamsModel::create()->getAllGams();
			$this->viewData['gams'] = $gams;
				
			$totalpages = MembersModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('members/add', 'admin_base_template');
		}
	}
	function AddFamily(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
				if(MembersModel::create()->checkMember($_POST["email"])){
					if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_MEMBERS)){
						$width = CommonModel::create()->getSettings('MEMBER_UPLOAD_WIDTH');
						$height = CommonModel::create()->getSettings('MEMBER_UPLOAD_HEIGHT');
						//if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfilesize($_FILES["simage"],$width,$height,true)){
							$action_msg = MembersModel::create()->insertRecord_family($_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('MEMBER_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('FAMILY_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('FAMILY_EMAIL_EXISTS');
				}
			}
			$countries = CountriesModel::create()->getAllCountries();
			$this->viewData['countries'] = $countries;
				
			$totalpages = MembersModel::create()->selectCount();
			$this->viewData['nume'] = $totalpages->cnt;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('members/addfamily', '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['member_id'] != ""){
				if(MembersModel::create()->checkMember($_POST["email"],$_POST["member_id"])){
					if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_MEMBERS)){
						$width = CommonModel::create()->getSettings('MEMBER_UPLOAD_WIDTH');
						$height = CommonModel::create()->getSettings('MEMBER_UPLOAD_HEIGHT');
						//if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfilesize($_FILES["simage"],$width,$height,true)){
							$action_msg = MembersModel::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('MEMBER_INVALID_FILE');
						}*/
					}else{
						$userDetails = new stdClass();
						foreach ($_POST as $key => $value){
								$userDetails->$key = $value;
							}
						$this->viewData['error_msg'] = CommonModel::create()->getSettings('MEMBER_INVALID_FILE');
					}
				}else{
					$userDetails = new stdClass();
					foreach ($_POST as $key => $value){
							$userDetails->$key = $value;
						}
					$this->viewData['error_msg'] = CommonModel::create()->getSettings('MEMBER_EMAIL_EXISTS');
				}
				
			}
			$id = $_REQUEST['member_id'];
			$countries = CountriesModel::create()->getAllCountries();
			$this->viewData['countries'] = $countries;
			
			$gams = GamsModel::create()->getAllGams();
			$this->viewData['gams'] = $gams;
			
			$userDetails = MembersModel::create()->selectPagesByPageId($id);	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('members/add', 'admin_base_template');
		}
	}
	function EditFamily(){		
		if($this->checkLogin()){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit" && $_POST['member_id'] != ""){
					if($_FILES["fam_simage"]["name"]=='' or CommonModel::create()->checkuploadfileext($_FILES["fam_simage"],VALID_EXT_MEMBERS)){
						$width = CommonModel::create()->getSettings('MEMBER_UPLOAD_WIDTH');
						$height = CommonModel::create()->getSettings('MEMBER_UPLOAD_HEIGHT');
						//if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfilesize($_FILES["simage"],$width,$height,true)){
							$action_msg = MembersModel::create()->editRecord_family($_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('MEMBER_INVALID_FILE');
						}*/
					}else{
						$userDetails = new stdClass();
						foreach ($_POST as $key => $value){
								$userDetails->$key = $value;
							}
						$this->viewData['error_msg'] = CommonModel::create()->getSettings('FAMILY_INVALID_FILE');
					}
				
				
			}
			$id = $_REQUEST['family_id'];
			$countries = CountriesModel::create()->getAllCountries();
			$this->viewData['countries'] = $countries;
			
			$userDetails = MembersModel::create()->selectPagesByPageId_family($id);	
			//exit;
			$this->viewData['userDetails'] = $userDetails;		
			//echo $this->viewData['totalpages'];
			$this->renderWithTemplate('members/addfamily', 'admin_base_template');
		}
	}
	
	// Function to delete the common page from admin panel.
	function Delete(){		
		if($this->checkLogin()){			
			$recId = $_POST['member_id'];		
			$this->viewData['error_msg'] = MembersModel::create()->delete($recId);	
		}
	}
	function DeleteFamily(){		
		if($this->checkLogin()){			
			$recId = $_POST['family_id'];		
			$this->viewData['error_msg'] = MembersModel::create()->deletefamily($recId);	
			$_POST['error_msg']=	$error_msg;			
			$this->redirectto('members/viewfamily',$_POST);
		}
	}
	function Approve(){		
		if($this->checkLogin()){			
			$recId = $_POST['member_id'];		
			$this->viewData['error_msg'] = MembersModel::create()->approve($recId);	
		}
	}
	function Delete_request(){		
		if($this->checkLogin()){			
			$recId = $_POST['member_id'];		
			$this->viewData['error_msg'] = MembersModel::create()->delete_request($recId);	
		}
		$this->redirectto('members/requests');
	}
	
	// Function to change page display status 
	function ChangeStatus(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = MembersModel::create()->changeStatus($_POST);		
		}
	}
	
	// Function to change page display status 
	function ChangeStatus_family(){
		if($this->checkLogin()){
			$error_msg = MembersModel::create()->changeStatus_family($_POST);	
			$_POST['error_msg']=	$error_msg;			
			$this->redirectto('members/viewfamily',$_POST);
		}
	}
	function ChangeStatus_request(){
		if($this->checkLogin()){
			$this->viewData['error_msg'] = MembersModel::create()->changeStatus_request($_POST);		
		}
		$this->redirectto('members/requests');
	}
	function requests(){
				$Members = MembersModel::create()->selectRequests($start,PAGING_LIMIT);		// List Common Pages.
				$this->viewData['Members'] = $Members;
				$totalpages = MembersModel::create()->selectCountRequest();					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('members/requests', 'admin_base_template');
				exit;
	}
	function viewwishes(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	     
				if($_POST["family_id"]   !=''){
					$Msgs = MembersModel::create()->selectPages_wishes($start,PAGING_LIMIT,$_POST['family_id']);		// List Common Pages.
				}else{
					$Msgs = MembersModel::create()->selectPages_wishes($start,PAGING_LIMIT,$_POST['member_id']);		// List Common Pages.
				}
				
				$this->viewData['Msgs'] = $Msgs;
				$totalpages = MembersModel::create()->selectCount_wishes($_POST['member_id']);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('members/wishes', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}exit;
	}
	function viewmsgs(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	
				if($_POST["family_id"]   !=''){
					$Msgs = MembersModel::create()->selectPages_msgs($start,PAGING_LIMIT,$_POST['family_id']);		// List Common Pages.
				}else{
					$Msgs = MembersModel::create()->selectPages_msgs($start,PAGING_LIMIT,$_POST['member_id']);		// List Common Pages.
				}        
				
				$this->viewData['Msgs'] = $Msgs;
				$totalpages = MembersModel::create()->selectCount_msgs($_POST['member_id']);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('members/msgs', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}exit;
	}
	function viewfamily(){
		if($this->checkLogin()){
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	        
				$Msgs = MembersModel::create()->selectPages_family($start,PAGING_LIMIT,$_POST['member_id']);		// List Common Pages.
				$this->viewData['Msgs'] = $Msgs;
				$totalpages = MembersModel::create()->selectCount_family($_POST['member_id']);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('members/family', 'admin_base_template');
				
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}exit;
	}
	function DeleteWish(){		
		if($this->checkLogin()){			
			$recId = $_POST['wish_id'];		
			$this->viewData['error_msg'] = MembersModel::create()->deletewish($recId);	
		}
		$Msgs = MembersModel::create()->selectPages_wishes($start,PAGING_LIMIT,$_POST['member_id']);		// List Common Pages.
				$this->viewData['Msgs'] = $Msgs;
				$totalpages = MembersModel::create()->selectCount_wishes($_POST['member_id']);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('members/wishes', 'admin_base_template');
				exit;
	}
	function DeleteMsg(){		
		if($this->checkLogin()){			
			$recId = $_POST['wish_id'];		
			$this->viewData['error_msg'] = MembersModel::create()->deletemsg($recId);	
		}
		$Msgs = MembersModel::create()->selectPages_msgs($start,PAGING_LIMIT,$_POST['member_id']);		// List Common Pages.
				$this->viewData['Msgs'] = $Msgs;
				$totalpages = MembersModel::create()->selectCount_msgs($_POST['member_id']);					
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;
				$this->renderWithTemplate('members/msgs', 'admin_base_template');
				exit;
	}
		
}
?>