Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Outlet extends AppModel {
- var $name = 'Outlet';
- public $virtualFields = array(
- 'distance' => 0.00,
- );
- var $hasMany = 'Image';
- public $validate = array(
- 'postcode' => array(
- 'rule-1' => array(
- 'rule' => array('postal', null, 'uk'),
- 'last' => true
- ),
- 'rule-2' => array(
- 'rule' => 'notEmpty',
- 'last' => true
- )
- ),
- 'name' => array(
- 'rule' => 'notEmpty'
- ),
- 'road' => array(
- 'rule' => 'notEmpty'
- ),
- 'cuisine' => array(
- 'rule' => 'notEmpty'
- ),
- 'town' => array(
- 'rule' => 'notEmpty'
- ),
- 'telephone' => array(
- 'rule' => 'notEmpty'
- ),
- 'privateRooms' => array(
- 'rule' => 'notEmpty'
- ),
- 'salah' => array(
- 'rule' => 'notEmpty'
- )
- );
- public $actsAs = array('Search.Searchable');
- public $filterArgs = array(
- array('name' => 'name', 'type' => 'like'),
- array('name' => 'cuisine', 'type' => 'value'),
- array('name' => 'postcode', 'type' => 'query', 'method' => 'postcodeQuery'),
- );
- public function postcodeQuery($data){
- if (empty($data['postcode'])) {
- return array();
- }
- //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...
- $latLong = $this->__getLatlong($data['postcode']);
- if(isset($latLong['latitude'])){
- //$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) ) ) )';
- $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) ) ) )';
- }
- return $this->virtualFields['distance'];
- }
- private function __getLatLong($postcode) {
- if($postcode == false){
- return false; // postcode not entered by user
- } else {
- $postcode = str_replace(" ", "", $postcode);
- $postcodeInstance = ClassRegistry::init('Postcode');
- // check if we have already got the lat and long for the given postcode in our DB
- $postcode_record = $postcodeInstance->find('first', array('conditions' => array('Postcode.postcode' => $postcode)));
- // get the lat and long for the given postcode using the postcode api and then save the values to our DB for future use
- if($postcode_record == false){
- $url = "******************"; // Build the URL
- // specify a proxy, only for work machine
- $aContext = array(
- 'http' => array(
- 'proxy' => 'blahblah',
- 'request_fulluri' => true,
- )
- );
- $cxContext = stream_context_create($aContext);
- $file = file_get_contents($url, false, $cxContext);
- if ($file != false) { // Some error checking - if file_get_contents returned false then something went wrong - most likely post code error
- $pieces = explode(",", $file);
- $result['postcode'] = $pieces[0];
- $result['lat'] = $pieces[1];
- $result['lng'] = $pieces[2];
- $new_postcode = array(
- 'Postcode' => array
- (
- 'postcode' => str_replace(" ", "", $result['postcode']),
- 'latitude' => $result['lat'],
- 'longitude' => $result['lng']
- )
- );
- $postcodeInstance->save($new_postcode);
- $postcode_data = array();
- $postcode_data['postcode'] = $result['postcode'];
- $postcode_data['latitude'] = $result['lat'];
- $postcode_data['longitude'] = $result['lng'];
- } else {
- $result['error'] = 'Postcode not found by API';
- }
- } else {
- $postcode_data['postcode'] = $postcode_record['Postcode']['postcode'];
- $postcode_data['latitude'] = $postcode_record['Postcode']['latitude'];
- $postcode_data['longitude'] = $postcode_record['Postcode']['longitude'];
- }
- return $postcode_data;
- }
- }
- /*
- *
- * Function that retrieves latitude and longitude values using __getLatLong()
- * for a newly added outlet.
- *
- */
- function beforeSave(){
- if($this->exists()){
- // do nothing for existing records...
- }else{
- //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...
- $latLong = $this->__getLatlong($this->data['Outlet']['postcode']);
- $this->data['Outlet']['latitude'] = $latLong['latitude'];
- $this->data['Outlet']['longitude'] = $latLong['longitude'];
- }
- return true;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement