Issues (26)

src/Api/DefaultDashboardProvider.php (9 issues)

1
<?php
2
3
namespace Sunnysideup\DashboardWelcomeQuicklinks\Api;
4
5
use SilverStripe\Admin\ModelAdmin;
0 ignored issues
show
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\Folder;
8
use SilverStripe\Assets\Image;
9
use SilverStripe\Assets\Upload;
10
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_ChangedPages;
0 ignored issues
show
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
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
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
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\Security\PermissionRole;
24
use SilverStripe\VersionedAdmin\ArchiveAdmin;
0 ignored issues
show
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...
25
use Sunnysideup\DashboardWelcomeQuicklinks\Admin\DashboardWelcomeQuicklinks;
26
use Sunnysideup\DashboardWelcomeQuicklinks\Interfaces\DashboardWelcomeQuicklinksProvider;
27
28
class DefaultDashboardProvider implements DashboardWelcomeQuicklinksProvider
29
{
30
    use Configurable;
31
32
    public function provideDashboardWelcomeQuicklinks(): array
33
    {
34
        $this->addPagesLinks();
35
        $this->addFindPages();
36
        $this->addFilesAndImages();
37
        $this->addSiteConfigLinks();
38
        if (Permission::check('ADMIN')) {
39
            $this->addSecurityLinks();
40
        }
41
        $this->addModelAdminLinks();
42
        $this->addMeLinks();
43
        return DashboardWelcomeQuicklinks::get_links();
44
    }
45
46
    private static $model_admins_to_skip = [
0 ignored issues
show
The private property $model_admins_to_skip is not used, and could be removed.
Loading history...
47
        ArchiveAdmin::class,
48
    ];
49
50
    private static $pages_to_skip = [];
0 ignored issues
show
The private property $pages_to_skip is not used, and could be removed.
Loading history...
51
52
    protected function addPagesLinks()
53
    {
54
        DashboardWelcomeQuicklinks::add_group('PAGES', 'Pages', 10);
55
        $pagesCount = DataObject::get('Page')->count();
56
        $draftCount = CMSSiteTreeFilter_StatusDraftPages::create()->getFilteredPages()->count();
57
        $revisedCount = CMSSiteTreeFilter_ChangedPages::create()->getFilteredPages()->count();
58
        $add = [
59
            'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'),
60
            'Link' => '/admin/pages/add',
61
        ];
62
        DashboardWelcomeQuicklinks::add_link(
63
            'PAGES',
64
            DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Pages (' . $pagesCount . ')',
65
            '/admin/pages',
66
            $add
67
        );
68
        DashboardWelcomeQuicklinks::add_link('PAGES', DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Unpublished Drafts (' . $draftCount . ')', '/admin/pages?q[FilterClass]=SilverStripe\CMS\Controllers\CMSSiteTreeFilter_StatusDraftPages');
69
        DashboardWelcomeQuicklinks::add_link('PAGES', DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Unpublished Changes (' . $revisedCount . ')', '/admin/pages?q[FilterClass]=SilverStripe\CMS\Controllers\CMSSiteTreeFilter_ChangedPages');
70
        $pageLastEdited = DataObject::get_one('Page', '', true, 'LastEdited DESC');
71
        if ($pageLastEdited) {
72
            DashboardWelcomeQuicklinks::add_link('PAGES', '✎ Last Edited Page: ' . $pageLastEdited->Title, $pageLastEdited->CMSEditLink());
73
        }
74
        $lastWeekLink = '/admin/pages?q[LastEditedFrom]=' . date('Y-m-d', strtotime('-1 week'));
75
        DashboardWelcomeQuicklinks::add_link('PAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Recently Modified Pages', $lastWeekLink);
76
        DashboardWelcomeQuicklinks::add_link('PAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Page Reports', '/admin/reports');
77
    }
78
79
    protected function addFindPages()
80
    {
81
        $pages = [];
82
        $pagesToSkip = (array) $this->Config()->get('pages_to_skip');
83
        foreach (ClassInfo::subclassesFor(SiteTree::class, false) as $className) {
84
            if (in_array($className, $pagesToSkip)) {
85
                continue;
86
            }
87
            $pages[$className] = $className;
88
        }
89
        DashboardWelcomeQuicklinks::add_group('PAGEFILTER', 'Page Types (' . count($pages) . ')', 300);
90
        $pagesArray = [];
91
        foreach ($pages as $pageClassName) {
92
            $pageCount = $pageClassName::get()->filter(['ClassName' => $pageClassName])->count();
93
            if ($pageCount < 1) {
94
                $page = Injector::inst()->get($pageClassName);
95
                if ($page->canCreate()) {
96
                    $pageTitle = $page->i18n_singular_name();
97
                    $pagesArray[] = [
98
                        'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add') . ' ' . $pageTitle . ' (0)',
99
                        'Link' => 'admin/pages/add?PageType=' . $pageClassName,
100
                        'Count' => 0,
101
                        'InsideLink' => [],
102
                    ];
103
                }
104
            } elseif ($pageCount === 1) {
105
                $page = DataObject::get_one($pageClassName, ['ClassName' => $pageClassName]);
106
                if ($page->canEdit()) {
107
                    $pageTitle = $page->i18n_singular_name();
108
                    $insideLink = [];
109
                    if ($page->canCreate() && $page->config()->get('can_be_root')) {
110
                        $insideLink = [
111
                            'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'),
112
                            'Link' => 'admin/pages/add?PageType=' . $pageClassName,
113
                        ];
114
                    }
115
                    $pagesArray[] = [
116
                        'Title' => DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' ' . $pageTitle . ' (1)',
117
                        'Link' => $page->CMSEditLink(),
118
                        'Count' => 1,
119
                        'InsideLink' => $insideLink,
120
                    ];
121
                }
122
            } else {
123
                $page = Injector::inst()->get($pageClassName);
124
                if ($page->canEdit()) {
125
                    $pageTitle = $page->i18n_plural_name();
126
                    $query = 'q[ClassName]=' . $pageClassName;
127
                    $link = 'admin/pages?' . $query;
128
                    $insideLink = [];
129
                    if ($page->canCreate() && $page->config()->get('can_be_root')) {
130
                        $insideLink = [
131
                            'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'),
132
                            'Link' => 'admin/pages/add?PageType=' . $pageClassName,
133
                        ];
134
                    }
135
                    $pagesArray[] = [
136
                        'Title' => DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' ' . $pageTitle . ' (' . $pageCount . ')',
137
                        'Link' => $link,
138
                        'Count' => $pageCount,
139
                        'InsideLink' => $insideLink,
140
                    ];
141
                }
142
            }
143
        }
