Passed
Pull Request — master (#123)
by Will
02:05
created

BaseElement::getIcon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace DNADesign\Elemental\Models;
4
5
use Exception;
6
use DNADesign\Elemental\Controllers\ElementController;
7
use SilverStripe\CMS\Controllers\CMSPageEditController;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Core\ClassInfo;
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Core\Manifest\ModuleResourceLoader;
14
use SilverStripe\Forms\CheckboxField;
15
use SilverStripe\Forms\DropdownField;
16
use SilverStripe\Forms\FieldGroup;
17
use SilverStripe\Forms\FieldList;
18
use SilverStripe\Forms\HiddenField;
19
use SilverStripe\Forms\NumericField;
20
use SilverStripe\Forms\ReadonlyField;
21
use SilverStripe\Forms\TextField;
22
use SilverStripe\Forms\GridField\GridField;
23
use SilverStripe\Forms\GridField\GridFieldConfig_RecordViewer;
24
use SilverStripe\Forms\GridField\GridFieldDataColumns;
25
use SilverStripe\Forms\GridField\GridFieldVersionedState;
26
use SilverStripe\Forms\GridField\GridFieldPageCount;
27
use SilverStripe\ORM\ArrayList;
28
use SilverStripe\ORM\CMSPreviewable;
29
use SilverStripe\ORM\DataObject;
30
use SilverStripe\ORM\DB;
31
use SilverStripe\ORM\FieldType\DBField;
32
use SilverStripe\ORM\Search\SearchContext;
33
use SilverStripe\Security\Permission;
34
use SilverStripe\Security\Member;
35
use SilverStripe\SiteConfig\SiteConfig;
36
use SilverStripe\Versioned\Versioned;
37
use SilverStripe\View\Parsers\URLSegmentFilter;
38
use SilverStripe\View\SSViewer;
39
40
class BaseElement extends DataObject implements CMSPreviewable
41
{
42
    /**
43
     * Override this on your custom elements to specify a cms icon
44
     *
45
     * @var string
46
     */
47
    private static $icon = 'dnadesign/silverstripe-elemental:images/base.svg';
0 ignored issues
show
Unused Code introduced by
The property $icon 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...
48
49
    private static $db = [
0 ignored issues
show
Unused Code introduced by
The property $db 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...
50
        'Title' => 'Varchar(255)',
51
        'ShowTitle' => 'Boolean',
52
        'Sort' => 'Int',
53
        'ExtraClass' => 'Varchar(255)',
54
        'Style' => 'Varchar(255)'
55
    ];
56
57
    private static $has_one = [
0 ignored issues
show
Unused Code introduced by
The property $has_one 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...
58
        'Parent' => ElementalArea::class
59
    ];
60
61
    private static $extensions = [
0 ignored issues
show
Unused Code introduced by
The property $extensions 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...
62
        Versioned::class
63
    ];
64
65
    private static $table_name = 'Element';
0 ignored issues
show
Unused Code introduced by
The property $table_name 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...
66
67
    /**
68
     * @var string
69
     */
70
    private static $controller_class = ElementController::class;
0 ignored issues
show
Unused Code introduced by
The property $controller_class 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...
71
72
    /**
73
     * @var string
74
     */
75
    private static $controller_template = 'ElementHolder';
0 ignored issues
show
Unused Code introduced by
The property $controller_template 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...
76
77
    /**
78
     * @var ElementController
79
     */
80
    protected $controller;
81
82
    private static $default_sort = 'Sort';
0 ignored issues
show
Unused Code introduced by
The property $default_sort 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...
83
84
    private static $singular_name = 'block';
0 ignored issues
show
Unused Code introduced by
The property $singular_name 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...
85
86
    private static $plural_name = 'blocks';
0 ignored issues
show
Unused Code introduced by
The property $plural_name 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...
87
88
    private static $summary_fields = [
0 ignored issues
show
Unused Code introduced by
The property $summary_fields 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...
89
        'EditorPreview' => 'Summary'
90
    ];
91
92
    /**
93
     * @var array
94
     */
95
    private static $styles = [];
0 ignored issues
show
Unused Code introduced by
The property $styles 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...
96
97
    private static $searchable_fields = [
0 ignored issues
show
Unused Code introduced by
The property $searchable_fields 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...
98
        'ID' => [
99
            'field' => NumericField::class,
100
        ],
101
        'Title',
102
        'LastEdited'
103
    ];
104
105
    /**
106
     * Enable for backwards compatibility
107
     *
108
     * @var boolean
109
     */
110
    private static $disable_pretty_anchor_name = false;
0 ignored issues
show
Unused Code introduced by
The property $disable_pretty_anchor_name 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...
111
112
    /**
113
     * Store used anchor names, this is to avoid title clashes
114
     * when calling 'getAnchor'
115
     *
116
     * @var array
117
     */
118
    protected static $_used_anchors = [];
119
120
    /**
121
     * For caching 'getAnchor'
122
     *
123
     * @var string
124
     */
125
    protected $_anchor = null;
126
127
    /**
128
     * Basic permissions, defaults to page perms where possible.
129
     *
130
     * @param Member $member
131
     *
132
     * @return boolean
133
     */
134 View Code Duplication
    public function canView($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        if ($this->hasMethod('getPage')) {
137
            if ($page = $this->getPage()) {
138
                return $page->canView($member);
139
            }
140
        }
141
142
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
143
    }
144
145
    /**
146
     * Basic permissions, defaults to page perms where possible.
147
     *
148
     * @param Member $member
149
     *
150
     * @return boolean
151
     */
152 View Code Duplication
    public function canEdit($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        if ($this->hasMethod('getPage')) {
155
            if ($page = $this->getPage()) {
156
                return $page->canEdit($member);
157
            }
158
        }
159
160
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
161
    }
162
163
    /**
164
     * Basic permissions, defaults to page perms where possible.
165
     *
166
     * Uses archive not delete so that current stage is respected i.e if a
167
     * element is not published, then it can be deleted by someone who doesn't
168
     * have publishing permissions.
169
     *
170
     * @param Member $member
171
     *
172
     * @return boolean
173
     */
174 View Code Duplication
    public function canDelete($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        if ($this->hasMethod('getPage')) {
177
            if ($page = $this->getPage()) {
178
                return $page->canArchive($member);
179
            }
180
        }
181
182
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
183
    }
184
185
    /**
186
     * Basic permissions, defaults to page perms where possible.
187
     *
188
     * @param Member $member
189
     * @param array $context
190
     *
191
     * @return boolean
192
     */
193
    public function canCreate($member = null, $context = array())
194
    {
195
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
196
    }
197
198
    /**
199
     *
200
     */
201
    public function onBeforeWrite()
202
    {
203
        parent::onBeforeWrite();
204
205
        if ($areaID = $this->ParentID) {
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on DNADesign\Elemental\Models\BaseElement. Since you implemented __get, consider adding a @property annotation.
Loading history...
206
            if ($elementalArea = ElementalArea::get()->byID($areaID)) {
207
                $elementalArea->write();
208
            }
209
        }
210
211
        if (!$this->Sort) {
212
            $parentID = ($this->ParentID) ? $this->ParentID : 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $parentID is dead and can be removed.
Loading history...
213
214
            $this->Sort = static::get()->max('Sort') + 1;
0 ignored issues
show
Bug Best Practice introduced by
The property Sort does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
215
        }
216
    }
217
218
    public function getCMSFields()
219
    {
220
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
221
            // Remove relationship fields
222
            $fields->removeByName('ParentID');
223
            $fields->removeByName('Sort');
224
225
            $fields->addFieldToTab(
226
                'Root.Settings',
227
                TextField::create('ExtraClass', _t(__CLASS__ . '.ExtraCssClassesLabel', 'Custom CSS classes'))
0 ignored issues
show
Bug introduced by
'ExtraClass' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

227
                TextField::create(/** @scrutinizer ignore-type */ 'ExtraClass', _t(__CLASS__ . '.ExtraCssClassesLabel', 'Custom CSS classes'))
Loading history...
228
                    ->setAttribute(
229
                        'placeholder',
230
                        _t(__CLASS__ . '.ExtraCssClassesPlaceholder', 'my_class another_class')
231
                    )
232
            );
233
234
            // Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group
235
            $fields->removeByName('ShowTitle');
236
            $fields->replaceField(
237
                'Title',
238
                FieldGroup::create(
239
                    TextField::create('Title', ''),
240
                    CheckboxField::create('ShowTitle', _t(__CLASS__ . '.ShowTitleLabel', 'Displayed'))
241
                )
242
                    ->setTemplate(__CLASS__ . '\\FieldGroup')
243
                    ->setTitle(_t(__CLASS__ . '.TitleLabel', 'Title (not displayed unless specified)'))
244
            );
245
246
            // Rename the "Main" tab
247
            $fields->fieldByName('Root.Main')
248
                ->setTitle(_t(__CLASS__ . '.MainTabLabel', 'Content'));
249
250
            // Remove divider lines on all block forms
251
            $fields->fieldByName('Root')->addExtraClass('form--no-dividers');
252
253
            $fields->addFieldsToTab('Root.Main', [
254
                HiddenField::create('AbsoluteLink', false, Director::absoluteURL($this->PreviewLink())),
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

254
                HiddenField::create('AbsoluteLink', /** @scrutinizer ignore-type */ false, Director::absoluteURL($this->PreviewLink())),
Loading history...
255
                HiddenField::create('LiveLink', false, Director::absoluteURL($this->Link())),
256
                HiddenField::create('StageLink', false, Director::absoluteURL($this->PreviewLink())),
257
            ]);
258
259
            $styles = $this->config()->get('styles');
260
261
            if ($styles && count($styles) > 0) {
262
                $fields->insertBefore($styles = new DropdownField(
263
                    'Style', _t(__CLASS__.'.STYLE', 'Style variation'), $styles
264
                ), 'ExtraClass');
0 ignored issues
show
Bug introduced by
'ExtraClass' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

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

264
                ), /** @scrutinizer ignore-type */ 'ExtraClass');
Loading history...
265
266
                $styles->setEmptyString(_t(__CLASS__.'.CUSTOM_STYLES', 'Select a style..'));
267
            } else {
268
                $fields->removeByName('Style');
269
            }
270
271
            $fields->addFieldsToTab('Root.History', $this->getHistoryFields());
0 ignored issues
show
Bug introduced by
$this->getHistoryFields() of type SilverStripe\Forms\FieldList is incompatible with the type array expected by parameter $fields of SilverStripe\Forms\FieldList::addFieldsToTab(). ( Ignorable by Annotation )

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

271
            $fields->addFieldsToTab('Root.History', /** @scrutinizer ignore-type */ $this->getHistoryFields());
Loading history...
272
        });
