<?php
class ENewsletter extends Controller {

   function ENewsletter(){
      parent::Controller();
	  //session_start();
	//$this->lang->load('public');
	//$this->output->enable_profiler(TRUE);
   }

	function index() {
		$this->validation->set_error_delimiters('<div class="warning">', '</div>');
	  	$data['cats'] = $this->MCats->getTopCategories();
		$data['title'] = "ThinkGlass - eNewsletter";
		$data['header'] = 'header_newsletter';
	  	$rules['name'] = "trim|min_length[5]|xss_clean";
	  	$rules['email'] = "trim|required|valid_email";
		$rules['occupation'] = "trim|min_length[5]|xss_clean";
		$rules['company'] = "trim|min_length[5]|xss_clean";
		$this->validation->set_rules($rules);
		
	  	$fields['name']	= 'Όνομα';
		$fields['email']	= 'Email';
		$fields['occupation']	= 'Επάγγελμα / Ιδιότητα';
		$fields['company'] = 'Εταιρία';
		$this->validation->set_fields($fields);
		
		# Input and textarea field attributes
		$data['email'] = array('name' => 'email', 'id' => 'email');
		$data["name"] = array('name' => 'name', 'id' => 'name');
		$data["occupation"] = array('name' => 'occupation', 'id' => 'occupation');
		$data["company"] = array('name' => 'company', 'id' => 'company');
		$data['main']='enewsletter';
		# Checkbox attributes
	//	$data['terms'] = array('name' => 'seminar[]', 'id' => 'purpose', 'value' => 'Purpose of Prayer', 'checked' => FALSE);
	//	$data['prepare'] = array('name' => 'seminar[]', 'id' => 'prepare', 'value' => 'Prepare for Prayer', 'checked' => FALSE);
	//	$data['principles'] = array('name' => 'seminar[]', 'id' => 'principles', 'value' => 'Principles of Prayer', 'checked' => FALSE);
	//	$data['power'] = array('name' => 'seminar[]', 'id' => 'power', 'value' => 'Power in Prayer', 'checked' => FALSE);
		
		
		if ($this->validation->run() == FALSE)
		{
			$this->load->view('template', $data);
		}else{
			if($this->input->post('email')){
				$check_email = $this->input->post('email');
				$check=$this->MSubscribers->checkSubscriber($check_email);
				//print_r($check);
				if ($check== 'not_done') {
					$this->MSubscribers->createSubscriber();
					$this->load->helper('typography');
			
					$name = $this->input->post('name');
					$email = $this->input->post('email');
					$occupation = $this->input->post('occupation');
					$company = $this->input->post('company');
			
					$details = "Όνομα εγγραφής:".$name;
					$details .= "<br>Εmail:".$email;
					$details .= "<br>Επάγγελμα:".$occupation;
					$details .= "<br>Εταιρία:" .$company;
			
					#Email send to the company!
					$this->email->from($email, $name);
					$this->email->to('info@thinkglass.gr', 'www.ThinkGlass.gr');
					$this->email->subject('www.ThinkGlass.gr - Εγγραφή Message in a Bottle');
					$this->email->message($details);
					//$this->email->send();
			
					#Email send to the user
					$this->email->from('info@thinkglass.gr', 'www.ThinkGlass.gr');
					$this->email->to($email);
					$this->email->subject('www.ThinkGlass.gr - Εγγραφή Message in a Bottle');
					$message="Σας ευχαριστούμε που γραφτήκατε στο Message in a Bottle. Τα στοιχεία που εισάγατε είναι τα ακόλουθα".br(3)."$details";
					//$message .=$this->lang->line('contact_email_post_message');
					$this->email->message($message);
					$this->email->send();
					redirect('ethankyou','refresh');
				}elseif($check=='done'){
					redirect('ethankyou','refresh');
				}else{
					redirect('email_exist','refresh');
				}
			}else{
			redirect('wrong_form','refresh');
			}
		}	
	}
		

}
?>