144
        $pagesArray = $this->sortByCountAndTitle($pagesArray);
145
        foreach ($pagesArray as $pageArray) {
146
            DashboardWelcomeQuicklinks::add_link('PAGEFILTER', $pageArray['Title'], $pageArray['Link'], $pageArray['InsideLink']);
147
        }
148
    }
149
150
    protected function addFilesAndImages()
151
    {
152
        // 'Files ('.$filesCount.') and Images ('.$imageCount.')'
153
        DashboardWelcomeQuicklinks::add_group('FILESANDIMAGES', 'Files and Images', 20);
154
        $uploadFolderName = Config::inst()->get(Upload::class, 'uploads_folder');
155
        $uploadFolder = Folder::find_or_make($uploadFolderName);
156
        // all
157
        DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Open File Browswer', '/admin/assets');
158
        // per type
159
        $filesCount = File::get()->excludeAny(['ClassName' => [Folder::class, Image::class]])->count();
160
        $imageCount = File::get()->filter(['ClassName' => Image::class])->exclude(['ClassName' => Folder::class])->count();
161
        DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Files (' . $filesCount . ')', 'admin/assets?filter[appCategory]=DOCUMENT');
162
        DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Images (' . $imageCount . ')', 'admin/assets?filter[appCategory]=IMAGE');
163
164
        // default upload folder
165
        DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Open Default Upload Folder', $uploadFolder->CMSEditLink());
166
167
        // recent
168
        $lastWeekLink = '/admin/assets?filter[lastEditedFrom]=' . date('Y-m-d', strtotime('-1 week')) . '&view=tile';
169
        DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Recently modified Files', $lastWeekLink);
170
    }
171
172
    protected function addSiteConfigLinks()
173
    {
174
        DashboardWelcomeQuicklinks::add_group('SITECONFIG', 'Site Wide Configuration', 20);
175
        DashboardWelcomeQuicklinks::add_link('SITECONFIG', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Site Settings', '/admin/settings');
176
    }
177
178
    protected function addSecurityLinks()
179
    {
180
        DashboardWelcomeQuicklinks::add_group('SECURITY', 'Security', 30);
181
        $userCount = Member::get()->count();
182
        $add = [
183
            'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'),
184
            'Link' => '/admin/security/users/EditForm/field/users/item/new',
185
        ];
186
        DashboardWelcomeQuicklinks::add_link(
187
            'SECURITY',
188
            DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Users (' . $userCount . ')',
189
            '/admin/security',
190
            $add
191
        );
192
        // groups
193
        $groupCount = Group::get()->count();
194
        $add = [
195
            'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'),
196
            'Link' => '/admin/security/groups/EditForm/field/groups/item/new',
197
        ];
198
        DashboardWelcomeQuicklinks::add_link(
199
            'SECURITY',
200
            DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Groups  (' . $groupCount . ')',
201
            '/admin/security/groups',
202
            $add
203
        );
204
        DefaultAdminService::singleton()->extend('addSecurityLinks', $this);
205
        $adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
206
        if ($adminGroup) {
207
            $userCount = $adminGroup->Members()->count();
208
            DashboardWelcomeQuicklinks::add_link('SECURITY', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Administrators (' . $userCount . ')', '/admin/security/groups/EditForm/field/groups/item/' . $adminGroup->ID . '/edit');
209
        }
210
211
        // roles
212
        $roleCount = PermissionRole::get()->count();
213
        $add = [
214
            'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'),
215
            'Link' => '/admin/security/roles/EditForm/field/roles/item/new',
216
        ];
217
        DashboardWelcomeQuicklinks::add_link(
218
            'SECURITY',
219
            DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Permission Roles  (' . $roleCount . ')',
220
            '/admin/security/groups',
221
            $add
222
        );
223
        // multi factor
224
        if (class_exists(EnabledMembers::class)) {
225
            $obj = Injector::inst()->get(EnabledMembers::class);
226
            DashboardWelcomeQuicklinks::add_link('SECURITY', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Multi-Factor Authentication Status', $obj->getLink());
227
        }
228
    }
229
230
    protected function addModelAdminLinks()
231
    {
232
        $modelAdmins = [];
233
        $skips = (array) $this->Config()->get('model_admins_to_skip');
234
        foreach (ClassInfo::subclassesFor(ModelAdmin::class, false) as $className) {
235
            if (in_array($className, $skips)) {
236
                continue;
237
            }
238
            $modelAdmins[$className] = $className;
239
        }
240
        foreach ($modelAdmins as $modelAdminClassName) {
241
            $groupAdded = false;
242
            $ma = Injector::inst()->get($modelAdminClassName);
243
            if ($ma->canView()) {
244
                $mas = $ma->getManagedModels();
245
                if (count($mas) > 0) {
246
                    $numberOfModels = count($mas);
0 ignored issues
show
The assignment to $numberOfModels is dead and can be removed.
Loading history...
247
                    $groupCode = strtoupper($modelAdminClassName);
248
                    $count = 0;
249
                    foreach ($mas as $model => $title) {
250
                        $count++;
251
                        if (is_array($title)) {
252
                            $title = $title['title'];
253
                            $model = $title['dataClass'] ?? $model;
254
                        }
255
                        if (! class_exists($model)) {
256
                            continue;
257
                        }
258
                        $obj = Injector::inst()->get($model);
259
                        if ($obj && $obj->canView()) {
260
                            if (! $groupAdded) {
261
                                DashboardWelcomeQuicklinks::add_group($groupCode, $ma->menu_title(), 100);
262
                                $groupAdded = true;
263
                            }
264
                            // $classNameList = ClassInfo::subclassesFor($model);
265
                            $ma = ReflectionHelper::allowAccessToProperty(get_class($ma), 'modelClass');
266
                            $ma->modelClass = $model;
267
                            $list = $ma->getList();
268
                            if (! $list) {
269
                                $list = $model::get();
270
                            }
271
                            $objectCount = $list->count();
272
                            if ($objectCount === 1) {
273
                                $baseTable = Injector::inst()->get($model)->baseTable();
274
                                $obj = DataObject::get_one($model, [$baseTable . '.ClassName' => $model]);
275
                                if (! $obj) {
276
                                    $obj = DataObject::get_one($model);
277
                                }
278
                                if ($obj && $obj->hasMethod('CMSEditLink')) {
279
                                    $link = $obj->CMSEditLink();
280
                                    if ($link) {
281
                                        DashboardWelcomeQuicklinks::add_link($groupCode, DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' ' . $model::singleton()->i18n_singular_name(), $link);
282
                                        continue;
283
                                    }
284
                                }
285
                            }
286
287
                            $link = '';
288
                            if ($obj->hasMethod('CMSListLink')) {
289
                                $link = $obj->CMSListLink();
290
                            }
291
                            if (! $link) {
292
                                $link = $ma->getLinkForModelTab($model);
293
                            }
294
                            $titleContainsObjectCount = strpos($title, ' (' . $objectCount . ')');
295
                            if ($titleContainsObjectCount === false) {
296
                                $title .= ' (' . $objectCount . ')';
297
                            }
298
                            $add = [];
299
                            if ($obj->canCreate()) {
300
                                $classNameEscaped = str_replace('\\', '-', $model);
301
                                $linkNew = $link . '/EditForm/field/' . $classNameEscaped . '/item/new';
302
                                $add = [
303
                                    'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'),
304
                                    'Link' => $linkNew,
305
                                ];
306
                            }
307
                            DashboardWelcomeQuicklinks::add_link(
308
                                $groupCode,
309
                                DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' ' . $title,
310
                                $link,
311
                                $add
312
                            );
313
                        }
314
                    }
315
                }
316
            }
317
        }
318
    }
319
320
    protected function addMeLinks()
321
    {
322
        DashboardWelcomeQuicklinks::add_group('ME', 'My Account', 200);
323
        DashboardWelcomeQuicklinks::add_link('ME', DashboardWelcomeQuicklinks::get_base_phrase('edit') . '  My Details', '/admin/myprofile');
324
        DashboardWelcomeQuicklinks::add_link('ME', DashboardWelcomeQuicklinks::get_base_phrase('review') . '  Test Password Reset', 'Security/lostpassword');
325
        DashboardWelcomeQuicklinks::add_link('ME', DashboardWelcomeQuicklinks::get_base_phrase('review') . '  Log-out', '/Security/logout');
326
    }
327
328
    protected function sortByCountAndTitle(array $array): array
329
    {
330
        usort($array, function ($a, $b) {
331
            $countComparison = $b['Count'] <=> $a['Count'];
332
            if ($countComparison === 0) {
333
                return $a['Title'] <=> $b['Title'];
334
            }
335
            return $countComparison;
336
        });
337
338
        return $array;
339
    }
340
}
341