mirror of
https://github.com/a2nt/silverstripe-elemental-basics.git
synced 2024-10-22 17:05:54 +02:00
commit
08a3e84cbb
@ -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
|
||||
|
@ -13,14 +13,12 @@
|
||||
}],
|
||||
"minimum-stability": "dev",
|
||||
"require": {
|
||||
"silverstripe/cms": "^4",
|
||||
"silverstripe/cms": "^5",
|
||||
"dnadesign/silverstripe-elemental": "*",
|
||||
"dnadesign/silverstripe-elemental-list": "*",
|
||||
"betterbrief/silverstripe-googlemapfield": "*",
|
||||
"symbiote/silverstripe-gridfieldextensions": "*",
|
||||
"dynamic/silverstripe-elemental-image": "*",
|
||||
"gorriecoe/silverstripe-linkfield": "*",
|
||||
"a2nt/silverstripe-mapboxfield": "dev-master",
|
||||
"lekoala/silverstripe-filepond": "*"
|
||||
},
|
||||
"autoload": {
|
||||
|
@ -33,9 +33,9 @@ class AccordionElement extends ElementList
|
||||
'KeepOpenned' => 'Boolean(0)',
|
||||
];
|
||||
|
||||
public function getType()
|
||||
public function getType(): string
|
||||
{
|
||||
return self::$singular_name;
|
||||
return _t(__CLASS__ . '.BlockType', self::$singular_name);
|
||||
}
|
||||
|
||||
public function Accordion()
|
||||
|
@ -28,9 +28,9 @@ class CustomSnippetElement extends ElementContent
|
||||
|
||||
private static $table_name = 'CustomSnippetElement';
|
||||
|
||||
public function getType()
|
||||
public function getType(): string
|
||||
{
|
||||
return self::$singular_name;
|
||||
return _t(__CLASS__ . '.BlockType', self::$singular_name);
|
||||
}
|
||||
|
||||
public function getCMSFields()
|
||||
|
@ -45,9 +45,9 @@ class InstagramElement extends BaseElement
|
||||
'DisplayGallery' => true,
|
||||
];
|
||||
|
||||
public function getType()
|
||||
public function getType(): string
|
||||
{
|
||||
return self::$singular_name;
|
||||
return _t(__CLASS__ . '.BlockType', self::$singular_name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,105 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: tony
|
||||
* Date: 6/30/18
|
||||
* Time: 11:54 PM
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -29,6 +29,6 @@ class SidebarElement extends BaseElement
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return self::$singular_name;
|
||||
return _t(__CLASS__ . '.BlockType', self::$singular_name);
|
||||
}
|
||||
}
|
||||
|
@ -25,9 +25,9 @@ class TeamMembersElement extends BaseElement
|
||||
|
||||
private static $description = 'Displays random Team Members';
|
||||
|
||||
public function getType()
|
||||
public function getType(): string
|
||||
{
|
||||
return self::$singular_name;
|
||||
return _t(__CLASS__ . '.BlockType', self::$singular_name);
|
||||
}
|
||||
|
||||
public function Members()
|
||||
|
@ -31,12 +31,15 @@ class BaseElementEx extends DataExtension
|
||||
parent::updateCMSFields($fields);
|
||||
|
||||
if ($obj->ID) {
|
||||
$fields->insertBefore(LiteralField::create(
|
||||
$fields->insertBefore(
|
||||
'Title',
|
||||
LiteralField::create(
|
||||
'AnchorName',
|
||||
'<div class="field"><div class="form__field-holder">'
|
||||
.'Element Anchor name: <b>#e'.$obj->ID.'</b>'
|
||||
.'</div></div>'
|
||||
), 'Title');
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$tab = $fields->findOrMakeTab('Root.Settings');
|
||||
|
@ -1,172 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: tony
|
||||
* Date: 9/12/18
|
||||
* Time: 2:55 AM
|
||||
*/
|
||||
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user