diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a7b4d3d --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "app/thirdparty/geocoding-example"] + path = app/thirdparty/geocoding-example + url = https://github.com/mapbox/geocoding-example.git diff --git a/app/src/Controllers/MapElementController.php b/app/src/Controllers/MapElementController.php new file mode 100644 index 0000000..1b1eb0a --- /dev/null +++ b/app/src/Controllers/MapElementController.php @@ -0,0 +1,23 @@ +removeByName('Locations'); + + return $fields; + } + + public function MapAPIKey() + { + return MapboxField::getAccessToken(); + } +} diff --git a/app/src/Extensions/MapExtension.php b/app/src/Extensions/MapExtension.php new file mode 100644 index 0000000..7b0cf6f --- /dev/null +++ b/app/src/Extensions/MapExtension.php @@ -0,0 +1,61 @@ + 'Int', + ]; + + private static $many_many = [ + 'Locations' => MapPin::class, + ]; + + private static $owns = [ + 'Locations', + ]; + + public function updateCMSFields(FieldList $fields) + { + parent::updateCMSFields($fields); + + $fields->addFieldsToTab('Root.Map', [ + NumericField::create('MapZoom', 'Initial Map Zoom (enter a number from 0 to 24)'), + GridField::create( + 'Locations', + 'Locations', + $this->owner->Locations(), + GridFieldConfig_RelationEditor::create(100) + ) + ]); + } + + public function getGeoJSON(): string + { + $pins = []; + foreach ($this->owner->Locations() as $off) { + $pins[] = $off->getGeo(); + } + return json_encode([ + 'type' => 'MarkerCollection', + 'features' => $pins + ]); + } +} diff --git a/app/src/Models/MapPin.php b/app/src/Models/MapPin.php new file mode 100644 index 0000000..9a03f8a --- /dev/null +++ b/app/src/Models/MapPin.php @@ -0,0 +1,117 @@ + 'Varchar(255)', + ]; + + private static $has_one = [ + 'PhoneNumber' => Link::class, + 'Fax' => Link::class, + ]; + + private static $extensions = [ + Addressable::class, + MarkerExtension::class, + Versioned::class, + ]; + + private static $belongs_many_many = [ + 'MapElements' => MapElement::class, + ]; + + private static $default_sort = 'Title ASC, ID DESC'; + + public function getCMSFields() + { + $fields = parent::getCMSFields(); + + $fields->replaceField( + 'PhoneNumberID', + LinkField::create('PhoneNumberID', 'Phone Number') + ->setAllowedTypes(['Phone']) + ); + + $fields->replaceField( + 'FaxID', + LinkField::create('FaxID', 'FAX') + ->setAllowedTypes(['Phone']) + ); + + $fields->addFieldsToTab('Root.Map', [ + MapboxField::create('Map', 'Choose a location', 'Lat', 'Lng'), + ]); + + return $fields; + } + + public function onBeforeWrite() + { + parent::onBeforeWrite(); + + $lng = $this->getField('Lng'); + $lat = $this->getField('Lat'); + + // geocode + try { + // reverse geocoding get address + if (!$this->hasAddress() && $lng && $lat) { + require_once BASE_PATH . '/app/thirdparty/geocoding-example/php/Mapbox.php'; + $mapbox = new \Mapbox(MapboxField::getAccessToken()); + + // GET Address + $res = $mapbox->reverseGeocode($lng, $lat); + if ($res->success() && $res->getCount()) { + $res = $res->getData(); + if (count($res) && isset($res[0]['place_name'])) { + $details = explode(',', $res[0]['place_name']); + $fields = [ + 'Address', + 'City', + 'State', + 'Country', + ]; + + $n = count($fields); + for($i = 0; $i < $n; $i++) { + if(!isset($details[$i])){ + continue; + } + + $name = $fields[$i]; + $val = $details[$i]; + + // get postal code + if ($name === 'State') { + $this->setField('PostalCode',substr($val ,strrpos($val, ' ')+1)); + } + + $this->setField($name, $val); + } + } + } + } + }catch (\Exception $e) { + } + } +} diff --git a/app/templates/Site/Elements/MapElement.ss b/app/templates/Site/Elements/MapElement.ss new file mode 100644 index 0000000..d9cffdc --- /dev/null +++ b/app/templates/Site/Elements/MapElement.ss @@ -0,0 +1,21 @@ +<% if $ShowTitle %> +

$Title

+<% end_if %> + +<% if $Content %> +
$Content
+<% end_if %> + +<% include Objects\Map %> + +<% if $Locations %> +
+
+ <% loop $Locations %> +
+ $forTemplate +
+ <% end_loop %> +
+
+<% end_if %> diff --git a/app/templates/Site/Models/MapPin.ss b/app/templates/Site/Models/MapPin.ss new file mode 100644 index 0000000..1ebcceb --- /dev/null +++ b/app/templates/Site/Models/MapPin.ss @@ -0,0 +1,28 @@ +
+
{$Title}
+
{$Address}
+ <% if $Address2 %> +
{$Address2}
+ <% end_if %> + <% if $City || $PostalCode %> +
+ {$City}, {$State} {$PostalCode} +
+ <% end_if %> + <% if $Country %> +
{$Country}
+ <% end_if %> + <% if $PhoneNumber %> + <% with $PhoneNumber %> + T: $Title
+ <% end_with %> + <% end_if %> + <% if $Fax %> + <% with $Fax %> + F: $Title + <% end_with %> + <% end_if %> + +
diff --git a/app/thirdparty/geocoding-example b/app/thirdparty/geocoding-example new file mode 160000 index 0000000..57f2097 --- /dev/null +++ b/app/thirdparty/geocoding-example @@ -0,0 +1 @@ +Subproject commit 57f2097c063461403df25c52fd5302f3ccc96f9c