Merge pull request #1 from a2nt/v5

V5
This commit is contained in:
Tony Air 2024-04-25 17:11:27 +02:00 committed by GitHub
commit 08a3e84cbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 39 additions and 312 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

@ -13,15 +13,13 @@
}],
"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": "*"
"lekoala/silverstripe-filepond": "*"
},
"autoload": {
"psr-4": {

View File

@ -28,14 +28,14 @@ class AccordionElement extends ElementList
private static $table_name = 'AccordionElement';
private static $db = [
'OpenFirst' => 'Boolean(0)',
'KeepOpenned' => 'Boolean(0)',
];
private static $db = [
'OpenFirst' => 'Boolean(0)',
'KeepOpenned' => 'Boolean(0)',
];
public function getType()
public function getType(): string
{
return self::$singular_name;
return _t(__CLASS__ . '.BlockType', self::$singular_name);
}
public function Accordion()
@ -43,15 +43,15 @@ class AccordionElement extends ElementList
return $this->Elements()->renderWith(static::class.'_AccordionArea');
}
public function getCMSFields()
{
$fields = parent::getCMSFields();
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Main', [
CheckboxField::create('OpenFirst', 'Open first accordion element on page load'),
CheckboxField::create('KeepOpenned', 'Keep elements open'),
]);
$fields->addFieldsToTab('Root.Main', [
CheckboxField::create('OpenFirst', 'Open first accordion element on page load'),
CheckboxField::create('KeepOpenned', 'Keep elements open'),
]);
return $fields;
}
return $fields;
}
}

View File

@ -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()

View File

@ -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);
}
/**
@ -89,7 +89,7 @@ class InstagramElement extends BaseElement
foreach ($attributes as $name => $value) {
if ($value === true) {
$value = $name;
} else if (is_scalar($value)) {
} elseif (is_scalar($value)) {
$value = (string) $value;
} else {
$value = json_encode($value);

View File

@ -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;
}
}

View File

@ -29,6 +29,6 @@ class SidebarElement extends BaseElement
public function getType(): string
{
return self::$singular_name;
return _t(__CLASS__ . '.BlockType', self::$singular_name);
}
}

View File

@ -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()

View File

@ -31,12 +31,15 @@ class BaseElementEx extends DataExtension
parent::updateCMSFields($fields);
if ($obj->ID) {
$fields->insertBefore(LiteralField::create(
'AnchorName',
'<div class="field"><div class="form__field-holder">'
.'Element Anchor name: <b>#e'.$obj->ID.'</b>'
.'</div></div>'
), 'Title');
$fields->insertBefore(
'Title',
LiteralField::create(
'AnchorName',
'<div class="field"><div class="form__field-holder">'
.'Element Anchor name: <b>#e'.$obj->ID.'</b>'
.'</div></div>'
)
);
}
$tab = $fields->findOrMakeTab('Root.Settings');
@ -74,7 +77,7 @@ class BaseElementEx extends DataExtension
public function updateCMSEditLink(&$link): void
{
if(!method_exists($this->owner, 'inlineEditable')) {
if (!method_exists($this->owner, 'inlineEditable')) {
return;
}

View File

@ -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) {
}
}
}