silverstripe-webpack/app/src/Extensions/ElementRows.php

180 lines
4.5 KiB
PHP
Raw Normal View History

2018-06-23 12:27:06 +02:00
<?php
/**
* Created by PhpStorm.
* User: tony
* Date: 6/23/18
* Time: 1:23 PM
*/
namespace Site\Extensions;
use DNADesign\ElementalList\Model\ElementList;
use Dynamic\Elements\Image\Elements\ElementImage;
2018-06-23 12:27:06 +02:00
use SilverStripe\Forms\DropdownField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
class ElementRows extends DataExtension
{
private static $container_max_width = 1140;
2018-06-23 12:27:06 +02:00
private static $column_class = 'col-md-';
2018-06-23 12:27:06 +02:00
private static $container_styles = [
'container' => 'Fixed container',
'container-fluid' => 'Fluid Container',
2018-06-23 12:27:06 +02:00
];
private static $db = [
'ContainerType' => 'Varchar(254)',
'Size' => 'Enum("1,2,3,4,5,6,7,8,9,10,11,12","6")',
2018-06-23 12:27:06 +02:00
];
public function updateCMSFields(FieldList $fields)
{
parent::updateCMSFields($fields);
2018-06-23 12:27:06 +02:00
$tab = $fields->findOrMakeTab('Root.Main');
// container type
if ($this->isRoot()) {
$tab->push(DropdownField::create(
'ContainerType',
_t(__CLASS__.'.CONTAINERTYPE', 'Container Type'),
self::$container_styles
));
} else {
$fields->removeByName('ContainerType');
2018-06-23 12:27:06 +02:00
}
// column size
if ($this->isColumn()) {
2018-06-23 12:27:06 +02:00
$sizes = $this->owner->dbObject('Size');
$defaultSize = $sizes->getDefaultValue();
$sizeDropdown = DropdownField::create(
'Size',
_t(
__CLASS__.'.SIZE',
'Column Size'
),
array_combine(
array_values($sizes->enumValues()),
[
'8.3%',
'16.6%',
'25%',
'33%',
'41.6%',
'50%',
'58.3%',
'66.4%',
'74.7%',
'83%',
'91.3%',
'100%',
]
)
2018-06-23 12:27:06 +02:00
);
$tab->push($sizeDropdown);
// set default size
if (!$this->owner->getField('Size')) {
$sibling = $this->getSibling();
2018-06-23 12:27:06 +02:00
$sizeDropdown->setValue($sibling ? $sibling->getField('Size') : $defaultSize);
}
} else {
2018-06-23 12:27:06 +02:00
$fields->removeByName('Size');
}
}
public function isList()
{
return is_a($this->owner, ElementList::class);
}
public function isRow()
{
if (!$this->isList()) {
2018-06-23 12:27:06 +02:00
return false;
}
return true;
}
public function isColumn()
{
if (!$this->isRoot()) {
2018-06-23 12:27:06 +02:00
return true;
}
return false;
}
public function isRoot()
{
$parent = $this->owner->Parent()->getOwnerPage();
if (is_a($parent, 'Page')) {
2018-06-23 12:27:06 +02:00
return true;
}
return false;
}
public function getSibling($any = true, $filter = [], $exclude = [])
{
$class = $any ? $this->owner->baseClass() : $this->owner->ClassName;
return $class::get()->filter(array_merge(
['ParentID' => $this->owner->Parent()->ID],
$filter
))->exclude(array_merge(
['ID' => $this->owner->ID],
$exclude
))->last();
}
public function getColumnSizeRecursive($object = null)
{
$object = $object ? $object : $this->owner;
if ($object->isColumn() && $object->getField('Size')) {
return (int) $object->getField('Size');
} else {
$parent = $object->Parent()->getOwnerPage();
if (is_a($parent, 'Page')) {
return ($this->owner->getField('ContainerType') === 'container-fluid') ? false : 12;
}
return $object->getColumnSizeRecursive($parent);
}
}
2018-06-23 12:27:06 +02:00
public function ExtraClass()
{
return $this->owner->getField('ExtraClass')
.(
$this->isColumn()
? ' col '.self::$column_class.$this->owner->getField('Size')
: ''
);
2018-06-23 12:27:06 +02:00
}
/*
* if it's root element and it doesn't contain any container styles
* add the first one
*/
public function ContainerClass()
2018-06-23 12:27:06 +02:00
{
$type = $this->owner->getField('ContainerType');
2018-06-23 12:27:06 +02:00
$container_styles = array_keys(self::$container_styles);
if (!$type && $this->isRoot()) {
$type = $container_styles[0];
2018-06-23 12:27:06 +02:00
}
return $type;
2018-06-23 12:27:06 +02:00
}
}