Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- App::uses('AppController', 'Controller');
- App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
- class RunnersController extends AppController {
- public $components = array('Session','Paginator', 'Notification');
- public $jobStatus = array('In Progress','');
- function rand_color() {
- $color = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
- if(!$this->Runner->hasAny(array('Runner.color'=>$color))){
- return $color;
- } else{
- $this->rand_color();
- }
- }
- function dashboard() {
- $this->set('titleForLayout', 'Union Courier-Dashboard');
- $breadcrumb = array(array('name'=>'Runners', 'link'=>''));
- $this->set('breadcrumb', $breadcrumb);
- $sql = "SELECT * FROM Runner ";
- if($this->request->is('post')){
- if(isset($this->data['search']) && $this->data['search'] != ''){
- $search = $this->data['search'];
- $conditions['Runner.runnername LIKE '] = "%$search%";
- $sql.= " WHERE Runner LIKE '%$search%' ";
- }
- }
- $runnerDetail = array();
- $this->Mssql->connect();
- if($this->Mssql->conn){
- $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
- if($stmt){
- while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
- $result['Runner']['password'] = $row['pass'];
- $result['Runner']['online'] = $row['Online'];
- $result['Runner']['runnername'] = $row['Runner'];
- $runnerDetail[] = $result;
- }
- } else {
- $this->Session->setFlash('Error on query.');
- }
- } else {
- $this->Session->setFlash('Connection Error.');
- }
- $this->set('runnerDetail',$runnerDetail);
- }
- function getRunnerLocations($runnerId){
- $this->layout = false;
- $this->autoRender = false;
- $this->loadModel('RunnerLocation');
- $conditions['RunnerLocation.runner_id'] = $runnerId;
- $conditions['DATE(RunnerLocation.created)'] = date('Y-m-d');
- $this->Paginator->settings = array(
- 'limit'=>100,
- 'fields'=>array('*'),
- 'order'=>'RunnerLocation.id DESC',
- 'conditions'=>$conditions
- );
- $things = $this->Paginator->paginate('RunnerLocation');
- $this->DATA['data']['RunnerLocation'] = Set::extract('/RunnerLocation/.', $things);
- echo json_encode($response);
- }
- function view($runner_id) {
- $this->loadModel('RunnerJob');
- $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' ";
- if($this->request->is('post')){
- if (isset($this->data['RunnerJob']['notify_runner'])
- && $this->data['RunnerJob']['notify_runner']==1) {
- $this->loadModel('RunnerMessage');
- $this->loadModel('GcmDevice');
- $runners = '('.$runner_id.')';
- $title = '('.$this->data['RunnerJob']['customer_code'].') New job assigned';
- $message = 'New job has been assigned to you, please check your distribution list.';
- $gcm_ids = $this->GcmDevice->find(
- 'list',array(
- 'fields'=>array('GcmDevice.id', 'GcmDevice.gcm_id'),
- 'conditions'=>array(
- 'GcmDevice.runner_id IN '.$runners
- )
- )
- );
- $gcm_ids = array_values($gcm_ids);
- $payLoad = array(
- 'success' => 1,
- 'push_title' => $title,
- 'push_desc' => $message
- );
- $data['RunnerMessage']['runner_id'] = $runner_id;
- $data['RunnerMessage']['title'] = $title;
- $data['RunnerMessage']['zip5_code'] = $this->data['RunnerJob']['customer_code'];
- $data['RunnerMessage']['message'] = $message;
- $this->RunnerMessage->save($data);
- $this->Notification->send($gcm_ids, $payLoad);
- }
- if($this->RunnerJob->save($this->data)){
- $this->Session->setFlash('Job assigned successfully.');
- } else {
- $this->Session->setFlash('Something went wrong.');
- }
- }
- $searchData = $this->request->query;
- if(isset($searchData['search_status']) && $searchData['search_status']!= ''){
- $sql.= " AND Tracking.ST_code = '$searchData[search_status]' ";
- } else {
- $searchData['search_status'] = '';
- }
- if(isset($searchData['search_issue_type']) && $searchData['search_issue_type']!= ''){
- $sql.= " AND Tracking.ISSUE = '$searchData[search_issue_type]' ";
- } else {
- $searchData['search_issue_type'] = '';
- }
- if(isset($searchData['search_customer_code']) && $searchData['search_customer_code']!= ''){
- $sql.= " AND Tracking.ZIP5 = '$searchData[search_customer_code]' ";
- } else {
- $searchData['search_customer_code'] = '';
- }
- if(isset($searchData['search_job_date']) && $searchData['search_job_date'] !=''){
- $searchData['search_job_date'] = date('Y/m/d', strtotime($searchData['search_job_date']));
- } else {
- $searchData['search_job_date'] = date('Y/m/d');
- }
- //$sql.= " AND Tracking.ST_DATE = '$searchData[search_job_date]' ";
- $this->set('searchData', $searchData);
- $this->set('titleForLayout', 'Union Courier-Runner Jobs');
- $breadcrumb = array(array('name'=>'Runners', 'link'=>'../dashboard'),array('name'=>'Runner Jobs', 'link'=>''));
- $this->set('breadcrumb', $breadcrumb);
- $this->set('runner_id',$runner_id);
- $this->Mssql->connect();
- $runnerJob = array();
- $status_codes = array();
- $issue_types = array();
- if($this->Mssql->conn){
- $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
- if($stmt){
- while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
- //debug($row);exit;
- $result['id'] = $row['Sfworx'];
- $result['customer_code'] = $row['ZIP5'];
- $result['customer_name'] = $row['NAME'];
- $result['issue_type'] = $row['ISSUE'];
- $result['address'] = $row['ADDRESS'];
- $result['phone'] = $row['Tel'];
- $result['barcode'] = "";
- $result['job_date'] = $row['ST_DATE'];
- $result['runner_id'] = $row['RUNNER'];
- $result['latitude'] = $row['latitude'];
- $result['longitude'] = $row['longitude'];
- $result['status_code'] = $row['ST_code'];
- $runnerJob[]['RunnerJob'] = $result;
- $status_codes[$row['ST_code']] = array($row['Result'] => $row['Status']);
- $issue_types[$row['ISSUE']] = $row['ISSUE_TXT'];
- }
- } else {
- $this->Session->setFlash('Error on query.');
- }
- } else {
- $this->Session->setFlash('Connection Error.');
- }
- $this->set('runnerJob',$runnerJob);
- $this->set('status_codes', $status_codes);
- $this->set('issue_types',$issue_types);
- }
- function view_job($job_id){
- $this->layout = false;
- $this->loadModel('RunnerJob');
- $this->loadModel('Status');
- $this->loadModel('IssueType');
- $this->Mssql->connect();
- $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' ";
- if($this->Mssql->conn){
- $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
- if($stmt){
- $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
- $result['id'] = $row['ZIP5']."-".$row['ISSUE'];
- $result['customer_code'] = $row['ZIP5'];
- $result['customer_name'] = $row['NAME'];
- $issue_type = $row['ISSUE'];
- $result['address'] = $row['ADDRESS'];
- $result['runnername'] = $row['RUNNER'];
- $result['phone'] = $row['Tel'];
- $result['job_date'] = $row['ST_DATE'];
- $result['runner_id'] = $row['RUNNER'];
- $result['latitude'] = $row['latitude'];
- $result['longitude'] = $row['longitude'];
- $result['comment'] = $row['COMMENT'];
- $result['custom_image'] = $row['Custom_image'];
- $data['RunnerJob'] = $result;
- $data['Status']['status_code'] = $row['ST_code'];
- $data['Status']['status'] = $row['Result'];
- $data['Status']['result'] = $row['Status'];
- $data['IssueType']['issue_name'] = $row['ISSUE_TXT'];
- $this->set('data', $data);
- } else {
- $this->set('error', 'Something went worng! try again');
- }
- } else {
- $this->set('error', 'Something went worng! try again');
- }
- }
- function delete($jobId,$runnerId) {
- $this->loadModel('RunnerJob');
- $this->layout = false;
- $this->autoRender = false;
- if ($this->RunnerJob->delete($jobId)) {
- } else {
- $this->Session->setFlash('We\'re facing problem while removing this job');
- }
- $this->redirect('view/'.$runnerId);
- }
- function sendNotification(){
- $this->loadModel('RunnerMessage');
- $this->loadModel('GcmDevice');
- $data = $this->data;
- $runners = array_diff($data['RunnerJob']['runners'],array('0'));
- foreach ($runners as $value) {
- $message_data['RunnerMessage']['runner_id'] = $value;
- $message_data['RunnerMessage']['title'] = $data['title'];
- $data['RunnerMessage']['zip5_code'] = '';
- $message_data['RunnerMessage']['message'] = $data['message'];
- $this->RunnerMessage->save($message_data);
- }
- $runners = "('".implode("','", $runners)."')";
- $gcm_ids = $this->GcmDevice->find(
- 'list',array(
- 'fields'=>array('GcmDevice.id', 'GcmDevice.gcm_id'),
- 'conditions'=>array(
- 'GcmDevice.runner_id IN '.$runners
- )
- )
- );
- $gcm_ids = array_values($gcm_ids);
- $payLoad = array(
- 'success' => 1,
- 'push_title' => $data['title'],
- 'push_desc' => $data['message']
- );
- $result = $this->Notification->send($gcm_ids, $payLoad);
- if($result){
- $this->Session->setFlash('Message posted successfully, to runners');
- } else{
- $this->Session->setFlash('Error occurred while posting message.');
- }
- $this->redirect('dashboard');
- }
- function maps(){
- $this->set('titleForLayout','Union Courier-Runners Map');
- $this->loadModel('RunnerLocation');
- $runners = $this->request->query('runners');
- $runners = str_replace(",", "','", $runners);
- $sql = "SELECT * FROM Runner WHERE RUNNER IN ('$runners')";
- $runnerJobs = array();
- $this->Mssql->connect();
- if($this->Mssql->conn){
- $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
- if($stmt){
- while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
- $result['Runner']['id'] = $row['Runner'];
- $result['Runner']['online'] = $row['Online'];
- $result['Runner']['runnername'] = $row['Runner'];
- $result['Runner']['color'] = $row['Color'];
- $runnerLocation = $this->RunnerLocation->find('all', array('order'=>'id DESC', 'conditions'=>array('RunnerLocation.runner_id'=>$row['Runner'])));
- $result['RunnerLocation'] = Set::extract('./RunnerLocation/.', $runnerLocation);
- $runnerJobs[] = $result;
- }
- } else {
- $this->Session->setFlash('Error on query.');
- }
- } else {
- $this->Session->setFlash('Connection Error.');
- }
- //debug($runnerJobs);exit;
- $this->set('runnerJobs',$runnerJobs);
- }
- function ajaxCordinates(){
- $this->loadModel('RunnerLocation');
- $runners = $this->request->query('runners');
- $runners = str_replace(",", "','", $runners);
- $sql = "SELECT * FROM Runner WHERE RUNNER IN ('$runners')";
- $runnerJobs = array();
- $this->Mssql->connect();
- if($this->Mssql->conn){
- $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
- if($stmt){
- while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
- $result['Runner']['online'] = $row['Online'];
- $result['Runner']['runnername'] = $row['Runner'];
- $result['Runner']['color'] = $row['Color'];
- $runnerLocation = $this->RunnerLocation->find('all', array('order'=>'id DESC', 'limit'=>'1','conditions'=>array('RunnerLocation.runner_id'=>$row['Runner'])));
- $result['RunnerLocation'] = Set::extract('./RunnerLocation/.', $runnerLocation);
- $runnerJobs[] = $result;
- }
- } else {
- $this->Session->setFlash('Error on query.');
- }
- } else {
- $this->Session->setFlash('Connection Error.');
- }
- debug($runnerJobs);exit;
- echo json_encode($runnerJobs);
- exit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement