Issues (18)

src/Control/TemplateOverviewPageController.php (5 issues)

1
<?php
2
3
namespace Sunnysideup\TemplateOverview\Control;
4
5
use PageController;
0 ignored issues
show
The type PageController 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\Admin\LeftAndMain;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Core\ClassInfo;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Core\Injector\Injector;
14
use SilverStripe\ORM\ArrayList;
15
use SilverStripe\ORM\DataObject;
16
use SilverStripe\ORM\DB;
17
use SilverStripe\ORM\FieldType\DBField;
18
use SilverStripe\Security\Permission;
19
use SilverStripe\Security\Security;
20
use SilverStripe\Versioned\Versioned;
21
use SilverStripe\View\ArrayData;
22
use SilverStripe\View\Requirements;
23
use SilverStripe\View\SSViewer;
24
use Sunnysideup\PrettyPhoto\PrettyPhoto;
0 ignored issues
show
The type Sunnysideup\PrettyPhoto\PrettyPhoto 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\TemplateOverview\Api\SiteTreeDetails;
26
27
/**
28
 * Class \Sunnysideup\TemplateOverview\Control\TemplateOverviewPageController
29
 *
30
 */
31
class TemplateOverviewPageController extends PageController
32
{
33
    protected $myMoreList;
34
    protected $totalPageCount = 0;
35
36
    private static $url_segment = 'admin/templates';
37
38
    /**
39
     *  folder for example images from root dir
40
     *  it is recommended to keep this outside of public dir!
41
     *
42
     *  @var string
43
     */
44
    private static $folder_for_example_images = '';
45
46
    private static $allowed_actions = [
47
        'showmore' => true,
48
        'quicklist' => true,
49
        'listofobjectsused' => true,
50
        'exampleimage' => true,
51
    ];
52
53
    private static $base_class = SiteTree::class;
54
55
    private static $base_class_provider = SiteTreeDetails::class;
56
57
    public function index(HTTPRequest $request = null)
58
    {
59
        // $this->renderWith(['Sunnysideup\\TemplateOverview\\TemplateOverviewPageController']);
60
        return [];
61
    }
62
63
    public function Content()
64
    {
65
        return $this->renderWith('Sunnysideup\\TemplateOverview\\Includes\\TemplateOverviewList');
66
    }
67
68
    public function showmore($request)
69
    {
70
        $id = $request->param('ID');
71
        $className = $this->getBaseClass();
72
        /** @var null|DataObject $obj */
73
        $obj = $className::get_by_id((int) $id);
74
        if (null !== $obj) {
75
            $className = $obj->ClassName;
76
            $list = $className::get()
77
                ->filter(['ClassName' => $obj->ClassName])
78
                ->limit(300);
79
            $this->myMoreList = ArrayList::create();
80
            foreach ($list as $count => $item) {
81
                $this->myMoreList->push(clone $this->createPageObject($item, $count));
82
            }
83
        }
84
85
        return $this->renderWith('Sunnysideup\\TemplateOverview\\TemplateOverviewPageShowMoreList');
86
    }
87
88
    public function getMyMoreList()
89
    {
90
        return $this->myMoreList;
91
    }
92
93
    public function Link($action = null)
94
    {
95
        $v = '/' . $this->Config()->url_segment;
96
        if ($action) {
97
            $v .= $action . '/';
98
        }
99
100
        return $v;
101
    }
102
103
    public function TestTaskLink()
104
    {
105
        return '/dev/tasks/CheckAllTemplates/';
106
    }
107
108
    public function QuickListLink()
109
    {
110
        return $this->Link('quicklist');
111
    }
112
113
    public function ImagesListLink()
114
    {
115
        return $this->Link('listofobjectsused/' . Image::class);
116
    }
117
118
    public function quicklist()
119
    {
120
        $list = $this->ListOfAllClasses();
121
        foreach ($list as $item) {
122
            DB::alteration_message($item->ClassName);
123
        }
124
    }
125
126
    public function listofobjectsused($request)
127
    {
128
        $classWeAreLookingFor = '\\' . str_replace('-', '\\', $request->param('ID'));
129
        $classWeAreLookingFor = Injector::inst()->get($classWeAreLookingFor);
130
        if ($classWeAreLookingFor instanceof DataObject) {
131
            $list = $this->ListOfAllClasses();
132
            foreach ($list as $item) {
133
                $config = Config::inst();
134
135
                /** @var array $listOfImages */
136
                $a = (array) $config->get($item->ClassName, 'has_one', Config::UNINHERITED);
137
                $b = (array) $config->get($item->ClassName, 'has_many', Config::UNINHERITED);
138
                $c = (array) $config->get($item->ClassName, 'many_many', Config::UNINHERITED);
139
                $listOfImages = array_merge($a, $b, $c);
140
                foreach ($listOfImages as $fieldName => $potentialImage) {
141
                    if (is_array($potentialImage)) {
142
                        $potentialImage = $potentialImage['to'] ?? '';
143
                    }
144
145
                    $potentialImage = (explode('.', $potentialImage))[0];
146
                    if (class_exists($potentialImage)) {
147
                        $innerSingleton = Injector::inst()->get($potentialImage);
148
                        if ($innerSingleton instanceof $classWeAreLookingFor) {
149
                            DB::alteration_message($item->ClassName . '.' . $fieldName);
150
                        }
151
                    }
152
                }
153
            }
154
        } else {
155
            user_error('Please specify the ID for the model you are looking for - e.g. /listofobjectsused/Image/', E_USER_ERROR);
156
        }
157
    }
158
159
    /**
160
     * returns a list of all (SiteTree) Classes.
161
     *
162
     * @return ArrayList
163
     */
164
    public function ListOfAllClasses()
165
    {
166
        $providerClass = Injector::inst()->get($this->Config()->get('base_class_provider'));
167
168
        $originalList = $providerClass->ListOfAllClasses();
169
        $listOfAllClasses = $providerClass->CountsPerClass();
170
        $newList = ArrayList::create();
171
        foreach ($originalList as $obj) {
172
            $count = (int) ($listOfAllClasses[$obj->ClassName] ?? 0);
173
            $newList->push($this->createPageObject($obj, $count));
174
            $this->totalPageCount += $count;
175
        }
176
177
        return $newList;
178
    }
179
180
    public function TotalTemplateCount(): int
181
    {
182
        $className = $this->getBaseClass();
183
184
        return count(ClassInfo::subclassesFor($className)) - 1;
185
    }
186
187
    public function TotalPageCount(): int
188
    {
189
        $this->ListOfAllClasses();
190
        return $this->totalPageCount;
191
    }
192
193
    protected function getBaseClass(): string
194
    {
195
        return $this->Config()->get('base_class');
196
    }
197
198
    protected function init()
199
    {
200
        parent::init();
201
        //important
202
        Versioned::set_stage(Versioned::DRAFT);
203
        if (Director::is_cli() || Director::isDev() || Permission::check('ADMIN')) {
204
            Config::modify()->set(SSViewer::class, 'theme_enabled', false);
205
            Requirements::css('sunnysideup/templateoverview: client/css/TemplateOverviewPage.css');
206
            Requirements::css('silverstripe/admin: client/dist/styles/bundle.css');
207
            Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js');
208
            Requirements::javascript('sunnysideup/templateoverview: client/javascript/TemplateOverviewPage.js');
209
            if (class_exists(PrettyPhoto::class)) {
210
                PrettyPhoto::include_code();
211
            }
212
213
            //user_error("It is recommended that you install the Sunny Side Up Pretty Photo Module", E_USER_NOTICE);
214
        } else {
215
            return Security::permissionFailure($this, 'Please login to access this list');
216
        }
217
    }
218
219
    /**
220
     * @param SiteTree $obj
221
     * @param int      $count
222
     *
223
     * @return ArrayData
224
     */
225
    protected function createPageObject($obj, $count)
226
    {
227
        $parent = $obj->Parent();
228
        $canCreateString = ($obj->canCreate() ? 'Yes' : 'No');
229
        $isAdmin = Permission::check('ADMIN');
230
        $listArray = [];
231
        $listArray['Name'] = 1 === $count ? $obj->i18n_singular_name() : $obj->i18n_plural_name();
232
        $listArray['Description'] = DBField::create_field('HTMLText', $obj->hasMethod('i18n_classDescription') ? $obj->i18n_classDescription() : Config::inst()->get($obj->ClassName, 'description'));
233
        $listArray['ClassName'] = $obj->ClassName;
234
        $listArray['Count'] = $count;
235
        $listArray['ID'] = $obj->ID;
236
        $listArray['URLSegment'] = $obj->URLSegment ?? 'n/a';
237
        $listArray['ControllerLink'] = $this->Link();
238
        $listArray['Title'] = $obj->getTitle();
239
        $listArray['LiveLink'] = $obj->hasMethod('Link') ? str_replace('?stage=Stage', '', (string) $obj->Link()) : 'please-add-Link-method';
240
        $listArray['PreviewLink'] = $obj->hasMethod('PreviewLink') ? $obj->PreviewLink() : 'please-add-PreviewLink-method';
241
        $listArray['CMSEditLink'] = $obj->hasMethod('CMSEditLink') ? $obj->CMSEditLink() : 'please-add-CMSEditLink-method';
242
        $listArray['MoreCanBeCreated'] = $isAdmin ? $canCreateString : 'Please login as ADMIN to see this value';
243
        $listArray['AllowedChildren'] = '';
244
        $listArray['Breadcrumbs'] = $parent ? $parent->Breadcrumbs() : '';
0 ignored issues
show
$parent is of type SilverStripe\CMS\Model\SiteTree, thus it always evaluated to true.
Loading history...
245
        $listArray['Icon'] = $this->getIcon($obj);
246
        if ($obj instanceof SiteTree) {
0 ignored issues
show
$obj is always a sub-type of SilverStripe\CMS\Model\SiteTree.
Loading history...
247
            $children = $this->listOfTitles($obj->allowedChildren());
248
            $listArray['AllowedChildren'] = implode(', ', $children);
249
        }
250
251
        return new ArrayData($listArray);
252
    }
253
254
    protected function getIcon($obj): string
255
    {
256
        $icon = '';
0 ignored issues
show
The assignment to $icon is dead and can be removed.
Loading history...
257
        $icon = $this->getIconInner($obj, 'getPageIconURL');
258
        if (!$icon) {
259
            $icon = $this->getIconInner($obj, 'getIconClass');
260
            if (!$icon) {
261
                $icon = $this->getIconInner($obj, 'getIcon');
262
                if (!$icon) {
263
                    return (string) LeftAndMain::menu_icon_for_class($obj->ClassName);
264
                }
265
            }
266
        }
267
268
        if (!$icon) {
269
            $icon = 'font-page';
270
        }
271
272
        if (false === strpos('.', $icon)) {
273
            // $icon = str_replace('font-icon-', 'fa-', $icon);
274
        }
275
276
        return $icon;
277
    }
278
279
    protected function getIconInner($obj, $method): ?string
280
    {
281
        if ($obj->hasMethod($method)) {
282
            $icon = (string) $obj->{$method}();
283
            if ($icon) {
284
                return $icon;
285
            }
286
        }
287
288
        return null;
289
    }
290
291
    protected function listOfTitles($array)
292
    {
293
        if (is_array($array) && count($array)) {
294
            $newArray = [];
295
            foreach ($array as $item) {
296
                $obj = Injector::inst()->get($item);
297
                $newArray[] = $obj->i18n_singular_name();
298
            }
299
300
            return $newArray;
301
        }
302
303
        return ['none'];
304
    }
305
}
306