273
274
        return parent::getCMSFields();
275
    }
276
277
    /**
278
     * Returns the history fields for this element.
279
     *
280
     * @return FieldList
281
     */
282
    public function getHistoryFields()
283
    {
284
        $config = GridFieldConfig_RecordViewer::create();
285
        $config->removeComponentsByType(GridFieldPageCount::class);
286
287
        $config->getComponentByType(GridFieldDataColumns::class)->setDisplayFields([
288
            'Version' => '#',
289
            'LastEdited' => _t(__CLASS__ . '.LastEdited', 'Last Edited'),
290
            'getAuthor.Name' => _t(__CLASS__ . '.Author', 'Author')
291
        ]);
292
293
        $history = Versioned::get_all_versions(__CLASS__, $this->ID);
294
        $history = $history->sort('Version', 'DESC');
295
296
        return new FieldList(
297
            GridField::create(
298
                'History',
0 ignored issues
show
Bug introduced by
'History' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

298
                /** @scrutinizer ignore-type */ 'History',
Loading history...
299
                '',
300
                $history,
0 ignored issues
show
Bug introduced by
$history of type SilverStripe\ORM\DataList is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

300
                /** @scrutinizer ignore-type */ $history,
Loading history...
301
                $config
302
            )
303
        );
304
    }
