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;
|
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
|
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()
|
|
|
|
{
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
|
|
|
|
$fields->removeByName('Default');
|
|
|
|
$fields->removeByName('Validation');
|
|
|
|
|
2017-10-25 23:00:02 +02:00
|
|
|
/** @skipUpgrade */
|
2016-07-21 07:53:59 +02:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
DropdownField::create(
|
2017-08-11 01:33:06 +02:00
|
|
|
'GroupID',
|
2017-10-25 23:00:02 +02:00
|
|
|
_t('SilverStripe\\UserForms\\Model\\EditableFormField.GROUP', 'Group'),
|
2016-07-21 07:53:59 +02:00
|
|
|
Group::get()->map()
|
|
|
|
)->setEmptyString(' ')
|
|
|
|
);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFormField()
|
|
|
|
{
|
|
|
|
if (empty($this->GroupID)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$members = Member::map_in_groups($this->GroupID);
|
2018-03-23 04:23:51 +01:00
|
|
|
$field = DropdownField::create($this->Name, $this->Title ?: false, $members);
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->doUpdateFormField($field);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|