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;
|
2018-07-18 13:44:24 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2018-12-12 12:58:55 +01:00
|
|
|
use SilverStripe\Forms\CheckboxField;
|
2018-06-23 12:27:06 +02:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
|
|
|
|
class ElementRows extends DataExtension
|
|
|
|
{
|
2018-07-02 03:54:18 +02:00
|
|
|
private static $container_max_width = 1140;
|
2018-07-29 12:58:28 +02:00
|
|
|
private static $column_class = 'col-block col-md-';
|
2018-07-02 03:54:18 +02:00
|
|
|
|
2018-06-23 12:27:06 +02:00
|
|
|
private static $container_styles = [
|
2018-07-02 03:54:18 +02:00
|
|
|
'container' => 'Fixed container',
|
|
|
|
'container-fluid' => 'Fluid Container',
|
2018-06-23 12:27:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $db = [
|
2018-07-02 03:54:18 +02:00
|
|
|
'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)
|
|
|
|
{
|
2018-07-02 03:54:18 +02:00
|
|
|
parent::updateCMSFields($fields);
|
|
|
|
|
2018-12-12 12:58:55 +01:00
|
|
|
// move available globaly to main tab
|
|
|
|
$fields->removeByName('AvailableGlobally');
|
2019-08-19 10:37:24 +02:00
|
|
|
|
2018-06-23 12:27:06 +02:00
|
|
|
$tab = $fields->findOrMakeTab('Root.Main');
|
2018-12-12 12:58:55 +01:00
|
|
|
$tab->push(CheckboxField::create('AvailableGlobally'));
|
2018-06-23 12:27:06 +02:00
|
|
|
|
2018-07-02 03:54:18 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2018-12-12 12:58:55 +01:00
|
|
|
// site-specific
|
|
|
|
$fields->removeByName('ContainerType');
|
|
|
|
|
2018-07-02 03:54:18 +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',
|
2018-07-02 03:54:18 +02:00
|
|
|
_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
|
2018-07-02 03:54:18 +02:00
|
|
|
if (!$this->owner->getField('Size')) {
|
|
|
|
$sibling = $this->getSibling();
|
2018-06-23 12:27:06 +02:00
|
|
|
|
|
|
|
$sizeDropdown->setValue($sibling ? $sibling->getField('Size') : $defaultSize);
|
|
|
|
}
|
2018-07-02 03:54:18 +02:00
|
|
|
} else {
|
2018-06-23 12:27:06 +02:00
|
|
|
$fields->removeByName('Size');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 00:55:42 +02:00
|
|
|
public function getWidthPercetage()
|
|
|
|
{
|
2019-05-23 13:02:32 +02:00
|
|
|
return $this->isColumn() ? $this->owner->getField('Size') / self::colsNumber() * 100 : false;
|
2018-09-15 00:55:42 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 12:27:06 +02:00
|
|
|
public function isList()
|
|
|
|
{
|
|
|
|
return is_a($this->owner, ElementList::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isRow()
|
|
|
|
{
|
2018-07-02 03:54:18 +02:00
|
|
|
if (!$this->isList()) {
|
2018-06-23 12:27:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isColumn()
|
|
|
|
{
|
2018-07-02 03:54:18 +02:00
|
|
|
if (!$this->isRoot()) {
|
2018-06-23 12:27:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isRoot()
|
|
|
|
{
|
|
|
|
$parent = $this->owner->Parent()->getOwnerPage();
|
2018-07-02 03:54:18 +02:00
|
|
|
if (is_a($parent, 'Page')) {
|
2018-06-23 12:27:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-02 03:54:18 +02:00
|
|
|
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');
|
2019-05-23 13:02:32 +02:00
|
|
|
}
|
2018-07-02 03:54:18 +02:00
|
|
|
|
2019-05-23 13:02:32 +02:00
|
|
|
$parent = $object->Parent()->getOwnerPage();
|
2018-07-02 03:54:18 +02:00
|
|
|
|
2019-05-23 13:02:32 +02:00
|
|
|
if (is_a($parent, 'Page')) {
|
|
|
|
return ($this->owner->getField('ContainerType') === 'container-fluid') ? false : self::colsNumber();
|
2018-07-02 03:54:18 +02:00
|
|
|
}
|
2019-05-23 13:02:32 +02:00
|
|
|
|
|
|
|
return $object->getColumnSizeRecursive($parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getColumnWidthRecursive($object = null, $max = null)
|
|
|
|
{
|
|
|
|
$max = $max ? $max : self::maxWidth();
|
|
|
|
|
|
|
|
$object = $object ? $object : $this->owner;
|
|
|
|
|
2019-08-19 10:37:24 +02:00
|
|
|
if (!$object->isRoot()) {
|
2019-05-23 13:02:32 +02:00
|
|
|
$size = $object->getField('Size');
|
|
|
|
$max = $size ? $max / (self::colsNumber() / $size) : $max;
|
|
|
|
$parent = $object->Parent()->getOwnerPage();
|
|
|
|
|
|
|
|
return $this->getColumnWidthRecursive($parent, $max);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $max;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function colsNumber()
|
|
|
|
{
|
|
|
|
$db = Config::inst()->get(self::class, 'db');
|
|
|
|
$sizes = $db['Size'];
|
2019-08-19 10:37:24 +02:00
|
|
|
$sizes = preg_replace('!Enum\("([0-9,]+)","([0-9]+)"\)!i', '$1', $sizes);
|
|
|
|
$sizes = explode(',', $sizes);
|
2019-05-23 13:02:32 +02:00
|
|
|
|
|
|
|
return max($sizes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function maxWidth()
|
|
|
|
{
|
|
|
|
return Config::inst()->get(self::class, 'container_max_width');
|
2018-07-02 03:54:18 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 12:27:06 +02:00
|
|
|
public function ExtraClass()
|
|
|
|
{
|
2018-07-02 03:54:18 +02:00
|
|
|
return $this->owner->getField('ExtraClass')
|
|
|
|
.(
|
2019-08-27 17:25:05 +02:00
|
|
|
$this->isColumn()
|
2018-07-18 13:44:24 +02:00
|
|
|
? ' '.Config::inst()->get(self::class, 'column_class').$this->owner->getField('Size')
|
2018-07-02 03:54:18 +02:00
|
|
|
: ''
|
|
|
|
);
|
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
|
|
|
|
*/
|
2018-07-02 03:54:18 +02:00
|
|
|
public function ContainerClass()
|
2018-06-23 12:27:06 +02:00
|
|
|
{
|
2018-07-02 03:54:18 +02:00
|
|
|
$type = $this->owner->getField('ContainerType');
|
2018-06-23 12:27:06 +02:00
|
|
|
$container_styles = array_keys(self::$container_styles);
|
|
|
|
|
2018-07-02 03:54:18 +02:00
|
|
|
if (!$type && $this->isRoot()) {
|
2019-08-19 10:37:24 +02:00
|
|
|
$type = \Page::DefaultContainer();
|
2018-06-23 12:27:06 +02:00
|
|
|
}
|
2018-07-02 03:54:18 +02:00
|
|
|
|
|
|
|
return $type;
|
2018-06-23 12:27:06 +02:00
|
|
|
}
|
2019-08-27 17:25:05 +02:00
|
|
|
}
|