Passed
Push — master ( 0f71a2...53ce28 )
by Nicolaas
03:04 queued 01:03
created

DefaultDashboardProvider::addSiteConfigLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Sunnysideup\DashboardWelcomeQuicklinks\Api;
4
5
use SilverStripe\Admin\ModelAdmin;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Admin\ModelAdmin 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\Core\ClassInfo;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Security\DefaultAdminService;
10
use SilverStripe\Security\Permission;
11
use SilverStripe\VersionedAdmin\ArchiveAdmin;
0 ignored issues
show
Bug introduced by
The type SilverStripe\VersionedAdmin\ArchiveAdmin 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...
12
use Sunnysideup\DashboardWelcomeQuicklinks\Interfaces\DashboardWelcomeQuickLinksProvider;
13
14
class DefaultDashboardProvider implements DashboardWelcomeQuickLinksProvider
15
{
16
    protected $links = [];
17
18
    public function provideDashboardWelcomeQuickLinks(): array
19
    {
20
        $this->addPagesLinks();
21
        $this->addSiteConfigLinks();
22
        $this->addSecurityLinks();
23
        $this->addModelAdminLinks();
24
        return $this->links;
25
    }
26
27
28
    protected function addPagesLinks()
29
    {
30
        $this->addGroup('PAGES', 'Pages', 10);
31
        $this->addLink('PAGES', '+ Add Page', '/admin/pages/add');
32
        $this->addLink('PAGES', '✎ Edit Pages', '/admin/pages');
33
        $pageLastEdited = DataObject::get_one('Page', '', true, 'LastEdited DESC');
34
        if ($pageLastEdited) {
35
            $this->addLink('PAGES', '✎ Last Edited Page: '.$pageLastEdited->Title, $pageLastEdited->CMSEditLink());
36
        }
37
        $this->addLink('PAGES', '☑ Review Page Reports', '/admin/reports');
38
    }
39
40
    protected function addSiteConfigLinks()
41
    {
42
        $this->addGroup('SITECONFIG', '☑ Site Wide Configuration', 20);
43
        $this->addLink('SITECONFIG', '☑ Review Site Configuration', '/admin/settings');
44
    }
45
    protected function addSecurityLinks()
46
    {
47
        $this->addGroup('SECURITY', 'Security', 30);
48
        $this->addLink('SECURITY', '+ Add user', '/admin/security/users/EditForm/field/users/item/new');
49
        $this->addLink('SECURITY', '☑ Review Users', '/admin/security');
50
        $this->addLink('SECURITY', '☑ Review Users Groups', '/admin/security/groups');
51
        DefaultAdminService::singleton()->extend('addSecurityLinks', $this);
52
        $adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
53
        if($adminGroup) {
54
            $this->addLink('SECURITY', '☑ Review Administrators', '/admin/security/groups/EditForm/field/groups/item/'.$adminGroup->ID.'/edit');
55
        }
56
    }
57
58
59
60
61
    protected function addModelAdminLinks()
62
    {
63
        $modelAdmins = [];
64
        foreach (ClassInfo::subclassesFor(ModelAdmin::class, false) as $className) {
65
            if($className === ArchiveAdmin::class) {
66
                continue;
67
            }
68
            $modelAdmins[$className] = $className;
69
70
        }
71
        foreach($modelAdmins as $modelAdminClassName) {
72
            $groupAdded = false;
73
            $ma = Injector::inst()->get($modelAdminClassName);
74
            $mas = $ma->getManagedModels();
75
            if(count($mas)) {
76
                $numberOfModels = count($mas);
77
                $groupCode = strtoupper($modelAdminClassName);
78
                $count = 0;
79
                foreach($mas as $model => $title) {
80
                    $count++;
81
                    if($count > 7) {
82
                        break;
83
                    }
84
                    if(is_array($title)) {
85
                        $title = $title['title'];
86
                        $model = $title['dataClass'] ?? $model;
87
                    }
88
                    if(! class_exists($model)) {
89
                        continue;
90
                    }
91
                    if(! $groupAdded) {
92
                        $this->addGroup($groupCode, $ma->menu_title(), 100);
93
                        $groupAdded = true;
94
                    }
95
                    $obj = DataObject::singleton($model);
96
                    $link = '';
97
                    if($obj->hasMethod('CMSListLink')) {
98
                        $link = $obj->CMSListLink();
99
                    } else {
100
                        $link = $ma->getLinkForModelTab($model);
101
                    }
102
                    $this->addLink($groupCode, '✎ '.$title, $link);
103
                    if($numberOfModels < 4) {
104
                        $obj = Injector::inst()->get($model);
105
                        if($obj->canCreate()) {
106
                            $classNameEscaped = str_replace('\\', '-', $model);
107
                            $linkNew = $link .= '/EditForm/field/'.$classNameEscaped.'/item/new';
108
                            $this->addLink($groupCode, '+ New '.$obj->i18n_singular_name(), $linkNew);
109
                        }
110
                    }
111
                }
112
            } else {
113
114
            }
115
        }
116
    }
117
118
119
    protected function addGroup(string $groupCode, string $title, $sort)
120
    {
121
        $this->links[$groupCode] = [
122
            'Title' => $title,
123
            'SortOrdre' => $sort,
124
        ];
125
    }
126
127
    protected function addLink($groupCode, $title, $link)
128
    {
129
        $this->links[$groupCode]['Items'][] = [
130
            'Title' => $title,
131
            'Link' => $link,
132
        ];
133
    }
134
135
}
136