<?php
class Contact extends Controller
{
	function Contact(){
		parent::Controller();
	}
	
	function index(){
		$data['title']      = hun() ? 'ARCHLine.XP - Kapcsolatfelvétel, CadLine Kft.' : 'ARCHLine.XP - Contact Us, CadLine Ltd.';
		$data['tartalom']   = 'contact/index';
		$data['javascript'] = array('regformcheck');
		$data['csik']       = array('home','contact');
		$data['mobilink']   = "/mobile/contact";
		$data['tema']       = $this->uri->segment(3);
		
		$this->load->view('site',$data);
	}
	
	function submit(){
		foreach($_POST as $k=>$v)
			$$k = $v;
		
		if($url){
			$record->osztaly = "contact";
			$record->fv = "submit";
			$record->hiba = "Robot? Új levél:";
			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);
		}
		
		
		if( $strEMail && !$url && ($subject!="Other" || trim($message) != "" && trim($message) != "<p>&#160;</p>") ){
			$mail_data = array(
				'name'    => $strName1.' '.$strName2,
				'company' => $strCompany,
				'address' => $ctrName." ".$strCity." ".$strStreet." ".$strZip,
				'phone'   => $strTel,
				'email'   => $strEMail,
				'subject' => $subject,
				'message' => $message
			);
			
			$this->load->library('email');
			$this->email->initialize();
			
			$this->email->message($this->load->view('mail/contact', $mail_data, true));
			
			if( hun() ){
				$lang = "hun";
				$this->email->from('info@cadline.hu', 'CadLine Weboldal');
				$this->email->subject('Kapcsolatfelvétel - '.$subject);
				$esemid = 282;
				$this->email->send('info@cadline.hu', 'smtp');
				//$this->email->send('eszter@cadline.hu', 'smtp');
				if( $subject=="ARCHLine ID kérés" ){
					$this->email->send('jozsa.timea@cadline.hu', 'smtp');
					$esemid = 41;
				}
			}
			else{
				$lang = "eng";
				$this->email->from('info@archlinexp.com', 'CadLine Website');
				$this->email->subject('Contact us - '.$subject);
				$this->email->send("info@archlinexp.com", 'smtp');
				//$this->email->send('eszter@cadline.hu', 'smtp');
				$esemid = 281;
				if( $subject=="Request ARCHLine ID" ){
					$this->email->send('kitti.toth@archlinexp.com', 'smtp');
					$esemid = 152;
				}
			}
			
			
			// Esemeny felvetele
			$this->load->model('mclusers');
			$nUserID = $this->mclusers->get_nUserIDByEmail($strEMail,$lang);
			
			// ha nem volt vagy nem egyertelmu, akkor felvenni
			if( !$nUserID ){
				$countries = array();
				
				$orszagok = $this->db->query('SELECT * FROM `'.CRMADATBAZIS.'`.`countries`')->result_array();
				foreach( $orszagok as $orszag )
					$countries[$orszag['ctrID']] = hun() ? strtolower($orszag['ctrName']) : strtolower($orszag['ctrNameEng']);
				
				$tomb['nCtrID'] = array_search(strtolower($ctrName), $countries);
				
				$tomb['strName']    = $strName1.' '.$strName2;
				$tomb['strCompany'] = $strCompany;
				$tomb['strTel']     = $strTel;
				$tomb['strEMail']   = $strEMail;
				$tomb['strCity']    = $strCity;
				$tomb['strStreet']  = $strStreet;
				$tomb['strZip']     = $strZip;
				$tomb['nUserProf']  = $nUserProf;
				
				$tomb2['email']     = $strEMail;
				
				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);
				}
			}
			else
				$this->mclusers->userUpdateIfEmpty($nUserID);
			
			$infotext = "({$subject}) ";
			$infotext .= '"'.preg_replace("/<.+>/Usi", "", $message).'"';
			
			if( $nUserID && hun() ){
				$this->mclusers->addEvent($nUserID, $esemid, $infotext);
			}elseif( $nUserID && !hun() ){
				$this->mclusers->addEvent($nUserID, $esemid, $infotext,'eng');
			}
			
			
			
			$record->osztaly = "contact";
			$record->fv = "submit";
			$record->hiba = "Új levél: ".$message.",\n".$name.",\n".$strEMail;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			$data['title'] = hun() ? 'ARCHLine.XP - Kapcsolatfelvétel' : 'ARCHLine.XP - Contact Us';
			$data['tartalom'] = 'forum/kiir';
			$data['csik']=array('home','contact');
			
			$data['info'] = "<div style='text-align: center; font-size: 1.3em;'>".lang('contact_elkuldve')."</div>";
			
			$this->load->view('site',$data);
		}else{
			$record->osztaly = "contact";
			$record->fv = "submit";
			$record->hiba = "Hibás email vagy üres vagy auto levél: ".$strEMail.",\n".$message.print_r($_POST,true);
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			redirect('contact');
		}
	}
	
	function partners(){
		$uri3 = $this->uri->segment(3);
		if(!$uri3){
			$data['tartalom2'] = 'contact/representatives';
			$data['title'] = 'ARCHLine.XP - Contact Us, CadLine Ltd.';
		}
		elseif($uri3=="tutors"){
			$data['tartalom2'] = 'contact/tutors';
			$data['title'] = 'ARCHLine.XP Tutors - Contact Us';
		}
		else{
			$data['tartalom2'] = 'contact/contactus';
			$data['title'] = 'ARCHLine.XP - Contact Us, CadLine Ltd.';
		}
		
		/* új angol oldal */
		$data['tartalom'] = 'contact/new';
		$data['csik']  = array('home','contact');
		$data['style'] = array('workshop_min');
		
		$this->load->view('site',$data);
	}
	
	
	//add brand to showroom
	function addbrandsubmit(){
		$oldclass = $this->uri->segment(3);
		
		foreach($_POST as $k=>$v)
			$$k = $v;
		
		if($url){
			$record->osztaly = "contact";
			$record->fv      = "addbrandsubmit";
			$record->hiba    = "Robot? Új levél:";
			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);
		}
		
		
		if( $email && !$url ){
			$mail_data = array(
				'name' => $name,
				'job'  => $job,
				'company' => $company,
				'email' => $email,
				'phone' => $phone,
				'comment' => $comment
			);
			
			$this->load->library('email');
			$this->email->initialize();
			
			$this->email->message($this->load->view('mail/addbrandsubmit', $mail_data, true));
			
			if( hun() ){
				$lang = "hun";
				$this->email->from('info@cadline.hu', 'CadLine Weboldal');
				$this->email->subject('ARCHLine.XP Bemutatóterem');
				$esemid = 20;
				$this->email->send('info@cadline.hu', 'smtp');
				//$this->email->send('eszter@cadline.hu', 'smtp');
			}
			else{
				$lang = "eng";
				$this->email->from('info@archlinexp.com', 'CadLine Website');
				$this->email->subject('ARCHLine.XP Showroom');
				$this->email->send("info@archlinexp.com", 'smtp');
				//$this->email->send('eszter@cadline.hu', 'smtp');
				$esemid = 138;
			}
			
		//	$this->email->send("sztikee@gmail.com", 'smtp');
			
			// Esemeny felvetele
			$this->load->model('mclusers');
			$nUserID = $this->mclusers->get_nUserIDByEmail($email,$lang);
			
			// ha nem volt vagy nem egyertelmu, akkor felvenni
			if( !$nUserID ){
				$tomb['strName'] = $name;
				$tomb['strCompany'] = $company;
				$tomb['strTel'] = $tomb['strTel'] = $phone;
				$tomb['strEMail'] = $email;
				$tomb['strCity'] = $address;
				
				$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);
				}
			}
			
			$infotext = "(Új márka a bemutatóterembe) ";
			$infotext .= '"'.$comment.'"';
			
			if( $nUserID && hun() ){
				$this->mclusers->addEvent($nUserID, $esemid, $infotext);
			}elseif( $nUserID && !hun() ){
				$this->mclusers->addEvent($nUserID, $esemid, $infotext,'eng');
			}
			
			
			
			$record->osztaly = "contact";
			$record->fv      = "addbrandsubmit";
			$record->hiba    = "Új levél: ".$comment.",\n".$name.",\n".$email;
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			$data['title']    = hun() ? 'ARCHLine.XP - Kapcsolatfelvétel' : 'ARCHLine.XP - Contact Us';
			$data['tartalom'] = 'forum/kiir';
			
			if($oldclass=="collection2"){
				$data['info']  = "<div style='text-align: center; font-size: 1.3em;'>";
				$data['info'] .= lang('contact_elkuldve');
				$data['info'] .= "<br><br><a href='/collection2/types'>Back</a>";
				$data['info'] .= "</div>";
			
				$this->load->view('site_upload',$data);
				return;
			}
			
			$data['info'] = "<div style='text-align: center; font-size: 1.3em;'>".lang('contact_elkuldve')."</div>";
			
			$this->load->view('site',$data);
		}else{
			$record->osztaly = "contact";
			$record->fv = "submit";
			$record->hiba = "Hibás email vagy üres vagy auto levél: ".$email.",\n".$message.print_r($_POST,true);
			$record->datum = date('Y-m-d H:i:s',time());
			$this->db->insert('a_hibak',$record);
			
			redirect('contact');
		}
		
	}
	
	
}
?>