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

Model/EditableFormField/EditableFieldGroupEnd.php (5 issues)

1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\Forms\HiddenField;
6
use SilverStripe\Forms\LabelField;
7
use SilverStripe\Security\Group;
8
use SilverStripe\UserForms\Model\EditableFormField;
9
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;
10
11
/**
12
 * Specifies that this ends a group of fields
13
 */
14
class EditableFieldGroupEnd extends EditableFormField
15
{
16
    private static $belongs_to = [
17
        'Group' => EditableFieldGroup::class
18
    ];
19
20
    /**
21
     * Disable selection of group class
22
     *
23
     * @config
24
     * @var bool
25
     */
26
    private static $hidden = true;
0 ignored issues
show
The private property $hidden is not used, and could be removed.
Loading history...
27
28
    /**
29
     * Non-data type
30
     *
31
     * @config
32
     * @var bool
33
     */
34
    private static $literal = true;
0 ignored issues
show
The private property $literal is not used, and could be removed.
Loading history...
35
36
    private static $table_name = 'EditableFieldGroupEnd';
37
38
    public function getCMSTitle()
39
    {
40
        $group = $this->Group();
0 ignored issues
show
The method Group() does not exist on SilverStripe\UserForms\M...d\EditableFieldGroupEnd. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

40
        /** @scrutinizer ignore-call */ 
41
        $group = $this->Group();
Loading history...
41
        return _t(
42
            __CLASS__.'.FIELD_GROUP_END',
43
            '{group} end',
44
            [
45
                'group' => ($group && $group->exists()) ? $group->CMSTitle : Group::class
46
            ]
47
        );
48
    }
49
50
    public function getCMSFields()
51
    {
52
        $fields = parent::getCMSFields();
53
        $fields->removeByName(['MergeField', 'Default', 'Validation', 'DisplayRules']);
54
        return $fields;
55
    }
56
57
    public function getInlineClassnameField($column, $fieldClasses)
58
    {
59
        return LabelField::create($column, $this->CMSTitle);
60
    }
61
62
    public function getInlineTitleField($column)
63
    {
64
        return HiddenField::create($column);
65
    }
66
67
    public function getFormField()
68
    {
69
        return null;
70
    }
71
72
    public function showInReports()
73
    {
74
        return false;
75
    }
76
77
    public function onAfterWrite()
78
    {
79
        parent::onAfterWrite();
80
81
        // If this is not attached to a group, find the first group prior to this
82
        // with no end attached
83
        $group = $this->Group();
84
        if (!($group && $group->exists()) && $this->ParentID) {
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on SilverStripe\UserForms\M...d\EditableFieldGroupEnd. Since you implemented __get, consider adding a @property annotation.
Loading history...
85
            $group = EditableFieldGroup::get()
86
                ->filter([
87
                    'ParentID' => $this->ParentID,
88
                    'Sort:LessThanOrEqual' => $this->Sort
89
                ])
90
                ->where('"EditableFieldGroup"."EndID" IS NULL OR "EditableFieldGroup"."EndID" = 0')
91
                ->sort('"Sort" DESC')
92
                ->first();
93
94
            // When a group is found, attach it to this end
95
            if ($group) {
0 ignored issues
show
The condition $group can never be true.
Loading history...
96
                $group->EndID = $this->ID;
97
                $group->write();
98
            }
99
        }
100
    }
101
}
102