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