Example #1
0
/**
 * Display list of addresses near to location specified by ID
 *
 * @param       $id
 * @param array $opts
 * @param bool  $return
 *
 * @return void|array
 */
function root_locator_nearby($id, array $opts = [], $return = false)
{
    $API = new PerchAPI(1.0, 'root_locator');
    $Addresses = new RootLocator_Addresses($API);
    $Address = $Addresses->find((int) $id);
    $coordinates = false;
    if (is_object($Address)) {
        $coordinates = $Address->getCoordinates();
    }
    if ($coordinates) {
        if (isset($opts['address'])) {
            $opts['address'] = null;
        }
        $opts['coordinates'] = $coordinates;
        $opts['exclude'] = $id;
        if ($return) {
            root_locator_get_custom($opts, $return);
        }
        root_locator_get_custom($opts, $return);
    }
}
<?php

// Data
$Addresses = new RootLocator_Addresses($API);
$Tasks = new RootLocator_Tasks($API);
$result = false;
// Master template
$Template->set('locator/address.html', 'locator');
// Edit / Create
if (isset($_GET['id']) && $_GET['id'] != '') {
    $addressID = (int) $_GET['id'];
    $Address = $Addresses->find($addressID);
    $details = $Address->to_array();
    $heading1 = 'Addresses / Edit Address';
} else {
    $addressID = false;
    $Address = false;
    $details = [];
    $heading1 = 'Locations / Add Address';
}
// Forms
$Form->handle_empty_block_generation($Template);
$Form->require_field('addressTitle', 'This field is required');
$Form->require_field('addressStreet', 'This field is required');
$Form->require_field('addressPostcode', 'This field is required');
$Form->set_required_fields_from_template($Template, $details);
if ($Form->submitted()) {
    $postvars = ['addressTitle', 'addressBuilding', 'addressStreet', 'addressTown', 'addressRegion', 'addressCountry', 'addressPostcode', 'force'];
    $data = $Form->receive($postvars);
    // Force?
    $force = false;
<?php

$Addresses = new RootLocator_Addresses($API);
if (isset($_GET['id']) && $_GET['id'] != '') {
    $Address = $Addresses->find($_GET['id'], true);
} else {
    PerchUtil::redirect($API->app_path());
}
$Form->set_name('delete');
if ($Form->submitted()) {
    if (is_object($Address)) {
        $Address->delete();
        if ($Form->submitted_via_ajax) {
            echo $API->app_path() . '/';
            exit;
        } else {
            PerchUtil::redirect($API->app_path() . '/');
        }
    } else {
        $Alert->set('error', $Lang->get('Sorry, the address could not be deleted.'));
    }
}
$details = $Address->to_array();
 /**
  * Run through task queue and mass-geocode
  *
  * @param bool $delay
  *
  * @return int
  */
 public function processQueue($delay = false)
 {
     if (!$this->api) {
         PerchUtil::debug('Locator: Perch API must be set on Tasks class to process queue', 'error');
         return false;
     }
     $Addresses = new RootLocator_Addresses($this->api);
     $Geocoder = RootLocator_GeocoderFactory::createGeocoder();
     $Settings = $this->api->get('Settings');
     $Template = $this->api->get('Template');
     $Template->set('locator/address.html', 'locator');
     $batch = $Settings->get('root_locator_batch_size')->val();
     $tasks = $this->getBatch('address.geocode', $batch);
     $count = 0;
     if (!$tasks) {
         return $count;
     }
     foreach ($tasks as $Task) {
         $Address = $Addresses->find($Task->addressID());
         $result = $Geocoder->geocode($Address->fullAddress());
         if (!$Address) {
             PerchUtil::debug(sprintf('Locator: unable to process address `%s` - no record found', $Task->addressID()), 'error');
             $Task->delete();
             continue;
         }
         // Success, update the address and remove the task
         if (!$result->hasError()) {
             PerchUtil::debug('Locator: Geocoding success - clearing task', 'success');
             $coordinates = $result->getFirstCoordinates();
             $Address->update(['addressLatitude' => $coordinates['latitude'], 'addressLongitude' => $coordinates['longitude'], 'addressError' => null]);
             $Address->index($Template);
             $Task->delete();
             continue;
         }
         // Firstly, if our API limit has been reached then we need to try again tomorrow
         if ($result->hasError() && $result->getErrorKey() === 'quota_exceeded') {
             PerchUtil::debug('Locator: API Quota has been exceeded. Delaying queue.', 'notice');
             $this->delayQueue();
             break;
         }
         // If the task has not failed multiple times we can give it the benefit
         // of the doubt and retry it.
         if ($result->hasError() && !$Task->isLastAttempt()) {
             PerchUtil::debug('Locator: Geocoding failed - task reset for a new attempt', 'notice');
             $Task->requeue();
             continue;
         }
         // Ok, we tried everything and now we really do need
         // to tell the user what's gone wrong.
         if ($result->hasError() && $Task->isLastAttempt()) {
             PerchUtil::debug('Locator: Geocoding failed after multiple attempts. Clearing task and logging error.', 'error');
             $Address->update(['addressLatitude' => null, 'addressLongitude' => null, 'addressError' => $result->getErrorKey()]);
             $Address->index($Template);
             $Task->delete();
             // Admit defeat...
         }
         $count++;
         if ($delay) {
             sleep((int) $delay);
         }
     }
     return $count;
 }