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 {
private static $singular_name = 'Member List Field';
2009-04-27 06:32:47 +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-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;
}
public function getFormField() {
2015-07-24 04:37:48 +02:00
if(empty($this->GroupID)) {
return false;
}
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;
}
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() : "";
}
return false;
}
}