EditableCheckboxGroupField   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isCheckBoxField() 0 3 1
A getSelectorFieldOnly() 0 3 1
A isCheckBoxGroupField() 0 3 1
A getSelectorField() 0 8 2
A getFormField() 0 14 3
A getValueFromData() 0 18 6
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\UserForms\FormField\UserFormsCheckboxSetField;
6
use SilverStripe\UserForms\Model\EditableCustomRule;
7
8
/**
9
 * EditableCheckboxGroup
10
 *
11
 * Represents a set of selectable radio buttons
12
 *
13
 * @package userforms
14
 */
15
16
class EditableCheckboxGroupField extends EditableMultipleOptionField
17
{
18
    private static $singular_name = 'Checkbox Group';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
19
20
    private static $plural_name = 'Checkbox Groups';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
21
22
    protected $jsEventHandler = 'click';
23
24
    private static $table_name = 'EditableCheckboxGroupField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
25
26
    public function getFormField()
27
    {
28
        $field = UserFormsCheckboxSetField::create($this->Name, $this->Title ?: false, $this->getOptionsMap())
29
            ->setFieldHolderTemplate(EditableMultipleOptionField::class . '_holder')
30
            ->setTemplate(UserFormsCheckboxSetField::class);
31
32
        // Set the default checked items
33
        $defaultCheckedItems = $this->getDefaultOptions();
34
        if ($defaultCheckedItems->count()) {
35
            $field->setDefaultItems($defaultCheckedItems->map('Value')->keys());
36
        }
37
38
        $this->doUpdateFormField($field);
39
        return $field;
40
    }
41
42
    public function getValueFromData($data)
43
    {
44
        $result = '';
45
        $entries = (isset($data[$this->Name])) ? $data[$this->Name] : false;
46
47
        if ($entries) {
48
            if (!is_array($data[$this->Name])) {
49
                $entries = [$data[$this->Name]];
50
            }
51
            foreach ($entries as $selected => $value) {
52
                if (!$result) {
53
                    $result = $value;
54
                } else {
55
                    $result .= ', ' . $value;
56
                }
57
            }
58
        }
59
        return $result;
60
    }
61
62
    public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
63
    {
64
        // watch out for checkboxs as the inputs don't have values but are 'checked
65
        // @todo - Test this
66
        if ($rule->FieldValue) {
67
            return "$(\"input[name='{$this->Name}[]'][value='{$rule->FieldValue}']\")";
68
        } else {
69
            return "$(\"input[name='{$this->Name}[]']:first\")";
70
        }
71
    }
72
73
    public function isCheckBoxField()
74
    {
75
        return true;
76
    }
77
78
    public function getSelectorFieldOnly()
79
    {
80
        return "[name='{$this->Name}[]']";
81
    }
82
83
    public function isCheckBoxGroupField()
84
    {
85
        return true;
86
    }
87
}
88