Issues (3)

src/Extensions/AvoidChildDeletion.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Sunnysideup\AvoidChildDeletion\Extensions;
4
5
use SilverStripe\CMS\Controllers\RootURLController;
0 ignored issues
show
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
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
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
use SilverStripe\Security\Member;
11
12
/**
13
 * Class \Sunnysideup\AvoidChildDeletion\Extensions\AvoidChildDeletion
14
 *
15
 * @property SiteTree|AvoidChildDeletion $owner
16
 */
17
class AvoidChildDeletion extends SiteTreeExtension
18
{
19
    public function updateCMSActions(FieldList $fields)
20
    {
21
        if ($this->hasChildrenOrIsTooImportant()) {
22
            $phrase = _t(__CLASS__ . '.ReasonsThisPageCanNotBeDeleted', 'This page can not be archived because it has children or it is the home page.');
23
            $fields->addFieldsToTab(
24
                'ActionMenus.MoreOptions',
25
                [
26
                    LiteralField::create(
27
                        'ArchiveNote',
28
                        '
29
                        <div class=\'cms-sitetree-information\'>
30
                            <p class="meta-info" style="white-space: normal;">' . $phrase . '</p>
31
                        </div>'
32
                    ),
33
                ]
34
            );
35
        }
36
    }
37
38
    /**
39
     * @param Member $member - optional
40
     */
41
    public function canDelete($member = null)
42
    {
43
        return $this->canArchive($member);
44
    }
45
46
    /**
47
     * @param Member $member - optional
48
     */
49
    public function canArchive($member = null)
50
    {
51
        return ! $this->hasChildrenOrIsTooImportant();
52
    }
53
54
    protected function hasChildrenOrIsTooImportant(): bool
55
    {
56
        $isHomePage = $this->getOwner()->URLSegment === RootURLController::get_homepage_link();
57
        $haschildren = SiteTree::get()->filter('ParentID', $this->getOwner()->ID)->exists();
58
59
        return $isHomePage || $haschildren;
60
    }
61
}
62