305
306
    /**
307
     * Get the type of the current block, for use in GridField summaries, block
308
     * type dropdowns etc. Examples are "Content", "File", "Media", etc.
309
     *
310
     * @return string
311
     */
312
    public function getType()
313
    {
314
        return _t(__CLASS__ . '.BlockType', 'Block');
315
    }
316
317
    /**
318
     * @param ElementController
319
     *
320
     * @return $this
321
     */
322
    public function setController($controller)
323
    {
324
        $this->controller = $controller;
325
326
        return $this;
327
    }
328
329
    /**
330
     * @throws Exception
331
     *
332
     * @return ElementController
333
     */
334
    public function getController()
335
    {
336
        if ($this->controller) {
337
            return $this->controller;
338
        }
339
340
        $controllerClass = self::config()->controller_class;
341
342
        if (!class_exists($controllerClass)) {
343
            throw new Exception('Could not find controller class ' . $controllerClass . ' as defined in ' . static::class);
344
        }
345
346
        $this->controller = Injector::inst()->create($controllerClass, $this);
347
        $this->controller->doInit();
348
349
        return $this->controller;
350
    }
351
352
    /**
353
     * @return Controller
354
     */
355
    public function Top()
356
    {
357
        return (Controller::has_curr()) ? Controller::curr() : null;
358
    }
359
360
    /**
361
     * Default way to render element in templates. Note that all blocks should
362
     * be rendered through their {@link ElementController} class as this
363
     * contains the holder styles.
364
     *
365
     * @return string HTML
366
     */
367
    public function forTemplate($holder = true)
0 ignored issues
show
Unused Code introduced by
The parameter $holder is not used and could be removed. ( Ignorable by Annotation )

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

367
    public function forTemplate(/** @scrutinizer ignore-unused */ $holder = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
368
    {
369
        $templates = $this->getRenderTemplates();
370
371
        if ($templates) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $templates of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
372
            return $this->renderWith($templates);
373
        }
374
    }
375
376
    /**
377
     * @param string $suffix
378
     *
379
     * @return array
380
     */
381
    public function getRenderTemplates($suffix = '')
382
    {
383
        $classes = ClassInfo::ancestry($this->ClassName);
384
        $classes[static::class] = static::class;
385
        $classes = array_reverse($classes);
386
        $templates = array();
387
388
        foreach ($classes as $key => $value) {
389
            if ($value == BaseElement::class) {
390
                continue;
391
            }
392
393
            if ($value == DataObject::class) {
394
                break;
395
            }
396
397
            $templates[] = $value . $suffix;
398
        }
399
400
        return $templates;
401
    }
402
403
    /**
404
     * Strip all namespaces from class namespace.
405
     *
406
     * @param string $classname e.g. "\Fully\Namespaced\Class"
407
     *
408
     * @return string following the param example, "Class"
409
     */
410
    protected function stripNamespacing($classname)
411
    {
412
        $classParts = explode('\\', $classname);
413
        return array_pop($classParts);
414
    }
415
416
    /**
417
     * @return string
418
     */
419
    public function getSimpleClassName()
420
    {
421
        return strtolower($this->sanitiseClassName($this->ClassName, '__'));
422
    }
423
424
    /**
425
     * @return SiteTree
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\Models\SiteTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
426
     */
427
    public function getPage()
428
    {
429
        $area = $this->Parent();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on DNADesign\Elemental\Models\BaseElement. 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

429
        /** @scrutinizer ignore-call */ 
430
        $area = $this->Parent();
Loading history...
430
431
        if ($area instanceof ElementalArea) {
432
            return $area->getOwnerPage();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $area->getOwnerPage() could also return false which is incompatible with the documented return type DNADesign\Elemental\Models\SiteTree. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
433
        }
434
435
        return null;
436
    }
437
438
    /**
439
     * Get a unique anchor name
440
     *
441
     * @return string
442
     */
443
    public function getAnchor()
444
    {
445
        if ($this->_anchor !== null) {
446
            return $this->_anchor;
447
        }
448
449
        $anchorTitle = '';
450
451
        if (!$this->config()->disable_pretty_anchor_name) {
452
            if ($this->hasMethod('getAnchorTitle')) {
453
                $anchorTitle = $this->getAnchorTitle();
454
            } elseif ($this->config()->enable_title_in_template) {
455
                $anchorTitle = $this->getField('Title');
456
            }
457
        }
458
459
        if (!$anchorTitle) {
460
            $anchorTitle = 'e'.$this->ID;
461
        }
462
463
        $filter = URLSegmentFilter::create();
464
        $titleAsURL = $filter->filter($anchorTitle);
465
466
        // Ensure that this anchor name isn't already in use
467
        // ie. If two elemental blocks have the same title, it'll append '-2', '-3'
468
        $result = $titleAsURL;
469
        $count = 1;
470
        while (isset(self::$_used_anchors[$result]) && self::$_used_anchors[$result] !== $this->ID) {
471
            ++$count;
472
            $result = $titleAsURL.'-'.$count;
473
        }
474
        self::$_used_anchors[$result] = $this->ID;
475
        return $this->_anchor = $result;
476
    }
477
478
    /**
479
     * @param string $action
480
     *
481
     * @return string
482
     */
483
    public function AbsoluteLink($action = null)
484
    {
485
        if ($page = $this->getPage()) {
486
            $link = $page->AbsoluteLink($action) . '#' . $this->getAnchor();
487
488
            return $link;
489
        }
490
    }
491
492
    /**
493
     * @param string $action
494
     *
495
     * @return string
496
     */
497
    public function Link($action = null)
498
    {
499
        if ($page = $this->getPage()) {
500
            $link = $page->Link($action) . '#' . $this->getAnchor();
501
502
            $this->extend('updateLink', $link);
503
504
            return $link;
505
        }
506
    }
507
508
    /**
509
     * @param string $action
510
     *
511
     * @return string
512
     */
513
    public function PreviewLink($action = null)
514
    {
515
        $action = $action . '?ElementalPreview=' . mt_rand();
516
        $link = $this->Link($action);
517
        $this->extend('updatePreviewLink', $link);
518
519
        return $link;
520
    }
521
522
    /**
523
     * @return boolean
524
     */
525
    public function isCMSPreview()
526
    {
527
        if (Controller::has_curr()) {
528
            $c = Controller::curr();
529
530
            if ($c->getRequest()->requestVar('CMSPreview')) {
531
                return true;
532
            }
533
        }
534
535
        return false;
536
    }
537
538
    /**
539
     * @return string
540
     */
541
    public function CMSEditLink()
542
    {
543
        $relationName = $this->getAreaRelationName();
544
545
        $link = Controller::join_links(
546
            singleton(CMSPageEditController::class)->Link('EditForm'),
547
            $this->getPage(true)->ID,
0 ignored issues
show
Unused Code introduced by
The call to DNADesign\Elemental\Models\BaseElement::getPage() has too many arguments starting with true. ( Ignorable by Annotation )

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

547
            $this->/** @scrutinizer ignore-call */ 
548
                   getPage(true)->ID,

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
548
            'field/' . $relationName . '/item/',
549
            $this->ID
550
        );
551
552
        if ($inList) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $inList seems to be never defined.
Loading history...
553
            return $link;
554
        }
555
556
        return Controller::join_links(
557
            $link,
558
            'edit'
559
        );
560
    }
561
562
    /**
563
     * Retrieve a elemental area relation for creating cms links
564
     *
565
     * @return string - the name of a valid elemental area relation
566
     */
567
    public function getAreaRelationName()
568
    {
569
        $page = $this->getPage(true);
0 ignored issues
show
Unused Code introduced by
The call to DNADesign\Elemental\Models\BaseElement::getPage() has too many arguments starting with true. ( Ignorable by Annotation )

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

569
        /** @scrutinizer ignore-call */ 
570
        $page = $this->getPage(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
570
        $has_one = $page->config()->get('has_one');
571
        $area = $this->Parent();
572
573
        foreach ($has_one as $relationName => $relationClass) {
574
            if ($relationClass === $area->ClassName) {
575
                return $relationName;
576
            }
577
        }
578
579
        return 'ElementalArea';
580
    }
581
582
    /**
583
     * Sanitise a model class' name for inclusion in a link.
584
     *
585
     * @return string
586
     */
587
    protected function sanitiseClassName($class, $delimiter = '-')
588
    {
589
        return str_replace('\\', $delimiter, $class);
590
    }
591
592
    /**
593
     * @return string
594
     */
595
    public function getEditLink()
596
    {
597
        return $this->CMSEditLink();
598
    }
599
600
    /**
601
     * @return HTMLText
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\Models\HTMLText was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
602
     */
603
    public function PageCMSEditLink()
604
    {
605
        if ($page = $this->getPage()) {
606
            return DBField::create_field('HTMLText', sprintf(
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\ORM\...tLink(), $page->Title)) returns the type SilverStripe\ORM\FieldType\DBField which is incompatible with the documented return type DNADesign\Elemental\Models\HTMLText.
Loading history...
607
                '<a href="%s">%s</a>',
608
                $page->CMSEditLink(),
609
                $page->Title
610
            ));
611
        }
612
    }
613
614
    /**
615
     * @return string
616
     */
617
    public function getMimeType()
618
    {
619
        return 'text/html';
620
    }
621
622
    /**
623
     * This can be overridden on child elements to create a summary for display
624
     * in GridFields.
625
     *
626
     * @return string
627
     */
628
    public function getSummary()
629
    {
630
        return '';
631
    }
632
633
634
    /**
635
     * Generate markup for element type icons suitable for use in GridFields.
636
     *
637
     * @return DBField
638
     */
639
    public function getIcon()
640
    {
641
        $icon = $this->config()->get('icon');
642
643
        if ($icon) {
644
            $icon = ModuleResourceLoader::resourceURL($icon);
645
646
            return DBField::create_field('HTMLVarchar', '<img width="16px" src="' . Director::absoluteBaseURL() . $icon . '" alt="" />');
647
        }
648
    }
649
650
    /**
651
     * Generate markup for element type, with description suitable for use in
652
     * GridFields.
653
     *
654
     * @return DBField
655
     */
656
    public function getTypeNice()
657
    {
658
        $description = $this->config()->get('description');
659
660
        return DBField::create_field(
661
            'HTMLVarchar',
662
            $this->getType() . ' <span class="el-description"> &mdash; ' . $description . '</span>'
663
        );
664
    }
665
666
    /**
667
     * @return HTMLText
668
     */
669
    public function getEditorPreview()
670
    {
671
        $templates = $this->getRenderTemplates('_EditorPreview');
672
        $templates[] = BaseElement::class . '_EditorPreview';
673
674
        return $this->renderWith($templates);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->renderWith($templates) returns the type SilverStripe\ORM\FieldType\DBHTMLText which is incompatible with the documented return type DNADesign\Elemental\Models\HTMLText.
Loading history...
675
    }
676
677
    /**
678
     * @return Member
679
     */
680
    public function getAuthor()
681
    {
682
        if ($this->AuthorID) {
0 ignored issues
show
Bug Best Practice introduced by
The property AuthorID does not exist on DNADesign\Elemental\Models\BaseElement. Since you implemented __get, consider adding a @property annotation.
Loading history...
683
            return Member::get()->byId($this->AuthorID);
684
        }
685
    }
686
687
    /**
688
     * @return string
689
     */
690
    public function getStyleVariant()
691
    {
692
        $style = $this->Style;
0 ignored issues
show
Bug Best Practice introduced by
The property Style does not exist on DNADesign\Elemental\Models\BaseElement. Since you implemented __get, consider adding a @property annotation.
Loading history...
693
694
        if (isset($styles[$style])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $styles does not exist. Did you maybe mean $style?
Loading history...
695
            $style = strtolower($styles[$style]);
696
        } else {
697
            $style = '';
698
        }
699
700
        $this->extend('updateStyleVariant', $style);
701
702
        return $style;
703
    }
704
705
}
706