<?php
class Unsubscribe extends Controller
{
	function __construct()
	{
		parent::Controller();
	}
	
	function index()
	{
		$this->index2();
		return;
        $data['tartalom'] = 'unsubscribe/index';

		$this->load->view('site', $data);
	}
	
	function validate()
	{
		$do = $this->input->post('do');
		$name = $this->input->post('name');
		$email = $this->input->post('email');
		
		if($do && $email && $name && $do=='unsub')
		{
			$mail_vars['name'] = $name;
			$mail_vars['email'] = $email;
			
            $this->load->library('email');
			$this->email->clear();
			$this->email->initialize();

			$this->email->from('info@cadline.hu', 'CadLine');
			$this->email->subject('ARCHLine.XP Leiratkozas');
			$this->email->message($this->load->view('mail/unsubscribe', $mail_vars, true));
			
			if(hun())
				$this->email->send('info@cadline.hu','smtp');
			else
				$this->email->send('kitti.toth@archlinexp.com','smtp');

			redirect('unsubscribe/done');
		}
		else
		{
			$data['tartalom'] = 'error/unsubscribe';
			
			$this->load->view('site', $data);
		}
	}
	
	function done()
	{
		$data['tartalom'] = 'unsubscribe/done';
		
		$this->load->view('site', $data);
	}
	
	
	// automatikus leiratkozás
	function index2(){
		$data['tartalom'] = 'unsubscribe/index2';
		$this->load->view('site', $data);
	}
	
	// megerősítő e-mailt küld
	function validate2(){
		$do = $this->input->post('do');
		$name = $this->input->post('name');
		$email = $this->input->post('email');
		
		
		if($do && $email && $name && $do=='unsub' && !$url){
			$code = md5('aefsa'.$name.$email.time());
			
			// adatbázisba
			$record = array(
					'name'  => $name,
					'email' => $email,
					'code'  => $code,
				);
			$this->db->insert('unsubscribe',$record);
			
			// e-mail
			
			$lapid = hun() ? 826 : 827;
			$lap = $this->mpages->get_row($lapid);
			$message = str_ireplace('#name#', $name, $lap->page_body);
			$message = str_ireplace('#code#', $code, $message);
			
            $this->load->library('email');
			$this->email->clear();
			$this->email->initialize();

			$this->email->from('info@cadline.hu', 'CadLine');
			$this->email->subject($lap->page_title);
			$this->email->message($message);
			
			$this->email->send($email,'smtp');

			redirect('unsubscribe/done2');
		}
		else{
			$data['tartalom'] = 'error/unsubscribe';
			$this->load->view('site', $data);
		}
		
	}
	
	function done2(){
		$data['info'] = hun() ? $this->mpages->get_row(829) : $this->mpages->get_row(828);
		$data['tartalom'] = 'unsubscribe/done2';
		$this->load->view('site', $data);
	}
	
	// megerősítés e-mailben
	function confirm(){
		$code = $this->uri->segment(3);
		$this->load->model('mclusers');
		
		$leiratkozo = $this->db->query("SELECT * FROM unsubscribe WHERE code='{$code}'")->row();
		if( !isset($leiratkozo->email) || !$leiratkozo->email ){
			print "error";
			return;
		}
		
		$email = trim($leiratkozo->email);
		
		// crm-ben kivenni az e-mailt + leiratkozas esemeny
		$userek = $this->db->query("SELECT DISTINCT(nUserID) nUserID FROM ".CRMADATBAZIS.".u_emails WHERE email='{$email}'")->result();
		$this->db->query("DELETE FROM ".CRMADATBAZIS.".u_emails WHERE email='{$email}'");
		
		$topicId = hun() ? 54 : 166;
		$lang    = hun() ? "hun" : "eng";
		
		foreach($userek as $user){
			$userlang = $this->db->query("SELECT old_db FROM ".CRMADATBAZIS.".users WHERE nUserID='{$user->nUserID}'")->row();
			switch($userlang->old_db){
				case 'clusers_eng': $topicId=166; break;
				case 'clusers_spa': $topicId=248; break;
				default: $topicId=54;
			}
			$this->mclusers->addEvent($user->nUserID, $topicId, $email, $lang);
		}
		$this->db->query("UPDATE unsubscribe SET `deleted`=1 WHERE code='{$code}'");
		
		redirect('unsubscribe/done');
	}
	
	
	
	
	
	
	
	
	
}
?>