Completed
Pull Request — master (#25)
by Robbie
06:58
created

code/models/BaseElement.php (1 issue)

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
 * @package elemental
5
 */
6
class BaseElement extends Widget
7
{
8
9
    private static $db = array(
10
        'ExtraClass' => 'Varchar(255)',
11
        'HideTitle' => 'Boolean'
12
    );
13
14
    private static $has_one = array(
15
        'List' => 'ElementList' // optional.
16
    );
17
18
    /**
19
     * @var string
20
    */
21
    private static $title = "Base Element";
22
23
    /**
24
     * @var array
25
     */
26
    private static $summary_fields = array(
27
        'ID',
28
        'Title',
29
        'ElementType'
30
    );
31
32
    /**
33
    * @var string
34
    */
35
    private static $description = "Base class for elements";
36
37
    /**
38
    * @var boolean
39
    */
40
    protected $enable_title_in_template = false;
41
42
43
    public function getCMSFields()
44
    {
45
        $fields = $this->scaffoldFormFields(array(
46
            'includeRelations' => ($this->ID > 0),
47
            'tabbed' => true,
48
            'ajaxSafe' => true
49
        ));
50
51
        $fields->insertAfter(new ReadonlyField('Type'), 'Title');
52
        $fields->removeByName('ListID');
53
        $fields->removeByName('ParentID');
54
        $fields->removeByName('Sort');
55
        $fields->removeByName('ExtraClass');
56
57
        if (!$this->enable_title_in_template) {
58
            $fields->removeByName('HideTitle');
59
            $title = $fields->fieldByName('Root.Main.Title');
60
            if ($title) {
61
                $title->setRightTitle('For reference only. Does not appear in the template.');
62
            }
63
        }
64
65
        $fields->addFieldToTab('Root.Settings', new TextField('ExtraClass', 'Extra CSS Classes to add'));
66
67
        if (!is_a($this, 'ElementList')) {
68
            $lists = ElementList::get()->filter('ParentID', $this->ParentID);
69
70
            if ($lists->exists()) {
71
                $fields->addFieldToTab('Root.Main',
72
                    $move = new DropdownField('MoveToListID', 'Move this to another list', $lists->map('ID', 'CMSTitle'), '')
73
                );
74
75
                $move->setEmptyString('Select a list..');
76
                $move->setHasEmptyDefault(true);
77
            }
78
        }
79
80
        $this->extend('updateCMSFields', $fields);
81
82
        if ($this->IsInDB()) {
83
            if ($this->isEndofLine('BaseElement') && $this->hasExtension('VersionViewerDataObject')) {
84
                $fields = $this->addVersionViewer($fields, $this);
85
            }
86
        }
87
88
        return $fields;
89
    }
90
91
    /**
92
     * Version viewer must only be added at if this is the final getCMSFields for a class.
93
     * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main
94
     * To do this we test if getCMSFields is from the current class
95
     */
96
    public function isEndofLine($className)
97
    {
98
        $methodFromClass = ClassInfo::has_method_from(
99
            $this->ClassName, 'getCMSFields', $className
100
        );
101
102
        if($methodFromClass) {
103
            return true;
104
        }
105
    }
106
107
108
    public function onBeforeWrite()
109
    {
110
        parent::onBeforeWrite();
111
112
        if (!$this->Sort) {
113
            $parentID = ($this->ParentID) ? $this->ParentID : 0;
114
115
            $this->Sort = DB::query("SELECT MAX(\"Sort\") + 1 FROM \"Widget\" WHERE \"ParentID\" = $parentID")->value();
116
        }
117
118
        if ($this->MoveToListID) {
119
            $this->ListID = $this->MoveToListID;
120
        }
121
    }
122
123
    public function i18n_singular_name()
124
    {
125
        return _t(__CLASS__, $this->config()->title);
126
    }
127
128
    public function getElementType()
129
    {
130
        return $this->i18n_singular_name();
131
    }
132
133 View Code Duplication
    public function getTitle()
134
    {
135
        if ($title = $this->getField('Title')) {
136
            return $title;
137
        } else {
138
            if (!$this->isInDb()) {
139
                return;
140
            }
141
142
            return $this->config()->title;
143
        }
144
    }
145
146 View Code Duplication
    public function getCMSTitle()
147
    {
148
        if ($title = $this->getField('Title')) {
149
            return $this->config()->title . ': ' . $title;
150
        } else {
151
            if (!$this->isInDb()) {
152
                return;
153
            }
154
            return $this->config()->title;
155
        }
156
    }
157
158
    public function canView($member = null)
159
    {
160
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
161
    }
162
163
    public function canEdit($member = null)
164
    {
165
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
166
    }
167
168
    public function canDelete($member = null)
169
    {
170
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
171
    }
172
173
    public function canCreate($member = null)
174
    {
175
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
176
    }
177
178
    public function ControllerTop()
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
179
    {
180
        return Controller::curr();
181
    }
182
183
    public function getPage()
184
    {
185
        $area = $this->Parent();
186
187
        if ($area instanceof ElementalArea) {
188
            return $area->getOwnerPage();
189
        }
190
191
        return null;
192
    }
193
194
    /**
195
     * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should
196
     * render with widget inside the
197
     *
198
     * @return HTML
199
     */
200
    public function forTemplate($holder = true) {
201
        return $this->renderWith($this->class);
202
    }
203
}
204