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