Passed
Push — master ( 9668c8...0b0bcd )
by Nicolaas
02:11
created

TemplateOverviewPageController::Link()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
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 SilverStripe\CMS\Model\SiteTree;
10
11
use SilverStripe\Core\ClassInfo;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Control\Director;
14
use SilverStripe\Control\HTTPRequest;
15
use SilverStripe\Security\Security;
16
use SilverStripe\Security\Member;
17
use SilverStripe\Security\Permission;
18
use SilverStripe\View\Requirements;
19
20
use SilverStripe\Core\Config\Config;
21
22
use SilverStripe\ORM\DB;
23
use SilverStripe\ORM\DataObject;
24
25
use Sunnysideup\PrettyPhoto\PrettyPhoto;
26
use Sunnysideup\TemplateOverview\Api\SiteTreeDetails;
27
28
use \PageController;
29
use \Page;
30
31
class TemplateOverviewPageController extends PageController
32
{
33
34
    private static $url_segment = 'templates';
35
36
    /**
37
     * The ContentController will take the URLSegment parameter from the URL and use that to look
38
     * up a SiteTree record.
39
     *
40
     * @param SiteTree $dataRecord
41
     */
42
    public function __construct($dataRecord = null)
43
    {
44
45
        $this->dataRecord = Page::get()->first();
46
47
        parent::__construct($this->dataRecord);
48
49
    }
50
51
    private static $allowed_actions = [
52
        "showmore" => true,
53
        "quicklist" => true,
54
        "listofobjectsused" => true,
55
    ];
56
57
    public function init()
58
    {
59
        parent::init();
60
        if (! Director::is_cli() && !Director::isDev() && ! Permission::check('ADMIN')) {
61
            return Security::permissionFailure();
62
        }
63
        Requirements::javascript('sunnysideup/templateoverview: client/javascript/TemplateOverviewPage.js');
64
        Requirements::css("sunnysideup/templateoverview: client/css/TemplateOverviewPage.css");
65
        if (class_exists(PrettyPhoto::class)) {
66
            PrettyPhoto::include_code();
67
        } else {
68
            //user_error("It is recommended that you install the Sunny Side Up Pretty Photo Module", E_USER_NOTICE);
69
        }
70
    }
71
72
    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

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