<?php
App::uses('AdminAppController', 'Admin.Controller');
App::uses('AppController', 'Controller');
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
App::uses('CakeEmail', 'Network/Email');
App::uses('Contact', 'Contact');
class UsersController 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('User.name'=>'asc'));
    public function index()
    {
        $this->Prg->commonProcess();
        $this->Paginator->settings = $this->paginate;
	$cond=array('Ugroup.type NOT IN'=>array('AGY','AGT'));
	if($this->luserId != 1){
		$cond += array('User.project_id'=>$this->agencyId);
//                $cond += array('User.project_id'=>$this->agencyId);
	}
//        if($type=='AT'){
//	    $this->set('type',$type);
//	    $this->set('pId',$this->agencyId);
//	}
        $this->Paginator->settings['conditions'] = array('deleted IS NULL',$this->User->parseCriteria($this->Prg->parsedParams()),$cond);
	$this->Paginator->settings['limit']=$this->pageLimit;
        $this->Paginator->settings['maxLimit']=$this->maxLimit;
        $this->set('User', $this->Paginator->paginate());
	if ($this->request->is('ajax'))
        {
            $this->render('index','ajax'); // View, Layout
        }
    }
    public function add($type=null)
    {
	if(!$type && $this->userType!='ADR'){
	    $this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
            return $this->redirect(array('action' => 'index'));
	}
	elseif($type!='AY' && $type!='AT' && $this->userType!='ADR'){
	    $this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
            return $this->redirect(array('action' => 'index'));
	}
        elseif($type=='AY' && $this->userType!='ADR'){
	    $this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
            return $this->redirect(array('action' => 'index'));
	}
        elseif($type=='AY'){
	    $this->set('type',$type);
	}
        elseif($type=='AT'){
	    $this->set('type',$type);
	    $this->set('pId',$this->agencyId);
	}
	$this->set('type',$type);
        $this->loadModel('Ugroup');
        $this->set('ugroup',$this->Ugroup->find('list',array('conditions'=>array('Ugroup.type NOT IN'=>array('AGY','AGT')))));
        $this->loadModel('Location');
        $this->set('location',$this->Location->find('list'));
        $this->loadModel('Project');
	$cond=array();
	if($this->luserId!=1){
		/*$cond=array('Project.id'=>$this->agencyId);
	        $this->set('project',$this->Project->find('list',array('conditions'=>$cond)));*/
	}
        $this->set('project',$this->CustomFunction->agencyWiseList($this->userType,$this->agencyId));
        if($this->request->is('post'))
        {
            $this->User->create();
            try
            {
                $password=$this->request->data['User']['password'];
		if(isset($this->request->data['User']['ugroup_id']) && $this->request->data['User']['ugroup_id']==4){
		    $projectRecord=array('name'=>$this->request->data['User']['name'],'email'=>$this->request->data['User']['email'],'phone'=>$this->request->data['User']['phone'],'mobile'=>$this->request->data['User']['mobile']);
		    $this->Project->save($projectRecord);
		    $lastId=$this->Project->id;
		    $this->request->data['User']['project_id']=$lastId;
		}
		if(!$this->request->data['User']['project_id']){
			$this->request->data['User']['project_id']=$this->luserId;
		}
		if($type =='AY'){
			$this->request->data['User']['ugroup_id']=4;
		}elseif($type == 'AT'){
			$this->request->data['User']['ugroup_id']=2;
                        $this->request->data['User']['api_code']=$this->request->data['User']['api_code'];;
                        $this->request->data['User']['api_password']=$this->request->data['User']['api_password'];;
                        $this->request->data['User']['api_mask']=$this->request->data['User']['api_mask'];;
		}else{
			$this->request->data['User']['ugroup_id']=1;
		}
		if ($this->User->save($this->request->data))
		{
		    $email=$this->request->data['User']['email'];$name=$this->request->data['User']['name'];$mobileNo=$this->request->data['User']['mobile'];
		    $userName=$this->request->data['User']['username'];
		    $siteName=$this->siteName;$siteEmailContact=$this->siteEmailContact;$url=$this->siteDomain.'/admin';
		    if($email)
		    {
			if($this->emailNotification)
			{
			    /* Send Email */
			    $this->loadModel('Emailtemplate');
			    $emailSettingArr=$this->Emailtemplate->findByType('ULC');
			    if($emailSettingArr['Emailtemplate']['status']=="Published")
			    {
				$message=eval('return "' . addslashes($emailSettingArr['Emailtemplate']['description']) . '";');
				$Email = new CakeEmail();
				$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('ULC');
			    if($smsTemplateArr['Smstemplate']['status']=="Published")
			    {
				$url="$this->siteDomain";
				$message=eval('return "' . addslashes($smsTemplateArr['Smstemplate']['description']) . '";');
				$this->CustomFunction->sendSms($mobileNo,$message,$this->smsSettingArr);
			    }
			    /* End Sms */
			}
		    }
		    if($type=='AY'){
			$this->Session->setFlash(__('Agency has been saved.'),'flash',array('alert'=>'success'));
			return $this->redirect(array('controller'=>'Agencies','action' => 'Index'));
		    }
		    elseif($type=='AT'){
			$this->Session->setFlash(__('Agent has been saved.'),'flash',array('alert'=>'success'));
			return $this->redirect(array('controller'=>'Agents','action' => 'Index'));
		    }
		    else{
			$this->Session->setFlash(__('User has been saved.'),'flash',array('alert'=>'success'));
			return $this->redirect(array('action' => 'add'));
		    }
		}
            }
            catch (Exception $e)
            {
              	$this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
                return $this->redirect(array('action' => 'index'));
            }
        }
    }
    public function edit($id = null)
    {
	$this->set('luserId',$this->adminValue['User']['project_id']);
        $this->loadModel('Ugroup');
        $this->set('ugroup',$this->Ugroup->find('list'));
        /*$this->loadModel('Project');
        $this->set('project',$this->Project->find('list'));*/
        $this->set('project',$this->CustomFunction->agencyWiseList($this->userType,$this->agencyId));
        $this->loadModel('Location');
        $this->set('location',$this->Location->find('list'));
        if (!$id)
        {
            throw new NotFoundException(__('Invalid post'));
        }
        $ids=explode(",",$id);
        $post=array();
        foreach($ids as $k=>$id)
        {
	    $k++;
            $post[$k]=$this->User->findById($id);
        }
        $this->set('User',$post);
        if (!$post)
        {
            throw new NotFoundException(__('Invalid post'));
        }
        if ($this->request->is(array('post', 'put')))
        {
	    $this->User->id = $id;
            try
            {
		$projectRecord=array();
		foreach($this->request->data as $key => $value)
                {
		    if(strlen($this->request->data[$key]['User']['project_id'])>0){
			$projectId=$this->request->data[$key]['User']['project_id'];
			$projectRecord[]=array('id'=>$projectId,'name'=>$this->request->data[$key]['User']['name'],'email'=>$this->request->data[$key]['User']['email'],'phone'=>$this->request->data[$key]['User']['phone'],'mobile'=>$this->request->data[$key]['User']['mobile']);
		    }
                }
                //unset($this->request->data[1]['User']['project_id']);
                if ($this->User->saveAll($this->request->data))
                {
		    //$this->Project->saveAll($projectRecord);
		    if($post[$k]['Ugroup']['type']=='AGY'){
			$this->Session->setFlash(__('Agency has been updated.'),'flash',array('alert'=>'success'));
			return $this->redirect(array('controller'=>'Agencies','action' => 'index'));
		    }
		    elseif($post[$k]['Ugroup']['type']=='AGT'){
			$this->Session->setFlash(__('Agent has been updated.'),'flash',array('alert'=>'success'));
			return $this->redirect(array('controller'=>'Agents','action' => 'index'));
		    }
		    else{
			$this->Session->setFlash(__('Users has been updated.'),'flash',array('alert'=>'success'));
			return $this->redirect(array('controller'=>'Users','action' => 'index'));
		    }
                }
            }
            catch (Exception $e)
            {
                $this->Session->setFlash($e.__('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 editprofile()
    {
	$this->loadModel('Location');
	$id=$this->luserId;
        $post=$this->User->findById($id);
	$this->set('location',$this->Location->find('list',array('order'=>array('name'=>'asc'))));
        if ($this->request->is(array('post', 'put')))
        {
	    try
            {
		$projectRecord=array();
		if($this->userType=='AGY'){
		    $projectId=$post['User']['project_id'];
		    $projectRecord=array('id'=>$projectId,'name'=>$this->request->data['User']['name'],'email'=>$this->request->data['User']['email'],'phone'=>$this->request->data['User']['phone'],'mobile'=>$this->request->data['User']['mobile']);
		}
		$this->request->data['User']['id']=$id;
                if ($this->User->save($this->request->data['User']))
                {
		    if($this->userType=='AGY'){
			$this->loadModel('Project');
			$this->Project->save($projectRecord);
		    }
		    $this->Session->setFlash(__('User has been updated.'),'flash',array('alert'=>'success'));
		    return $this->redirect(array('controller'=>'Users','action' => 'myProfile'));
                }
            }
            catch (Exception $e)
            {
                $this->Session->setFlash(__('Invalid Post'),'flash',array('alert'=>'danger'));
                return $this->redirect(array('action' => 'myProfile'));
            }
        }
	$this->set('post',$post);
        if (!$this->request->data)
        {
            $this->request->data = $post;
        }
    }
    public function deleteall()
    {
        if ($this->request->is('post'))
        {
            foreach($this->data['User']['id'] as $key => $value)
            {
                if($value!=1)
                $this->User->delete($value);
            }
            $this->Session->setFlash(__('User has been deleted.'),'flash',array('alert'=>'success'));
        }
        $this->redirect(array('action' => 'index'));
    }
    public function myProfile()
    {
        $userValue=$this->Session->read('User');
        $post = $this->User->findById($userValue['User']['id']);
	if(strlen($post['User']['photo'])>0)
        $std_img='user_thumb/'.$post['User']['photo'];
        else
        $std_img='User.png';
        $this->set('std_img', $std_img);
	$this->set('post',$post);
    }
    public function assignrights()
    {
	if($this->adminValue['User']['id']!=1){
	    return $this->redirect(array('action' => 'index'));
	}
        $this->loadModel('Ugroup');
        $Ugroup=$this->Ugroup->find('all',array('conditions'=>array('id >'=>1),'order'=>'name asc'));
        $this->set('Ugroup',$Ugroup);
    }
    public function assignrightsedit($id=null)
    {
	if($this->adminValue['User']['id']!=1){
	    return $this->redirect(array('action' => 'index'));
	}
        $this->layout = null;
        $this->loadModel('Ugroup');
        $this->loadModel('PageRight');
        if (!$id)
        {
            throw new NotFoundException(__('Invalid post'));
        }
        $isPost=$this->Ugroup->find('count',array('conditions'=>array('id'=>$id)));
        if($isPost==0)
        {
            throw new NotFoundException(__('Invalid post'));
        }
        $post=$this->User->assingPages($id);
        $this->set('PageRight',$post);
        if ($this->request->is(array('post', 'put')))
        {
            try
            {
                $this->PageRight->deleteAll(array('ugroup_id'=>$id));
                foreach($this->request->data['PageRight']['id'] as $value)
                {
                    if($value>0)
                    {
                        $this->PageRight->create();
                        $this->PageRight->save(array('page_id'=>$value,'ugroup_id'=>$id,'view_right'=>1));

                    }
                }
                $this->Session->setFlash(__('Permission update successfully'),'flash',array('alert'=>'success'));
                return $this->redirect(array('action' => 'assignrights'));
            }
            catch (Exception $e)
            {
                $this->Session->setFlash(__('Error in updation'),'flash',array('alert'=>'danger'));
                return $this->redirect(array('action' => 'assignrights'));
            }
        }
        $this->set('id',$id);
        if (!$this->request->data)
        {
            $this->request->data = $post;
        }
    }
    function login_form() {

        // echo json_encode(array('alert'=> $this->data));

        $this->api();
        if (isset($_GET['type']) && $_GET['type'] == 'api') {
                error_reporting(0);
            if (empty($this->data['username']) == false) {
                $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
                $password = $passwordHasher->hash($this->request->data['password']);


                //  $user = $this->User->find('first',array('fields'=>array('id','name','ugroup_id','project_id','username','phone','mobile','address','email')) , array('conditions' => array('User.username' => $this->data['username'],'User.password' =>$password,'User.status'=>'Active','User.deleted IS NULL')));
                $user = $this->User->find('first', array('conditions' => array('User.username' => $this->data['username'], 'User.password' => $password, 'User.status' => 'Active', 'User.deleted IS NULL')));
                if ($user != false) {
                    unset($user->password);
                    $this->Session->setFlash(__('Thank you for logging in!'), 'flash', array('alert' => 'success'));
                    $this->Session->write('User', $user);
                    echo json_encode(array('code' => '201', 'msg' => 'Thank you for logging in!', 'User' => $user));
                    exit;
                } else {
                    $this->Session->setFlash(__('Incorrect username/password!'), 'flash', array('alert' => 'danger'));
                    echo json_encode(array('code' => '401', 'msg' => 'Incorrect username or password'));
                    exit;
                }
            }
        } else if (empty($this->data['User']['username']) == false) {
            $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
            $password = $passwordHasher->hash($this->request->data['User']['password']);
            $user = $this->User->find('first', array('conditions' => array('User.username' => $this->data['User']['username'], 'User.password' => $password, 'User.status' => 'Active', 'User.deleted IS NULL')));
            if ($user != false) {
                $this->Session->setFlash(__('Thank you for logging in!'), 'flash', array('alert' => 'success'));
                $this->Session->write('User', $user);
                $this->Redirect(array('controller' => 'Dashboards', 'action' => 'index'));
                exit();
            } else {
                $this->Session->setFlash(__('Incorrect username/password!'), 'flash', array('alert' => 'danger'));
                $this->Redirect(array('action' => 'login_form'));
                exit();
            }
        }
    }

    function login_api()
    {
        
         
                echo json_encode(array('alert'=> 'danger'));
                exit;
        if (empty($this->data['User']['username']) == false)
        {
            $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
            $password=$passwordHasher->hash($this->request->data['User']['password']);
            $user = $this->User->find('first', array('conditions' => array('User.username' => $this->data['User']['username'],'User.password' =>$password,'User.status'=>'Active','User.deleted IS NULL')));
            if($user != false)
            {
                $this->Session->setFlash(__('Thank you for logging in!'),'flash', array('alert'=> 'success'));
                $this->Session->write('User', $user);
                echo json_encode($user);
                exit;
                $this->Redirect(array('controller' => 'Dashboards', 'action' => 'index'));
                exit();
            }
            else
            {
                
                echo json_encode(array('alert'=> 'danger'));
                exit;
                
                $this -> Session -> setFlash(__('Incorrect username/password!'),'flash', array('alert'=> 'danger'));
                $this -> Redirect(array('action' => 'login_form'));
                exit();
            }
        }
    }
    function logout() {

        $this -> Session -> destroy();
        $this -> Session -> setFlash(__('You have logged out'),'flash', array('alert'=> 'success'));

        $this -> redirect(array('action' => 'login_form'));
        exit();
    }
    public function changePass()
    {
        if ($this->request->is(array('post', 'put')))
        {
            $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
            $userValue=$this->Session->read('User');
            $post = $this->User->findById($userValue['User']['id']);
            if($post['User']['password']==$passwordHasher->hash($this->request->data['User']['oldPassword']))
            {
                $this->User->id = $userValue['User']['id'];
                $this->User->unbindValidation('remove', array('name','username','address','phone','mobile','email','skype','twitter','facebook','linkedin','description','photo'), true);
                if ($this->User->save($this->request->data))
                {
                    $this->Session->setFlash(__('Password Changed Successfully'),'flash',array('alert'=>'success'));
                }
            }
            else
            {
                $this->Session->setFlash(__('Invalid Password.'),'flash',array('alert'=>'danger'));
            }
            $this->redirect(array('action' => 'changePass'));
        }
    }
    public function leveladd()
    {
	if($this->adminValue['User']['id']!=1){
	    return $this->redirect(array('action' => 'index'));
	}
        $this->loadModel('Ugroup');
        if ($this->request->is('post'))
        {
            $this->Ugroup->create();
            try
            {
                $this->request->data['Ugroup']['name']=$this->request->data['User']['name'];
		$this->Ugroup->unbindValidation('keep', array('name'), true);
                if ($this->Ugroup->save($this->request->data))
                {
                    $this->Session->setFlash(__('Level User has been saved'),'flash',array('alert'=>'success'));
                    return $this->redirect(array('action' => 'leveladd'));
                }
            }
            catch (Exception $e)
	    {
		$this->Session->setFlash(__($e->getMessage()),'flash',array('alert'=>'danger'));
		return $this->redirect(array('action' => 'index'));
	    }
        }
    }
    public function deletelevel()
    {
	if($this->adminValue['User']['id']!=1){
	    return $this->redirect(array('action' => 'index'));
	}
        $this->loadModel('Ugroup');
        if ($this->request->is('post'))
        {
            try
            {
		$del=false;
                foreach($this->data['Ugroup']['id'] as $key => $value)
                {
		    $this->Ugroup->deleteAll(array('Ugroup.id'=>$value,'Ugroup.type'=>NULL));
                }
		$this->Session->setFlash(__('Level user has been deleted'),'flash',array('alert'=>'success'));
            }
            catch (Exception $e)
	    {
		$this->Session->setFlash(__('Can not delete level user'),'flash',array('alert'=>'danger'));
		return $this->redirect(array('action' => 'assignrights'));
	    }
        }
        $this->redirect(array('action' => 'assignrights'));
    }
    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->User->findById($id);
        if(strlen($post['User']['photo'])>0)
        $std_img='user_thumb/'.$post['User']['photo'];
        else
        $std_img='User.png';
        $this->set('std_img', $std_img);
        $this->set('post',$post);
    }
    
    
       public function api()
    {
         $api_token = (isset($_REQUEST['api_token']))?$_REQUEST['api_token']:''; 
         $user_id = (isset($_REQUEST['user_id']))?$_REQUEST['user_id']:'';
         $login = $this->User->find('first', array(
                        'conditions' => array(
                            'User.api_token' => $api_token
                        )
                    ));
         if(empty($user_id)){
             return false;  
         }
         if(empty($api_token)){
            $res['code'] = '400';
            $res['msg'] = 'Token not found';
            echo json_encode($res);
            exit(); 
         }else
         if(empty($login)){
            $res['code'] = '400';
            $res['msg'] = 'Authenticate token failed';
            echo json_encode($res);
            exit(); 
         }
           
           
        $type = (isset($_REQUEST['type']))?$_REQUEST['type']:'';
        
        if(empty($type)){
            return false;
        }
        
        
//          $data = array();
//            $data['category_id'] = '5';
//            $data['user_id'] = '1';
//            $data['project_id'] = 'null';
//            $data['email'] = 'client@webagency.pk';
//            $data['password'] = 'etc';
//            $data['mobile'] = '03004444444';
//            $data['intrest_in'] = 'Plot';
//            $data['company_name'] = 'Business Man';
//            $data['phone'] = '03004444444';
//            $data['name'] = 'Mr One';
//            $data['address'] = 'Thankyou';
//            $data['district'] = 'Lahore';
//            $data['state'] = 'Lahore';
//            $data['pincode'] = 'pincode';
//            $data['status'] = 'Active';
//            $data['reg_status'] = 'Live';
// 
//            $data_e[] = $data;
//            $data_e[] = $data;
//            echo json_encode($data_e);
//            exit;
        
        if(empty($user_id)){
            
            $res['code'] = '400';
            $res['msg'] = 'Authenticate failed';
            echo json_encode($res);
            exit();
            
        }
        
        if($type == 'agency'){
         $this->loadModel('Agency');
     //   $this->load('Agency');
        $agency = new Agency();
        $agencies = $agency->find('all');
         $inner = array();
            foreach ($agencies as $value) {
               $inner[] =  $value['Agency'];
            }
     
            $re = $inner;
            echo json_encode($re);
        exit;
        $this->layout = null;
//        if (!$id)
//        {
//            $this->Session->setFlash(__('Invalid Post.'),'flash',array('alert'=>'danger'));
//            $this->redirect(array('action' => 'index'));
//        }
        // $post = $this->set('Agency' , $this->Agency->findById($id));
//        $post =  $this->Agency->findById($id);
//        $post =  $agency->findById($id);
//        $post =  $agency->find('list');
       // $post =  $agency->find('all');
        
        
      //  echo json_encode($post);
        exit;
        }else if($type == 'category_list'){
            $this->loadModel('Category');
            $post =  $this->Category->find('all');
            //$re['Category'] =array();
            $inner = array();
            foreach ($post as $value) {
               $inner[] =  $value['Category'];
            }
     
            $re['Category'] = $inner;
            echo json_encode($re);
            exit;
            echo json_encode($post);
            exit;  
        }else if($type == 'location_list'){
            $this->loadModel('Location');
            $post =  $this->Location->find('all');
            $inner = array();
            foreach ($post as $value) {
               $inner[] =  $value['Location'];
            }
            
            
            echo json_encode($inner);
            exit;  
        }else if($type == 'save_contact'){
            if(isset($_REQUEST['userid'])){
                 try
            {
                $this->loadModel('Contact');
//                $ff = $this->Contact->find('all');
//                echo "<pre>";
//                print_r($_POST);
//                echo"</pre>";
//                exit;
//                
               $contacts =  $_POST['contact'];
               $userid =  $_POST['userid'];
               $category =  $_POST['category'];
               $location =  $_POST['location'];
               $agency =  $_POST['agency'];
               $name =  $_POST['name'];
                //$data = json_decode($_REQUEST['json']);
                
//                echo "<pre>";
//                print_r($data);
//                echo"</pre>";
//                exit;
//                echo "<pre>";
//               // print_r( $this->Contact->findByid('105'));
//                echo"</pre>";
//                exit;
                $ress = array();
                foreach ($contacts as $value) {
                  
                    $this->Contact->create();
                    
                    $data = array();
                    $data['phone'] = $value;
                    $data['category_id'] = $category;
                    $data['project_id'] = $agency;
                    $data['location_id'] = $location;
                    $data['name'] = $name;
                    $data['reg_status'] = 'Live';
                    $data['district'] = $location;
                    $data['pincode'] = 1;
                    $data['address'] = 'nill';
                    $data['state'] = $location;
                    $data['email'] = 'a@a.com';
                    $data['user_id'] = $userid;
                   
                    
                    
                    $save['Contact'] =  $data;
//                    echo "<pre>";
//                  //  print_r($save);
//                    echo"</pre>";
//                    exit;
                    if ($this->Contact->save($save)){
                        $res['code'] = '200';
                        $res['msg'] = 'Saved Successfully';
                    }else{
                         $res['code'] = '400';
                        $res['msg'] = 'Please try again Contact not saved';
                    }
                    $ress[] = $res;
                    $resrt = $res;
                    
                }
                echo json_encode($resrt);
                exit;
                 }
            catch (Exception $e)
            {
                     echo "<pre>";
                     print_r($e->getMessage());
                     echo"</pre>";
                     exit;
            }
                echo "<pre>";
                print_r($data);
                echo"</pre>";
                exit;
            }
            $data = array();
            $data['category_id'] = '5';
            $data['user_id'] = '1';
            $data['project_id'] = 'null';
            $data['email'] = 'client@webagency.pk';
            $data['password'] = 'etc';
            $data['mobile'] = '03004444444';
            $data['intrest_in'] = 'Plot';
            $data['company_name'] = 'Business Man';
            $data['phone'] = '03004444444';
            $data['name'] = 'Mr One';
            $data['address'] = 'Thankyou';
            $data['district'] = 'Lahore';
            $data['state'] = 'Lahore';
            $data['pincode'] = 'pincode';
            $data['status'] = 'Active';
            $data['reg_status'] = 'Live';
 
            $data_e[] = 'json required in given format';
            $data_e[] = $data;
            echo json_encode($data_e);
            exit;
            
        }
        
        $this->render('index','ajax'); 
        exit;
        $this->Prg->commonProcess();
        $this->Paginator->settings = $this->paginate;
        $cond=array('ugroup_id'=>4);
        $this->Paginator->settings['conditions'] = array($this->Agency->parseCriteria($this->Prg->parsedParams()),$cond);
        $this->Paginator->settings['limit']=$this->pageLimit;
        $this->Paginator->settings['maxLimit']=$this->maxLimit;
        $this->set('Agency', $this->Paginator->paginate());
        $this->set('Category', $this->Paginator->paginate());
        if ($this->request->is('ajax')){
            $this->render('index','ajax'); // View, Layout
        }
    }
}
