<?php
class Newsletter extends Controller
{
	function Newsletter()
	{
		parent::Controller();
	}
	
	
	function show(){
		/*$this->load->library('user_agent');
		// gugli látta-e
		if($this->agent->is_robot()){
			$agent = "robot, ".$this->agent->robot();
			
			$info = "nyelv: ".$this->muser->user['lang'];
			$info .= "\ndomain: ".$_SERVER['SERVER_NAME'];
			$info .= "\nagent valtozo: ".$agent;
			$info .= "\ntenyleges: ".$this->agent->agent_string();
			$info .= "\nhírlevél id: ".$this->uri->segment(3);
			$info .= "\nelőző oldal: ".$_SERVER['HTTP_REFERER'];
			
			$record->osztaly = "newsletter";
			$record->fv = "show";
			$record->hiba = $info;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
		}*/
		
		
		$hl_id = $this->uri->segment(3);
		
		if( !$hl_id ){
			$hl_id = hun() ? $this->muser->lastnewsletter("hun") : $this->muser->lastnewsletter("eng");
		}
		
		if($hl_id == "milora-anyagok"){
			Header("HTTP/1.1 301 Moved Permanently");
			Header("Location: /newsletter/show/anyagmintak");
		}
		
		if(hun())	$lang = "hun";
		else		$lang = "eng";
		
		if(is_numeric($hl_id))
			$data['level'] = $this->db->query("SELECT * FROM `cadline2`.`newsletter` WHERE nl_id = ".$hl_id)->row();
		else
			$data['level'] = $this->db->query("SELECT * FROM `cadline2`.`newsletter` WHERE url = '".$hl_id."'")->row();
		
		$data['levelek'] = $this->db->query("SELECT * FROM `cadline2`.`newsletter` WHERE lang = '".$lang."' AND deleted = 0 ORDER BY nl_id DESC")->result();
		
		if( !$data['level'] ){
			header("HTTP/1.1 404 Not Found");
		}
		
		
		$data['javascript'] = array();
		
		if( is_dir($data['level']->video) || is_dir($data['level']->link) ){
			$data['javascript'] = array(
					'jquery.mousewheel.min',
					'jscrollpane/jquery.jscrollpane.min',
					'fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.pack'
				);
				
			$data['style'] = array(
					'../js/fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4v2_min'
				);
		}elseif( $hl_id=='modelling-furniture-design' ){
			$data['javascript'] = array('fancybox/jquery-1.3.2.min','fancybox/jquery.easing.1.3','fancybox/jquery.fancybox-1.2.1.pack','fancybox/fancy');
            $data['style'] = array('fancybox2');
		}
		
		$metadesc = trim(str_replace(array("\n",'"'),array(" ",''),substr(($str=wordwrap(strip_tags($data['level']->text),140,'$$')),0,strpos($str,'$$'))." ..."));
		
		hun() ? $data['title'] = 'ARCHLine.XP Hírlevelek' : $data['title'] = 'ARCHLine.XP Newsletter';
		$data['title']       .= " - ".$data['level']->title;
		$data['meta']['desc'] = $metadesc;
		$data['mobilink']     = "/mobile/newsletter/".$hl_id;
		$data['tartalom']     = '/newsletter/index';
		$data['page']         = $lap;
		$data['javascript'][] = hun() ? "lnkbuilding-hun" : "lnkbuilding-eng";
		//$data['javascript'][] = "lnkbuilding-eng";
		
		$this->load->view('site_new',$data);
		
	}
	
	function show2(){
		$this->load->model('mclusers');
		
		$hl_id = $this->uri->segment(3);
		
		if(is_numeric($hl_id))
			$level = $this->db->query("SELECT * FROM `cadline2`.`newsletter` WHERE nl_id = ".$hl_id)->row();
		else
			$level = $this->db->query("SELECT * FROM `cadline2`.`newsletter` WHERE url = '".$hl_id."'")->row();
		
		// email atalakitasa
		$temptomb = explode("/", $_SERVER[ 'REQUEST_URI'] );
		$email = str_replace("%40","@",end($temptomb));
		
		
		$egyezes = "newsletter/show/".$hl_id;
		if(hun())
			$info = "Hírlevél megtekintve: ".$level->title." (http://archline.hu/newsletter/show/".$hl_id.")";
		else
			$info = "Has been seen: ".$level->title." (http://archlinexp.com/newsletter/show/".$hl_id.")";
		$hibainfo = $level->url;
		
		if(hun()){
			if(UJUGYFELKEZELO===true)
				$this->mclusers->levelesemeny($email, "newsletter", "show2", 64, $egyezes, $info, "hun", $hibainfo);
			else
				$this->mclusers->levelesemeny($email, "newsletter", "show2", 126, $egyezes, $info, "hun", $hibainfo);
		}
		else{
			if(UJUGYFELKEZELO===true)
				$this->mclusers->levelesemeny($email, "newsletter", "show2", 183, $egyezes, $info, "eng", $hibainfo);
			else
				$this->mclusers->levelesemeny($email, "newsletter", "show2", 147, $egyezes, $info, "eng", $hibainfo);
		}
		
		
		$user = $this->phpsession->get('user');
		$user['voltmobiloldal'] = true;
		$this->phpsession->save('user',$user);
		$mobil = $user['mobil'];
		
		// átírányítás a hírlevélre
		if($mobil===true)
			redirect("mobile/newsletter/".$level->nl_id);
		else
			redirect("newsletter/show/".$hl_id);
		return;
	}
	
	function show2esp(){
		$this->load->model('mclusers');
		
		$hl_id = $this->uri->segment(3);
		
		if(is_numeric($hl_id))
			$level = $this->db->query("SELECT * FROM `cadline2`.`newsletter` WHERE nl_id = ".$hl_id)->row();
		else
			$level = $this->db->query("SELECT * FROM `cadline2`.`newsletter` WHERE url = '".$hl_id."'")->row();
		
		// email atalakitasa
		$temptomb = explode("/", $_SERVER[ 'REQUEST_URI'] );
		$email = str_replace("%40","@",end($temptomb));
		
		
		$egyezes = "newsletter/show/".$hl_id;
		$info = "Hírlevél megtekintve: ".$level->title." (http://archlinexp.com/newsletter/show/".$hl_id.")";
		$hibainfo = $level->url;
		
		if(UJUGYFELKEZELO===true)
			$this->mclusers->levelesemeny($email, "newsletter", "show2esp", 253, $egyezes, $info, "esp", $hibainfo);
		else
			$this->mclusers->levelesemenyEsp($email, "newsletter", "show2esp", 132, $egyezes, $info, "esp", $hibainfo);
		
		redirect("newsletter/show/".$hl_id);
	}
	
	function show2cz(){
		$hl_id = $this->uri->segment(3);
		
		if(is_numeric($hl_id))
			$level = $this->db->query("SELECT * FROM `cadline2`.`newsletter` WHERE nl_id = ".$hl_id)->row();
		else
			$level = $this->db->query("SELECT * FROM `cadline2`.`newsletter` WHERE url = '".$hl_id."'")->row();
		
		$temptomb = explode("/", $_SERVER[ 'REQUEST_URI'] );
		$email = str_replace("%40","@",end($temptomb));
		
		$info = "Newsletter: ".$level->title." (http://archlinexp.com/newsletter/show/".$hl_id.")";
		
		$volte = $this->db->query(" SELECT * FROM cz_newsletter WHERE email = '{$email}' AND info = '{$info}'")->result();
		
		if( count($volte) == 0 ){
			// mentes
			$czuser["email"] = $email;
			$czuser["info"] = $info;
			$czuser["date"] = date('Y-m-d, H:i:s',time());
			$this->db->insert('cz_newsletter',$czuser);
		}else{
			$record->osztaly = "newsletter";
			$record->fv = "show2cz";
			$record->hiba = "Dupla kattintás: ".$email."\n".$hl_id;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
		}
		
		
		redirect("newsletter/show/".$hl_id);
	}
	
