Passed
Push — master ( ebdfbb...20570e )
by
unknown
02:32
created

EditableFormField/EditableCheckboxGroupField.php (5 issues)

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
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
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
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->EscapedTitle, $this->getOptionsMap())
0 ignored issues
show
Bug Best Practice introduced by
The property EscapedTitle does not exist on SilverStripe\UserForms\M...tableCheckboxGroupField. Since you implemented __get, consider adding a @property annotation.
Loading history...
$this->Name of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
        $field = UserFormsCheckboxSetField::create(/** @scrutinizer ignore-type */ $this->Name, $this->EscapedTitle, $this->getOptionsMap())
Loading history...
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