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