Issues (18)

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

Labels
Severity
1
<?php
2
3
namespace Sunnysideup\TemplateOverview\Api\Providers;
4
5
use SilverStripe\Admin\CMSMenu;
6
use SilverStripe\CMS\Model\SiteTree;
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 AllLinksArchiveAdmin 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
    public function getAllLinksInner(): array
25
    {
26
        $links = [];
27
        $modelAdmins = CMSMenu::get_cms_classes(ArchiveAdmin::class);
28
        foreach ($modelAdmins as $modelAdmin) {
29
            $obj = Injector::inst()->get($modelAdmin);
30
            $modelAdminLink = '/' . $obj->Link();
31
            $modelAdminLinkArray = explode('?', $modelAdminLink);
32
            $modelAdminLink = $modelAdminLinkArray[0];
33
            //$extraVariablesLink = $modelAdminLinkArray[1];
34
            $links[] = $modelAdminLink;
35
            $modelsToAdd = $obj->getManagedModels();
36
            if ($modelsToAdd && count($modelsToAdd)) {
37
                foreach ($modelsToAdd as $key => $model) {
38
                    if (is_array($model) || ! is_subclass_of($model, DataObject::class)) {
39
                        $model = $key;
40
                    }
41
42
                    if (! is_subclass_of($model, DataObject::class)) {
43
                        continue;
44
                    }
45
46
                    $links = array_merge(
47
                        $links,
48
                        $this->workOutLinksForModel($obj, $model, $modelAdminLink, $modelAdmin)
49
                    );
50
                }
51
            }
52
        }
53
54
        return $links;
55
    }
56
57
    protected function workOutLinksForModel($obj, $model, $modelAdminLink, $modelAdmin)
58
    {
59
        $links = [];
60
        $sanitizedModel = AllLinks::sanitise_class_name($model);
61
        $modelLink = $modelAdminLink . $sanitizedModel . '/';
62
        for ($i = 0; $i < $this->numberOfExamples; ++$i) {
63
            $item = $this->getRandomArchivedItem($model);
64
            $exceptionMethod = '';
65
            foreach ($this->Config()->get('model_admin_alternatives') as $test => $method) {
66
                if (! $method) {
67
                    $method = 'do-not-use';
68
                }
69
70
                if (false !== strpos($modelAdminLink, $test)) {
71
                    $exceptionMethod = $method;
72
                }
73
            }
74
75
            if ($exceptionMethod) {
76
                if ($item && $item->hasMethod($exceptionMethod)) {
77
                    $links = array_merge($links, $item->{$exceptionMethod}($modelAdminLink));
78
                }
79
            } else {
80
                //needs to stay here for exception!
81
                $links[] = $modelLink;
82
                if ($item) {
83
                    $test1 = is_subclass_of($model, SiteTree::class);
84
                    $test2 = SiteTree::class === (string) $model;
85
                    if ($test1 || $test2) {
86
                        $links[] = $modelLink . 'EditForm/field/Pages/item/' . $item->ID . '/view/';
87
                    } else {
88
                        $links[] = $modelLink . 'EditForm/field/Others/item/' . $item->ID . '/view/';
89
                    }
90
                }
91
            }
92
        }
93
94
        return $links;
95
    }
96
97
    protected function getRandomArchivedItem($class)
98
    {
99
        $list = \Singleton($class)->get();
100
        $baseTable = \Singleton($list->dataClass())->baseTable();
101
        $liveTable = $baseTable . '_Live';
102
        $draftTable = $baseTable . '_Draft';
103
        if ($this->tableExists($liveTable) && $this->tableExists($draftTable)) {
104
            $list = $list
105
                ->setDataQueryParam('Versioned.mode', 'latest_versions')
106
            ;
107
            // Join a temporary alias BaseTable_Draft, renaming this on execution to BaseTable
108
            // See Versioned::augmentSQL() For reference on this alias
109
            $list = $list
110
                ->leftJoin(
111
                    $draftTable,
112
                    "\"{$baseTable}\".\"ID\" = \"{$draftTable}\".\"ID\""
113
                )
114
            ;
115
116
            $list = $list->leftJoin(
117
                $liveTable,
118
                "\"{$baseTable}\".\"ID\" = \"{$liveTable}\".\"ID\""
119
            );
120
121
            $list = $list->where("\"{$draftTable}\".\"ID\" IS NULL");
122
            $list = $list->sort(DB::get_conn()->random() . ' ASC');
123
124
            return $list->First();
125
        }
126
    }
127
}
128