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