	function subscribe()
	{
		$data['email']	= $this->input->post('email');
		$data['nev']	= $this->input->post('nev');
		$data['tel'] 	= $this->input->post('tel');
		$data['mailcontent'] = 'newsletter/mail_sub';
	
		$this->load->library('email');
		$this->email->initialize();
		$this->email->addOfficialImages();
		
		$this->email->from('info@cadline.hu', 'CadLine');
		$this->email->subject('Hirlevel jelentkezes');
		$this->email->message($this->load->view('mailb',$data,true));
		$this->email->send('info@cadline.hu','smtp');
		
		$this->load->view('site',array('tartalom'=>'newsletter/ok_sub','csik'=>array('Hírlevél')));
	}
	
	function unsubscribe()
	{
		$data['email']	= $this->input->post('email');
		
		if(!$data['email'])
		    redirect('page/show/27');
		
		$data['nev']	= $this->input->post('nev');
		$data['tel'] 	= $this->input->post('tel');
		$data['mailcontent'] = 'newsletter/mail_unsub';
	
		$this->load->library('email');
		$this->email->initialize();
		$this->email->addOfficialImages();
		
		$this->email->from('info@cadline.hu', 'CadLine');
	
		
		$this->email->subject('Hirlevel leiratkozas');
		
		$this->email->message($this->load->view('mail',$data,true));
		
		$this->email->send('info@cadline.hu','smtp');
		
		$this->load->view('site',array('tartalom'=>'newsletter/ok_unsub','csik'=>array('Hírlevél')));
	}
	
	function signup(){
		$name  = $this->input->post('name');
		$email = $this->input->post('email');
		$url   = $this->input->post('url');
		
		if( !$email || $url ){
			print "Robot? ";
			
			$record->osztaly = "newsletter";
			$record->fv = "signup";
			$record->hiba = "Robot? Új email:";
			foreach($_POST as $k=>$v)
				$record->hiba .= "\n['".$k."'] = ".$v;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			return;
		}
		
		
		$this->load->model('mclusers');
		
		$lang = hun() ?	"hun" : "eng";
		
		$nUserID = $this->mclusers->get_nUserIDByEmail($email,$lang);
		if( !$nUserID ){
			$tomb['strName']  = $name;
			$tomb['strEMail'] = $email;
			
			$tomb2['email'] = $email;
			
			if( $lang == "hun" ){
				$nUserID = $this->mclusers->addUserHun($tomb);
				$tomb2['nUserID'] = $nUserID;
				$this->mclusers->addUserEmailHun($tomb2);
			}else{
				$nUserID = $this->mclusers->addUserEng($tomb);
				$tomb2['nUserID'] = $nUserID;
				$this->mclusers->addUserEmailEng($tomb2);
			}
		}
		
		
		if( $nUserID && hun() ){
			$this->mclusers->addEvent($nUserID, 38, '');
		}elseif( $nUserID && !hun() ){
			$this->mclusers->addEvent($nUserID, 150, '','eng');
		}
		
		
		
		$record->osztaly = "newsletter";
		$record->fv = "signup";
		$record->hiba = "Új feliratkozo: ".print_r($_POST,true)." ".$lang;
		$record->datum = date('Y-m-d H:i:s',time());
		$this->db->insert('a_hibak',$record);
		
		$data['title'] = hun() ? 'ARCHLine.XP - Sikeres feliratkozás' : 'ARCHLine.XP - Sign Up';
		$data['tartalom'] = 'forum/kiir';
		$data['csik']=array('home');
		
		$data['info'] = "<div style='text-align: center; font-size: 1.3em;'>".lang('nl_submitok')."</div>";
		
		$this->load->view('site',$data);
	}
	
}
?>