Advertisement
Guest User

Untitled

a guest
Jan 9th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | None | 0 0
  1. <?php
  2.  
  3. class Outlet extends AppModel {
  4.  
  5.     var $name = 'Outlet';
  6.    
  7.     public $virtualFields = array(
  8.         'distance' => 0.00,
  9.     );
  10.  
  11.     var $hasMany = 'Image';
  12.  
  13.     public $validate = array(
  14.         'postcode' => array(
  15.             'rule-1' => array(
  16.                 'rule' => array('postal', null, 'uk'),
  17.                 'last' => true
  18.             ),
  19.             'rule-2' => array(
  20.                 'rule' => 'notEmpty',
  21.                 'last' => true
  22.             )
  23.         ),
  24.         'name' => array(
  25.             'rule' => 'notEmpty'
  26.         ),     
  27.         'road' => array(
  28.             'rule' => 'notEmpty'
  29.         ),
  30.         'cuisine' => array(
  31.             'rule' => 'notEmpty'
  32.         ),
  33.         'town' => array(
  34.             'rule' => 'notEmpty'
  35.         ),
  36.         'telephone' => array(
  37.             'rule' => 'notEmpty'
  38.         ),
  39.         'privateRooms' => array(
  40.             'rule' => 'notEmpty'
  41.         ),
  42.         'salah' => array(
  43.             'rule' => 'notEmpty'
  44.         )      
  45.     );
  46.    
  47.     public $actsAs = array('Search.Searchable');   
  48.    
  49.     public $filterArgs = array(
  50.         array('name' => 'name', 'type' => 'like'),
  51.         array('name' => 'cuisine', 'type' => 'value'),
  52.         array('name' => 'postcode', 'type' => 'query', 'method' => 'postcodeQuery'),
  53.     );
  54.    
  55.     public function postcodeQuery($data){
  56.         if (empty($data['postcode'])) {
  57.             return array();
  58.         }
  59.         //TODO: we have set cakephp's validator on the postcode, however we need to implement a check here to handle any unexpected values/errors from the api...
  60.         $latLong = $this->__getLatlong($data['postcode']);
  61.        
  62.         if(isset($latLong['latitude'])){
  63.             //$this->virtualFields['distance'] = '( 3959 * acos( cos( radians("'.$latLong['latitude'].'") ) * cos( radians( Outlet.latitude ) ) * cos( radians( Outlet.longitude ) - radians("'.$latLong['longitude'].'") ) + sin( radians("'.$latLong['latitude'].'") ) * sin( radians( Outlet.latitude) ) ) )';
  64.            
  65.             $this->virtualFields['distance'] = '(3963.0 * ACOS(   SIN("'.$latLong['latitude'].'"*PI()/180 ) * SIN( Outlet.latitude*PI()/180 ) + COS( "'.$latLong['latitude'].'"*PI()/180 ) * COS( Outlet.latitude*PI()/180 )  *  COS( (Outlet.longitude*PI()/180) - ("'.$latLong['longitude'].'"*PI()/180) )   )  )';
  66.    
  67.         }
  68.                                
  69.             return $this->virtualFields['distance'];
  70.     }
  71.    
  72.     private function __getLatLong($postcode) {
  73.         if($postcode == false){
  74.             return false; // postcode not entered by user
  75.         } else {
  76.             $postcode = str_replace(" ", "", $postcode);
  77.            
  78.             $postcodeInstance = ClassRegistry::init('Postcode');
  79.            
  80.             // check if we have already got the lat and long for the given postcode in our DB
  81.             $postcode_record = $postcodeInstance->find('first', array('conditions' => array('Postcode.postcode' => $postcode)));
  82.            
  83.             // get the lat and long for the given postcode using the postcode api and then save the values to our DB for future use
  84.             if($postcode_record == false){
  85.                 $url = "******************"; // Build the URL
  86.                            
  87.                 // specify a proxy, only for work machine
  88.                 $aContext = array(
  89.                     'http' => array(
  90.                         'proxy' => 'blahblah',
  91.                         'request_fulluri' => true,
  92.                     )
  93.                 );
  94.                 $cxContext = stream_context_create($aContext);
  95.                
  96.  
  97.                 $file = file_get_contents($url, false, $cxContext);
  98.                
  99.                 if ($file != false) { // Some error checking - if file_get_contents returned false then something went wrong - most likely post code error
  100.                     $pieces = explode(",", $file);
  101.                     $result['postcode'] = $pieces[0];
  102.                     $result['lat'] = $pieces[1];
  103.                     $result['lng'] = $pieces[2];
  104.                    
  105.                     $new_postcode = array(
  106.                         'Postcode' => array
  107.                             (
  108.                                 'postcode' => str_replace(" ", "", $result['postcode']),
  109.                                 'latitude' => $result['lat'],
  110.                                 'longitude' => $result['lng']
  111.                             )
  112.                     );
  113.                    
  114.                     $postcodeInstance->save($new_postcode);
  115.                    
  116.                     $postcode_data = array();
  117.                     $postcode_data['postcode'] = $result['postcode'];
  118.                     $postcode_data['latitude'] = $result['lat'];
  119.                     $postcode_data['longitude'] = $result['lng'];              
  120.                 } else {
  121.                     $result['error'] = 'Postcode not found by API';
  122.                 }
  123.                
  124.             } else {
  125.                 $postcode_data['postcode'] = $postcode_record['Postcode']['postcode'];
  126.                 $postcode_data['latitude'] = $postcode_record['Postcode']['latitude'];
  127.                 $postcode_data['longitude'] = $postcode_record['Postcode']['longitude'];   
  128.             }
  129.             return $postcode_data;
  130.         }      
  131.     }
  132.    
  133.     /*
  134.     *
  135.     * Function that retrieves latitude and longitude values using __getLatLong()
  136.     * for a newly added outlet.
  137.     *
  138.     */
  139.     function beforeSave(){
  140.         if($this->exists()){
  141.             // do nothing for existing records...
  142.         }else{
  143.             //TODO: we have set cakephp's validator on the postcode, however we need to implement a check here to handle any unexpected values/errors from the api...
  144.             $latLong = $this->__getLatlong($this->data['Outlet']['postcode']);
  145.             $this->data['Outlet']['latitude'] = $latLong['latitude'];
  146.             $this->data['Outlet']['longitude'] = $latLong['longitude'];
  147.         }
  148.         return true;
  149.     }
  150.    
  151. }
  152.  
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement