Passed
Push — master ( 948a64...9ad73f )
by Nicolaas
02:30
created

AvoidChildDeletionExtension::canArchive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sunnysideup\AvoidChildDeletion\Extensions;
4
5
use SilverStripe\CMS\Controllers\RootURLController;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Controllers\RootURLController 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...
6
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\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...
7
use SilverStripe\CMS\Model\SiteTreeExtension;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTreeExtension 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...
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\LiteralField;
10
11
class AvoidChildDeletion extends SiteTreeExtension
12
{
13
    public function updateCMSActions(FieldList $fields)
14
    {
15
        if ($this->hasChildrenOrIsTooImportant()) {
16
            $phrase = _t(__CLASS__ . '.ReasonsThisPageCanNotBeDeleted', 'This page can not be archived because it has children or it is the home page.');
17
            $fields->addFieldsToTab(
18
                'ActionMenus.MoreOptions',
19
                [
20
                    LiteralField::create(
21
                        'ArchiveNote',
22
                        '
23
                        <div class=\'cms-sitetree-information\'>
24
                        	<p class="meta-info" style="white-space: normal;">' . $phrase . '</p>
25
                        </div>'
26
                    ),
27
                ]
28
            );
29
        }
30
    }
31
32
    public function canDelete($member = null)
33
    {
34
        return $this->canArchive($member);
35
    }
36
37
    public function canArchive($member = null)
38
    {
39
        return ! $this->hasChildrenOrIsTooImportant();
40
    }
41
42
    protected function hasChildrenOrIsTooImportant(): bool
43
    {
44
        $isHomePage = $this->owner->URLSegment === RootURLController::get_homepage_link();
45
        $haschildren = SiteTree::get()->filter('ParentID', $this->owner->ID)->count() > 0;
46
47
        return $isHomePage || $haschildren;
48
    }
49
}
50