<?php
App::uses('AppController', 'Controller');
/**
 * Enquiries Controller
 *
 * @property Enquiry $Enquiry
 * @property PaginatorComponent $Paginator
 */
class EnquiriesController extends AppController

	{
	/**
	 * Components
	 *
	 * @var array
	 */
	public $components = array(

		'Paginator'
	);
	/**
	 * enquiry_add method
	 *
	 * @return void
	 */
  
  
   function process_document($image_file = null,$service_slug = null,$folderpath = '') {

		$final_image = "";
		$ext = substr(strtolower(strrchr($image_file['name'], '.')), 1);  
		$arr_ext = array('pdf','doc','docx','ppt','pptx','txt');  
		$target_path = 'resumes/';

		if(in_array($ext, $arr_ext))
							   {
		//$newName = rand(2,10).''.time().".".$ext;
		$newName = $service_slug.time().".".$ext;

		$image_file['tmp_name'];  

		move_uploaded_file($image_file['tmp_name'],'resumes/'.$newName);

		$final_image = $newName;  
		}

		else {

		$final_image = 'error';

		}

		return $final_image;
		}
	 
	 public function career() {
		
		 $this->layout = "ajax";
		 
		    if ($this->request->is('ajax')) {
			   
			   $this->request->data['formrequest']  = 'ajax';
		   }
		   else {
			   
			   $this->request->data['formrequest']  = 'post';
		   }		    
		 
		 
		   if($this->request->data['formrequest'] == 'post') { die; }
		   
		   foreach ($this->request->data as $key => $value) {
					$this->request->data[$key] = trim($value);
				}
		   
			 
			 
		if ($this->request->is('post')) {
		 
			
			$resume_title = '';	
			
			$contact_name = strtolower($this->request->data['name1']);

			if(!empty(@$_FILES['file']['name'])) {	

			$service_slug = str_replace(' ','_', $contact_name).'_resume';
			$service_slug = trim($service_slug);

			$resume_title = $this->process_document($_FILES['file'],$service_slug);

			if($resume_title == 'error') {

			echo $msg = 'Invalid File Type Only txt, pdf, ppt, pptx, doc or docx allowed';
			exit;
 			
			}

			$this->request->data['file'] = $resume_title;

			}

			$this->request->data['ip_address'] = $_SERVER['REMOTE_ADDR'];
			$this->Enquiry->create();
			
			if ($this->Enquiry->save($this->request->data)) {
				
		 		if($this->request->data['formrequest'] == 'post') { die; }

			App::import('Vendor', 'phpmailer', array('file' => 'phpmailer' . DS . 'class.phpmailer.php'));
			$enquiry_for     = $this->request->data['enquiry_for'];
			$contact_name    = @$this->request->data['name1'];
			$select_job = @$this->request->data['name'];
			$applied_jobs = @$this->request->data['appiled_post'];
			$contact_email   = @$this->request->data['email'];
	 		$contact_mobile   = @$this->request->data['mobile'];
			$contact_company = @$this->request->data['company'];	
			$contact_address = @$this->request->data['address'];			
			$contact_message = @$this->request->data['message'];		
			$services        = @$this->request->data['services'];		
			$company_details = $this->Session->read('company_details');
			$company_name    = $company_details['company_name']; 
			$company_email   = $company_details['company_email']; 
			$services = @$this->request->data['service'];	   
 

			if(empty($contact_name)) { die; }
			
			$page_url        = @$this->request->data['page_url']; 
				
			$CustomMessageText = "<br><br />Employee Name : $contact_name <br />";
			if($contact_email) { 
			$CustomMessageText .= "<br />Email Id : $contact_email <br />"; 	
			}
			if($contact_mobile) { 
			$CustomMessageText .= "<br />Phone Number : $contact_mobile <br />";	
			}
			if($contact_company) { 
			$CustomMessageText .= "<br />Company : $contact_company <br />";
			}	
			if($contact_address) { 
			$CustomMessageText .= "<br />Address : $contact_address <br />";
			}	
			if($select_job) { 
			$CustomMessageText .= "<br />Job Category : $select_job <br />";
			}	
			if($applied_jobs) { 
			$CustomMessageText .= "<br />Applied Job For : $applied_jobs <br />";
			}			
			if($contact_message) { 
			$CustomMessageText .= "<br />Total Year of experience (with detail description) : $contact_message <br />";
			}		
			$CustomMessageText .= "<br />Page Url : $page_url <br /> <br /> <br />";	
			
			$CustomMessageSubject = "Career Enquiry from www.adolf7.com";  
		
	
 
			$subject = $CustomMessageSubject;
			$message = $CustomMessageText;

			$to = "salesadolf7@gmail.com";
				$company_email = "salesadolf7@gmail.com"; 
		 
				$mail = new PHPMailer();
				$mail->Priority = 1;
				$mail->ContentType = "text/html";
				$mail->From = $company_email;
				$mail->FromName = $company_name;
				$mail->Host = "localhost";
				$mail->Subject = $subject;
				$mail->AltBody = "";
				
	if(!empty($resume_title)) {	
		
	  	$attachment =  'resumes/'. $resume_title;				
					
		 $filetype = $file_type;									
			
		$mail->AddAttachment($attachment,$resume_title,'base64',$filetype);		   

	}  
 				$mail->AddAddress(trim($to), '');
				$mail->AddAddress("customercare@adolf7.com.test-google-a.com", "");
				$mail->AddBCC("anzar.webclickindia@gmail.com", "");
				$mail->AddBCC("raj.webclick@gmail.com", "");
				$mail->AddBCC("webclickindia.enquiry@gmail.com", "");
			 
		

        $mail->Body	= $message;
        $mail->AddReplyTo("$company_email");
	 	 		
		$mail->Send();
		 
		
		die;
				 
		} 
		else {
			die;
			 
		}
		}
	}
  
  
	public function add() {
		 $this->layout = "ajax";
		 
		    if ($this->request->is('ajax')) {
			   
			   $this->request->data['formrequest']  = 'ajax';
		   }
		   else {
			   
			   $this->request->data['formrequest']  = 'post';
		   }
		   
			if($this->request->data['formrequest'] == 'post') { die; }
			
			if($this->request->data['name'] == 'Joe Miller' OR $this->request->data['name'] == 'HenryNeide' OR $this->request->data['name'] == 'Den' OR $this->request->data['email'] == 'info@domainworld.com' OR $this->request->data['email'] == 'info@orderdomains.com' OR $this->request->data['email'] == 'info@domainregistration.com') { die; }

				foreach ($this->request->data as $key => $value) {
				$this->request->data[$key] = trim($value);
				}
			  			
		if ($this->request->is('post')) {
			
			$this->request->data['ip_address'] = $_SERVER['REMOTE_ADDR'];
			$this->Enquiry->create();
			
			if ($this->Enquiry->save($this->request->data)) {
				
			if($this->request->data['formrequest'] == 'post') { die; }

			App::import('Vendor', 'phpmailer', array('file' => 'phpmailer' . DS . 'class.phpmailer.php'));
			$enquiry_for     = $this->request->data['enquiry_for'];
			$contact_name    = @$this->request->data['name'];
			$contact_last_name = @$this->request->data['last_name'];
			$contact_email   = @$this->request->data['email'];
			$contact_phone   = @$this->request->data['phone'];
			$contact_mobile  = @$this->request->data['mobile'];
			$contact_company = @$this->request->data['company'];	
			$contact_address = @$this->request->data['address'];			
			$contact_message = @$this->request->data['message'];
			$city = @$this->request->data['city'];
			$company_details = $this->Session->read('company_details');
			$company_name    = $company_details['company_name']; 
			$company_email   = $company_details['company_email']; 	   

			if(empty($contact_name)) { die; }
			
			$page_url        = @$this->request->data['page_url']; 
				
			$CustomMessageText = "<br><br />Name : $contact_name <br />";
			
			if($contact_last_name) { 
			$CustomMessageText = "<br />Name : $contact_name $contact_last_name <br />";	
			}
			
			$CustomMessageText .= "<br />Email Address : $contact_email <br />"; 					
				
				 
			if($contact_phone) { 
			$CustomMessageText .= "<br />Phone : $contact_phone <br />";	
			}
			
			if($contact_mobile) { 
			$CustomMessageText .= "<br />Phone : $contact_mobile <br />";	
			}	
			
			if($city) { 
			$CustomMessageText .= "<br />City : $city <br />";	
			}
						
			if($contact_company) { 
			$CustomMessageText .= "<br />Company : $contact_company <br />";
			}	
			if($contact_address) { 
			$CustomMessageText .= "<br />Address : $contact_address <br />";
			}	
			if($contact_message) { 
			$CustomMessageText .= "<br />Message : $contact_message <br />";
			}		
			
			$CustomMessageText .= "<br />Page Url : $page_url <br /> <br /> <br />";	
			
			$CustomMessageSubject = "Enquiry from ADOLF7 Automotive Industries Pvt. Ltd.";  
            
            $website = "www.adolf7.com";
						
		$subject = $CustomMessageSubject;
		$message = $CustomMessageText;
		
		$company_email = "customercare@adolf7.com";
		
		$data_string = '[{"name": "'.$contact_name.'","mobile": '.$contact_mobile.',"email": "'.$contact_email.'","detail1": "'.$contact_message.'","detail2": "'.$contact_address.'"}]';

		try
		{
		//	$curl = curl_init('https://api.neodove.com/integration/custom/29958d04-832d-4506-995f-0d031cb4325a/leads');
		    $curl = curl_init('https://app.lmsbaba.com/capture/website/09b0cf2124a83e2b47649faa470a3dae');
			curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curl, CURLOPT_HEADER, 0);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_HTTPHEADER, array(
					"Content-Type:application/json",
					"Content-Length:".strlen($data_string)
					));
			$json_response = curl_exec($curl);
			echo $json_response;
			curl_close($curl);
		} catch (Exception $ex) {
			curl_close($curl);
		}
		
		$mail_to = "salesadolf7@gmail.com,customercare@adolf7.com";
		//	$mail_to = "anzar.webclickindia@gmail.com";
		//	$mail_bbc = "anzar.webclickindia@gmail.com";
		$mail_bbc = "webclickindia.enquiry@gmail.com,anzar.webclickindia@gmail.com";
		
		$contactus = "contactus";
         	
        $base_url =   "http://www.webclickindia.in/";  
          
        $url2 = $base_url.'smtp/common.php';
        
        $ch = curl_init($url2);
        curl_setopt($ch, CURLOPT_POST, true);  
        						
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        "mode"    => $contactus,
        "contact_name" => $contact_name,
        "contact_email" => $contact_email,
        "contact_phone" => $contact_phone,
        "contact_mobile" => $contact_mobile,
        "contact_address" => $contact_address,
        "contact_message" => $contact_message,
        "contact_city" => $contact_city,
        "website" => $website,
        "company_email" => $company_email,
        "company_name" => $company_name,
        "mail_to" => $mail_to,
        "mail_bbc" => $mail_bbc,
        "enquiry_for" => $enquiry_for,
        "page_url" => $page_url,
        ));
        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         
         
        echo $err = curl_error($ch);
        
        $result = curl_exec($ch);
        
        exit;
				
				$to = "salesadolf7@gmail.com";
				
				$company_email = "salesadolf7@gmail.com"; 
				
				$mail = new PHPMailer();
				$mail->Priority = 1;
				$mail->ContentType = "text/html";
				$mail->From = $company_email;
				$mail->FromName = $company_name;
				$mail->Host = "localhost";
				$mail->Subject = $subject;
				$mail->AltBody = "";
				
				$mail->AddAddress(trim($to), '');
				$mail->AddAddress("customercare@adolf7.com.test-google-a.com", "");
				$mail->AddBCC("webclickindia.enquiry@gmail.com", "");
				$mail->AddBCC("anzar.webclickindia@gmail.com", "");

				$mail->Body = $message;
				$mail->AddReplyTo("$company_email");
				if ($mail->Send())
					{
					}
				  else
					{
					}

				die;
				}
			  else
				{
				die;
				}
			}
		}

	public

	function thanks()
		{
		}

	/**
	 * admin_index method
	 *
	 * @return void
	 */
	public

	function admin_index()
		{
		$enquiries = $this->Enquiry->find('all', array(
			'order' => array(
				'Enquiry.id' => 'DESC'
			) ,
		));
		$this->set(compact('enquiries'));
		}

	/**
	 * admin_view method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public

	function admin_view($id = null)
		{
		if (!$this->Enquiry->exists($id))
			{
			throw new NotFoundException(__('Invalid enquiry'));
			}

		$options = array(
			'conditions' => array(
				'Enquiry.' . $this->Enquiry->primaryKey => $id
			)
		);
		$this->set('enquiry', $this->Enquiry->find('first', $options));
		}

	/**
	 * admin_send method
	 *
	 * @return void
	 */
	public

	function admin_send($id = null)
		{
		if (!$this->Enquiry->exists($id))
			{
			throw new NotFoundException(__('Invalid enquiry'));
			}

		$options = array(
			'conditions' => array(
				'Enquiry.' . $this->Enquiry->primaryKey => $id
			)
		);
		$this->set('enquiry', $this->Enquiry->find('first', $options));
		if ($this->request->is('post'))
			{
			if ($this->Enquiry->Emailsent($this->request->data))
				{
				$this->Session->setFlash(__('The enquiry responded successully.'));
				return $this->redirect(array(
					'action' => 'index'
				));
				}
			  else
				{
				$this->Session->setFlash(__('The enquiry could not be responded. Please, try again.'));
				}
			}
		}

	/**
	 * admin_add method
	 *
	 * @return void
	 */
	public

	function admin_add()
		{
		if ($this->request->is('post'))
			{
			$this->Enquiry->create();
			if ($this->Enquiry->save($this->request->data))
				{
				$this->Session->setFlash(__('The enquiry has been saved.'));
				return $this->redirect(array(
					'action' => 'index'
				));
				}
			  else
				{
				$this->Session->setFlash(__('The enquiry could not be saved. Please, try again.'));
				}
			}
		}

	/**
	 * admin_edit method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public

	function admin_edit($id = null)
		{
		if (!$this->Enquiry->exists($id))
			{
			throw new NotFoundException(__('Invalid enquiry'));
			}

		if ($this->request->is(array(
			'post',
			'put'
		)))
			{
			if ($this->Enquiry->save($this->request->data))
				{
				$this->Session->setFlash(__('The enquiry has been saved.'));
				return $this->redirect(array(
					'action' => 'index'
				));
				}
			  else
				{
				$this->Session->setFlash(__('The enquiry could not be saved. Please, try again.'));
				}
			}
		  else
			{
			$options = array(
				'conditions' => array(
					'Enquiry.' . $this->Enquiry->primaryKey => $id
				)
			);
			$this->request->data = $this->Enquiry->find('first', $options);
			}
		}

	/**
	 * admin_delete method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public

	function admin_delete($id = null)
		{
		$this->Enquiry->id = $id;
		if (!$this->Enquiry->exists())
			{
			throw new NotFoundException(__('Invalid enquiry'));
			}

		$this->request->allowMethod('post', 'delete');
		if ($this->Enquiry->delete())
			{
			$this->Session->setFlash(__('The enquiry has been deleted.'));
			}
		  else
			{
			$this->Session->setFlash(__('The enquiry could not be deleted. Please, try again.'));
			}

		return $this->redirect(array(
			'action' => 'index'
		));
		}
	}