silverstripe-userforms/code/model/editableformfields/EditableMemberListField.php

59 lines
1.2 KiB
PHP
Raw Normal View History

<?php
/**
* Creates an editable field that displays members in a given group
*
* @package userforms
*/
class EditableMemberListField extends EditableFormField {
2015-09-11 00:20:06 +02:00
private static $singular_name = 'Member List Field';
2015-09-11 00:20:06 +02:00
private static $plural_name = 'Member List Fields';
2015-07-24 04:37:48 +02:00
private static $has_one = array(
'Group' => 'Group'
);
/**
* @return FieldList
*/
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('Default');
$fields->removeByName('Validation');
2015-09-11 00:20:06 +02:00
2015-07-24 04:37:48 +02:00
$fields->addFieldToTab(
'Root.Main',
DropdownField::create(
"GroupID",
_t('EditableFormField.GROUP', 'Group'),
Group::get()->map()
)->setEmptyString(' ')
2009-04-27 08:17:02 +02:00
);
2015-07-24 04:37:48 +02:00
2009-04-27 08:17:02 +02:00
return $fields;
}
2015-09-11 00:20:06 +02:00
public function getFormField() {
2015-07-24 04:37:48 +02:00
if(empty($this->GroupID)) {
return false;
}
2015-09-11 00:20:06 +02:00
2015-07-24 04:37:48 +02:00
$members = Member::map_in_groups($this->GroupID);
$field = new DropdownField($this->Name, $this->EscapedTitle, $members);
$this->doUpdateFormField($field);
return $field;
}
2015-09-11 00:20:06 +02:00
public function getValueFromData($data) {
if(isset($data[$this->Name])) {
2015-07-24 04:37:48 +02:00
$memberID = $data[$this->Name];
$member = Member::get()->byID($memberID);
return $member ? $member->getName() : "";
}
2015-09-11 00:20:06 +02:00
return false;
}
}