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

class Registration extends CI_Controller {
	protected $noemail = FALSE;
	public function __construct(){
		parent::__construct();
		$this->load->model('registration/Registrationmain','regmain');
		$this->lang->load('registration', $this->session->userdata('language'));
		$this->noemail = FALSE;
		checkFront();
	}
	
	public function index() {
		$data = $this->templates->getData();
		$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->templates->setView('registration/index')->setData($data)->render();
	}
	
	public function save() {
		$projecttitle = (isset($_POST['projecttitle']) && trim($_POST['projecttitle'])) ? trim($_POST['projecttitle']) : NULL;
		$fullname = (isset($_POST['fullname']) && trim($_POST['fullname'])) ? trim($_POST['fullname']) : NULL;
		$phone = (isset($_POST['phone']) && trim($_POST['phone'])) ? trim($_POST['phone']) : NULL;
		$email = (isset($_POST['email']) && trim($_POST['email'])) ? trim($_POST['email']) : NULL;
		$password = (isset($_POST['password']) && trim($_POST['password'])) ? trim($_POST['password']) : NULL;
		$confirm = (isset($_POST['confirm']) && trim($_POST['confirm'])) ? trim($_POST['confirm']) : NULL;		
		
		$proceed = TRUE;
		$message = NULL;
		
		if(trim( $projecttitle ) === "") { $proceed = FALSE; $message[] = "Please enter your project title."; }	
		if(trim( $fullname ) === "") { $proceed = FALSE; $message[] = "Please enter fullname."; }	
		if(trim( $phone ) === "") { $proceed = FALSE; $message[] = "Please enter phone no."; }
		if(trim( $email ) === "") { $proceed = FALSE; $message[] = "Please enter email."; }
		if(trim( $password ) === "") { $proceed = FALSE; $message[] = "Please enter password."; }
		if(trim( $confirm ) === "") { $proceed = FALSE; $message[] = "Please enter password confirmation."; }	
		if(trim( $confirm ) !== trim( $password )) { $proceed = FALSE; $message[] = "Password confirmation not matched."; }	
		if($this->regmain->isEmailExists($email)) { $proceed = FALSE; $message[] = "Email already registered."; }
		
		if($proceed) {
			$user = $this->session->userdata('logged_in');
			$userid = ($user && $user['ID']) ? $user['ID'] : 0;
			
			if($b = $this->regmain->saveRegistration($fullname, $phone, $email, md5($password))) {
				if($this->regmain->saveProject($projecttitle, $b)) {
					$proceed = TRUE;
					$this->flashdata->clear()->set('SUCCESS');
					
					$tosend['from']['email'] = $this->setup->getSetupValue('EMAILSENDER');
					$tosend['from']['name'] = $this->setup->getSetupValue('EMAILSENDERNAME');
					$tosend['to']['email'] = $email;
					$tosend['to']['name'] = $fullname;
					$tosend['email'] = $email;
					$tosend['password'] = $password;
					$tosend['view'] = "registration/email/email_confirmation";
					$this->sendEmail($tosend);
				} else {
					$proceed = FALSE;
					$message[] = "Fail to save project due to technical issue";
				}
			} else {
				$proceed = FALSE;
				$message[] = "Fail to save registration due to technical issue";
			}
		}
		
		if(!$proceed && $message && !empty($message)) {
			$this->flashdata->clear()->set(implode("<br />",$message),'message');
			$this->flashdata->clear()->set('FAIL','type');
		} else {
			$this->flashdata->clear()->set('SUCCESS','message');
			$this->flashdata->clear()->set('SUCCESS','type');
		}
		
		$url = base_url()."registration";
		redirect($url);
	}
	
	/**
	* AJAX
	*/
	public function checkemailexistsajax() {
		$email = (isset($_POST['email']) && trim($_POST['email'])) ? trim($_POST['email']) : NULL;
		echo ($this->regmain->isEmailExists($email)) ? 1 : 0;
	}
	
	/**
	* PRIVATE
	*/
	private function sendEmail($data) {
		if($this->noemail) return true;
		if(!trim($data['from']['email'])) return true;
		if(!trim($data['to']['email'])) return true;
		
		$this->load->library('email');
		
		$this->email->from($data['from']['email'], $data['from']['name']);
		$this->email->to($data['to']['email'], $data['to']['name']);
		$this->email->set_newline("\r\n");
		
		$this->email->subject('Thank you for signing up with SPS E-Voting');
		$this->email->message($this->load->view($data['view'], $data, true));
		
		return($this->email->send()) ? TRUE : FALSE;
	}
	
}

?>