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

class Contact extends CI_Controller {
	protected $noemail = FALSE;
	
	public function __construct() {
		parent::__construct();
		$this->load->model('setup/Setupsystem','setup');
		$this->load->model('contact/contactmain', 'cmain');
		$this->lang->load('contact', $this->session->userdata('language'));
		checkFront();
		$this->noemail = FALSE;
	}
	
	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('contact/index')->setData($data)->render();
	}
	
	public function send() {
		$from = ($_POST['from_url']) ? trim($_POST['from_url']) : NULL;
		$data['phoneNo'] = ($_POST['phoneNo']) ? trim($_POST['phoneNo']) : NULL;
		$data['email'] = ($_POST['email']) ? trim($_POST['email']) : NULL;
		$data['name'] = ($_POST['name']) ? trim($_POST['name']) : NULL;
		$data['subject'] = ($_POST['subject']) ? trim($_POST['subject']) : NULL;
		$data['message'] = ($_POST['message']) ? trim($_POST['message']) : NULL;
		
		// VALIDATE, IN CASE JQUERY VALIDATION BYPASSED
		$proceed = TRUE;
		$message = NULL;		
		if(trim( $data['email'] ) === "") { $proceed = FALSE; $message[] = "Please enter email address."; }
		if (!preg_match("/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/",$data['email'])) {
			$proceed = FALSE;
			$message[] = "Please enter a valid email address.";
		}				
		if(trim( $data['phoneNo'] ) === "") { $proceed = FALSE; $message[] = "Please enter phone no."; }
		if(trim( $data['name'] ) === "") { $proceed = FALSE; $message[] = "Please enter name."; }
		if(trim( $data['subject'] ) === "") { $proceed = FALSE; $message[] = "Please enter subject"; }			
		if(trim( $data['message'] ) === "") { $proceed = FALSE; $message[] = "Please enter message."; }
		
		$b = 0;
		if($proceed) {
			$user = $this->session->userdata('logged_in');
			$userid = ($user && $user['ID']) ? $user['ID'] : 0;
			$data['subject'] = addslashes($data['subject']);
			$data['message'] = addslashes($data['message']);
			if($b = $this->cmain->insertContact($data, $userid)) {
				$ticket = $this->cmain->getContactTicket($b);
				$message = NULL;
				
				// SEND CONFIRMATION MESSAGE
				$email = NULL;
				$email['from']['email'] = $this->setup->getSetupValue('EMAILSENDER');
				$email['from']['name'] = $this->setup->getSetupValue('EMAILSENDERNAME');
				$email['to']['email'] = $data['email'];
				$email['to']['name'] = $data['name'];
				$email['title'] = 'Confirmation on Contacting Us from SPS E-Voting';
				$email['subject'] = $data['subject'];
				$email['message'] = $data['message'];
				$email['view'] = "contact/email/email_confirmation";
				$email['ticket'] = $ticket;
				
				if(!$this->sendEmail($email)) $message[] = "Message saved, but unable to send confirmation email due to technical issue.";
				
				// SEND DETAIL MESSAGE
				$email = NULL;
				$email['to']['email'] = $this->setup->getSetupValue('EMAILSENDER');
				$email['to']['name'] = $this->setup->getSetupValue('EMAILSENDERNAME');
				$email['from']['email'] = $data['email'];
				$email['from']['name'] = $data['name'];
				$email['title'] = 'A New Contact Message Received from SPS E-Voting';
				$email['name'] = $data['name'];
				$email['email'] = $data['email'];
				$email['subject'] = $data['subject'];
				$email['message'] = $data['message'];
				$email['view'] = "contact/email/email_detail";
				$email['ticket'] = $ticket;
				
				if(!$this->sendEmail($email)) $message[] = "Message saved, but unable to send notification email to representative due to technical issue.";
			} else {
				$message[] = "Fail to save contact message due to technical issue";
			}
		}
		
		$this->flashdata->clear();
		if($message && !empty($message)) {
			$this->flashdata->set(implode("<br />",$message),'message');
			$this->flashdata->set('FAIL','type');
		} else {
			$this->flashdata->set('SUCCESS','message');
			$this->flashdata->set('SUCCESS','type');
		}
		
		$url = base_url();
		$url .= ($from) ? $from."#contact" : "contact";
		redirect($url, "refresh");
	}
	
	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($data['title']);
		$this->email->message($this->load->view($data['view'], $data, true));
		
		return($this->email->send()) ? TRUE : FALSE;
	}
	
}
























?>
