<?php
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
App::uses('CakeEmail', 'Network/Email');
ini_set('max_execution_time', 900);
App::uses('ContactLog', 'ContactLog');
class ContactLogsController 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('ContactLog.id'=>'desc'));
    public function index($id=null)
    {
        $condition = array();
        
        if($this->userType=='AT')$condition[]=array('ContactLog.user_id'=>$this->agencyId);
	    if($this->userType=='AGT')$condition[]=array('ContactLog.user_id'=>$this->adminValue['User']['id']);


        $this->loadModel('Category');
        $this->loadModel('Location');
        $this->set('category',$this->Category->find('list'));
        $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('ContactLog.location_id'=>$location_id);
	   }
	   $this->set('location_id',$location_id);
	   
	   $category_id = 0;
        if(isset($this->params['named']['category_id']) && strlen($this->params['named']['category_id'])>0){
    	    $category_id = $this->params['named']['category_id'];
    	    $condition[]=array('ContactLog.category_id'=>$category_id);
	   }
	   $this->set('category_id',$category_id);
       

       if(isset($this->params['named']['user_id']) && strlen($this->params['named']['user_id']) > 0){
       	    $condition[]=array('ContactLog.user_id'=>$this->params['named']['user_id']);
       	}
        
        $this->Prg->commonProcess();
        $this->Paginator->settings = $this->paginate;
        $this->Paginator->settings['conditions'] = array($this->ContactLog->parseCriteria($this->Prg->parsedParams()),$condition);

        $ContactLogs = $this->Paginator->paginate();
        $this->set('ContactLog', $ContactLogs);
        
	    $this->set('luserId',$this->luserId);
	    if($this->request->is('ajax')){
            $this->render('index','ajax');
	    }
    }

    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->ContactLog->findById($id);
        $this->set('post',$post);
    }
}
