Passed
Push — master ( e2682f...a96ff7 )
by Nicolaas
02:17
created

DefaultDashboardProvider::addMeLinks()   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 0
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\Assets\File;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Assets\Folder;
9
use SilverStripe\Assets\Upload;
10
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...
11
use SilverStripe\Core\ClassInfo;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Core\Config\Configurable;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\MFA\Report\EnabledMembers;
0 ignored issues
show
Bug introduced by
The type SilverStripe\MFA\Report\EnabledMembers 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...
16
use SilverStripe\ORM\DataObject;
17
use SilverStripe\Security\DefaultAdminService;
18
use SilverStripe\Security\Group;
19
use SilverStripe\Security\Member;
20
use SilverStripe\Security\Permission;
21
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...
22
use Sunnysideup\DashboardWelcomeQuicklinks\Interfaces\DashboardWelcomeQuickLinksProvider;
23
24
class DefaultDashboardProvider implements DashboardWelcomeQuickLinksProvider
25
{
26
    use Configurable;
27
    protected $links = [];
28
29
    public function provideDashboardWelcomeQuickLinks(): array
30
    {
31
        $this->addPagesLinks();
32
        $this->addFilesAndImages();
33
        $this->addSiteConfigLinks();
34
        $this->addSecurityLinks();
35
        $this->addModelAdminLinks();
36
        $this->addMeLinks();
37
        $this->addFindPages();
38
        return $this->links;
39
    }
40
41
    private static $new_phrase = '+';
0 ignored issues
show
introduced by
The private property $new_phrase is not used, and could be removed.
Loading history...
42
    private static $review_phrase = '☑';
0 ignored issues
show
introduced by
The private property $review_phrase is not used, and could be removed.
Loading history...
43
    private static $edit_phrase = '✎';
0 ignored issues
show
introduced by
The private property $edit_phrase is not used, and could be removed.
Loading history...
44
45
46
    protected function addPagesLinks()
47
    {
48
        $this->addGroup('PAGES', 'Pages', 10);
49
        $this->addLink('PAGES', $this->phrase('add'). ' Page', '/admin/pages/add');
50
        $pagesCount = DataObject::get('Page')->count();
51
        $this->addLink('PAGES', $this->phrase('edit'). ' Pages ('.$pagesCount.')', '/admin/pages');
52
        $pageLastEdited = DataObject::get_one('Page', '', true, 'LastEdited DESC');
53
        if ($pageLastEdited) {
54
            $this->addLink('PAGES', '✎ Last Edited Page: '.$pageLastEdited->Title, $pageLastEdited->CMSEditLink());
55
        }
56
        $this->addLink('PAGES', $this->phrase('review'). ' Page Reports', '/admin/reports');
57
        $lastWeekLink = '/admin/pages?'.'q[LastEditedFrom]='.date('Y-m-d', strtotime('-1 week'));
58
        $this->addLink('PAGES', $this->phrase('review'). ' Recently Modified Pages', $lastWeekLink);
59
    }
60
61
    protected function addFilesAndImages()
62
    {
63
        // 'Files ('.$filesCount.') and Images ('.$imageCount.')'
64
        $this->addGroup('FILESANDIMAGES', 'Files and Images', 20);
65
        $uploadFolderName = Config::inst()->get(Upload::class, 'uploads_folder');
66
        $uploadFolder = Folder::find_or_make($uploadFolderName);
67
        // all
68
        $this->addLink('FILESANDIMAGES', $this->phrase('edit'). ' Open File Browswer', '/admin/assets');
69
        // per type
70
        $filesCount = File::get()->exclude(['ClassName' => [Folder::class, Image::class]])->count();
71
        $imageCount = File::get()->filter(['ClassName' => [Image::class]])->count();
72
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Images ('.$imageCount.')', 'admin/assets?filter[appCategory]=IMAGE');
73
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Files ('.$filesCount.')', 'admin/assets?filter[appCategory]=DOCUMENT');
74
75
        // default upload folder
76
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Open Default Upload Folder', $uploadFolder->CMSEditLink());
77
78
        // recent
79
        $lastWeekLink = '/admin/assets?'.'filter[lastEditedFrom]='.date('Y-m-d', strtotime('-1 week')).'&view=tile';
80
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Recently modified Files', $lastWeekLink);
81
    }
82
83
    protected function addSiteConfigLinks()
84
    {
85
        $this->addGroup('SITECONFIG', 'Site Wide Configuration', 20);
86
        $this->addLink('SITECONFIG', $this->phrase('review'). ' Site Settings', '/admin/settings');
87
    }
88
89
    protected function addSecurityLinks()
90
    {
91
        $this->addGroup('SECURITY', 'Security', 30);
92
        $this->addLink('SECURITY', $this->phrase('add'). ' User', '/admin/security/users/EditForm/field/users/item/new');
93
        $userCount = Member::get()->count();
94
        $groupCount = Group::get()->count();
95
        $this->addLink('SECURITY', $this->phrase('review'). ' Users ('.$userCount.')', '/admin/security');
96
        $this->addLink('SECURITY', $this->phrase('review'). ' Groups  ('.$groupCount.')', '/admin/security/groups');
97
        DefaultAdminService::singleton()->extend('addSecurityLinks', $this);
98
        $adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
99
        if($adminGroup) {
100
            $userCount = $adminGroup->Members()->count();
101
            $this->addLink('SECURITY', $this->phrase('review'). ' Administrators ('.$userCount.')', '/admin/security/groups/EditForm/field/groups/item/'.$adminGroup->ID.'/edit');
102
        }
103
        if(class_exists(EnabledMembers::class)) {
104
            $obj = Injector::inst()->get(EnabledMembers::class);
105
            $this->addLink('SECURITY', $this->phrase('review'). ' Multi-Factor Authentication Status', $obj->getLink());
106
        }
107
    }
108
109
110
111
112
    protected function addModelAdminLinks()
113
    {
114
        $modelAdmins = [];
115
        foreach (ClassInfo::subclassesFor(ModelAdmin::class, false) as $className) {
116
            if($className === ArchiveAdmin::class) {
117
                continue;
118
            }
119
            $modelAdmins[$className] = $className;
120
121
        }
122
        foreach($modelAdmins as $modelAdminClassName) {
123
            $groupAdded = false;
124
            $ma = Injector::inst()->get($modelAdminClassName);
125
            $mas = $ma->getManagedModels();
126
            if(count($mas)) {
127
                $numberOfModels = count($mas);
128
                $groupCode = strtoupper($modelAdminClassName);
129
                $count = 0;
130
                foreach($mas as $model => $title) {
131
                    $count++;
132
                    if($count > 7) {
133
                        break;
134
                    }
135
                    if(is_array($title)) {
136
                        $title = $title['title'];
137
                        $model = $title['dataClass'] ?? $model;
138
                    }
139
                    if(! class_exists($model)) {
140
                        continue;
141
                    }
142
                    if(! $groupAdded) {
143
                        $this->addGroup($groupCode, $ma->menu_title(), 100);
144
                        $groupAdded = true;
145
                    }
146
                    $obj = DataObject::singleton($model);
147
                    $link = '';
148
                    if($obj->hasMethod('CMSListLink')) {
149
                        $link = $obj->CMSListLink();
150
                    } else {
151
                        $link = $ma->getLinkForModelTab($model);
152
                    }
153
                    $this->addLink($groupCode, $this->phrase('edit'). ' '.$title, $link);
154
                    if($numberOfModels < 4) {
155
                        $obj = Injector::inst()->get($model);
156
                        if($obj->canCreate()) {
157
                            $classNameEscaped = str_replace('\\', '-', $model);
158
                            $linkNew = $link .= '/EditForm/field/'.$classNameEscaped.'/item/new';
159
                            $objectCount = $model::get()->count();
160
                            $this->addLink($groupCode, $this->phrase('add'). ' '.$obj->i18n_singular_name() . ' ('.$objectCount.')', $linkNew);
161
                        }
162
                    }
163
                }
164
            }
165
        }
166
    }
167
168
169
    protected function addFindPages()
170
    {
171
        $pages = [];
172
        foreach (ClassInfo::subclassesFor(SiteTree::class, false) as $className) {
173
            $pages[$className] = $className;
174
175
        }
176
        $this->addGroup('PAGEFILTER', 'Page Types ('.count($pages).')', 300);
177
        $count = 0;
178
        foreach($pages as $pageClassName) {
179
            $pageCount = $pageClassName::get()->count();
180
            if($pageCount < 1) {
181
                continue;
182
            }
183
            $count++;
184
            if($count > 12) {
185
                break;
186
            }
187
            $page = Injector::inst()->get($pageClassName);
188
            $pageTitle = $page->i18n_singular_name();
189
            $query = urlencode('q[ClassName]='.$pageClassName);
190
            $link = 'admin/pages?' . $query;
191
            $this->addLink('PAGEFILTER', $this->phrase('edit'). ' '.$pageTitle.' ('.$pageCount.')', $link);
192
        }
193
    }
194
195
    protected function addMeLinks()
196
    {
197
        $this->addGroup('ME', 'My Account', 200);
198
        $this->addLink('ME', $this->phrase('edit') . '  My Details', '/admin/myprofile');
199
        $this->addLink('ME', $this->phrase('review') . '  Log-out', '/Security/logout');
200
    }
201
202
203
    protected function addGroup(string $groupCode, string $title, $sort)
204
    {
205
        $this->links[$groupCode] = [
206
            'Title' => $title,
207
            'SortOrdre' => $sort,
208
        ];
209
    }
210
211
    protected function addLink($groupCode, $title, $link)
212
    {
213
        $this->links[$groupCode]['Items'][] = [
214
            'Title' => $title,
215
            'Link' => $link,
216
        ];
217
    }
218
219
    protected function phrase(string $phrase): string
220
    {
221
        if($phrase === 'add') {
222
            $phrase = $this->config()->get('new_phrase');
223
        } elseif($phrase === 'review') {
224
            $phrase = $this->config()->get('review_phrase');
225
        } elseif($phrase === 'edit') {
226
            $phrase = $this->config()->get('edit_phrase');
227
        }
228
        return _t('DashboardWelcomeQuicklinks.'.$phrase, $phrase);
229
    }
230
231
}
232