Completed
Push — master ( c6b309...12981b )
by Robbie
25:33
created

model/editableformfields/EditableFieldGroupEnd.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Specifies that this ends a group of fields
5
 */
6
class EditableFieldGroupEnd extends EditableFormField
7
{
8
9
    private static $belongs_to = array(
10
        'Group' => 'EditableFieldGroup'
11
    );
12
13
    /**
14
     * Disable selection of group class
15
     *
16
     * @config
17
     * @var bool
18
     */
19
    private static $hidden = true;
20
21
    /**
22
     * Non-data type
23
     *
24
     * @config
25
     * @var bool
26
     */
27
    private static $literal = true;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $literal is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
29
    public function getCMSTitle()
30
    {
31
        $group = $this->Group();
0 ignored issues
show
Documentation Bug introduced by
The method Group does not exist on object<EditableFieldGroupEnd>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
32
        return _t(
33
            'EditableFieldGroupEnd.FIELD_GROUP_END',
34
            '{group} end',
35
            array(
36
                'group' => ($group && $group->exists()) ? $group->CMSTitle : 'Group'
37
            )
38
        );
39
    }
40
41
    public function getCMSFields()
42
    {
43
        $fields = parent::getCMSFields();
44
        $fields->removeByName(array('MergeField', 'Default', 'Validation', 'DisplayRules'));
45
        return $fields;
46
    }
47
48
    public function getInlineClassnameField($column, $fieldClasses)
49
    {
50
        return new LabelField($column, $this->CMSTitle);
51
    }
52
53
    public function getInlineTitleField($column)
54
    {
55
        return HiddenField::create($column);
56
    }
57
58
    public function getFormField()
59
    {
60
        return null;
61
    }
62
63
    public function showInReports()
64
    {
65
        return false;
66
    }
67
68 26
    public function onAfterWrite()
69
    {
70 26
        parent::onAfterWrite();
71
72
        // If this is not attached to a group, find the first group prior to this
73
        // with no end attached
74 26
        $group = $this->Group();
75 26
        if (!($group && $group->exists()) && $this->ParentID) {
76 1
            $group = EditableFieldGroup::get()
77 1
                ->filter(array(
78 1
                    'ParentID' => $this->ParentID,
79 1
                    'Sort:LessThanOrEqual' => $this->Sort
80 1
                ))
81 1
                ->where('"EditableFieldGroup"."EndID" IS NULL OR "EditableFieldGroup"."EndID" = 0')
82 1
                ->sort('"Sort" DESC')
83 1
                ->first();
84
85
            // When a group is found, attach it to this end
86 1
            if ($group) {
87
                $group->EndID = $this->ID;
88
                $group->write();
89
            }
90 1
        }
91 26
    }
92
93
    protected function onAfterDelete()
94
    {
95
        parent::onAfterDelete();
96
97
        // Delete group
98
        if (($group = $this->Group()) && $group->exists()) {
99
            $group->delete();
100
        }
101
    }
102
}
103