EditableMemberListField   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 30
c 1
b 1
f 0
dl 0
loc 61
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 18 1
A getValueFromData() 0 9 3
A getFormField() 0 15 3
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Forms\DropdownField;
6
use SilverStripe\Security\Group;
7
use SilverStripe\Security\Member;
8
use SilverStripe\UserForms\Model\EditableFormField;
9
10
/**
11
 * Creates an editable field that displays members in a given group
12
 *
13
 * @package userforms
14
 */
15
class EditableMemberListField extends EditableFormField
16
{
17
    private static $singular_name = 'Member List Field';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
18
19
    private static $plural_name = 'Member List Fields';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
20
21
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
22
        'Group' => Group::class
23
    ];
24
25
    private static $table_name = 'EditableMemberListField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
26
27
    /**
28
     * @return FieldList
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...ableFormField\FieldList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     */
30
    public function getCMSFields()
31
    {
32
        $fields = parent::getCMSFields();
33
34
        $fields->removeByName('Default');
35
        $fields->removeByName('Validation');
36
37
        /** @skipUpgrade */
38
        $fields->addFieldToTab(
39
            'Root.Main',
40
            DropdownField::create(
41
                'GroupID',
42
                _t('SilverStripe\\UserForms\\Model\\EditableFormField.GROUP', 'Group'),
43
                Group::get()->map()
44
            )->setEmptyString(' ')
45
        );
46
47
        return $fields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fields returns the type SilverStripe\Forms\FieldList which is incompatible with the documented return type SilverStripe\UserForms\M...ableFormField\FieldList.
Loading history...
48
    }
49
50
    public function getFormField()
51
    {
52
        if (empty($this->GroupID)) {
0 ignored issues
show
Bug Best Practice introduced by
The property GroupID does not exist on SilverStripe\UserForms\M...EditableMemberListField. Since you implemented __get, consider adding a @property annotation.
Loading history...
53
            return false;
54
        }
55
56
        $members = Member::map_in_groups($this->GroupID);
57
58
        $field = DropdownField::create($this->Name, $this->Title ?: false, $members)
59
            ->setTemplate(EditableDropdown::class)
60
            ->setFieldHolderTemplate(EditableFormField::class . '_holder');
61
62
        $this->doUpdateFormField($field);
63
64
        return $field;
65
    }
66
67
    public function getValueFromData($data)
68
    {
69
        if (isset($data[$this->Name])) {
70
            $memberID = $data[$this->Name];
71
            $member = Member::get()->byID($memberID);
72
            return $member ? $member->getName() : '';
0 ignored issues
show
introduced by
$member is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
73
        }
74
75
        return false;
76
    }
77
}
78