Issues (18)

src/Api/Providers/AllLinksModelAdmin.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Sunnysideup\TemplateOverview\Api\Providers;
4
5
use SilverStripe\Admin\CMSMenu;
6
use SilverStripe\Admin\ModelAdmin;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\ORM\DB;
10
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...
11
use Sunnysideup\TemplateOverview\Api\AllLinks;
12
use Sunnysideup\TemplateOverview\Api\AllLinksProviderBase;
13
14
class AllLinksModelAdmin extends AllLinksProviderBase
15
{
16
    /**
17
     * List of alternative links for modeladmins
18
     * e.g. 'admin/archive' => 'CMSEditLinkForTestPurposesNOTINUSE'.
19
     *
20
     * @var array
21
     */
22
    private static $model_admin_alternatives = [];
23
24
    /**
25
     * e.g. Search => Replace.
26
     *
27
     * @var array
28
     */
29
    private static $replacers = [
30
        '/admin/queuedjobs/Symbiote-QueuedJobs-DataObjects-QueuedJobDescriptor/EditForm/field/Symbiote-QueuedJobs-DataObjects-QueuedJobDescriptor' => '/admin/queuedjobs/Symbiote-QueuedJobs-DataObjects-QueuedJobDescriptor/EditForm/field/QueuedJobDescriptor/',
31
    ];
32
33
    public function getAllLinksInner(): array
34
    {
35
        $links = [];
36
        $modelAdmins = CMSMenu::get_cms_classes(ModelAdmin::class);
37
        unset($modelAdmins[array_search(ArchiveAdmin::class, $modelAdmins, true)]);
38
        foreach ($modelAdmins as $modelAdmin) {
39
            $modelAdminSingleton = Injector::inst()->get($modelAdmin);
40
            $modelAdminLink = '/' . $modelAdminSingleton->Link();
41
            $modelAdminLinkArray = explode('?', $modelAdminLink);
42
            $modelAdminLink = $modelAdminLinkArray[0];
43
            //$extraVariablesLink = $modelAdminLinkArray[1];
44
            $links[] = $modelAdminLink;
45
            $modelsToAdd = $modelAdminSingleton->getManagedModels();
46
            if ($modelsToAdd && count($modelsToAdd)) {
47
                foreach ($modelsToAdd as $key => $model) {
48
                    if (is_array($model) || ! is_subclass_of($model, DataObject::class)) {
49
                        $model = $key;
50
                    }
51
52
                    if (! is_subclass_of($model, DataObject::class)) {
53
                        continue;
54
                    }
55
56
                    $links = array_merge(
57
                        $links,
58
                        $this->workOutLinksForModel($modelAdminSingleton, $model, $modelAdminLink, $modelAdmin)
59
                    );
60
                }
61
            }
62
        }
63
64
        return $this->runReplacements($links);
65
    }
66
67
    protected function workOutLinksForModel($modelAdminSingleton, string $model, string $modelAdminLink, string $modelAdmin)
68
    {
69
        $links = [];
70
        $sanitizedModel = AllLinks::sanitise_class_name($model);
71
        $modelLink = $modelAdminLink . $sanitizedModel . '/';
72
        for ($i = 0; $i < $this->numberOfExamples; ++$i) {
73
            $item = $model::get()
74
                ->sort(DB::get_conn()->random() . ' ASC')
75
                ->First()
76
            ;
77
            $singleton = $item ?: Injector::inst()->get($model);
78
            $exceptionMethod = '';
79
            foreach ($this->Config()->get('model_admin_alternatives') as $test => $method) {
80
                if (! $method) {
81
                    $method = 'do-not-use';
82
                }
83
84
                if (false !== strpos($modelAdminLink, $test)) {
85
                    $exceptionMethod = $method;
86
                }
87
            }
88
89
            if ($exceptionMethod) {
90
                if ($item && $item->hasMethod($exceptionMethod)) {
91
                    $links = array_merge($links, $item->{$exceptionMethod}($modelAdminLink));
92
                }
93
            } else {
94
                //needs to stay here for exception!
95
                $links[] = $modelLink;
96
                if ($singleton->canCreate(null)) {
97
                    $links[] = $modelLink . 'EditForm/field/' . $sanitizedModel . '/item/new/';
98
                }
99
100
                if ($item) {
101
                    if ($item->canEdit()) {
102
                        $links[] = $modelLink . 'EditForm/field/' . $sanitizedModel . '/item/' . $item->ID . '/edit/';
103
                    }
104
                }
105
            }
106
        }
107
108
        return $links;
109
    }
110
111
    protected function runReplacements(array $links): array
112
    {
113
        foreach ($this->Config()->get('replacers') as $search => $replace) {
114
            foreach ($links as $key => $link) {
115
                $links[$key] = str_replace($search, $replace, $link);
116
            }
117
        }
118
119
        return $links;
120
    }
121
}
122