Passed
Push — master ( af50b1...024788 )
by Nicolaas
08:22
created

DefaultDashboardProvider::addGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
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\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...
10
use Sunnysideup\DashboardWelcomeQuicklinks\Interfaces\DashboardWelcomeQuickLinksProvider;
11
12
class DefaultDashboardProvider implements DashboardWelcomeQuickLinksProvider
13
{
14
    protected $links = [];
15
16
    public function provideDashboardWelcomeQuickLinks(): array
17
    {
18
        $this->addPagesLinks();
19
        $this->addSecurityLinks();
20
        $this->addModelAdminLinks();
21
        return $this->links;
22
    }
23
24
25
    protected function addPagesLinks()
26
    {
27
        $this->addGroup('PAGES', 'Pages', 10);
28
        $this->addLink('PAGES', 'Create new page', '/admin/pages/add');
29
        $this->addLink('PAGES', 'Open Sitetree', '/admin/pages');
30
        $pageLastEdited = DataObject::get_one('Page', '', true, 'LastEdited DESC');
31
        if ($pageLastEdited) {
32
            $this->addLink('PAGES', 'Last Edited Page: '.$pageLastEdited->Title, $pageLastEdited->CMSEditLink());
33
        }
34
    }
35
36
    protected function addSecurityLinks()
37
    {
38
        $this->addGroup('SECURITY', 'Security', 20);
39
        $this->addLink('SECURITY', 'Create new user', '/admin/security/addmember');
40
        $this->addLink('SECURITY', 'Review Users', '/admin/security');
41
        $this->addLink('SECURITY', 'Review Users Groups', '/admin/security/groups');
42
    }
43
44
    protected function addGroup(string $groupCode, string $title, $sort)
45
    {
46
        $this->links[$groupCode] = [
47
            'Title' => $title,
48
            'SortOrdre' => $sort,
49
        ];
50
    }
51
52
    protected function addLink($groupCode, $title, $link)
53
    {
54
        $this->links[$groupCode]['Items'][] = [
55
            'Title' => $title,
56
            'Link' => $link,
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