Passed
Push — master ( 430fc9...c7d62c )
by Nicolaas
02:07
created

TemplateOverviewPageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 *@author: nicolaas [at] sunnysideup.co.nz
4
 *@description Add a page to your site that allows you to view all the html that can be used in the typography section - if applied correctly.
5
 */
6
7
namespace Sunnysideup\TemplateOverview;
8
9
use \Page;
10
11
use \PageController;
12
use SilverStripe\CMS\Model\SiteTree;
13
use SilverStripe\CMS\Controllers\ContentController;
14
use SilverStripe\Control\Director;
15
use SilverStripe\Control\HTTPRequest;
16
use SilverStripe\Core\ClassInfo;
17
use SilverStripe\Core\Config\Config;
18
use SilverStripe\Core\Injector\Injector;
19
20
use SilverStripe\ORM\DataObject;
21
22
use SilverStripe\ORM\DB;
23
use SilverStripe\Security\Permission;
24
25
use SilverStripe\Security\Security;
26
use SilverStripe\View\Requirements;
27
28
use Sunnysideup\PrettyPhoto\PrettyPhoto;
29
use Sunnysideup\TemplateOverview\Api\SiteTreeDetails;
30
31
class TemplateOverviewPageController extends ContentController
32
{
33
    private static $url_segment = 'templates';
34
35
    private static $allowed_actions = [
36
        'showmore' => true,
37
        'quicklist' => true,
38
        'listofobjectsused' => true,
39
    ];
40
41
42
43
    public function init()
44
    {
45
        parent::init();
46
        if (Director::is_cli() || Director::isDev() || Permission::check('ADMIN')) {
47
            Requirements::javascript('//code.jquery.com/jquery-1.7.2.min.js');
48
            Requirements::javascript('sunnysideup/templateoverview: client/javascript/TemplateOverviewPage.js');
49
            Requirements::css('sunnysideup/templateoverview: client/css/TemplateOverviewPage.css');
50
            if (class_exists(PrettyPhoto::class)) {
51
                PrettyPhoto::include_code();
52
            }
53
            //user_error("It is recommended that you install the Sunny Side Up Pretty Photo Module", E_USER_NOTICE);
54
        } else{
55
            return Security::permissionFailure();
56
        }
57
    }
58
59
    public function index(HTTPRequest $request = null)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

59
    public function index(/** @scrutinizer ignore-unused */ HTTPRequest $request = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        $this->renderWith(['Page', 'Page']);
62
        return [];
63
    }
64
65
    public function Content()
66
    {
67
        return $this->renderWith('Sunnysideup\\TemplateOverview\\Includes\\TemplateOverviewList');
68
    }
69
70
    public function showmore($request)
71
    {
72
        $id = $request->param('ID');
73
        $obj = SiteTree::get()->byID(intval($id));
74
        if ($obj) {
0 ignored issues
show
introduced by
$obj is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
75
            $className = $obj->ClassName;
76
            $data = $className::get()
77
                ->filter(['ClassName' => $obj->ClassName])
78
                ->limit(200);
79
            $array = [
80
                'Results' => $data,
81
            ];
82
        } else {
83
            $array = [];
84
        }
85
        return $this->customise($array)->renderWith('Sunnysideup\\TemplateOverview\\TemplateOverviewPageShowMoreList');
86
    }
87
88
    public function Link($action = null)
89
    {
90
        $v = '/' . $this->Config()->url_segment;
91
        if ($action) {
92
            $v .= $action . '/';
93
        }
94
95
        return $v;
96
    }
97
98
    public function TestTaskLink()
99
    {
100
        return '/dev/tasks/CheckAllTemplates/';
101
    }
102
103
    public function QuickListLink()
104
    {
105
        return $this->Link('quicklist');
106
    }
107
108
    public function ImagesListLink()
109
    {
110
        return $this->Link('listofobjectsused/Image');
111
    }
112
113
    public function quicklist()
114
    {
115
        $list = $this->ListOfAllSiteTreeClasses();
116
        foreach ($list as $item) {
117
            DB::alteration_message($item->ClassName);
118
        }
119
    }
120
121
    public function listofobjectsused($request)
122
    {
123
        $classWeAreLookingFor = $request->param('ID');
124
        $classWeAreLookingFor = Injector::inst()->get($classWeAreLookingFor);
125
        if ($classWeAreLookingFor instanceof DataObject) {
126
            $list = $this->ListOfAllSiteTreeClasses();
127
            foreach ($list as $item) {
128
                $config = Config::inst();
129
                $listOfImages = $config->get($item->ClassName, 'has_one')
130
                 + $config->get($item->ClassName, 'has_many')
131
                 + $config->get($item->ClassName, 'many_many');
132
                foreach ($listOfImages as $fieldName => $potentialImage) {
133
                    $innerSingleton = singleton($potentialImage);
134
                    if ($innerSingleton instanceof $classWeAreLookingFor) {
135
                        DB::alteration_message($item->ClassName . '.' . $fieldName);
136
                    }
137
                }
138
            }
139
        } else {
140
            user_error('Please specify the ID for the model you are looking for - e.g. /listofobjectsused/Image/', E_USER_ERROR);
141
        }
142
    }
143
144
    /**
145
     * returns a list of all SiteTree Classes
146
     * @return Array(String)
147
     */
148
    public function ListOfAllSiteTreeClasses()
149
    {
150
        $siteTreeDetails = Injector::inst()->get(SiteTreeDetails::class);
151
152
        return $siteTreeDetails->ListOfAllSiteTreeClasses();
153
    }
154
155
    public function TotalCount()
156
    {
157
        return count(ClassInfo::subclassesFor(SiteTree::class)) - 1;
158
    }
159
}
160