Completed
Pull Request — master (#9)
by
unknown
01:12
created

getPlaceholderImageURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace BenManu\SimpleStyleguide;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Security\Permission;
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\CMS\Controllers\ModelAsController;
10
use SilverStripe\View\Requirements;
11
use SilverStripe\View\ArrayData;
12
use SilverStripe\ORM\FieldType\DBField;
13
use SilverStripe\ORM\ArrayList;
14
use SilverStripe\Forms\FieldList;
15
use SilverStripe\Forms\TextField;
16
use SilverStripe\Forms\NumericField;
17
use SilverStripe\Forms\EmailField;
18
use SilverStripe\Forms\DropdownField;
19
use SilverStripe\Forms\CheckboxField;
20
use SilverStripe\Forms\CheckboxSetField;
21
use SilverStripe\Forms\OptionsetField;
22
use SilverStripe\Forms\FormAction;
23
use SilverStripe\Forms\RequiredFields;
24
use SilverStripe\Forms\Form;
25
use SilverStripe\Assets\File;
26
27
/**
28
 * @package simple-styleguide
29
 */
30
class SimpleStyleguideController extends Controller
31
{
32
    /**
33
     * @config
34
     * @var array
35
     */
36
    private static $color_swatches = [];
0 ignored issues
show
Unused Code introduced by
The property $color_swatches is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
38
    /**
39
     * @config
40
     * @var string
41
     */
42
    private static $placeholder_image_url = '/simple-styleguide/images/placeholder.png';
0 ignored issues
show
Unused Code introduced by
The property $placeholder_image_url is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
43
44
    /**
45
     * @var array
46
     */
47
    private static $allowed_actions = [
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
48
        'index',
49
    ];
50
51
    private static $url_segment = '_styleguide';
0 ignored issues
show
Unused Code introduced by
The property $url_segment is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
52
53
    /**
54
     * Runs the permissiion checks, and setup of the controller view.
55
     */
56
    public function index()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
57
    {
58
        if (!Director::isDev() && !Permission::check('ADMIN')) {
59
            return Security::permissionFailure();
60
        }
61
62
        // If the subsite module is installed then set the theme based on the current subsite
63
        if (class_exists('Subsite') && Subsite::currentSubsite()) {
64
            Config::inst()->update('SSViewer', 'theme', Subsite::currentSubsite()->Theme);
65
        }
66
67
        $page = SiteTree::get()->first();
68
        $controller = ModelAsController::controller_for($page);
69
        $controller->init();
70
71
        // requirements
72
        Requirements::css('simple-styleguide/css/styleguide.css');
73
        Requirements::javascript('simple-styleguide/js/styleguide.js');
74
75
        return $controller
76
            ->customise($this->getStyleGuideData())
77
            ->renderWith(['SimpleStyleguideController', 'Page']);
78
    }
79
80
    /**
81
     * Provides access to any custom function on the controller for use on the template output.
82
     * @return ArrayData
83
     */
84
    public function getStyleguideData()
85
    {
86
        $data = new ArrayData([
87
            'Title' => 'Styleguide',
88
            'Message' => DBField::create_field(
89
                'HTMLText',
90
                '<p>This controller is only accessible to developers and admin users.</p>'
91
            ),
92
            'TestForm' => $this->getTestForm(),
93
            'Content' => $this->getContent(),
94
            'ColorSwatches' => $this->getColorSwatches(),
95
            'PlaceholderImageURL' => $this->getPlaceholderImageURL(),
96
        ]);
97
98
        // extensions for adding/overriding template data.
99
        $this->extend('updateStyleguideData', $data);
100
101
        return $data;
102
    }
103
104
    /**
105
     * Return a form with fields to match rendering through controller/template output.
106
     * @return Form
107
     */
108
    public function getTestForm()
109
    {
110
        $fields = FieldList::create(
111
            TextField::create('SimpleText', 'Simple Text Field'),
112
            TextField::create('SimpleTextGood', 'Simple Text Field (good)'),
113
            TextField::create('SimpleTextWarning', 'Simple Text Field (warning)'),
114
            TextField::create('SimpleTextBad', 'Simple Text Field (bad)'),
115
            NumericField::create('Number', 'Number Field'),
116
            EmailField::create('Email', "Email Field"),
117
            DropdownField::create('Dropdown', 'Normal dropdown', [
118
                '1' => 'One option',
119
                '2' => 'Two option',
120
            ]),
121
            CheckboxField::create('Checkbox', 'Checkbox'),
122
            CheckboxSetField::create('CheckboxSet', 'Checkbox set', [
123
                '1' => 'One option',
124
                '2' => 'Two option',
125
                '3' => 'Three option',
126
            ]),
127
            OptionsetField::create('Option', 'Option', [
128
                '1' => 'One option',
129
            ]),
130
            OptionsetField::create('OptionSet', 'Option set', [
131
                '1' => 'One option',
132
                '2' => 'Two option',
133
                '3' => 'Three option',
134
            ]),
135
            TextField::create('Text', 'Text')
136
                ->setDescription('This is a description')
137
        );
138
139
        $actions = FieldList::create(
140
            FormAction::create('doForm', 'Submit')
141
        );
142
143
        $required = new RequiredFields(
144
            'SimpleText',
145
            'Email',
146
            'Checkbox',
147
            'Dropdown'
148
        );
149
150
        $form = new Form($this, 'TestForm', $fields, $actions, $required);
151
        $form->setMessage('This is a form wide message. See the alerts component for site wide messages.', 'warning');
152
153
        $this->extend('updateForm', $form);
154
155
        return $form;
156
    }
157
158
    /**
159
     * Emulate an HTMLEditorField output useful for testing shortcodes and output extensions etc.
160
     * @return HTMLText
161
     */
162
    public function getContent()
163
    {
164
        $content = '';
165
166
        // add file link to html content
167
        $file = File::get()->filter('ClassName', 'File')->first();
168
        if ($file) {
169
            $content .= '<p>This is an internal <a href="[file_link,id=' . $file->ID . ']">link to a file</a> inside content</p>';
170
        }
171
172
        // add external link to html content
173
        $content .= '<p>This is an external <a href="http://google.com">link to google</a> inside content.</p>';
174
175
        $this->extend('updateContent', $content);
176
177
        return DBField::create_field('HTMLText', $content);
178
    }
179
180
    /**
181
     * @return ArrayList
182
     */
183
    public function getColorSwatches()
184
    {
185
        $list = ArrayList::create();
186
        $colors = $this->config()->color_swatches;
187
188
        if ($colors) {
189
            foreach ($colors as $color) {
190
                $list->push(ArrayData::create($color));
191
            }
192
        }
193
194
        $this->extend('updateColorSwatches', $list);
195
196
        return $list;
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    public function getPlaceholderImageURL()
203
    {
204
        $url = $this->config()->placeholder_image_url;
205
206
        $this->extend('updatePlaceholderImageURL', $url);
207
208
        return $url;
209
    }
210
}
211