2013-04-24 18:57:33 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
2017-08-13 23:26:53 +02:00
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
2018-05-01 06:07:03 +02:00
|
|
|
use SilverStripe\Forms\CheckboxField;
|
2017-08-14 00:06:17 +02:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
2022-03-03 04:47:45 +01:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2018-05-01 06:07:03 +02:00
|
|
|
use SilverStripe\Forms\TextField;
|
2017-08-14 00:06:17 +02:00
|
|
|
use SilverStripe\i18n\i18n;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableCustomRule;
|
2017-08-11 02:36:28 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2013-04-24 18:57:33 +02:00
|
|
|
/**
|
|
|
|
* A dropdown field which allows the user to select a country
|
|
|
|
*
|
2021-02-26 04:13:23 +01:00
|
|
|
* @package userforms
|
2018-05-01 06:07:03 +02:00
|
|
|
* @property bool $UseEmptyString
|
|
|
|
* @property string $EmptyString
|
2013-04-24 18:57:33 +02:00
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableCountryDropdownField extends EditableFormField
|
|
|
|
{
|
|
|
|
private static $singular_name = 'Country Dropdown';
|
|
|
|
|
|
|
|
private static $plural_name = 'Country Dropdowns';
|
|
|
|
|
2018-05-01 06:07:03 +02:00
|
|
|
private static $db = array(
|
|
|
|
'UseEmptyString' => 'Boolean',
|
|
|
|
'EmptyString' => 'Varchar(255)',
|
|
|
|
);
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $table_name = 'EditableCountryDropdownField';
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
2018-04-29 11:33:05 +02:00
|
|
|
* @return \SilverStripe\Forms\FieldList
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2022-03-03 04:47:45 +01:00
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
|
|
|
$fields->removeByName('Default');
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
DropdownField::create('Default', _t(__CLASS__ . '.DEFAULT', 'Default value'))
|
|
|
|
->setSource(i18n::getData()->getCountries())
|
|
|
|
->setHasEmptyDefault(true)
|
|
|
|
->setEmptyString('---')
|
|
|
|
);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2022-03-03 04:47:45 +01:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
CheckboxField::create('UseEmptyString', _t(__CLASS__ . '.USE_EMPTY_STRING', 'Set default empty string'))
|
|
|
|
);
|
2018-05-01 06:07:03 +02:00
|
|
|
|
2022-03-03 04:47:45 +01:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
TextField::create('EmptyString', _t(__CLASS__ . '.EMPTY_STRING', 'Empty String'))
|
|
|
|
);
|
|
|
|
});
|
2018-05-01 06:07:03 +02:00
|
|
|
|
2022-03-03 04:47:45 +01:00
|
|
|
return parent::getCMSFields();
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFormField()
|
|
|
|
{
|
2018-03-23 04:23:51 +01:00
|
|
|
$field = DropdownField::create($this->Name, $this->Title ?: false)
|
2017-08-14 00:06:17 +02:00
|
|
|
->setSource(i18n::getData()->getCountries())
|
2017-09-04 23:45:16 +02:00
|
|
|
->setFieldHolderTemplate(EditableFormField::class . '_holder')
|
|
|
|
->setTemplate(EditableDropdown::class);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2018-05-01 06:07:03 +02:00
|
|
|
// Empty string
|
|
|
|
if ($this->UseEmptyString) {
|
|
|
|
$field->setEmptyString($this->EmptyString ?: '');
|
|
|
|
}
|
|
|
|
|
2018-04-29 11:33:05 +02:00
|
|
|
// Set default
|
|
|
|
if ($this->Default) {
|
|
|
|
$field->setValue($this->Default);
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->doUpdateFormField($field);
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValueFromData($data)
|
|
|
|
{
|
2019-04-30 11:03:22 +02:00
|
|
|
if (!empty($data[$this->Name])) {
|
2016-07-21 07:53:59 +02:00
|
|
|
$source = $this->getFormField()->getSource();
|
|
|
|
return $source[$data[$this->Name]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIcon()
|
|
|
|
{
|
2017-11-02 22:44:45 +01:00
|
|
|
$resource = ModuleLoader::getModule('silverstripe/userforms')->getResource('images/editabledropdown.png');
|
|
|
|
|
|
|
|
if (!$resource->exists()) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resource->getURL();
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
|
|
|
|
{
|
|
|
|
return "$(\"select[name='{$this->Name}']\")";
|
|
|
|
}
|
|
|
|
}
|