2015-03-04 08:05:32 +00:00
|
|
|
<?php namespace Backend\FormWidgets;
|
|
|
|
|
|
2018-08-12 07:33:51 +00:00
|
|
|
use Lang;
|
|
|
|
|
use ApplicationException;
|
2015-03-04 08:05:32 +00:00
|
|
|
use Backend\Classes\FormWidgetBase;
|
2019-02-04 18:50:40 +00:00
|
|
|
use October\Rain\Html\Helper as HtmlHelper;
|
2015-03-04 08:05:32 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Repeater Form Widget
|
|
|
|
|
*/
|
|
|
|
|
class Repeater extends FormWidgetBase
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// Configurable properties
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array Form field configuration
|
|
|
|
|
*/
|
|
|
|
|
public $form;
|
|
|
|
|
|
2015-03-05 08:02:23 +00:00
|
|
|
/**
|
|
|
|
|
* @var string Prompt text for adding new items.
|
|
|
|
|
*/
|
|
|
|
|
public $prompt = 'Add new item';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool Items can be sorted.
|
|
|
|
|
*/
|
|
|
|
|
public $sortable = false;
|
|
|
|
|
|
2018-08-12 02:27:53 +00:00
|
|
|
/**
|
|
|
|
|
* @var string Field name to use for the title of collapsed items
|
|
|
|
|
*/
|
|
|
|
|
public $titleFrom = false;
|
|
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
/**
|
2018-08-12 07:33:51 +00:00
|
|
|
* @var int Minimum items required. Pre-displays those items when not using groups
|
|
|
|
|
*/
|
2018-08-15 16:33:24 +00:00
|
|
|
public $minItems;
|
2018-08-12 07:33:51 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int Maximum items permitted
|
2017-04-22 11:04:37 +00:00
|
|
|
*/
|
2018-08-15 16:33:24 +00:00
|
|
|
public $maxItems;
|
2017-04-22 11:04:37 +00:00
|
|
|
|
2015-03-04 08:05:32 +00:00
|
|
|
//
|
|
|
|
|
// Object properties
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2015-03-04 08:05:32 +00:00
|
|
|
*/
|
|
|
|
|
protected $defaultAlias = 'repeater';
|
|
|
|
|
|
2015-08-22 00:18:41 +00:00
|
|
|
/**
|
|
|
|
|
* @var int Count of repeated items.
|
|
|
|
|
*/
|
2015-03-04 08:05:32 +00:00
|
|
|
protected $indexCount = 0;
|
|
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
/**
|
|
|
|
|
* @var array Meta data associated to each field, organised by index
|
|
|
|
|
*/
|
|
|
|
|
protected $indexMeta = [];
|
|
|
|
|
|
2015-08-22 00:18:41 +00:00
|
|
|
/**
|
|
|
|
|
* @var array Collection of form widgets.
|
|
|
|
|
*/
|
2015-03-04 08:05:32 +00:00
|
|
|
protected $formWidgets = [];
|
|
|
|
|
|
2017-03-01 02:36:32 +00:00
|
|
|
/**
|
|
|
|
|
* @var bool Stops nested repeaters populating from previous sibling.
|
|
|
|
|
*/
|
2015-08-22 00:18:41 +00:00
|
|
|
protected static $onAddItemCalled = false;
|
2017-03-01 03:31:37 +00:00
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
protected $useGroups = false;
|
|
|
|
|
|
|
|
|
|
protected $groupDefinitions = [];
|
2015-08-22 00:18:41 +00:00
|
|
|
|
2015-03-04 08:05:32 +00:00
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2015-03-04 08:05:32 +00:00
|
|
|
*/
|
|
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$this->fillFromConfig([
|
|
|
|
|
'form',
|
2015-03-05 08:02:23 +00:00
|
|
|
'prompt',
|
|
|
|
|
'sortable',
|
2018-08-12 02:27:53 +00:00
|
|
|
'titleFrom',
|
2018-08-12 07:33:51 +00:00
|
|
|
'minItems',
|
2017-03-01 02:36:32 +00:00
|
|
|
'maxItems',
|
2015-03-04 08:05:32 +00:00
|
|
|
]);
|
|
|
|
|
|
2017-07-08 19:34:04 +00:00
|
|
|
if ($this->formField->disabled) {
|
|
|
|
|
$this->previewMode = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
$fieldName = $this->formField->getName(false);
|
|
|
|
|
|
|
|
|
|
$this->processGroupMode();
|
|
|
|
|
|
2015-08-22 00:18:41 +00:00
|
|
|
if (!self::$onAddItemCalled) {
|
2019-04-04 07:23:32 +00:00
|
|
|
$this->processItems();
|
2015-08-22 00:18:41 +00:00
|
|
|
}
|
2015-03-04 08:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2015-03-04 08:05:32 +00:00
|
|
|
*/
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
$this->prepareVars();
|
|
|
|
|
return $this->makePartial('repeater');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepares the form widget view data
|
|
|
|
|
*/
|
|
|
|
|
public function prepareVars()
|
|
|
|
|
{
|
2017-11-01 20:18:41 +00:00
|
|
|
// Refresh the loaded data to support being modified by filterFields
|
|
|
|
|
// @see https://github.com/octobercms/october/issues/2613
|
|
|
|
|
if (!self::$onAddItemCalled) {
|
2019-04-04 07:23:32 +00:00
|
|
|
$this->processItems();
|
2017-11-01 20:18:41 +00:00
|
|
|
}
|
2018-08-12 02:27:53 +00:00
|
|
|
|
2017-07-08 19:34:04 +00:00
|
|
|
if ($this->previewMode) {
|
|
|
|
|
foreach ($this->formWidgets as $widget) {
|
|
|
|
|
$widget->previewMode = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 08:02:23 +00:00
|
|
|
$this->vars['prompt'] = $this->prompt;
|
|
|
|
|
$this->vars['formWidgets'] = $this->formWidgets;
|
2018-08-12 02:27:53 +00:00
|
|
|
$this->vars['titleFrom'] = $this->titleFrom;
|
2018-08-12 07:33:51 +00:00
|
|
|
$this->vars['minItems'] = $this->minItems;
|
2017-03-01 02:36:32 +00:00
|
|
|
$this->vars['maxItems'] = $this->maxItems;
|
2017-04-22 11:04:37 +00:00
|
|
|
|
|
|
|
|
$this->vars['useGroups'] = $this->useGroups;
|
|
|
|
|
$this->vars['groupDefinitions'] = $this->groupDefinitions;
|
2015-03-04 08:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2015-03-04 08:05:32 +00:00
|
|
|
*/
|
2015-08-04 09:32:51 +00:00
|
|
|
protected function loadAssets()
|
2015-03-04 08:05:32 +00:00
|
|
|
{
|
|
|
|
|
$this->addCss('css/repeater.css', 'core');
|
|
|
|
|
$this->addJs('js/repeater.js', 'core');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2015-03-04 08:05:32 +00:00
|
|
|
*/
|
|
|
|
|
public function getSaveValue($value)
|
|
|
|
|
{
|
2017-04-24 03:24:26 +00:00
|
|
|
return (array) $this->processSaveValue($value);
|
2015-03-04 08:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-22 12:01:17 +00:00
|
|
|
/**
|
|
|
|
|
* Splices in some meta data (group and index values) to the dataset.
|
|
|
|
|
* @param array $value
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function processSaveValue($value)
|
2017-04-22 11:04:37 +00:00
|
|
|
{
|
2017-04-25 22:10:45 +00:00
|
|
|
if (!is_array($value) || !$value) {
|
2017-04-22 12:01:17 +00:00
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-12 07:33:51 +00:00
|
|
|
if ($this->minItems && count($value) < $this->minItems) {
|
|
|
|
|
throw new ApplicationException(Lang::get('backend::lang.repeater.min_items_failed', ['name' => $this->fieldName, 'min' => $this->minItems, 'items' => count($value)]));
|
|
|
|
|
}
|
|
|
|
|
if ($this->maxItems && count($value) > $this->maxItems) {
|
|
|
|
|
throw new ApplicationException(Lang::get('backend::lang.repeater.max_items_failed', ['name' => $this->fieldName, 'max' => $this->maxItems, 'items' => count($value)]));
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 18:50:40 +00:00
|
|
|
/*
|
|
|
|
|
* Give repeated form field widgets an opportunity to process the data.
|
|
|
|
|
*/
|
2019-03-16 04:39:34 +00:00
|
|
|
foreach ($value as $index => $data) {
|
|
|
|
|
if (isset($this->formWidgets[$index])) {
|
2019-04-01 18:37:32 +00:00
|
|
|
if ($this->useGroups) {
|
|
|
|
|
$value[$index] = array_merge($this->formWidgets[$index]->getSaveData(), ['_group' => $data['_group']]);
|
|
|
|
|
} else {
|
|
|
|
|
$value[$index] = $this->formWidgets[$index]->getSaveData();
|
|
|
|
|
}
|
2019-02-04 18:50:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-22 12:01:17 +00:00
|
|
|
return array_values($value);
|
2017-04-22 11:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-04-04 07:23:32 +00:00
|
|
|
* Processes form data and applies it to the form widgets.
|
2017-04-22 11:04:37 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2019-04-04 07:23:32 +00:00
|
|
|
protected function processItems()
|
2015-03-04 08:05:32 +00:00
|
|
|
{
|
2019-04-09 07:35:07 +00:00
|
|
|
$groups = [];
|
2019-04-05 15:00:26 +00:00
|
|
|
$currentValue = post($this->formField->getName(), $this->getLoadValue());
|
2017-04-24 09:06:59 +00:00
|
|
|
|
2018-08-12 07:33:51 +00:00
|
|
|
// Ensure that the minimum number of items are preinitialized
|
|
|
|
|
// ONLY DONE WHEN NOT IN GROUP MODE
|
|
|
|
|
if (!$this->useGroups && $this->minItems > 0) {
|
2019-04-04 07:23:32 +00:00
|
|
|
if (!is_array($currentValue)) {
|
|
|
|
|
$currentValue = [];
|
2018-08-12 07:33:51 +00:00
|
|
|
for ($i = 0; $i < $this->minItems; $i++) {
|
2019-04-04 07:23:32 +00:00
|
|
|
$currentValue[$i] = [];
|
2018-08-12 07:33:51 +00:00
|
|
|
}
|
2019-04-04 07:23:32 +00:00
|
|
|
} elseif (count($currentValue) < $this->minItems) {
|
|
|
|
|
for ($i = 0; $i < ($this->minItems - count($currentValue)); $i++) {
|
|
|
|
|
$currentValue[] = [];
|
2018-08-12 07:33:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-04 07:23:32 +00:00
|
|
|
if (is_array($currentValue)) {
|
2019-04-09 07:35:07 +00:00
|
|
|
foreach ($currentValue as $value) {
|
2019-04-04 07:23:32 +00:00
|
|
|
$groups[] = array_get($value, '_group');
|
2017-04-24 02:32:08 +00:00
|
|
|
}
|
2015-03-04 08:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-09 07:35:07 +00:00
|
|
|
if (!count($groups)) {
|
2017-04-22 11:40:35 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 07:35:07 +00:00
|
|
|
foreach ($groups as $index => $groupCode) {
|
2019-04-04 07:23:32 +00:00
|
|
|
$this->makeItemFormWidget($index, $groupCode);
|
2015-03-04 08:05:32 +00:00
|
|
|
}
|
2019-04-09 07:35:07 +00:00
|
|
|
$this->indexCount = max(count($groups), $this->indexCount);
|
2015-03-04 08:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
/**
|
|
|
|
|
* Creates a form widget based on a field index and optional group code.
|
|
|
|
|
* @param int $index
|
|
|
|
|
* @param string $index
|
|
|
|
|
* @return \Backend\Widgets\Form
|
|
|
|
|
*/
|
|
|
|
|
protected function makeItemFormWidget($index = 0, $groupCode = null)
|
2015-03-04 08:05:32 +00:00
|
|
|
{
|
2017-04-22 11:04:37 +00:00
|
|
|
$configDefinition = $this->useGroups
|
|
|
|
|
? $this->getGroupFormFieldConfig($groupCode)
|
|
|
|
|
: $this->form;
|
|
|
|
|
|
|
|
|
|
$config = $this->makeConfig($configDefinition);
|
2015-03-04 08:05:32 +00:00
|
|
|
$config->model = $this->model;
|
2019-04-04 07:23:32 +00:00
|
|
|
$config->data = $this->getValueFromIndex($index);
|
2015-03-04 08:05:32 +00:00
|
|
|
$config->alias = $this->alias . 'Form'.$index;
|
2016-11-27 20:50:06 +00:00
|
|
|
$config->arrayName = $this->getFieldName().'['.$index.']';
|
2016-11-04 22:53:23 +00:00
|
|
|
$config->isNested = true;
|
2019-02-11 15:54:55 +00:00
|
|
|
if (self::$onAddItemCalled || $this->minItems > 0) {
|
|
|
|
|
$config->enableDefaults = true;
|
|
|
|
|
}
|
2015-03-04 08:05:32 +00:00
|
|
|
|
|
|
|
|
$widget = $this->makeWidget('Backend\Widgets\Form', $config);
|
|
|
|
|
$widget->bindToController();
|
|
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
$this->indexMeta[$index] = [
|
|
|
|
|
'groupCode' => $groupCode
|
|
|
|
|
];
|
|
|
|
|
|
2015-03-04 08:05:32 +00:00
|
|
|
return $this->formWidgets[$index] = $widget;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-22 12:01:17 +00:00
|
|
|
/**
|
2019-04-04 07:23:32 +00:00
|
|
|
* Returns the data at a given index.
|
2017-04-22 12:01:17 +00:00
|
|
|
* @param int $index
|
|
|
|
|
*/
|
2019-04-04 07:23:32 +00:00
|
|
|
protected function getValueFromIndex($index)
|
2017-04-22 12:01:17 +00:00
|
|
|
{
|
2019-04-04 07:23:32 +00:00
|
|
|
$value = post($this->formField->fieldName, $this->getLoadValue());
|
|
|
|
|
if (!is_array($value)) {
|
|
|
|
|
$value = [];
|
2017-04-22 12:01:17 +00:00
|
|
|
}
|
2017-04-25 22:10:45 +00:00
|
|
|
|
2019-04-04 07:23:32 +00:00
|
|
|
return array_get($value, $index, []);
|
2017-04-22 12:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
//
|
|
|
|
|
// AJAX handlers
|
|
|
|
|
//
|
|
|
|
|
|
2015-03-04 08:05:32 +00:00
|
|
|
public function onAddItem()
|
|
|
|
|
{
|
2015-08-22 00:18:41 +00:00
|
|
|
self::$onAddItemCalled = true;
|
|
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
$groupCode = post('_repeater_group');
|
|
|
|
|
|
2015-03-04 08:05:32 +00:00
|
|
|
$this->prepareVars();
|
2017-04-22 11:04:37 +00:00
|
|
|
$this->vars['widget'] = $this->makeItemFormWidget($this->indexCount, $groupCode);
|
2015-03-04 08:05:32 +00:00
|
|
|
$this->vars['indexValue'] = $this->indexCount;
|
|
|
|
|
|
|
|
|
|
$itemContainer = '@#'.$this->getId('items');
|
2019-04-08 06:37:36 +00:00
|
|
|
|
|
|
|
|
// Increase index count after item is created
|
|
|
|
|
++$this->indexCount;
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
$itemContainer => $this->makePartial('repeater_item')
|
|
|
|
|
];
|
2015-03-04 08:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function onRemoveItem()
|
|
|
|
|
{
|
|
|
|
|
// Useful for deleting relations
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 22:38:32 +00:00
|
|
|
public function onRefresh()
|
|
|
|
|
{
|
2016-07-23 04:22:36 +00:00
|
|
|
$index = post('_repeater_index');
|
2017-04-22 11:04:37 +00:00
|
|
|
$group = post('_repeater_group');
|
2016-07-21 22:38:32 +00:00
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
$widget = $this->makeItemFormWidget($index, $group);
|
2016-07-21 22:38:32 +00:00
|
|
|
|
|
|
|
|
return $widget->onRefresh();
|
|
|
|
|
}
|
2017-04-22 11:04:37 +00:00
|
|
|
|
2019-04-08 15:40:41 +00:00
|
|
|
public function onReorder()
|
|
|
|
|
{
|
|
|
|
|
// Handle reordering of repeater items
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
//
|
|
|
|
|
// Group mode
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the form field configuration for a group, identified by code.
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @return array|null
|
|
|
|
|
*/
|
|
|
|
|
protected function getGroupFormFieldConfig($code)
|
|
|
|
|
{
|
|
|
|
|
if (!$code) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fields = array_get($this->groupDefinitions, $code.'.fields');
|
|
|
|
|
|
|
|
|
|
if (!$fields) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-12 07:19:37 +00:00
|
|
|
return ['fields' => $fields, 'enableDefaults' => object_get($this->config, 'enableDefaults')];
|
2017-04-22 11:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Process features related to group mode.
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function processGroupMode()
|
|
|
|
|
{
|
|
|
|
|
$palette = [];
|
|
|
|
|
|
|
|
|
|
if (!$group = $this->getConfig('groups', [])) {
|
|
|
|
|
$this->useGroups = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-24 02:47:46 +00:00
|
|
|
if (is_string($group)) {
|
|
|
|
|
$group = $this->makeConfig($group);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-22 11:04:37 +00:00
|
|
|
foreach ($group as $code => $config) {
|
|
|
|
|
$palette[$code] = [
|
|
|
|
|
'code' => $code,
|
|
|
|
|
'name' => array_get($config, 'name'),
|
|
|
|
|
'icon' => array_get($config, 'icon', 'icon-square-o'),
|
|
|
|
|
'description' => array_get($config, 'description'),
|
|
|
|
|
'fields' => array_get($config, 'fields')
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->groupDefinitions = $palette;
|
|
|
|
|
$this->useGroups = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a field group code from its index.
|
|
|
|
|
* @param $index int
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getGroupCodeFromIndex($index)
|
|
|
|
|
{
|
|
|
|
|
return array_get($this->indexMeta, $index.'.groupCode');
|
|
|
|
|
}
|
2017-04-24 02:44:53 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the group title from its unique code.
|
|
|
|
|
* @param $groupCode string
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getGroupTitle($groupCode)
|
|
|
|
|
{
|
|
|
|
|
return array_get($this->groupDefinitions, $groupCode.'.name');
|
|
|
|
|
}
|
2015-03-04 08:05:32 +00:00
|
|
|
}
|