2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
2017-08-09 01:55:09 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
2021-02-26 04:13:23 +01:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Security\Group;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
/**
|
|
|
|
* Creates an editable field that displays members in a given group
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* @package userforms
|
2021-02-26 04:13:23 +01:00
|
|
|
* @property int $GroupID
|
|
|
|
* @method Group Group()
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableMemberListField extends EditableFormField
|
|
|
|
{
|
|
|
|
private static $singular_name = 'Member List Field';
|
|
|
|
|
|
|
|
private static $plural_name = 'Member List Fields';
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $has_one = [
|
2017-08-09 01:55:09 +02:00
|
|
|
'Group' => Group::class
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'EditableMemberListField';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2022-03-03 04:47:45 +01:00
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
|
|
|
$fields->removeByName('Default');
|
|
|
|
$fields->removeByName('Validation');
|
|
|
|
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
DropdownField::create(
|
|
|
|
'GroupID',
|
|
|
|
_t('SilverStripe\\UserForms\\Model\\EditableFormField.GROUP', 'Group'),
|
|
|
|
Group::get()->map()
|
|
|
|
)->setEmptyString(' ')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFormField()
|
|
|
|
{
|
|
|
|
if (empty($this->GroupID)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$members = Member::map_in_groups($this->GroupID);
|
2018-03-23 02:11:39 +01:00
|
|
|
|
2018-03-23 02:04:48 +01:00
|
|
|
$field = DropdownField::create($this->Name, $this->Title ?: false, $members)
|
|
|
|
->setTemplate(EditableDropdown::class)
|
|
|
|
->setFieldHolderTemplate(EditableFormField::class . '_holder');
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->doUpdateFormField($field);
|
2018-03-23 02:04:48 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValueFromData($data)
|
|
|
|
{
|
|
|
|
if (isset($data[$this->Name])) {
|
|
|
|
$memberID = $data[$this->Name];
|
|
|
|
$member = Member::get()->byID($memberID);
|
2017-08-11 01:33:06 +02:00
|
|
|
return $member ? $member->getName() : '';
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|