<?php 
/**
* CUSTOM FUNCTIONS
* By: Zul Zolkaffly
*/

/**
* DISPLAY OBJECT AND ITS PROPERTIES
* FOR DEBUGGING PURPOSE
*/
if(!function_exists('debug')) {
	function debug() {
		$vars = func_get_args();
		if($vars && !empty($vars)) {
			echo "<pre>";
			foreach($vars as $item) {
				print_r($item);
				echo "<br />";
			}
			echo "</pre><br />";
		}
	}
}

/**
* CHECK ACCESS INTO DASHBOARD SITE
* INITIALIZE DASHBOARD TEMPLATE
* GET AND PASS DATA
*/
if(!function_exists('checkDashboard')) {
	function checkDashboard() {
		
		$CI = & get_instance();
		$_c = strtolower($CI->router->fetch_class());
		$_a = strtolower($CI->router->fetch_method());
		
		$CI->load->model('setup/Setupsystem','setup');
		
		if($user = $CI->session->userdata('logged_in')) {
			$projectID = $CI->session->userdata('projectID');
			$acceptedClass = array('login', 'logout', 'project', 'v', 'voting', 'account', 'changelanguage');			
			
			$data['projectID'] = NULL;
			$data['user'] = $user;
			$data['system_setups'] = $CI->setup->getAllSetups();
			
    	if(!$projectID && !in_array(strtolower($_c), $acceptedClass)){
				redirect(base_url().'project', 'refresh');
			} else {
				$data['projectID'] = $projectID;
			}
			
			$data['data']['user'] = $data['user'];
			$CI->templates->setPath('templates');
			$CI->templates->setLayout('dashboard');
			$CI->templates->setData($data);
		} else {
			$redirect = base_url()."login/index/".($CI->uri->uri_string());
			redirect($redirect);
		}
	}
}

/**
* INITIALIZE FRONT SITE TEMPLATE
* GET AND PASS DATA
*/
if(!function_exists('checkFront')) {
	function checkFront() {	
		$CI = & get_instance();
		$CI->load->model('general');
		$CI->load->model('setup/Setupsystem','setup');

		$user = $CI->session->userdata('logged_in');
		$data['user'] = $user;
		$data['mode'] = 'LOCAL';
		
		$data['data'] = $data;
		$CI->templates->setData($data);
		$CI->templates->setPath('templates');
		$CI->templates->setLayout('front');
	}
}

/**
* CHECK PROJECT PROPERTIES
*/
if(!function_exists('hasVoterStarted')) {
	function hasVoterStarted($projectID = NULL) {		
		$CI = & get_instance();
		$CI->load->model('project/projectmain','projmain');
		$projectID = ($projectID) ? $projectID : $CI->session->userdata('projectID');		
		return ($CI->projmain->hasVoterStarted($projectID)) ? TRUE : FALSE;
	}
}

if(!function_exists('isProjectStarted')) {
	function isProjectStarted($projectID = NULL) {		
		$CI = & get_instance();
		$CI->load->model('project/projectmain','projmain');
		$projectID = ($projectID) ? $projectID : $CI->session->userdata('projectID');		
		return ($CI->projmain->isProjectStarted($projectID)) ? TRUE : FALSE;
	}
}

if(!function_exists('isProjectEnded')) {
	function isProjectEnded($projectID = NULL) {		
		$CI = & get_instance();
		$CI->load->model('project/projectmain','projmain');
		$projectID = ($projectID) ? $projectID : $CI->session->userdata('projectID');		
		return ($CI->projmain->isProjectEnded($projectID)) ? TRUE : FALSE;
	}
}

if(!function_exists('isProjectStopped')) {
	function isProjectStopped($projectID = NULL) {		
		$CI = & get_instance();
		$CI->load->model('project/projectmain','projmain');
		$projectID = ($projectID) ? $projectID : $CI->session->userdata('projectID');		
		return ($CI->projmain->isProjectStopped($projectID)) ? TRUE : FALSE;
	}
}

if(!function_exists('isProjectPaid')) {
	function isProjectPaid($projectID = NULL) {		
		$CI = & get_instance();
		$CI->load->model('project/projectmain','projmain');
		$projectID = ($projectID) ? $projectID : $CI->session->userdata('projectID');		
		return ($CI->projmain->isProjectPaid($projectID)) ? TRUE : FALSE;
	}
}

























