Advertisement
androlizer

Untitled

Apr 8th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.45 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. class ApiController extends AppController {
  6.  
  7.     public $components = array('RequestHandler', 'Paginator');
  8.     public $DATA = array();
  9.     public $apiKey =  "098f6bcd4621d373cade4e832627b4f6";
  10.     public $requestedData;
  11.     public $limit = 50;
  12.  
  13.     public function beforeFilter() {
  14.  
  15.         $this->Auth->allow();
  16.  
  17.         $this->requestedData = $this->request->query;
  18.  
  19.         $this->requestedData = $this->request->data;
  20.  
  21.         // if(isset($this->requestedData['api_key'])) {
  22.  
  23.         //     if($this->requestedData['api_key'] != $this->apiKey) {
  24.  
  25.         //       $this->DATA['error'] = "Authentication failed! Please contact to administrator.";
  26.  
  27.         //       echo json_encode($this->DATA);
  28.  
  29.         //       exit;
  30.  
  31.         //     }
  32.  
  33.         // } else {
  34.  
  35.         //      $this->DATA['error'] = "Authentication failed! Please contact to administrator.";
  36.  
  37.         //      echo json_encode($this->DATA);
  38.  
  39.         //      exit;
  40.  
  41.         // }
  42.     }
  43.  
  44.     function check_login() {
  45.         if(!isset($this->requestedData['username'])){
  46.             $this->DATA["error"] = "username is not given";
  47.         } else if(!isset($this->requestedData['password'])){
  48.             $this->DATA["error"] = "password is not given";
  49.         } else {
  50.             $username = $this->requestedData['username'];
  51.             $password = $this->requestedData['password'];
  52.  
  53.             $this->Mssql->connect();
  54.             if($this->Mssql->conn){
  55.                 $sql = "SELECT * FROM Runner WHERE Runner = '$username' AND pass = '$password'";
  56.                 $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
  57.                 if($stmt){
  58.                     $params = array();
  59.                     $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
  60.                     $stmt = sqlsrv_query( $this->Mssql->conn, $sql , $params, $options );
  61.                     $row_count = sqlsrv_num_rows($stmt);
  62.  
  63.                     if($row_count !=1) {
  64.                       $this->DATA["error"] = "Wrong username or password!";
  65.                     } else {
  66.                         $result = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
  67.                         $this->DATA["success"] = "Logged in successfully!";
  68.                         $this->DATA['data']['runner_id'] = $result['Runner'];
  69.  
  70.                         sqlsrv_begin_transaction ($this->Mssql->conn);
  71.                         $sql = "UPDATE Runner SET online = '1' WHERE Runner = '$result[Runner]'";
  72.                         $stmt = sqlsrv_prepare( $this->Mssql->conn, $sql);
  73.                         if($stmt) {
  74.                             if(sqlsrv_execute( $stmt )) {
  75.                                
  76.                                 sqlsrv_commit ($this->Mssql->conn );
  77.                                 $this->DATA['success'] = "Runner status has been saved successfully.";
  78.                             } else {
  79.                                 $this->DATA["error"] = "error on query!";
  80.                             }    
  81.                         } else {
  82.                             $this->DATA["error"] = "error on query!";
  83.                         }
  84.  
  85.                         $this->loadModel('Status');
  86.                         $things = $this->Status->find('all');
  87.                         $this->DATA['Status'] = Set::extract('/Status/.', $things);
  88.  
  89.                         $this->loadModel('IssueType');
  90.                         $things = $this->IssueType->find('all');
  91.                         $this->DATA['IssueType'] = Set::extract('/IssueType/.', $things);
  92.                    }
  93.                 } else {
  94.                     $this->DATA["error"] = "error on query!";
  95.                 }
  96.             } else {
  97.                 $this->DATA["error"] = "Connection error!";
  98.             }
  99.         }
  100.  
  101.         $this->set(array(
  102.            'result' => $this->DATA,
  103.            '_serialize' => 'result'
  104.         ));
  105.     }
  106.  
  107.  
  108.     function saveGcmId(){
  109.         $this->loadModel('GcmDevice');
  110.         $gcmId = $this->requestedData['gcm_id'];
  111.         $runnerId = $this->requestedData['runner_id'];
  112.  
  113.         $data['GcmDevice']['gcm_id'] = $gcmId;
  114.         $data['GcmDevice']['runner_id'] = $runnerId;
  115.  
  116.         $deviceData = $this->GcmDevice->findByRunnerId($runnerId);
  117.  
  118.         if($deviceData){
  119.             $this->GcmDevice->id = $deviceData['GcmDevice']['id'];
  120.         }
  121.  
  122.         if($this->GcmDevice->save($data)){
  123.             $this->DATA['success'] = "GCM ID has been saved successfully.";
  124.         } else {
  125.             $this->DATA['error'] = "We're facing problem, while saving GCM ID.";
  126.         }
  127.  
  128.         $this->set(
  129.             array(
  130.                 'result' => $this->DATA,
  131.                 '_serialize' => 'result'
  132.             )
  133.         );
  134.     }
  135.  
  136.     function saveRunnerLocation(){
  137.         $this->loadModel('RunnerLocation');
  138.  
  139.         $data['RunnerLocation']['runner_id'] = $this->requestedData['runner_id'];
  140.         $data['RunnerLocation']['latitude'] = $this->requestedData['latitude'];
  141.         $data['RunnerLocation']['longitude'] = $this->requestedData['longitude'];
  142.  
  143.         if($this->RunnerLocation->save($data)){
  144.             $this->DATA['success'] = "Runner location has been saved successfully.";
  145.         } else {
  146.             $this->DATA['error'] = "We're facing problem, while saving runner location.";
  147.         }
  148.  
  149.         $this->set(
  150.             array(
  151.                 'result' => $this->DATA,
  152.                 '_serialize' => 'result'
  153.             )
  154.         );
  155.     }
  156.  
  157.     function saveRunnerOffline(){
  158.         $runnerId = $this->requestedData['runner_id'];
  159.         $this->Mssql->connect();
  160.         if($this->Mssql->conn){
  161.             sqlsrv_begin_transaction ($this->Mssql->conn);
  162.             $sql = "UPDATE Runner SET online = '0' WHERE Runner = '$runnerId'";
  163.             $stmt = sqlsrv_prepare( $this->Mssql->conn, $sql);
  164.             if($stmt) {
  165.                 if(sqlsrv_execute( $stmt )) {
  166.                     echo "Updated";
  167.                     sqlsrv_commit ($this->Mssql->conn );
  168.                     $this->DATA['success'] = "Runner status has been saved successfully.";
  169.                 } else {
  170.                     $this->DATA["error"] = "error on query!";
  171.                 }    
  172.             } else {
  173.                 $this->DATA["error"] = "error on query!";
  174.             }
  175.            
  176.         } else {
  177.             $this->DATA["error"] = "Connection error!";
  178.         }
  179.        
  180.  
  181.         $this->set(
  182.             array(
  183.                 'result' => $this->DATA,
  184.                 '_serialize' => 'result'
  185.             )
  186.         );
  187.     }
  188.  
  189.     function getRunnerMessages(){
  190.         $this->loadModel('RunnerMessage');
  191.         $conditions['RunnerMessage.runner_id'] = $this->requestedData['runner_id'];
  192.         $conditions['DATE(RunnerMessage.created)'] = date('Y-m-d');
  193.  
  194.         $this->Paginator->settings = array(
  195.           'limit'=>100,
  196.           'fields'=>array('*'),
  197.           'order'=>'RunnerMessage.id DESC',
  198.           'conditions'=>$conditions
  199.         );
  200.  
  201.         $things = $this->Paginator->paginate('RunnerMessage');
  202.         $this->DATA['data']['messages'] = Set::extract('/RunnerMessage/.', $things);
  203.         $this->set(array(
  204.            'result' => $this->DATA,
  205.            '_serialize' => 'result'
  206.         ));
  207.     }
  208.  
  209.     function getDistributions(){
  210.         $this->Mssql->connect();
  211.         $runner_id = $this->requestedData['runner_id'];
  212.        
  213.         $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' ";
  214.  
  215.         if(isset($this->requestedData['search_status'])){
  216.             $search_status = $this->requestedData['search_status'];
  217.             $sql.= " AND Tracking.ST_code = '$search_status' ";
  218.         }
  219.  
  220.         if(isset($this->requestedData['search_customer_code'])){
  221.             $search_customer_code = '%'.$this->requestedData['search_customer_code'].'%';
  222.             $sql.= " AND Tracking.ZIP5 LIKE '$search_customer_code' ";
  223.         }
  224.  
  225.         if(isset($this->requestedData['search_customer_name'])){
  226.             $search_customer_name = '%'.$this->requestedData['search_customer_name'].'%';
  227.             $sql.= " AND Main.NAME LIKE '$search_customer_name' ";
  228.         }
  229.  
  230.         if(isset($this->requestedData['search_job_date'])){
  231.             $search_job_date = date('Y/m/d', strtotime($this->requestedData['search_job_date']));
  232.         } else {
  233.             $search_job_date = date('Y/m/d');
  234.         }
  235.  
  236.         //$sql.= " AND Tracking.ST_DATE = '$search_job_date' ";
  237.  
  238.         $results = array();
  239.         if($this->Mssql->conn){
  240.             $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
  241.  
  242.             if($stmt){
  243.                
  244.                 while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  245.                   $result['RunnerJob']['id'] = $row['Sfworx'];
  246.                   $result['RunnerJob']['customer_code'] = $row['ZIP5'];
  247.                   $result['RunnerJob']['customer_name'] = $row['NAME'];
  248.                   $result['RunnerJob']['issue_type'] = $row['ISSUE'];
  249.                   $result['RunnerJob']['address'] = $row['ADDRESS'];
  250.                   $result['RunnerJob']['phone'] = $row['Tel'];
  251.                   $result['RunnerJob']['barcode'] = "";
  252.                   $result['RunnerJob']['job_date'] = $row['ST_DATE'];
  253.                   $result['RunnerJob']['runner_id'] = $row['RUNNER'];
  254.                   $result['RunnerJob']['latitude'] = $row['latitude'];
  255.                   $result['RunnerJob']['longitude'] = $row['longitude'];
  256.                   $result['RunnerJob']['status_code'] = $row['ST_code'];
  257.                   $result['RunnerJob']['signature'] = $row['COMMENT'];
  258.                   $result['RunnerJob']['custom_image'] = $row['Custom_image'];
  259.  
  260.                   $result['IssueType']['id'] = $row['ISSUE'];
  261.                   $result['IssueType']['issue_name'] = $row['ISSUE_TXT'];
  262.                  
  263.                   $result['Status']['id'] = $row['ISSUE_TXT'];
  264.                   $result['Status']['status'] = $row['Result'];
  265.                   $result['Status']['status_code'] = $row['ST_code'];
  266.                   $result['Status']['result'] = $row['Status'];
  267.                   $result['Status']['result_code'] = $row['Result_code'];
  268.  
  269.                   $results[] = $result;
  270.                 }  
  271.             } else {
  272.                 $this->DATA["error"] = "error on query!";
  273.             }
  274.         } else {
  275.             $this->DATA["error"] = "Connection error!";
  276.         }
  277.  
  278.         $this->DATA['data']['jobs'] = $results;
  279.         $this->set(array(
  280.            'result' => $this->DATA,
  281.            '_serialize' => 'result'
  282.         ));
  283.     }
  284.  
  285.     function getReports(){
  286.         $this->Mssql->connect();
  287.         $this->loadModel('RunnerJob');
  288.         $this->loadModel('Status');
  289.         $runner_id = $this->requestedData['runner_id'];
  290.        
  291.         $date = date('Y/m/d');
  292.        
  293.         if(isset($this->requestedData['issue_type']) && $this->requestedData['issue_type']!=''){
  294.             $conditions['RunnerJob.issue_type'] = $this->requestedData['issue_type'];
  295.             $issue = " AND ISSUE =  '".$this->requestedData['issue_type']."' ";
  296.         } else {
  297.             $issue = " AND ISSUE != '' ";
  298.         }
  299.  
  300.         if($this->Mssql->conn){
  301.             $sql = "SELECT COUNT(*) as count FROM Tracking_Android WHERE Runner = '$runner_id' AND ST_DATE = '$date' $issue AND Result LIKE '%UnDelivered%'";
  302.             $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
  303.             if($stmt){
  304.                 $result = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
  305.                 $this->DATA['data']['undelivered'] = $result['count'];
  306.             } else {
  307.                 $this->DATA["error"] = "error on query!";
  308.             }
  309.  
  310.             $sql = "SELECT COUNT(*) as count FROM Tracking_Android WHERE Runner = '$runner_id' AND ST_DATE = '$date' $issue AND Result LIKE '%Delivered%'";
  311.             $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
  312.             if($stmt){
  313.                 $result = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
  314.                 $this->DATA['data']['delivered'] = $result['count'];
  315.                
  316.             } else {
  317.                 $this->DATA["error"] = "error on query!";
  318.             }
  319.  
  320.             $sql = "SELECT COUNT(*) as count FROM Tracking_Android WHERE Runner = '$runner_id' AND ST_DATE = '$date' $issue AND Result LIKE '%In Progress%'";
  321.             $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
  322.             if($stmt){
  323.                 $result = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);
  324.                 $this->DATA['data']['in_progress'] = $result['count'];
  325.                
  326.             } else {
  327.                 $this->DATA["error"] = "error on query!";
  328.             }
  329.         } else {
  330.             $this->DATA["error"] = "Connection error!";
  331.         }
  332.  
  333.         $this->set(array(
  334.            'result' => $this->DATA,
  335.            '_serialize' => 'result'
  336.         ));
  337.     }
  338.  
  339.     function saveStatusUsingCron(){
  340.         $this->Mssql->connect();
  341.         if($this->Mssql->conn){
  342.             $sql = "SELECT * FROM Status";
  343.             $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
  344.             if($stmt){
  345.                 $this->loadModel('Status');
  346.                 $this->Status->query('TRUNCATE TABLE uc_statuses;');
  347.                 $result = array();
  348.                 while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  349.                     $result['Status']['status'] = $row['Status'];
  350.                     $result['Status']['status_code'] = $row['Status_code'];
  351.                     $result['Status']['result'] = $row['Result'];
  352.                     $result['Status']['result_code'] = $row['Result_code'];
  353.  
  354.                     $result[] = $result;
  355.                 }
  356.                
  357.                 if($this->Status->saveMany($result)){
  358.                     $this->DATA['success'] = "Staus table updated using CRON JOB.";
  359.                 } else {
  360.                     $this->DATA['error'] = "Staus table failed updation using CRON JOB.";
  361.                 }
  362.             } else {
  363.                 $this->DATA["error"] = "error on query!";
  364.             }
  365.         } else {
  366.             $this->DATA["error"] = "Connection error!";
  367.         }
  368.        
  369.         $this->set(
  370.             array(
  371.                 'result' => $this->DATA,
  372.                 '_serialize' => 'result'
  373.             )
  374.         );
  375.     }
  376.  
  377.     function saveIssueUsingCron(){
  378.         $this->Mssql->connect();
  379.         if($this->Mssql->conn){
  380.             $sql = "SELECT * FROM Issue";
  381.             $stmt = sqlsrv_query( $this->Mssql->conn, $sql);
  382.             if($stmt){
  383.                 $this->loadModel('IssueType');
  384.                 $this->IssueType->query('TRUNCATE TABLE uc_issue_types;');
  385.                 $result = array();
  386.                 while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  387.                     $result['IssueType']['issue_name'] = trim($row['ISSUE_TXT']);
  388.                     $result[] = $result;
  389.                 }
  390.                
  391.                 if($this->IssueType->saveMany($result)){
  392.                     $this->DATA['success'] = "Issue Type table updated using CRON JOB.";
  393.                 } else {
  394.                     $this->DATA['error'] = "Issue Type table failed updation using CRON JOB.";
  395.                 }
  396.             } else {
  397.                 $this->DATA["error"] = "error on query!";
  398.             }
  399.         } else {
  400.             $this->DATA["error"] = "Connection error!";
  401.         }
  402.        
  403.         $this->set(
  404.             array(
  405.                 'result' => $this->DATA,
  406.                 '_serialize' => 'result'
  407.             )
  408.         );
  409.     }
  410.  
  411.     function saveDistributionReport(){
  412.         $this->loadModel('RunnerJob');
  413.         $this->Mssql->connect();
  414.  
  415.         if(!isset($this->requestedData['job_id']) || $this->requestedData['job_id']==''){
  416.             $this->DATA["error"] = "Job ID is not provided or empty.";
  417.         } else if(!isset($this->requestedData['latitude']) || $this->requestedData['latitude']==''){
  418.             $this->DATA["error"] = "Latitude is not provided or empty.";
  419.         } else if(!isset($this->requestedData['longitude']) || $this->requestedData['longitude']==''){
  420.             $this->DATA["error"] = "Longitude is not provided or empty.";
  421.         } else if(!isset($this->requestedData['status_code']) || $this->requestedData['status_code']==''){
  422.             $this->DATA["error"] = "Status code is not provided or empty.";
  423.         } else {
  424.  
  425.             $job_id          = $this->requestedData['job_id'];
  426.             $latitude        = $this->requestedData['latitude'];
  427.             $longitude       = $this->requestedData['longitude'];
  428.             $status_code     = $this->requestedData['status_code'];
  429.             $custom_image_name = '';
  430.             $signature_name = '';
  431.            
  432.             if(isset($_FILES['s_image'])){
  433.                 $data = $_FILES['s_image'];
  434.                 $allowed = array('image/jpeg', 'image/JPEG', 'image/JPG', 'image/jpg','image/png', 'image/*');
  435.  
  436.                 if(in_array($data['type'], $allowed)){
  437.                     $path = "files/signatures/job-".$job_id;
  438.                     $name = $path."/".$data['name'];
  439.  
  440.                     if(!is_dir($path)){
  441.                         mkdir($path);  
  442.                     }
  443.  
  444.                     if(file_exists($name)){
  445.                         unlink($name);
  446.                     }
  447.  
  448.                     App::uses('Folder', 'Utility');
  449.                     App::uses('File', 'Utility');
  450.  
  451.                     if(move_uploaded_file($data['tmp_name'], $name)){
  452.                         $signature_name = $name;
  453.                         $this->DATA['success'] = "Image has been saved successfully, Please proceed.";    
  454.                     } else {
  455.                         $this->DATA['error'] = "We're facing problem, while saving data.";
  456.                     }
  457.                 } else {
  458.                     $this->DATA['error'] = "Not allowed filetype, Please retry.";
  459.                 }
  460.             }          
  461.  
  462.             if(isset($_FILES['custom_image'])){
  463.                 $custom_image_data = $_FILES['custom_image'];
  464.                 $allowed = array('image/jpeg', 'image/JPEG', 'image/JPG', 'image/jpg','image/png', 'image/*');
  465.  
  466.                 if(in_array($custom_image_data['type'], $allowed)){
  467.                     $custom_image_path = "files/custom_images/job-".$job_id;
  468.                     $custom_image_name = $custom_image_path."/".$custom_image_data['name'];
  469.  
  470.                     if(!is_dir($custom_image_path)){
  471.                         mkdir($custom_image_path);  
  472.                     }
  473.  
  474.                     if(file_exists($custom_image_name)){
  475.                         unlink($custom_image_name);
  476.                     }
  477.  
  478.                     App::uses('Folder', 'Utility');
  479.                     App::uses('File', 'Utility');
  480.  
  481.                     if(move_uploaded_file($custom_image_data['tmp_name'], $custom_image_name)){
  482.                        
  483.                         $this->DATA['success'] = "Custom image has been saved successfully, Please proceed.";    
  484.                     } else {
  485.                         $this->DATA['error'] = "We're facing problem, while saving data.";
  486.                     }
  487.                 } else {
  488.                     $this->DATA['error'] = "Not allowed filetype, Please retry.";
  489.                 }
  490.             }
  491.  
  492.             if($this->Mssql->conn){
  493.                 sqlsrv_begin_transaction ($this->Mssql->conn);
  494.                 $sql = "UPDATE Tracking SET latitude = '$latitude', longitude = '$longitude', ST_code = '$status_code', COMMENT = '$signature_name', Custom_image = '$custom_image_name' WHERE Sfworx = '$job_id' ";
  495.                 $stmt = sqlsrv_prepare( $this->Mssql->conn, $sql);
  496.                 if($stmt){
  497.                     if(sqlsrv_execute( $stmt )) {        
  498.                         sqlsrv_commit ($this->Mssql->conn );
  499.                         $this->DATA['success'] = "Job data has been saved successfully.";
  500.                     } else {
  501.                        $this->DATA["error"] = "error on query!";
  502.                     }  
  503.                 } else {
  504.                     $this->DATA["error"] = "error on query!";
  505.                 }
  506.             } else {
  507.                 $this->DATA["error"] = "Connection error!";
  508.             }
  509.         }
  510.  
  511.         $this->set(
  512.             array(
  513.                 'result' => $this->DATA,
  514.                 '_serialize' => 'result'
  515.             )
  516.         );
  517.     }
  518. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement