Passed
Push — master ( 15b471...17fc67 )
by Nicolaas
02:18
created

DefaultDashboardProvider::addGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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