2019-09-07 01:50:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace A2nt\SilverStripeMapboxField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use Symbiote\Addressable\Geocodable;
|
2020-02-13 23:38:16 +01:00
|
|
|
use Symbiote\Addressable\Addressable;
|
2019-09-07 01:50:52 +02:00
|
|
|
|
|
|
|
class MarkerExtension extends Geocodable
|
|
|
|
{
|
2019-09-07 02:57:59 +02:00
|
|
|
private static $icon = '<i class="fas fa-map-marker-alt"></i>';
|
|
|
|
private $curr_icon = null;
|
|
|
|
|
2020-02-13 23:38:16 +01:00
|
|
|
private static $db = [
|
|
|
|
'DirectionsByAddress' => 'Boolean(0)',
|
|
|
|
];
|
|
|
|
|
2019-09-07 01:50:52 +02:00
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
parent::updateCMSFields($fields);
|
|
|
|
|
|
|
|
$record = $this->getOwner();
|
|
|
|
|
2020-02-13 23:38:16 +01:00
|
|
|
$fields->removeByName(['DirectionsByAddress', 'LatLngOverride', 'Lng', 'Lat']);
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->owner->hasExtension(Addressable::class)) {
|
|
|
|
$fields->addFieldsToTab('Root.Map', [
|
|
|
|
CheckboxField::create('DirectionsByAddress', 'Directions by address')
|
|
|
|
->setDescription('Check this box to link directions by address')
|
|
|
|
]);
|
|
|
|
}
|
2019-09-07 02:57:59 +02:00
|
|
|
|
2019-09-07 01:50:52 +02:00
|
|
|
$fields->addFieldsToTab('Root.Map', [
|
|
|
|
CheckboxField::create('LatLngOverride', 'Override Latitude and Longitude?')
|
2020-02-13 23:38:16 +01:00
|
|
|
->setDescription('Check this box and save to be able to set the latitude and longitude manually.'),
|
2019-09-07 01:50:52 +02:00
|
|
|
MapboxField::create('Map', 'Choose a location', 'Lat', 'Lng'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDirectionsURL()
|
|
|
|
{
|
|
|
|
$obj = $this->owner;
|
|
|
|
return 'https://www.google.com/maps/dir/Current+Location/'
|
2020-02-13 23:38:16 +01:00
|
|
|
.(
|
|
|
|
$obj->getField('DirectionsByAddress')
|
|
|
|
? urlencode(
|
|
|
|
$obj->getField('Address').', '.$obj->getField('Suburb')
|
|
|
|
.', '.$obj->getField('State').' '.$obj->getField('Postcode')
|
|
|
|
.', '.$obj->getField('Country')
|
|
|
|
)
|
|
|
|
: $obj->getField('Lat').',' .$obj->getField('Lng')
|
|
|
|
);
|
2019-09-07 01:50:52 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 02:57:59 +02:00
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
$obj = $this->owner;
|
|
|
|
$class = get_class($obj);
|
|
|
|
return $this->curr_icon ?: $class::config()->get('icon');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setIcon($icon)
|
|
|
|
{
|
|
|
|
$this->curr_icon = $icon;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGeo()
|
|
|
|
{
|
|
|
|
$obj = $this->owner;
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $obj->ID,
|
|
|
|
'type' => 'Feature',
|
|
|
|
'icon' => $obj->getIcon(),
|
|
|
|
'properties' => [
|
|
|
|
'content' => $obj->forTemplate()->RAW(),
|
|
|
|
],
|
|
|
|
'geometry' => [
|
|
|
|
'type' => 'Point',
|
|
|
|
'coordinates' => [
|
|
|
|
$obj->getField('Lng'),
|
|
|
|
$obj->getField('Lat')
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-09-07 01:50:52 +02:00
|
|
|
public function forTemplate()
|
|
|
|
{
|
|
|
|
$obj = $this->owner;
|
|
|
|
$class =get_class($obj);
|
|
|
|
|
|
|
|
return $obj->renderWith($class);
|
|
|
|
}
|
|
|
|
}
|