Completed
Push — master ( 59c799...149ad0 )
by Will
02:15
created

code/models/BaseElement.php (2 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
 * @package elemental
5
 */
6
class BaseElement extends Widget
7
{
8
    /**
9
     * @var array $db
10
     */
11
    private static $db = array(
12
        'ExtraClass' => 'Varchar(255)',
13
        'HideTitle' => 'Boolean'
14
    );
15
16
    /**
17
     * @var array $has_one
18
     */
19
    private static $has_one = array(
20
        'List' => 'ElementList' // optional.
21
    );
22
23
    /**
24
     * @var array $has_many
25
     */
26
    private static $has_many = array(
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 $has_many 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...
27
        'VirtualClones' => 'ElementVirtualLinked'
28
    );
29
30
    /**
31
     * @var string
32
     */
33
    private static $title = "Content Block";
34
35
    /**
36
     * @var string
37
     */
38
    private static $singular_name = 'Content Block';
39
40
    /**
41
     * @var array
42
     */
43
    private static $summary_fields = array(
44
        'ID' => 'ID',
45
        'Title' => 'Title',
46
        'ElementType' => 'Type'
47
    );
48
49
    /**
50
     * @var boolean
51
     */
52
    protected $enable_title_in_template = false;
53
54
55
    public function getCMSFields()
56
    {
57
        $fields = $this->scaffoldFormFields(array(
58
            'includeRelations' => ($this->ID > 0),
59
            'tabbed' => true,
60
            'ajaxSafe' => true
61
        ));
62
63
        $fields->insertAfter(new ReadonlyField('ClassNameTranslated', _t('BaseElement.TYPE', 'Type'), $this->i18n_singular_name()), 'Title');
64
        $fields->removeByName('ListID');
65
        $fields->removeByName('ParentID');
66
        $fields->removeByName('Sort');
67
        $fields->removeByName('ExtraClass');
68
69
        if (!$this->enable_title_in_template) {
70
            $fields->removeByName('HideTitle');
71
            $title = $fields->fieldByName('Root.Main.Title');
72
73
            if ($title) {
74
                $title->setRightTitle('For reference only. Does not appear in the template.');
75
            }
76
        }
77
78
        $fields->addFieldToTab('Root.Settings', new TextField('ExtraClass', 'Extra CSS Classes to add'));
79
80
        if (!is_a($this, 'ElementList')) {
81
            $lists = ElementList::get()->filter('ParentID', $this->ParentID);
82
83
            if ($lists->exists()) {
84
                $fields->addFieldToTab('Root.Main',
85
                    $move = new DropdownField('MoveToListID', 'Move this to another list', $lists->map('ID', 'CMSTitle'), '')
86
                );
87
88
                $move->setEmptyString('Select a list..');
89
                $move->setHasEmptyDefault(true);
90
            }
91
        }
92
93
        $this->extend('updateCMSFields', $fields);
94
95
        if ($this->IsInDB()) {
96
            if ($this->isEndofLine('BaseElement') && $this->hasExtension('VersionViewerDataObject')) {
97
                $fields = $this->addVersionViewer($fields, $this);
98
            }
99
        }
100
101
        return $fields;
102
    }
103
104
    /**
105
     * Version viewer must only be added at if this is the final getCMSFields for a class.
106
     * in order to avoid having to rename all fields from eg Root.Main to Root.Current.Main
107
     * To do this we test if getCMSFields is from the current class
108
     */
109
    public function isEndofLine($className)
110
    {
111
        $methodFromClass = ClassInfo::has_method_from(
112
            $this->ClassName, 'getCMSFields', $className
113
        );
114
115
        if($methodFromClass) {
116
            return true;
117
        }
118
    }
119
120
121
    public function onBeforeWrite()
122
    {
123
        parent::onBeforeWrite();
124
125
        if (!$this->Sort) {
126
            $parentID = ($this->ParentID) ? $this->ParentID : 0;
127
128
            $this->Sort = DB::query("SELECT MAX(\"Sort\") + 1 FROM \"Widget\" WHERE \"ParentID\" = $parentID")->value();
129
        }
130
131
        if ($this->MoveToListID) {
132
            $this->ListID = $this->MoveToListID;
133
        }
134
    }
135
136
    public function i18n_singular_name()
137
    {
138
        return _t(__CLASS__, $this->config()->title);
139
    }
140
141
    public function getElementType()
142
    {
143
        return $this->i18n_singular_name();
144
    }
145
146 View Code Duplication
    public function getTitle()
147
    {
148
        if ($title = $this->getField('Title')) {
149
            return $title;
150
        } else {
151
            if (!$this->isInDb()) {
152
                return;
153
            }
154
155
            return $this->config()->title;
156
        }
157
    }
158
159 View Code Duplication
    public function getCMSTitle()
160
    {
161
        if ($title = $this->getField('Title')) {
162
            return $this->config()->title . ': ' . $title;
163
        } else {
164
            if (!$this->isInDb()) {
165
                return;
166
            }
167
            return $this->config()->title;
168
        }
169
    }
170
171
    public function canView($member = null)
172
    {
173
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
174
    }
175
176
    public function canEdit($member = null)
177
    {
178
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
179
    }
180
181
    public function canDelete($member = null)
182
    {
183
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
184
    }
185
186
    public function canCreate($member = null)
187
    {
188
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
189
    }
190
191
    public function ControllerTop()
192
    {
193
        return Controller::curr();
194
    }
195
196
    public function getPage()
197
    {
198
        $area = $this->Parent();
199
200
        if ($area instanceof ElementalArea) {
201
            return $area->getOwnerPage();
202
        }
203
204
        return null;
205
    }
206
207
    /**
208
     * Override the {@link Widget::forTemplate()} method so that holders are not rendered twice. The controller should
209
     * render with widget inside the
210
     *
211
     * @return HTML
212
     */
213
    public function forTemplate($holder = true) {
214
        return $this->renderWith($this->class);
215
    }
216
217
    /**
218
     * @return string
219
     */
220
    public function getEditLink() {
221
        return Controller::join_links(
222
            Director::absoluteBaseURL(),
223
            'admin/elemental/BaseElement/EditForm/field/BaseElement/item',
224
            $this->ID,
225
            'edit'
226
        );
227
    }
228
}
229