<?php
class Tell_A_Friend extends Controller {

   function Tell_A_Friend(){
      parent::Controller();

    session_start();
	$this->lang->load('public');
		#check to see if the default lang_id exists, and if not set it
	if($this->session->userdata('lang_id')==null){
		$ld='1';
		$this->session->set_userdata('lang_id',$ld);
		//echo $ld;
	}else{
		$ld=$this->session->userdata('lang_id');
	}
   }

	function index() {
		//$ld=$this->session->userdata('lang_id');
	  	# Validations
		//echo $ld;
	  	$this->load->library('validation');
		$data['liID']=$this->uri->segment(1);
		$id= $this->session->userdata('ses_user_id');
		if (!$id) {
 		redirect('page','refresh');
		}
		$data['userdata'] = $this->MUsers->getUserData($id);
		//$data['userdata'] = $this->MUsers->getUserData($id);
		$user=$data['userdata'];
		$data['title'] = "Welcome to myFriendsOnBoard";
		$data['page_title'] = $this->lang->line('link_tell_a_friend');
  		$data['header'] = 'header_user';
  		$data['nav_user'] = 'nav_user';
  		$data['main_user'] = 'tell_a_friend';
  		$data['footer'] = 'footer';

	  	$rules['name'] = "trim|required|min_length[2]|xss_clean";
	  	$rules['email_1'] = "trim|required|valid_email";
		$rules['name_1'] = "trim|required|min_length[2]|xss_clean";
		//$rules['company'] = "trim|min_length[5]|xss_clean";
		$rules['email'] = "trim|required|valid_email";
	  	$this->validation->set_rules($rules);

		$fields['name'] = 'Name';
		$fields['email'] = 'Email';		
		$fields['email_1'] = 'Email';
		$fields['email_2'] = 'Email';
		$fields['email_3'] = 'Email';
		$fields['email_4'] = 'Email';
		$fields['email_5'] = 'Email';
		$fields['name_1'] = 'Name';
		$fields['name_2'] = 'Name';
		$fields['name_3'] = 'Name';
		$fields['name_4'] = 'Name';
		$fields['name_5'] = 'Name';

		$this->validation->set_fields($fields);
		
		# Input and textarea field attributes
		$data['email'] = array('name' => 'email', 'id' => 'email', 'value'=>$user['email']);
		$data['name'] = array('name' => 'name', 'id' => 'name', 'value'=>$user['fname']." ".$user['lname']);
		$data['email_1'] = array('name' => 'email_1', 'id' => 'email_1');
		$data['email_2'] = array('name' => 'email_3', 'id' => 'email_2');
		$data['email_3'] = array('name' => 'email_2', 'id' => 'email_3');
		$data['email_4'] = array('name' => 'email_4', 'id' => 'email_4');
		$data['email_5'] = array('name' => 'email_5', 'id' => 'email_5');
		$data['name_1'] = array('name' => 'name_1', 'id' => 'name_1');
		$data['name_2'] = array('name' => 'name_3', 'id' => 'name_2');
		$data['name_3'] = array('name' => 'name_2', 'id' => 'name_3');
		$data['name_4'] = array('name' => 'name_4', 'id' => 'name_4');
		$data['name_5'] = array('name' => 'name_5', 'id' => 'name_5');
		# 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);
		$this->load->view('template_master_user', $data);
		}
		
		
		function send(){
		$this->validation->set_error_delimiters('<div class="warning">', '</div>');
		$data['liID']=$this->uri->segment(1);
        $this->load->library('validation');
		$id= $this->session->userdata('ses_user_id');
		$data['userdata'] = $this->MUsers->getUserData($id);
		$data['title'] = "Welcome to myFriendsOnBoard";
		$data['page_title'] = $this->lang->line('link_tell_a_friend');
  		$data['header'] = 'header_user';
  		$data['nav_user'] = 'nav_user';
  		$data['main_user'] = 'tell_a_friend';
  		$data['footer'] = 'footer';


	  	$rules['name'] = "trim|required|min_length[2]|xss_clean";
	  	$rules['email_1'] = "trim|required|valid_email";
		$rules['name_1'] = "trim|required|min_length[2]|xss_clean";
		//$rules['company'] = "trim|min_length[5]|xss_clean";
		$rules['email'] = "trim|required|valid_email";
	  	$this->validation->set_rules($rules);

		$fields['name'] = 'Name';
		$fields['email'] = 'Email';
		$fields['email_1'] = 'Email';
		$fields['email_2'] = 'Email';
		$fields['email_3'] = 'Email';
		$fields['email_4'] = 'Email';
		$fields['email_5'] = 'Email';
		$fields['name_1'] = 'Name';
		$fields['name_2'] = 'Name';
		$fields['name_3'] = 'Name';
		$fields['name_4'] = 'Name';
		$fields['name_5'] = 'Name';

		$this->validation->set_fields($fields);
		
		if ($this->validation->run()) {


			$this->load->helper('typography');
			
			$name = $this->input->post('name');
			$email = $this->input->post('email');
			$email_1 = $this->input->post('email_1');
			$name_1 = $this->input->post('name_1');
			$email_2 = $this->input->post('email_2');
			$name_2 = $this->input->post('name_2');
			$email_3 = $this->input->post('email_3');
			$name_3 = $this->input->post('name_3');
			$email_4 = $this->input->post('email_4');
			$name_4 = $this->input->post('name_4');
			$email_5 = $this->input->post('email_5');
			$name_5 = $this->input->post('name_5');
			$comments = $this->input->post('comments');
			$comments=auto_typography($comments);
			
			$message = $comments;
			



			//check to see if the email is already registered in the db
			$exist=$this->MUsers->checkEmail($email_1);
			if($exist==true){
				$this->session->set_flashdata('message','This Email is already registered to our web site!');
				redirect('tell_a_friend','refresh');
			}

			#Email send to the company!
			$this->email->from($email, $name);
			$this->email->to('dimitristangos@gmail.com', 'www.myfob.gr');
			$this->email->subject('www.myfob.gr - Tell A Friend');
			$this->email->message($message);
			$this->email->send();
			
			#Email send to the user who tells!
			$this->email->from('info@myfob.gr', 'www.myfob.gr');
			$this->email->to($email);
			$this->email->subject('www.myfob.gr - Tell A Friend');
			$message="You have invited the foloowing friends of yours in MyFOB: "."$comments";
			//$message .=$this->lang->line('contact_email_post_message');
			$this->email->message($message);
			$this->email->send();
			
			#Email send to the user
			$this->email->from('info@myfob.gr', 'www.myfob.gr');
			$this->email->to($email_1);
			$this->email->subject('www.myfob.gr - Tell A Friend');
			//$message="Message From a Friend: "."$comments";
			//$message .=$this->lang->line('contact_email_post_message');
			$this->email->message($message);
			$this->email->send();
	
			if ($email_2) {
			//check to see if the email is already registered in the db
			$exist=$this->MUsers->checkEmail($email_2);
			if($exist==true){
				$this->session->set_flashdata('message','This Email is already registered to our web site!');
				redirect('tell_a_friend','refresh');
			}

			#Email send to the user
			$this->email->from('info@myfob.gr', 'www.myfob.gr');
			$this->email->to($email_2);
			$this->email->subject('www.myfob.gr - Tell A Friend');
			//$message="Message From a Friend: "."$comments";
			//$message .=$this->lang->line('contact_email_post_message');
			$this->email->message($message);
			$this->email->send();
			}
			if ($email_3) {
						//check to see if the email is already registered in the db
			$exist=$this->MUsers->checkEmail($email_3);
			if($exist==true){
				$this->session->set_flashdata('message','This Email is already registered to our web site!');
				redirect('tell_a_friend','refresh');
			}

			#Email send to the user
			$this->email->from('info@myfob.gr', 'www.myfob.gr');
			$this->email->to($email_3);
			$this->email->subject('www.myfob.gr - Tell A Friend');
			//$message="Message From a Friend: "."$comments";
			//$message .=$this->lang->line('contact_email_post_message');
			$this->email->message($message);
			$this->email->send();
			}
			if ($email_4) {
			//check to see if the email is already registered in the db
			$exist=$this->MUsers->checkEmail($email_4);
			if($exist==true){
				$this->session->set_flashdata('message','This Email is already registered to our web site!');
				redirect('tell_a_friend','refresh');
			}

			#Email send to the user
			$this->email->from('info@myfob.gr', 'www.myfob.gr');
			$this->email->to($email_4);
			$this->email->subject('www.myfob.gr - Tell A Friend');
			//$message="Message From a Friend: "."$comments";
			//$message .=$this->lang->line('contact_email_post_message');
			$this->email->message($message);
			$this->email->send();
			}
			if ($email_5) {
			//check to see if the email is already registered in the db
			$exist=$this->MUsers->checkEmail($email_5);
			if($exist==true){
				$this->session->set_flashdata('message','This Email is already registered to our web site!');
				redirect('tell_a_friend','refresh');
			}

			#Email send to the user
			$this->email->from('info@myfob.gr', 'www.myfob.gr');
			$this->email->to($email_5);
			$this->email->subject('www.myfob.gr - Tell A Friend');
			//$message="Message From a Friend: "."$comments";
			//$message .=$this->lang->line('contact_email_post_message');
			$this->email->message($message);
			$this->email->send();
			}

				$this->session->set_flashdata('message','The invitation(s) are sent to all emails!');
				redirect('tell_a_friend','refresh');

        } else {
		$data['liID']=$this->uri->segment(1);
		$id= $this->session->userdata('ses_user_id');
		$data['userdata'] = $this->MUsers->getUserData($id);

		$data['email'] = array('name' => 'email', 'id' => 'email', 'value'=>$data['userdata']['email']);
		$data['name'] = array('name' => 'name', 'id' => 'name', 'value'=>$data['userdata']['fname']." ".$data['userdata']['lname']);
		$data['email_1'] = array('name' => 'email_1', 'id' => 'email_1');
		$data['email_2'] = array('name' => 'email_3', 'id' => 'email_2');
		$data['email_3'] = array('name' => 'email_2', 'id' => 'email_3');
		$data['email_4'] = array('name' => 'email_4', 'id' => 'email_4');
		$data['email_5'] = array('name' => 'email_5', 'id' => 'email_5');
		$data['name_1'] = array('name' => 'name_1', 'id' => 'name_1');
		$data['name_2'] = array('name' => 'name_3', 'id' => 'name_2');
		$data['name_3'] = array('name' => 'name_2', 'id' => 'name_3');
		$data['name_4'] = array('name' => 'name_4', 'id' => 'name_4');
		$data['name_5'] = array('name' => 'name_5', 'id' => 'name_5');
			
		$data['title'] = "Welcome to myFriendsOnBoard";
		$data['page_title'] = $this->lang->line('link_tell_a_friend');
  		$data['header'] = 'header_user';
  		$data['nav_user'] = 'nav_user';
  		$data['main_user'] = 'tell_a_friend';
  		$data['footer'] = 'footer';
	
			$this->load->view('template_master_user', $data);
        }
			
		
	}


}
?>