Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
colintucker committed Feb 12, 2018
2 parents 45a8f5e + e89ef7c commit 13f734f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 8 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"require": {
"php": ">=5.6.0",
"embed/embed": "^3.0",
"silverstripe/asset-admin": "^1@dev",
"silverstripe/cms": "^4@dev",
"silverstripe/framework": "^4@dev",
"silverstripe/asset-admin": "^1.0",
"silverstripe/cms": "^4.0",
"silverstripe/framework": "^4.0",
"silverware/colorpicker": "^1.0",
"silverware/font-icons": "^1.0",
"silverware/select2": "^1.0",
Expand Down
22 changes: 18 additions & 4 deletions src/Dev/FixtureBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBComposite;
use SilverStripe\ORM\Hierarchy\Hierarchy;
use SilverStripe\Versioned\Versioned;
use Exception;
Expand Down Expand Up @@ -504,8 +505,20 @@ public function populateField(DataObject $object, $name, $value)

// Handle Array Value:

foreach ($value as $k => $v) {
$object->dbObject($name)->setField($k, $this->processValue($v));
if ($object->dbObject($name) instanceof DBComposite) {

// Handle Composite Field:

foreach ($value as $k => $v) {
$object->dbObject($name)->setField($k, $this->processValue($v));
}

} else {

// Handle Regular Field (JSON-encode):

$object->setField($name, $this->processArray($value, true));

}

} else {
Expand Down Expand Up @@ -1432,18 +1445,19 @@ protected function processValue($value)
* Processes the given array value and answers the resulting data.
*
* @param array $value
* @param boolean $json
*
* @return mixed
*/
protected function processArray($value)
protected function processArray($value, $json = false)
{
if (is_array($value)) {

if ($this->isCallbackArray($value)) {
return $this->processCallbackArray($value);
}

return implode(', ', $value);
return $json ? json_encode($value) : implode(', ', $value);

}

Expand Down
90 changes: 89 additions & 1 deletion src/Extensions/RenderableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

namespace SilverWare\Extensions;

use SilverStripe\Core\Convert;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\CheckboxSetField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\SelectionGroup;
use SilverStripe\Forms\SelectionGroup_Item;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayLib;
use SilverStripe\ORM\DataExtension;
use SilverWare\Forms\FieldSection;

Expand All @@ -46,6 +49,7 @@ class RenderableExtension extends DataExtension
private static $db = [
'StyleID' => 'Varchar(255)',
'StyleClasses' => 'Varchar(255)',
'CustomStyles' => 'Varchar(255)',
'CacheLifetime' => 'Int',
'Cached' => 'Boolean',
'Disabled' => 'Boolean'
Expand Down Expand Up @@ -93,7 +97,7 @@ public function updateCMSFields(FieldList $fields)
$fields->addFieldsToTab(
'Root.Style',
[
FieldSection::create(
$selectors = FieldSection::create(
'SelectorStyle',
$this->owner->fieldLabel('Selectors'),
[
Expand All @@ -120,6 +124,25 @@ public function updateCMSFields(FieldList $fields)
]
);

// Create Custom Styles Field (if available):

if ($this->owner->hasCustomStylesConfig()) {

$selectors->push(
CheckboxSetField::create(
'CustomStyles',
$this->owner->fieldLabel('CustomStyles'),
$this->owner->getCustomStylesOptions()
)->setRightTitle(
_t(
__CLASS__ . '.CUSTOMSTYLESRIGHTTITLE',
'This component supports custom styles. Select one or more of the options above.'
)
)
);

}

// Create Options Fields:

$fields->addFieldsToTab(
Expand Down Expand Up @@ -173,6 +196,7 @@ public function updateFieldLabels(&$labels)
$labels['Options'] = _t(__CLASS__ . '.OPTIONS', 'Options');
$labels['Enabled'] = _t(__CLASS__ . '.ENABLED', 'Enabled');
$labels['Selectors'] = _t(__CLASS__ . '.SELECTORS', 'Selectors');
$labels['CustomStyles'] = _t(__CLASS__ . '.CUSTOMSTYLES', 'Custom Styles');
$labels['StyleClasses'] = _t(__CLASS__ . '.STYLECLASSES', 'Style Classes');
$labels['CacheLifetime'] = _t(__CLASS__ . '.CACHELIFETIMEINSECONDS', 'Cache lifetime (in seconds)');
$labels['Disabled'] = $labels['Disabled.Nice'] = _t(__CLASS__ . '.DISABLED', 'Disabled');
Expand Down Expand Up @@ -214,4 +238,68 @@ public function onAfterWrite()
{
$this->owner->clearRenderCache();
}

/**
* Answers a sorted array of any custom styles configured for the extended object.
*
* @return array
*/
public function getCustomStylesConfig()
{
$styles = [];

if (($config = $this->owner->config()->custom_styles) && is_array($config)) {
$styles = $config;
}

ksort($styles);

array_walk($styles, function (&$item) {
$item = $this->owner->cleanStyleClasses($item);
});

return $styles;
}

/**
* Answers true if the extended object has custom styles configured.
*
* @return boolean
*/
public function hasCustomStylesConfig()
{
return !empty($this->owner->getCustomStylesConfig());
}

/**
* Answers an array of selected custom styles mapped to their class names.
*
* @return array
*/
public function getCustomStylesMappings()
{
$config = $this->owner->getCustomStylesConfig();

if ($values = $this->owner->getField('CustomStyles')) {

$styles = Convert::json2array($values);

return array_intersect_key($config, array_flip($styles));

}

return [];
}

/**
* Answers an array of options for the custom styles field.
*
* @return array
*/
public function getCustomStylesOptions()
{
$keys = array_keys($this->owner->getCustomStylesConfig());

return ArrayLib::valuekey($keys, $keys);
}
}
17 changes: 17 additions & 0 deletions src/View/Renderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public function getClassNames()

$classes = array_merge(
$this->getStyleClassNames(),
$this->getCustomClassNames(),
$this->getAncestorClassNames()
);

Expand All @@ -343,6 +344,22 @@ public function getStyleClassNames()
return explode(' ', $this->getField('StyleClasses'));
}

/**
* Answers an array of custom style class names for the HTML template.
*
* @return array
*/
public function getCustomClassNames()
{
$classes = [];

if (is_array($this->CustomStylesMappings)) {
return array_values($this->CustomStylesMappings);
}

return $classes;
}

/**
* Answers an array of ancestor class names for the HTML template.
*
Expand Down

0 comments on commit 13f734f

Please sign in to comment.