<?php
require_once './lib/simplemvcwf/view_data.php';
require_once './lib/simplemvcwf/controller_base.php';
require_once './model/admin_model.php';

class AdminController Extends ControllerBase {
    function index($actionValues=array()) {		
        // Render the action with template
		
		if(count($actionValues) > 0){	// Look for any action called. Call action related function.
				$ctrlAction=$actionValues[0];
				$args=array();
				if(count($actionValues) > 1){	
					$args = array_slice($actionValues,1,count($actionValues));					
				}								
				if(!empty($ctrlAction))
				$this->$ctrlAction($args);
			}		
		if($this->checkLogin()){			
						$app_events = EventsModel::create()->needapprove();
						$this->viewData['app_events'] = $app_events;	
						$app_updates = UpdatesModel::create()->needapprove();
						$this->viewData['app_updates'] = $app_updates;	
						$app_msgs = ExpquotesModel::create()->needapprove();
						$this->viewData['app_msgs'] = $app_msgs;	
	        $this->renderWithTemplate('admin/index', 'admin_base_template');
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}
    }    
	
	function checkLogin(){
		if(isset($_SESSION['user_admin_login']) && $_SESSION['user_admin_login'] != ""){
			return true;
		}else{
			$this->renderWithTemplate('admin/login', 'admin_login_template');
		}
	}
	
	function authentication(){		
			if(AdminModel::create()->authenticateUser($_POST)){		
				$this->redirectto("admin/index"); 	
				//$this->index();		
				//$this->renderWithTemplate('admin/index', 'admin_base_template');
			}else{
				$this->viewData['error_msg'] = "Invalid Login details.";
				$this->renderWithTemplate('admin/login', 'admin_login_template');
			}			
	}
	
	function profile(){	
		if($this->checkLogin()){		        				
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
				if($_FILES["simage"]["name"]=='' or CommonModel::create()->checkuploadfileext($_FILES["simage"],VALID_EXT_MEMBERS)){
					$action_msg = AdminModel::create()->profile($_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');
				}
			}		
			
			
			$UsersDetail = AdminModel::create()->getAdminDetails($_SESSION['user_admin_login_id']);	
			
			$this->viewData['UsersDetail'] = $UsersDetail;		
			$this->renderWithTemplate('admin/profile', 'admin_base_template');			
		}
	}
	function changepassword(){	
		if($this->checkLogin()){		        				
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
				$action_msg = AdminModel::create()->changepassword($_POST);
				$this->viewData['error_msg'] = $action_msg;
			}		
			
			
			$this->renderWithTemplate('admin/changepassword', 'admin_base_template');			
		}
	}
	
	function errorlog(){	
		if($this->checkLogin()){		        		
			
			if(isset($_POST['List']) && $_POST['List'] == "Delete"){
				$action_msg = AdminModel::create()->deleteErrors($_POST);
				$this->viewData['error_msg'] = $action_msg;
			}
			
				$start = $_POST['start'];
				if(empty($start)){
					$start=0;
				}	  
				      
				$this->viewData['errorlogs'] = AdminModel::create()->selectErrors($start,PAGING_LIMIT);		
				$totalpages = AdminModel::create()->selectErrorCount();									
				$this->viewData['totalpages'] = $totalpages->cnt;
				$this->viewData['start'] = $start;		
				$this->renderWithTemplate('admin/errorlog', 'admin_base_template');			
		}
	}
	
	function forgotpassword(){
			
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){				
				$action_msg = AdminModel::create()->forgotpassword($_POST);
				$this->viewData['error_msg'] = $action_msg;
			}	
			
		$this->renderWithTemplate('admin/forgotpwd', 'admin_login_template');
	}
	
	
	
	function changesettings($actionValues=array()){	
		$tabArr = array("designersTab","miscTab","emailTab");
		$tabValArr = array("Members","Misc.","Emails");
		$site_messages=array();
		
		if($this->checkLogin()){
			if(isset($_POST['Submit']) && $_POST['Submit'] == "Submit"){
				$action_msg = AdminModel::create()->changeSettings($_POST);
				$this->viewData['error_msg'] = $action_msg;
				$this->viewData['option_cat_val'] = $_POST['option_cat'];
			}
			if($actionValues[0]==''){
				$actionValues[0] = '1';
			}
			$this->viewData['configoption']=$actionValues[0];
			$clients_settings = AdminModel::create()->selectSettings(1);
			//$designers_settings = AdminModel::create()->selectSettings(2);
			$projects_settings = AdminModel::create()->selectSettings(3);
			$site_settings = AdminModel::create()->selectSettings(4);
			$misc_settings = AdminModel::create()->selectSettings(5);
			$email_settings = AdminModel::create()->selectSettings(6);
			
			$site_messages[] = $clients_settings;
			//$site_messages[] = $designers_settings;
			$site_messages[] = $projects_settings;
			$site_messages[] = $site_settings;
			$site_messages[] = $misc_settings;
			$site_messages[] = $email_settings;
						
			
			$this->viewData['tabArr'] = $tabArr;
			$this->viewData['tabValArr'] = $tabValArr;
			$this->viewData['site_messages'] = $site_messages;
			$this->renderWithTemplate('admin/site_settings', 'admin_base_template');			
			
		}
	}
	function logout(){		
			$_SESSION['user_admin_login'] = false;
			$_SESSION['user_admin_login_id'] = false;
			$this->index();
	}
	
}
?>