2008-09-29 03:18:23 +00:00
|
|
|
<?php
|
2016-11-16 17:04:54 -06:00
|
|
|
|
2017-08-09 11:55:09 +12:00
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
2021-02-26 16:13:23 +13:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-08-11 11:33:06 +12:00
|
|
|
use SilverStripe\Forms\TextField;
|
2017-08-09 11:55:09 +12:00
|
|
|
use SilverStripe\UserForms\Model\EditableCustomRule;
|
2017-09-05 09:45:16 +12:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 11:55:09 +12:00
|
|
|
|
2008-09-29 03:18:23 +00:00
|
|
|
/**
|
|
|
|
* EditableDropdown
|
2009-04-17 02:26:40 +00:00
|
|
|
*
|
2010-09-03 05:06:13 +00:00
|
|
|
* Represents a modifiable dropdown (select) box on a form
|
2009-04-17 02:26:40 +00:00
|
|
|
*
|
2021-02-26 16:13:23 +13:00
|
|
|
* @package userforms
|
2016-11-16 17:04:54 -06:00
|
|
|
* @property bool $UseEmptyString
|
|
|
|
* @property string $EmptyString
|
2008-09-29 03:18:23 +00:00
|
|
|
*/
|
2016-07-21 17:53:59 +12:00
|
|
|
class EditableDropdown extends EditableMultipleOptionField
|
|
|
|
{
|
|
|
|
private static $singular_name = 'Dropdown Field';
|
|
|
|
|
|
|
|
private static $plural_name = 'Dropdowns';
|
|
|
|
|
2016-11-16 17:04:54 -06:00
|
|
|
private static $db = array(
|
|
|
|
'UseEmptyString' => 'Boolean',
|
|
|
|
'EmptyString' => 'Varchar(255)',
|
|
|
|
);
|
|
|
|
|
2017-08-11 11:33:06 +12:00
|
|
|
private static $table_name = 'EditableDropdown';
|
|
|
|
|
2016-07-21 17:53:59 +12:00
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2022-03-03 16:47:45 +13:00
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
CheckboxField::create('UseEmptyString')
|
|
|
|
->setTitle('Set default empty string')
|
|
|
|
);
|
2016-07-21 17:53:59 +12:00
|
|
|
|
2022-03-03 16:47:45 +13:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
TextField::create('EmptyString')
|
|
|
|
->setTitle('Empty String')
|
|
|
|
);
|
2016-11-16 17:04:54 -06:00
|
|
|
|
2022-03-03 16:47:45 +13:00
|
|
|
$fields->removeByName('Default');
|
|
|
|
});
|
2016-11-16 17:04:54 -06:00
|
|
|
|
2022-03-03 16:47:45 +13:00
|
|
|
return parent::getCMSFields();
|
2016-07-21 17:53:59 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return DropdownField
|
|
|
|
*/
|
|
|
|
public function getFormField()
|
|
|
|
{
|
2018-03-23 16:23:51 +13:00
|
|
|
$field = DropdownField::create($this->Name, $this->Title ?: false, $this->getOptionsMap())
|
2017-08-14 12:29:57 +12:00
|
|
|
->setFieldHolderTemplate(EditableFormField::class . '_holder')
|
|
|
|
->setTemplate(__CLASS__);
|
2016-07-21 17:53:59 +12:00
|
|
|
|
2016-11-16 17:04:54 -06:00
|
|
|
if ($this->UseEmptyString) {
|
|
|
|
$field->setEmptyString(($this->EmptyString) ? $this->EmptyString : '');
|
|
|
|
}
|
|
|
|
|
2016-07-21 17:53:59 +12:00
|
|
|
// Set default
|
|
|
|
$defaultOption = $this->getDefaultOptions()->first();
|
|
|
|
if ($defaultOption) {
|
2017-06-13 09:49:02 +10:00
|
|
|
$field->setValue($defaultOption->Value);
|
2016-07-21 17:53:59 +12:00
|
|
|
}
|
|
|
|
$this->doUpdateFormField($field);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
|
|
|
|
{
|
|
|
|
return "$(\"select[name='{$this->Name}']\")";
|
|
|
|
}
|
|
|
|
}
|