<?php
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
App::uses('CakeEmail', 'Network/Email');
ini_set('max_execution_time', 900);
class ContactsController extends AdminAppController {
    public $helpers = array('Html', 'Form','Session','Paginator','Js'=> array('Jquery'));
    public $components = array('Session','Paginator','search-master.Prg');
    public $presetVars = true;
    var $paginate = array('page'=>1,'order'=>array('Contact.id'=>'desc'));
    public function index($id=null)
    {
    	if($this->userType=='AGT'){
		    $condition=$this->CustomFunction->agencyWisePermission($id,$this->userType,$this->agencyId,'Contact.project_id');
	    }else{
            $condition=$this->CustomFunction->agencyWisePermission($id,$this->userType,$this->luserId,'Contact.project_id');
        }

        $this->loadModel('Category');
        $this->set('category',$this->Category->find('list'));
        $this->loadModel('Location');
        $this->set('location',$this->Location->find('list'));
        $location_id = 0;
        if(isset($this->params['named']['location_id']) && strlen($this->params['named']['location_id'])>0)
	    {
    	    $location_id = $this->params['named']['location_id'];
    	    $condition[]=array('Contact.location_id'=>$location_id);
	   }
	   $this->set('location_id',$location_id);
       if(isset($this->params['named']['user_id']) && strlen($this->params['named']['user_id'])>0)
       	{
       	    $condition[]=array('Contact.user_id'=>$this->params['named']['user_id']);
       	}
        if(isset($this->params['named']['client_id']) && strlen($this->params['named']['client_id'])>0)
    	{
    	    $condition[]=array('Contact.id'=>$this->params['named']['client_id']);
    	}
        $this->Prg->commonProcess();
        $this->Paginator->settings = $this->paginate;
        $this->Paginator->settings['conditions'] = array($this->Contact->parseCriteria($this->Prg->parsedParams()),$condition);

        if(isset($this->params->query['export']) && $this->params->query['export'] == 'Excel'){
            $this->Paginator->settings['limit']=40000;
        	$this->Paginator->settings['maxLimit']=40000;
        }else{
		    $this->Paginator->settings['limit']=$this->pageLimit;
        	$this->Paginator->settings['maxLimit']=$this->maxLimit;
        }
        $Contacts = $this->Paginator->paginate();
        $this->set('Contact', $Contacts);
        if(isset($this->params->query['export']) && $this->params->query['export'] == 'Excel'){
	        //Excel Export
	        include(APP.'Plugin/Excel/PHPExcel.php');
		$Excel = new PHPExcel();
		$Excel->getProperties()->setCreator('Umer Umair')
			->setLastModifiedBy('Umer Umair')
			->setTitle('Properties')
			->setSubject('All Properties List');
		$Excel->setActiveSheetIndex(0);
		$Sheet = $Excel->getActiveSheet();
		$Sheet->setTitle('Account Report');
		//Setting Headers
		$Sheet->setCellValue('A1','#')
			->setCellValue('B1','Name')
			->setCellValue('C1','Company Name')
			->setCellValue('D1','Location')
			->setCellValue('E1','Intrested In')
			->setCellValue('F1','Company Number')
			->setCellValue('G1','Email')
			->setCellValue('H1','District');

		//Writing Data
		foreach($Contacts as $K=>$Row){
			$Sheet->setCellValue(sprintf('A%d',$K+2),$Row['Contact']['id'])
				->setCellValue(sprintf('B%d',$K+2),$Row['Contact']['name'])
				->setCellValue(sprintf('B%d',$K+2),$Row['Contact']['company_name'])
				->setCellValue(sprintf('D%d',$K+2),$Row['Location']['name'])
				->setCellValue(sprintf('E%d',$K+2),$Row['Contact']['intrest_in'])
				->setCellValue(sprintf('F%d',$K+2),$Row['Contact']['mobile'])
				->setCellValue(sprintf('G%d',$K+2),$Row['Contact']['email'])
				->setCellValue(sprintf('H%d',$K+2),$Row['Contact']['district']);
		}

		//Downloading File
		$Writer = PHPExcel_IOFactory::createWriter($Excel, 'Excel2007');
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;Filename="Contacts.xlsx"');
		$Writer->save('php://output');
		return $this->redirect(array('controller'=>'Contacts','action' => 'index'));
	}
	$this->set('luserId',$this->luserId);
	if ($this->request->is('ajax'))
        {
            $this->render('index','ajax'); // View, Layout
        }
    }
    public function add()
    {
        $this->loadModel('Category');
        $this->set('category',$this->Category->find('list',array('conditions'=>array('type'=>NULL))));
        $this->set('agency',$this->CustomFunction->agencyWiseList($this->userType,$this->agencyId));
        $this->loadModel('Location');
        $this->set('location',$this->Location->find('list'));
        if ($this->request->is('post'))
        {

           // echo '<pre>';print_r($this->$this->request->is('post'));
            //die;
            $this->Contact->create();
            try
            {
                $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
                $password = rand();
                $this->request->data['Contact']['password'] = $passwordHasher->hash($password);
		if($this->userType=='AGT'){
		    $this->request->data['Contact']['project_id']=$this->agencyId;
		}
		if($this->userType=='AGY'){
			$this->request->data['Contact']['project_id']=$this->luserId;
		}
		$this->request->data['Contact']['user_id']=$this->luserId;
                if ($this->Contact->save($this->request->data))
                {
                    $email=$this->request->data['Contact']['email'];$clientName=$this->request->data['Contact']['name'];
                    $mobileNo=$this->request->data['Contact']['phone'];
                    $siteName=$this->siteName;$contactNo=$this->contact;$url=$this->siteDomain;
                    if($email)
                    {
                        if($this->emailNotification)
                        {
                            /* Send Email */
                            $this->loadModel('Emailtemplate');
                            $emailSettingArr=$this->Emailtemplate->findByType('CLC');
                            if($emailSettingArr['Emailtemplate']['status']=="Published")
                            {
                                $message=eval('return "' . addslashes($emailSettingArr['Emailtemplate']['description']) . '";');
                                $Email = new CakeEmail();
                                $Email->transport($this->emailSettype);
                                if($this->emailSettype=="Smtp")
                                $Email->config(array('host' => $this->emailHost,'port' =>  $this->emailPort,'username' => $this->emailUsername,'password' => $this->emailPassword));
                                $Email->from(array($this->siteEmail =>$this->siteName));
                                $Email->to($email);
                                $Email->template('default');
                                $Email->emailFormat('html');
                                $Email->subject($emailSettingArr['Emailtemplate']['name']);
                                $Email->send($message);
                                /* End Email */
                            }
                        }
                    }
                    if($mobileNo)
                    {
                        if($this->smsNotification)
                        {
                            /* Send Sms */
                            $this->loadModel('Smstemplate');
                            $smsTemplateArr=$this->Smstemplate->findByType('CLC');
                            if($smsTemplateArr['Smstemplate']['status']=="Published")
                            {
                                $url="$this->siteDomain";
                                $message=eval('return "' . addslashes($smsTemplateArr['Smstemplate']['description']) . '";');
                                $this->CustomFunction->sendSms($mobileNo,$message,$this->smsSettingArr);
                            }
                            /* End Sms */
                        }
                    }
                    $this->Session->setFlash(__('Your Contact Manager has been saved.'),'flash',array('alert'=>'success'));
                    return $this->redirect(array('action' => 'add'));
                }
            }
            catch (Exception $e)
            {
                $this->Session->setFlash($e->getMessage(),'flash',array('alert'=>'danger'));
                return $this->redirect(array('action' => 'index'));
            }
        }
    }
    public function edit($id = null)
    {
        if (!$id)
        {
            throw new NotFoundException(__('Invalid post'));
        }
        $ids=explode(",",$id);
        $post=array();
        foreach($ids as $k=>$id)
        {$k++;
            $post[$k]=$this->Contact->findByid($id);
	    if($this->userType=='AGT') {
		if($post[$k]['Contact']['user_id']!=$this->luserId) {
		    $this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
		    return $this->redirect(array('action' => 'index'));
		}
	    }
	    elseif($this->userType=='AGY') {
		if($post[$k]['Contact']['project_id']!=$this->agencyId) {
		    $this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
		    return $this->redirect(array('action' => 'index'));
		}
	    }
        }
        $this->set('Contact',$post);
        if (!$post)
        {
            throw new NotFoundException(__('Invalid post'));
        }
        $this->loadModel('Category');
        $this->set('category',$this->Category->find('list'));
        $this->loadModel('Location');
        $this->set('location',$this->Location->find('list'));
        if ($this->request->is(array('post', 'put')))
        {
            try
            {
                if ($this->Contact->save(isset($this->request->data[1]) ? $this->request->data[1] : $this->request->data[0]))
                {
                    $this->Session->setFlash(__('Your Contact Manager has been Updated.'),'flash',array('alert'=>'success'));
                    return $this->redirect(array('action' => 'index'));
                }
            }
            catch (Exception $e)
            {
                $this->Session->setFlash(__('Invalid Post.'),'flash',array('alert'=>'danger'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->set('isError',true);
        }
        else
        {
            $this->layout = null;
            $this->set('isError',false);
        }
        if (!$this->request->data)
        {
            $this->request->data = $post;
        }
    }
    public function deleteall()
    {
        try
        {
            if ($this->request->is('post'))
            {
                foreach($this->data['Contact']['id'] as $key => $value)
                {
		    if($value > 0) {
			$prop[$key]=$this->Contact->findByid($value);
			if($this->userType=='AGT') {
			    if($prop[$key]['Contact']['user_id']!=$this->luserId) {
				$this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
				return $this->redirect(array('action' => 'index'));
			    }
			}
			elseif($this->userType=='AGY') {
			    if($prop[$key]['Contact']['project_id']!=$this->agencyId) {
				$this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
				return $this->redirect(array('action' => 'index'));
			    }
			}
                    $this->Contact->delete($value);
		    }
                }
                $this->Session->setFlash(__('Your Contact Manager has been deleted.'),'flash',array('alert'=>'success'));
            }
            $this->redirect(array('action' => 'index'));
        }
        catch (Exception $e)
        {
            $this->Session->setFlash(__('Please Delete Deal first.'),'flash',array('alert'=>'danger'));
            return $this->redirect(array('action' => 'index'));
        }
    }
    public function view($id = null)
    {
        $this->layout = null;
        if (!$id)
        {
            $this->Session->setFlash(__('Invalid Post.'),'flash',array('alert'=>'danger'));
            $this->redirect(array('action' => 'index'));
        }
        $post = $this->Contact->findById($id);
        $this->set('post',$post);
    }
    public function printclient($id = null)
    {
        $this->layout = null;
        if (!$id)
        {
            $this->Session->setFlash(__('Invalid Post.'),'flash',array('alert'=>'danger'));
            $this->redirect(array('action' => 'index'));
        }
        $post = $this->Contact->findById($id);
        $this->set('post',$post);
    }
    public function changepass($id = null)
    {
        $this->layout = null;
        if (!$id)
        {
            $this->Session->setFlash(__('Invalid Post.'),'flash',array('alert'=>'danger'));
            $this->redirect(array('action' => 'index'));
        }
        $post = $this->Contact->findById($id);
        if (!$post)
        {
            $this->Session->setFlash(__('Invalid Post.'),'flash',array('alert'=>'danger'));
            $this->redirect(array('action' => 'index'));
        }
	if($this->userType=='AGT') {
	    if($post['Contact']['user_id']!=$this->luserId) {
		$this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
		return $this->redirect(array('action' => 'index'));
	    }
	}
	elseif($this->userType=='AGY') {
	    if($post['Contact']['project_id']!=$this->agencyId) {
		$this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
		return $this->redirect(array('action' => 'index'));
	    }
	}
        if ($this->request->is(array('post', 'put')))
        {
	    try
	    {
		$passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
		$this->Contact->id = $id;
		$this->request->data['Contact']['password'] = $passwordHasher->hash($this->request->data['Contact']['password']);
		if ($this->Contact->save($this->request->data,array('validate'=>false)))
		{
		    $this->Session->setFlash(__('Password Changed Successfully'),'flash',array('alert'=>'success'));
		    $this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Invalid Post.'),'flash',array('alert'=>'danger'));
		$this->redirect(array('action' => 'index'));
	    }
	    catch (Exception $e)
            {
                $this->Session->setFlash(__('Invalid Post.'),'flash',array('alert'=>'danger'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->set('isError',true);
        }
        else
        {
            $this->layout = null;
            $this->set('isError',false);
        }
        if (!$this->request->data)
        {
            $this->request->data = $post;
        }
    }
}
