Advertisement
androlizer

Untitled

Apr 8th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.06 KB | None | 0 0
  1. <?php
  2. App::uses('AppController', 'Controller');
  3. App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
  4.  
  5. class RunnersController extends AppController {
  6.  
  7.   public $components = array('Session','Paginator', 'Notification');
  8.   public $jobStatus = array('In Progress','');
  9.  
  10.   function rand_color() {
  11.     $color = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
  12.     if(!$this->Runner->hasAny(array('Runner.color'=>$color))){
  13.       return $color;
  14.     } else{
  15.       $this->rand_color();
  16.     }
  17.   }
  18.  
  19.   function dashboard() {
  20.     $this->set('titleForLayout', 'Union Courier-Dashboard');
  21.     $breadcrumb = array(array('name'=>'Runners', 'link'=>''));
  22.     $this->set('breadcrumb', $breadcrumb);
  23.    
  24.  
  25.     $sql = "SELECT * FROM Runner ";
  26.  
  27.     if($this->request->is('post')){
  28.       if(isset($this->data['search']) && $this->data['search'] != ''){
  29.         $search = $this->data['search'];
  30.         $conditions['Runner.runnername LIKE '] = "%$search%";
  31.         $sql.= " WHERE Runner LIKE '%$search%' ";
  32.       }
  33.     }
  34.  
  35.     $runnerDetail = array();
  36.     $this->Mssql->connect();
  37.    
  38.     if($this->Mssql->conn){
  39.       $stmt = sqlsrv_query( $this->Mssql->conn, $sql);  
  40.  
  41.       if($stmt){
  42.         while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  43.           $result['Runner']['password'] = $row['pass'];
  44.           $result['Runner']['online'] = $row['Online'];
  45.           $result['Runner']['runnername'] = $row['Runner'];
  46.           $runnerDetail[] = $result;
  47.         }
  48.       } else {
  49.         $this->Session->setFlash('Error on query.');
  50.       }
  51.     }  else {
  52.       $this->Session->setFlash('Connection Error.');
  53.     }
  54.  
  55.     $this->set('runnerDetail',$runnerDetail);
  56.   }
  57.  
  58.  
  59.   function getRunnerLocations($runnerId){
  60.     $this->layout = false;
  61.     $this->autoRender = false;
  62.     $this->loadModel('RunnerLocation');
  63.    
  64.     $conditions['RunnerLocation.runner_id'] = $runnerId;
  65.     $conditions['DATE(RunnerLocation.created)'] = date('Y-m-d');
  66.  
  67.     $this->Paginator->settings = array(
  68.       'limit'=>100,
  69.       'fields'=>array('*'),
  70.       'order'=>'RunnerLocation.id DESC',
  71.       'conditions'=>$conditions
  72.     );
  73.  
  74.     $things = $this->Paginator->paginate('RunnerLocation');
  75.     $this->DATA['data']['RunnerLocation'] = Set::extract('/RunnerLocation/.', $things);
  76.     echo json_encode($response);
  77.   }
  78.  
  79.   function view($runner_id) {
  80.     $this->loadModel('RunnerJob');
  81.    
  82.     $sql = "SELECT TOP 10 * FROM Tracking as Tracking, Main as Main, Phones as Phones, Issue as Issue, Status as Status WHERE Status.Status_code = Tracking.ST_code AND Issue.ISSUE = Tracking.ISSUE AND Tracking.ZIP5 = Main.ZIP5 AND Tracking.ZIP5 = Phones.Zip5 AND Tracking.RUNNER = '$runner_id' ";
  83.  
  84.    
  85.    
  86.     if($this->request->is('post')){
  87.       if (isset($this->data['RunnerJob']['notify_runner'])
  88.         && $this->data['RunnerJob']['notify_runner']==1) {
  89.        
  90.         $this->loadModel('RunnerMessage');
  91.         $this->loadModel('GcmDevice');
  92.         $runners = '('.$runner_id.')';
  93.        
  94.         $title = '('.$this->data['RunnerJob']['customer_code'].') New job assigned';
  95.         $message = 'New job has been assigned to you, please check your distribution list.';
  96.         $gcm_ids = $this->GcmDevice->find(
  97.           'list',array(
  98.             'fields'=>array('GcmDevice.id', 'GcmDevice.gcm_id'),
  99.             'conditions'=>array(
  100.               'GcmDevice.runner_id IN '.$runners
  101.             )
  102.           )
  103.         );
  104.         $gcm_ids = array_values($gcm_ids);
  105.  
  106.         $payLoad =  array(
  107.           'success' => 1,
  108.           'push_title' => $title,
  109.           'push_desc' => $message
  110.         );
  111.  
  112.         $data['RunnerMessage']['runner_id'] = $runner_id;
  113.         $data['RunnerMessage']['title'] = $title;
  114.         $data['RunnerMessage']['zip5_code'] = $this->data['RunnerJob']['customer_code'];
  115.         $data['RunnerMessage']['message'] = $message;
  116.         $this->RunnerMessage->save($data);
  117.         $this->Notification->send($gcm_ids, $payLoad);
  118.       }
  119.      
  120.         if($this->RunnerJob->save($this->data)){
  121.           $this->Session->setFlash('Job assigned successfully.');
  122.         } else {
  123.           $this->Session->setFlash('Something went wrong.');
  124.         }
  125.       }
  126.  
  127.       $searchData = $this->request->query;
  128.  
  129.       if(isset($searchData['search_status']) && $searchData['search_status']!= ''){
  130.        
  131.         $sql.= " AND Tracking.ST_code = '$searchData[search_status]' ";
  132.       } else {
  133.         $searchData['search_status'] = '';
  134.       }
  135.  
  136.       if(isset($searchData['search_issue_type']) && $searchData['search_issue_type']!= ''){
  137.        
  138.         $sql.= " AND Tracking.ISSUE = '$searchData[search_issue_type]' ";
  139.       } else {
  140.         $searchData['search_issue_type'] = '';
  141.       }
  142.  
  143.       if(isset($searchData['search_customer_code']) && $searchData['search_customer_code']!= ''){
  144.        
  145.         $sql.= " AND Tracking.ZIP5 = '$searchData[search_customer_code]' ";
  146.       } else {
  147.         $searchData['search_customer_code'] = '';
  148.       }
  149.  
  150.       if(isset($searchData['search_job_date']) && $searchData['search_job_date'] !=''){
  151.         $searchData['search_job_date'] = date('Y/m/d', strtotime($searchData['search_job_date']));
  152.       } else {
  153.         $searchData['search_job_date'] = date('Y/m/d');
  154.       }
  155.  
  156.       //$sql.= " AND Tracking.ST_DATE = '$searchData[search_job_date]' ";
  157.  
  158.     $this->set('searchData', $searchData);
  159.     $this->set('titleForLayout', 'Union Courier-Runner Jobs');
  160.     $breadcrumb = array(array('name'=>'Runners', 'link'=>'../dashboard'),array('name'=>'Runner Jobs', 'link'=>''));
  161.     $this->set('breadcrumb', $breadcrumb);
  162.  
  163.     $this->set('runner_id',$runner_id);
  164.  
  165.  
  166.     $this->Mssql->connect();
  167.     $runnerJob = array();
  168.     $status_codes = array();
  169.     $issue_types = array();
  170.     if($this->Mssql->conn){
  171.       $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
  172.       if($stmt){
  173.         while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  174.           //debug($row);exit;
  175.           $result['id'] = $row['Sfworx'];
  176.           $result['customer_code'] = $row['ZIP5'];
  177.           $result['customer_name'] = $row['NAME'];
  178.           $result['issue_type'] = $row['ISSUE'];
  179.           $result['address'] = $row['ADDRESS'];
  180.           $result['phone'] = $row['Tel'];
  181.           $result['barcode'] = "";
  182.           $result['job_date'] = $row['ST_DATE'];
  183.           $result['runner_id'] = $row['RUNNER'];
  184.           $result['latitude'] = $row['latitude'];
  185.           $result['longitude'] = $row['longitude'];
  186.           $result['status_code'] = $row['ST_code'];
  187.  
  188.           $runnerJob[]['RunnerJob'] = $result;
  189.           $status_codes[$row['ST_code']] = array($row['Result'] => $row['Status']);
  190.           $issue_types[$row['ISSUE']] = $row['ISSUE_TXT'];
  191.         }  
  192.       } else {
  193.         $this->Session->setFlash('Error on query.');
  194.       }
  195.     } else {
  196.       $this->Session->setFlash('Connection Error.');
  197.     }
  198.  
  199.     $this->set('runnerJob',$runnerJob);
  200.  
  201.     $this->set('status_codes', $status_codes);
  202.    
  203.     $this->set('issue_types',$issue_types);
  204.    
  205.   }
  206.  
  207.   function view_job($job_id){
  208.     $this->layout = false;
  209.     $this->loadModel('RunnerJob');
  210.     $this->loadModel('Status');
  211.     $this->loadModel('IssueType');
  212.    
  213.     $this->Mssql->connect();
  214.    
  215.     $sql = "SELECT * FROM Tracking as Tracking, Main as Main, Phones as Phones, Issue as Issue, Status as Status WHERE Status.Status_code = Tracking.ST_code AND Issue.ISSUE = Tracking.ISSUE AND Tracking.ZIP5 = Main.ZIP5 AND Tracking.ZIP5 = Phones.Zip5 AND Tracking.Sfworx = '$job_id'   ";
  216.  
  217.     if($this->Mssql->conn){
  218.       $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
  219.       if($stmt){
  220.         $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
  221.        
  222.           $result['id'] = $row['ZIP5']."-".$row['ISSUE'];
  223.           $result['customer_code'] = $row['ZIP5'];
  224.           $result['customer_name'] = $row['NAME'];
  225.           $issue_type = $row['ISSUE'];
  226.           $result['address'] = $row['ADDRESS'];
  227.           $result['runnername'] = $row['RUNNER'];
  228.           $result['phone'] = $row['Tel'];
  229.           $result['job_date'] = $row['ST_DATE'];
  230.           $result['runner_id'] = $row['RUNNER'];
  231.           $result['latitude'] = $row['latitude'];
  232.           $result['longitude'] = $row['longitude'];
  233.           $result['comment'] = $row['COMMENT'];
  234.           $result['custom_image'] = $row['Custom_image'];
  235.        
  236.           $data['RunnerJob'] = $result;
  237.    
  238.           $data['Status']['status_code'] = $row['ST_code'];
  239.           $data['Status']['status'] = $row['Result'];
  240.           $data['Status']['result'] = $row['Status'];
  241.           $data['IssueType']['issue_name'] = $row['ISSUE_TXT'];
  242.          
  243.           $this->set('data', $data);    
  244.       } else {
  245.         $this->set('error', 'Something went worng! try again');
  246.       }
  247.     } else {
  248.       $this->set('error', 'Something went worng! try again');
  249.     }
  250.  
  251.    
  252.   }
  253.  
  254.   function delete($jobId,$runnerId) {
  255.     $this->loadModel('RunnerJob');
  256.     $this->layout = false;
  257.     $this->autoRender = false;
  258.     if ($this->RunnerJob->delete($jobId)) {
  259.  
  260.     } else {
  261.       $this->Session->setFlash('We\'re facing problem while removing this job');
  262.     }
  263.     $this->redirect('view/'.$runnerId);
  264.   }
  265.  
  266.   function sendNotification(){
  267.       $this->loadModel('RunnerMessage');
  268.       $this->loadModel('GcmDevice');
  269.       $data = $this->data;
  270.  
  271.       $runners = array_diff($data['RunnerJob']['runners'],array('0'));
  272.       foreach ($runners as $value) {
  273.         $message_data['RunnerMessage']['runner_id'] = $value;
  274.         $message_data['RunnerMessage']['title'] = $data['title'];
  275.         $data['RunnerMessage']['zip5_code'] = '';
  276.         $message_data['RunnerMessage']['message'] = $data['message'];
  277.         $this->RunnerMessage->save($message_data);
  278.       }
  279.  
  280.       $runners = "('".implode("','", $runners)."')";
  281.       $gcm_ids = $this->GcmDevice->find(
  282.         'list',array(
  283.           'fields'=>array('GcmDevice.id', 'GcmDevice.gcm_id'),
  284.           'conditions'=>array(
  285.             'GcmDevice.runner_id IN '.$runners
  286.           )
  287.         )
  288.       );
  289.       $gcm_ids = array_values($gcm_ids);
  290.  
  291.       $payLoad =  array(
  292.         'success' => 1,
  293.         'push_title' => $data['title'],
  294.         'push_desc' => $data['message']
  295.       );
  296.      
  297.       $result = $this->Notification->send($gcm_ids, $payLoad);
  298.       if($result){
  299.         $this->Session->setFlash('Message posted successfully, to runners');
  300.       } else{
  301.         $this->Session->setFlash('Error occurred while posting message.');
  302.       }
  303.       $this->redirect('dashboard');
  304.     }
  305.  
  306.     function maps(){
  307.       $this->set('titleForLayout','Union Courier-Runners Map');
  308.       $this->loadModel('RunnerLocation');
  309.       $runners = $this->request->query('runners');
  310.       $runners = str_replace(",", "','", $runners);
  311.       $sql = "SELECT * FROM Runner WHERE RUNNER IN ('$runners')";
  312.       $runnerJobs = array();
  313.       $this->Mssql->connect();
  314.      
  315.       if($this->Mssql->conn){
  316.         $stmt = sqlsrv_query( $this->Mssql->conn, $sql);  
  317.  
  318.         if($stmt){
  319.           while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  320.             $result['Runner']['id'] = $row['Runner'];
  321.             $result['Runner']['online'] = $row['Online'];
  322.             $result['Runner']['runnername'] = $row['Runner'];
  323.             $result['Runner']['color'] = $row['Color'];
  324.             $runnerLocation = $this->RunnerLocation->find('all', array('order'=>'id DESC', 'conditions'=>array('RunnerLocation.runner_id'=>$row['Runner'])));
  325.             $result['RunnerLocation'] = Set::extract('./RunnerLocation/.', $runnerLocation);
  326.             $runnerJobs[] = $result;
  327.           }
  328.         } else {
  329.           $this->Session->setFlash('Error on query.');
  330.         }
  331.       }  else {
  332.         $this->Session->setFlash('Connection Error.');
  333.       }
  334.  
  335.  
  336.       //debug($runnerJobs);exit;
  337.       $this->set('runnerJobs',$runnerJobs);
  338.     }
  339.  
  340.     function ajaxCordinates(){
  341.       $this->loadModel('RunnerLocation');
  342.       $runners = $this->request->query('runners');
  343.      
  344.       $runners = str_replace(",", "','", $runners);
  345.       $sql = "SELECT * FROM Runner WHERE RUNNER IN ('$runners')";
  346.       $runnerJobs = array();
  347.       $this->Mssql->connect();
  348.      
  349.       if($this->Mssql->conn){
  350.         $stmt = sqlsrv_query( $this->Mssql->conn, $sql);  
  351.  
  352.         if($stmt){
  353.           while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  354.            
  355.             $result['Runner']['online'] = $row['Online'];
  356.             $result['Runner']['runnername'] = $row['Runner'];
  357.             $result['Runner']['color'] = $row['Color'];
  358.             $runnerLocation = $this->RunnerLocation->find('all', array('order'=>'id DESC', 'limit'=>'1','conditions'=>array('RunnerLocation.runner_id'=>$row['Runner'])));
  359.             $result['RunnerLocation'] = Set::extract('./RunnerLocation/.', $runnerLocation);
  360.             $runnerJobs[] = $result;
  361.           }
  362.         } else {
  363.           $this->Session->setFlash('Error on query.');
  364.         }
  365.       }  else {
  366.         $this->Session->setFlash('Connection Error.');
  367.       }
  368.  
  369.       debug($runnerJobs);exit;
  370.       echo json_encode($runnerJobs);
  371.       exit;
  372.     }
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement