<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Project extends CI_Controller {
	protected $projectID = 0;
	
	public function __construct(){
		parent::__construct();
		checkDashboard();
		$this->load->model('project/projectmain','projmain');
		
		$lang = ($this->session->userdata('language')) ? $this->session->userdata('language') : 'en';
		$this->projectID = $this->session->userdata('projectID');
		$this->session->set_userdata('menu', "project");
		$this->lang->load('project',$lang);
	}
	
	public function index() {
		$user = $this->session->userdata('logged_in');
		
		$data['current'] = ($this->projectID) ? $this->projmain->getProject($this->projectID) : NULL;
		$data['respond']['type'] = NULL;
		$data['respond']['message'] = NULL;
		if($flashdata = $this->flashdata->get()) {
			$data['respond']['type'] = (isset($flashdata['type'])) ? strtoupper($flashdata['type']) : NULL;
			$data['respond']['message'] = (isset($flashdata['message'])) ? $flashdata['message'] : NULL;
		}
		
		$data['projects'] = $this->projmain->getAllProjects($user['userID']);
		
		$this->layouts->view('project/index', array(), $data);
	}
	
	public function detail($id = NULL) {
		if(!$id) $id = $this->projectID;
		if(!$id) redirect(base_url()."project");
		
		$data['respond']['type'] = NULL;
		$data['respond']['message'] = NULL;
		if($flashdata = $this->flashdata->get()) {
			$data['respond']['type'] = (isset($flashdata['type'])) ? strtoupper($flashdata['type']) : NULL;
			$data['respond']['message'] = (isset($flashdata['message'])) ? $flashdata['message'] : NULL;
		}
		
		$data['project'] = $this->projmain->getProject($id);
		$data['voters'] = $this->projmain->getProjectVoters($id);
		
		$data['pid'] = $id;
		$this->layouts->view('project/detail', array(), $data);
	}
	
	public function add() {		
		$data['respond']['type'] = NULL;
		$data['respond']['message'] = NULL;
		if($flashdata = $this->flashdata->get()) {
			$data['respond']['type'] = (isset($flashdata['type'])) ? strtoupper($flashdata['type']) : NULL;
			$data['respond']['message'] = (isset($flashdata['message'])) ? $flashdata['message'] : NULL;
		}
		
		$this->layouts->view('project/add', array(), $data);
	}
	
	public function save() {
		$data['ID'] = (isset($_POST['ID']) && trim($_POST['ID'])) ? trim($_POST['ID']) : 0;
		$data['title'] = (isset($_POST['title']) && trim($_POST['title'])) ? trim($_POST['title']) : NULL;
		$data['startDT'] = (isset($_POST['startDT']) && trim($_POST['startDT'])) ? trim($_POST['startDT']) : NULL;
		$data['endDT'] = (isset($_POST['endDT']) && trim($_POST['endDT'])) ? trim($_POST['endDT']) : NULL;
		$data['venue'] = (isset($_POST['venue']) && trim($_POST['venue'])) ? trim($_POST['venue']) : NULL;
		
		$resetBanner = (isset($_POST['resetBanner']) && trim($_POST['resetBanner'])) ? TRUE : FALSE;
		
		$proceed = TRUE;
		$message = NULL;
		
		if(trim( $data['title'] ) === "") { $proceed = FALSE; $message[] = "Please enter project title."; }	
		if(trim( $data['startDT'] ) === "") { $proceed = FALSE; $message[] = "Please enter project start date."; }	
		
		$url = base_url()."project/".(($data['ID']) ? "detail/".$data['ID'] : "add");
		if($proceed) {
			$user = $this->session->userdata('logged_in');
			$userid = ($user && $user['userID']) ? $user['userID'] : 0;
			
			$language = array();
			if($lang = (isset($_POST['language']) && !empty($_POST['language'])) ? $_POST['language'] : NULL) {
				foreach($lang as $key=>$value) {
					if($value) $language[] = $key;
				}
			}
			$data['language'] = ($language) ? implode("|",$language) : NULL;
			unset($lang, $language);
			
			$data['showResultToVoter'] = (isset($_POST['showResultToVoter']) && $_POST['showResultToVoter']) ? 1 : 0;
			$data['color'] = (isset($_POST['color']) && $_POST['color']) ? str_replace('#','',trim($_POST['color'])) : NULL;
			
			if($b = $this->projmain->saveProject($data, $userid)) {
				if($resetBanner) {
					if($this->projmain->resetBanner($b)) {
						$proceed = TRUE;
						$message = NULL;
					} else {
						$proceed = FALSE;
						$message[] = "Fail to reset project banner";
					}
				} else {
					if($_FILES && !empty($_FILES) && isset($_FILES['banner']) && $_FILES['banner']['size']) {
						$r = $this->uploadProjectBanner($b);
						if($r['message'] == 'SUCCESS') {
							if($this->projmain->saveBanner($b, $r['file'])) {
								$proceed = TRUE;
								$message = NULL;
							} else {
								$proceed = FALSE;
								$message[] = "Fail to save banner image";
							}
						} else {
							$proceed = FALSE;
							$message[] = "Fail to upload banner image";
						}
					} else {
						$proceed = TRUE;
						$message = NULL;
					}
				}
				/*
				$proceed = TRUE;
				$message = NULL;
				*/
				$url = base_url()."project/".(($b) ? "detail/".$b : "index");
			} else {
				$proceed = FALSE;
				$message[] = "Fail to save due to technical issue";
			}
		}
		
		if(!$proceed && $message && !empty($message)) {
			$this->flashdata->clear();
			$this->flashdata->set(implode("<br />",$message),'message');
			$this->flashdata->set('FAIL','type');
		} else {
			$this->flashdata->clear();
			$this->flashdata->set('SUCCESS','message');
			$this->flashdata->set('SUCCESS','type');
		}
		
		redirect($url);
	}
	
	public function stop() {
		$this->load->model('account/accountmain','accmain');
		$user = $this->session->userdata('logged_in');
		$projectID = (isset($_POST['projectID']) && trim($_POST['projectID'])) ? trim($_POST['projectID']) : 0;
		
		$proceed = TRUE;
		$message = NULL;
		if(!$projectID) {
			$proceed = FALSE;
			$message[] = "Project is invalid.";
		} else {			
			if($this->projmain->checkProjectExists($projectID)) {
				$userpass = (isset($_POST['userpass']) && trim($_POST['userpass'])) ? trim($_POST['userpass']) : NULL;
				$userpassconf = (isset($_POST['userpassconf']) && trim($_POST['userpassconf'])) ? trim($_POST['userpassconf']) : NULL;
				
				if(trim( $userpass ) === "") { $proceed = FALSE; $message[] = "Please enter your password."; }
				if(trim( $userpassconf ) === "") { $proceed = FALSE; $message[] = "Please enter your password confirmation."; }
		
				if($proceed) {
					$oldpass = $this->accmain->getOldPassword($user['userID']);
					if(md5(trim($userpass)) == trim($oldpass)) {
						if(trim($userpass) != trim($userpassconf)) {
							$proceed = FALSE;
							$message[] = "Password confirmation not matched.";
						} else {
							if($this->projmain->stopProject($projectID, $user['userID'])) {
								$proceed = TRUE;
								$message = NULL;
							} else {
								$proceed = FALSE;
								$message[] = "Unable to stop project due to technical issue.";
							}
						}
					} else {
						$proceed = FALSE;
						$message[] = "Password is not correct.";
					}
				}				
			} else {
				$proceed = FALSE;
				$message[] = "Project does not exist.";
			}
		}
		
		if(!$proceed && $message && !empty($message)) {
			$this->flashdata->clear();
			$this->flashdata->set(implode("<br />",$message),'message');
			$this->flashdata->set('FAIL','type');
		} else {
			$this->flashdata->clear();
			$this->flashdata->set('SUCCESS','message');
			$this->flashdata->set('SUCCESS','type');
		}
		
		$url = base_url()."project".(($projectID) ? "/detail/".$projectID : NULL);
		redirect($url,'refresh');
		exit();
	}
	
	public function select($p) {
		if(!$p) redirect(base_url()."project");
		
		if($this->projmain->checkProjectExists($p)) {
			$this->session->set_userdata('projectID', $p);
			redirect(base_url()."dashboard");
		} else {
			$this->flashdata->clear();
			$this->flashdata->set('Project does not exist', 'message');
			$this->flashdata->set('FAIL','type');
			redirect(base_url()."project");
		}		
	}
	
	public function delete($p) {
		if(!$p) redirect(base_url()."project");
		
		if(!$this->projmain->checkProjectExists($p)) {
			$this->flashdata->set('Project does not exist','message');
			$this->flashdata->set('FAIL','type');
			redirect(base_url()."project");
		}
		
		if(!$this->projmain->deleteProject($p)) {
			$this->flashdata->set('Fail to delete project','message');
			$this->flashdata->set('FAIL','type');
			redirect(base_url()."project/detail/".$p);
		}
		
		redirect(base_url()."project");
		
	}

	private function uploadProjectBanner($id) {
		$ext = array('jpg','jpe','jpeg','png','gif','ico','bmp','svg');
		
		$rel = "img/project";
		$virt = base_url().$rel;
		$abso = str_replace('\\','/',FCPATH);
		$dir = str_replace('\\','/',$abso).$rel;
		$key = 'banner';
		$config = array(
			'allowed_types' => implode("|",$ext),
			'upload_path' => $dir,
			'file_name' => $id,
			'max_size' => '6048',
			'overwrite' => TRUE
		);
	
		$return['path'] = NULL;
		$return['file'] = NULL;
		$return['message'] = NULL;
		if(file_exists($dir)) {				
			chmod($dir, 0755);
			if (is_uploaded_file($_FILES[$key]['tmp_name'])) {
				$this->load->library('upload', $config);			
				if ($this->upload->do_upload($key)) {
					if($data = $this->upload->data()) {	
						$return['path'] = $data['full_path'];
						$return['file'] = $data['file_name'];
						$return['message'] = "SUCCESS";
					} else {
						$return['message'] = $this->upload->display_errors();
						$return['file'] = NULL;
						$return['path'] = NULL;
					}
				} else {
					$return['message'] = $this->upload->display_errors();
					$return['file'] = NULL;
					$return['path'] = NULL;
				}
			}
		} else {
			$return['message'] = "Location to upload does not exist";
			$return['file'] = NULL;
			$return['path'] = NULL;
		}
		
		return $return;
	}

	
	
	
	
	
	
	
	
}

?>