2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
2017-08-09 01:55:09 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
|
|
|
use SilverStripe\UserForms\FormField\UserFormsCheckboxSetField;
|
|
|
|
use SilverStripe\UserForms\Model\EditableCustomRule;
|
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
/**
|
2009-04-17 04:26:40 +02:00
|
|
|
* EditableCheckboxGroup
|
|
|
|
*
|
2008-09-29 05:18:23 +02:00
|
|
|
* Represents a set of selectable radio buttons
|
2015-09-11 00:20:06 +02:00
|
|
|
*
|
2009-04-17 04:26:40 +02:00
|
|
|
* @package userforms
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
2009-12-07 03:04:20 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableCheckboxGroupField extends EditableMultipleOptionField
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $singular_name = 'Checkbox Group';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $plural_name = 'Checkbox Groups';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
protected $jsEventHandler = 'click';
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $table_name = 'EditableCheckboxGroupField';
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function getFormField()
|
|
|
|
{
|
2018-03-23 04:23:51 +01:00
|
|
|
$field = UserFormsCheckboxSetField::create($this->Name, $this->Title ?: false, $this->getOptionsMap())
|
2017-08-22 02:15:22 +02:00
|
|
|
->setFieldHolderTemplate(EditableMultipleOptionField::class . '_holder')
|
|
|
|
->setTemplate(UserFormsCheckboxSetField::class);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
// Set the default checked items
|
|
|
|
$defaultCheckedItems = $this->getDefaultOptions();
|
|
|
|
if ($defaultCheckedItems->count()) {
|
2017-06-13 01:49:02 +02:00
|
|
|
$field->setDefaultItems($defaultCheckedItems->map('Value')->keys());
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->doUpdateFormField($field);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValueFromData($data)
|
|
|
|
{
|
|
|
|
$result = '';
|
|
|
|
$entries = (isset($data[$this->Name])) ? $data[$this->Name] : false;
|
|
|
|
|
|
|
|
if ($entries) {
|
|
|
|
if (!is_array($data[$this->Name])) {
|
2017-08-11 01:33:06 +02:00
|
|
|
$entries = [$data[$this->Name]];
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
foreach ($entries as $selected => $value) {
|
|
|
|
if (!$result) {
|
|
|
|
$result = $value;
|
|
|
|
} else {
|
2017-08-11 01:33:06 +02:00
|
|
|
$result .= ', ' . $value;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
|
|
|
|
{
|
|
|
|
// watch out for checkboxs as the inputs don't have values but are 'checked
|
|
|
|
// @todo - Test this
|
|
|
|
if ($rule->FieldValue) {
|
|
|
|
return "$(\"input[name='{$this->Name}[]'][value='{$rule->FieldValue}']\")";
|
|
|
|
} else {
|
|
|
|
return "$(\"input[name='{$this->Name}[]']:first\")";
|
|
|
|
}
|
|
|
|
}
|
2017-04-28 00:22:15 +02:00
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
public function isCheckBoxField()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-28 00:22:15 +02:00
|
|
|
|
2017-08-11 02:37:03 +02:00
|
|
|
public function getSelectorFieldOnly()
|
2017-04-28 00:22:15 +02:00
|
|
|
{
|
|
|
|
return "[name='{$this->Name}[]']";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isCheckBoxGroupField()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-08 09:08:21 +02:00
|
|
|
}
|