Source of file Geocodable.php
Size: 0,879 Bytes - Last Modified: 2021-12-24T05:16:09+00:00
/var/www/docs.ssmods.com/process/src/code/Geocodable.php
123456789101112131415161718192021222324252627282930313233343536373839 | <?php /** * Adds automatic geocoding to a {@link Addressable} object. Uses the Google * Maps API to save latitude and longitude on write. * * @package silverstripe-addressable */ class Geocodable extends DataExtension { private static $db = array( 'Lat' => 'Decimal(9,5)', 'Lng' => 'Decimal(9,5)' ); public function onBeforeWrite() { if (!$this->owner->isAddressChanged()) return; $address = $this->owner->getFullAddress(); $region = strtolower($this->owner->Country); if(!$point = GoogleGeocoding::address_to_point($address, $region)) { return; } $this->owner->Lat = $point['lat']; $this->owner->Lng = $point['lng']; } public function updateCMSFields(FieldList $fields) { $fields->removeByName('Lat'); $fields->removeByName('Lng'); } public function updateFrontEndFields(FieldList $fields) { $this->updateCMSFields($fields); } } |