Compare commits

...

6 Commits

Author SHA1 Message Date
Tony Air 79369d46e6 IMPR: Map Elements 2024-04-25 18:11:07 +02:00
Tony Air ba5e94ac7f
Merge branch 'main' into v5 2024-04-25 17:11:19 +02:00
Tony Air d08aa69f14
Update README.md 2024-04-25 17:10:51 +02:00
Tony Air d02c86b2ea FIX: composer.json 2024-04-22 14:11:25 +02:00
Tony Air 54cd20f678 UPD: composer 2024-04-22 13:55:09 +02:00
Tony Air 466a4cbe6e UPD: composer 2024-04-22 13:54:24 +02:00
4 changed files with 272 additions and 2 deletions

View File

@ -1,4 +1,7 @@
# silverstripe-elemental-basics
[![Silverstripe Version](https://img.shields.io/badge/Silverstripe-5.1-005ae1.svg?labelColor=white&logoColor=ffffff&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDEuMDkxIDU4LjU1NSIgZmlsbD0iIzAwNWFlMSIgeG1sbnM6dj0iaHR0cHM6Ly92ZWN0YS5pby9uYW5vIj48cGF0aCBkPSJNNTAuMDE1IDUuODU4bC0yMS4yODMgMTQuOWE2LjUgNi41IDAgMCAwIDcuNDQ4IDEwLjY1NGwyMS4yODMtMTQuOWM4LjgxMy02LjE3IDIwLjk2LTQuMDI4IDI3LjEzIDQuNzg2czQuMDI4IDIwLjk2LTQuNzg1IDI3LjEzbC02LjY5MSA0LjY3NmM1LjU0MiA5LjQxOCAxOC4wNzggNS40NTUgMjMuNzczLTQuNjU0QTMyLjQ3IDMyLjQ3IDAgMCAwIDUwLjAxNSA1Ljg2MnptMS4wNTggNDYuODI3bDIxLjI4NC0xNC45YTYuNSA2LjUgMCAxIDAtNy40NDktMTAuNjUzTDQzLjYyMyA0Mi4wMjhjLTguODEzIDYuMTctMjAuOTU5IDQuMDI5LTI3LjEyOS00Ljc4NHMtNC4wMjktMjAuOTU5IDQuNzg0LTI3LjEyOWw2LjY5MS00LjY3NkMyMi40My0zLjk3NiA5Ljg5NC0uMDEzIDQuMTk4IDEwLjA5NmEzMi40NyAzMi40NyAwIDAgMCA0Ni44NzUgNDIuNTkyeiIvPjwvc3ZnPg==)](https://packagist.org/packages/goldfinch/google-maps)
Some useful elemental blocks
## Usefull UNIX console utilities
@ -20,4 +23,4 @@ https://github.com/ogham/exa
### git diff tool with bat code hightlighting (git show)
https://github.com/dandavison/delta
https://github.com/dandavison/delta

View File

@ -19,7 +19,9 @@
"symbiote/silverstripe-gridfieldextensions": "*",
"dynamic/silverstripe-elemental-image": "*",
"gorriecoe/silverstripe-linkfield": "*",
"lekoala/silverstripe-filepond": "*"
"lekoala/silverstripe-filepond": "*",
"symbiote/silverstripe-addressable": "*",
"a2nt/silverstripe-mapboxfield": "*"
},
"autoload": {
"psr-4": {

View File

@ -0,0 +1,99 @@
<?php
namespace A2nt\ElementalBasics\Elements;
use A2nt\SilverStripeMapboxField\MapboxField;
use BetterBrief\GoogleMapField;
use Colymba\BulkManager\BulkManager;
use DNADesign\Elemental\Models\ElementContent;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\NumericField;
use A2nt\ElementalBasics\Controllers\MapElementController;
use A2nt\ElementalBasics\Extensions\MapExtension;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
/**
* Class \A2nt\ElementalBasics\Elements\MapElement
*
* @property int $MapZoom
* @method \SilverStripe\ORM\ManyManyList|\A2nt\ElementalBasics\Models\MapPin[] Locations()
* @mixin \A2nt\ElementalBasics\Extensions\MapExtension
*/
class MapElement extends ElementContent
{
private static $icon = 'font-icon-globe-1';
private static $singular_name = 'Map Element';
private static $plural_name = 'Map Element';
private static $description = 'Displays dynamic map';
private static $table_name = 'MapElement';
private static $controller_class = MapElementController::class;
private static $map_type = 'mapbox';
private static $extensions = [
MapExtension::class,
];
public function getType(): string
{
return self::$singular_name;
}
public function getCMSFields(): FieldList
{
$fields = parent::getCMSFields();
$fields->remove('HTML');
$tab = $fields->findOrMakeTab('Root.MapPins');
$tab->setTitle('Description');
$tab->push(HTMLEditorField::create('HTML', 'Content'));
$fields->addFieldsToTab('Root.Main', [
NumericField::create('MapZoom', 'Initial Map Zoom (enter a number from 0 to 24)'),
GridField::create(
'Locations',
'Locations',
$this->Locations(),
$cfg = GridFieldConfig_RelationEditor::create(100)
)
]);
$cfg->getComponentByType(GridFieldDataColumns::class)->setFieldFormatting([
'ShowAtMap' => static function ($v, $obj) {
return $v ? 'YES' : 'NO';
}
]);
$cfg->addComponent(new BulkManager());
return $fields;
}
public static function MapAPIKey(): string
{
$type = self::config()->get('map_type');
switch ($type) {
case 'mapbox':
$key = MapboxField::getAccessToken();
break;
case 'google-maps':
$cfg = Config::inst()->get(GoogleMapField::class, 'default_options');
$key = $cfg['api_key'];
break;
default:
$key = '';
break;
}
return $key;
}
}

166
src/Models/MapPin.php Normal file
View File

@ -0,0 +1,166 @@
<?php
namespace A2nt\ElementalBasics\Models;
use A2nt\SilverStripeMapboxField\MapboxField;
use A2nt\SilverStripeMapboxField\MarkerExtension;
use gorriecoe\LinkField\LinkField;
use gorriecoe\Link\Models\Link;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
use A2nt\ElementalBasics\Elements\MapElement;
use Symbiote\Addressable\Addressable;
/**
* Class \A2nt\ElementalBasics\Models\MapPin
*
* @property int $Version
* @property boolean $DirectionsByAddress
* @property boolean $LatLngOverride
* @property float $Lat
* @property float $Lng
* @property string $Address
* @property string $Suburb
* @property string $State
* @property string $Postcode
* @property string $Country
* @property string $Title
* @property boolean $ShowAtMap
* @property int $PhoneNumberID
* @property int $FaxID
* @method \Sheadawson\Linkable\Models\Link PhoneNumber()
* @method \Sheadawson\Linkable\Models\Link Fax()
* @method \SilverStripe\ORM\ManyManyList|\A2nt\ElementalBasics\Elements\MapElement[] MapElements()
* @mixin \Symbiote\Addressable\Addressable
* @mixin \A2nt\SilverStripeMapboxField\MarkerExtension
* @mixin \SilverStripe\Versioned\Versioned
*/
class MapPin extends DataObject
{
private static $table_name = 'MapPin';
private static $db = [
'Title' => 'Varchar(255)',
'ShowAtMap' => 'Boolean(1)',
];
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';
private static $summary_fields = [
'Title',
'Address',
'ShowAtMap',
];
private static $defaults = [
'ShowAtMap' => '1',
'Suburb' => 'Syracuse',
'State' => 'NY',
'Postcode' => '13210',
'Country' => 'US',
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeByName('MapElements');
$fields->replaceField(
'PhoneNumberID',
LinkField::create('PhoneNumber', 'Phone Number', $this, [
'types' => ['Phone']
])
);
$fields->replaceField(
'Fax',
LinkField::create('Fax', 'FAX', $this, [
'types' => ['Phone']
])
);
$fields->removeByName(['Map', 'LatLngOverride', 'Lng','Lat']);
$fields->addFieldsToTab('Root.Main', [
CheckboxField::create('ShowAtMap', 'Show at the map?'),
CheckboxField::create('LatLngOverride', 'Override Latitude and Longitude?')
->setDescription('Check this box and save to be able to edit the latitude and longitude manually.'),
MapboxField::create('Map', 'Choose a location', 'Lat', 'Lng'),
]);
$this->extend('updateMapPinFields', $fields);
return $fields;
}
public function onBeforeWrite()
{
parent::onBeforeWrite();
$lng = $this->getField('Lng');
$lat = $this->getField('Lat');
if (!$this->getField('Country')) {
$this->setField('Country', 'us');
}
// 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) {
}
}
}