1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package simple-styleguide |
4
|
|
|
*/ |
5
|
|
|
class SimpleStyleguideController extends Controller |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @config |
9
|
|
|
* @var array |
10
|
|
|
*/ |
11
|
|
|
private static $color_swatches = []; |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private static $allowed_actions = [ |
|
|
|
|
17
|
|
|
'index', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Runs the permissiion checks, and setup of the controller view. |
22
|
|
|
*/ |
23
|
|
|
public function index() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
if (!Director::isDev() && !Permission::check('ADMIN')) { |
26
|
|
|
return Security::permissionFailure(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// If the subsite module is installed then set the theme based on the current subsite |
30
|
|
|
if (class_exists('Subsite') && Subsite::currentSubsite()) { |
31
|
|
|
Config::inst()->update('SSViewer', 'theme', Subsite::currentSubsite()->Theme); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$page = Page::get()->first(); |
35
|
|
|
$controller = ModelAsController::controller_for($page); |
36
|
|
|
$controller->init(); |
37
|
|
|
|
38
|
|
|
// requirements |
39
|
|
|
Requirements::css('simple-styleguide/css/styleguide.css'); |
40
|
|
|
|
41
|
|
|
return $controller |
42
|
|
|
->customise($this->getStyleGuideData()) |
43
|
|
|
->renderWith(['SimpleStyleguideController', 'Page']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Provides access to any custom function on the controller for use on the template output. |
48
|
|
|
* @return ArrayData |
49
|
|
|
*/ |
50
|
|
|
public function getStyleguideData() |
51
|
|
|
{ |
52
|
|
|
$data = new ArrayData([ |
53
|
|
|
'Title' => 'Styleguide', |
54
|
|
|
'Message' => DBField::create_field( |
55
|
|
|
'HTMLText', |
56
|
|
|
'<p>This controller is only accessible to developers and admin users.</p>' |
57
|
|
|
), |
58
|
|
|
'TestForm' => $this->getTestForm(), |
59
|
|
|
'Content' => $this->getContent(), |
60
|
|
|
'ColorSwatches' => $this->getColorSwatches(), |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
// extensions for adding/overriding template data. |
64
|
|
|
$this->extend('updateStyleguideData', $data); |
65
|
|
|
|
66
|
|
|
return $data; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return a form with fields to match rendering through controller/template output. |
71
|
|
|
* @return Form |
72
|
|
|
*/ |
73
|
|
|
public function getTestForm() |
74
|
|
|
{ |
75
|
|
|
$fields = FieldList::create( |
76
|
|
|
TextField::create('SimpleText', 'Simple Text Field'), |
77
|
|
|
TextField::create('SimpleTextGood', 'Simple Text Field (good)') |
78
|
|
|
->setError('This is a good message', 'good'), |
79
|
|
|
TextField::create('SimpleTextWarning', 'Simple Text Field (warning)') |
80
|
|
|
->setError('This is a warning message', 'warning'), |
81
|
|
|
TextField::create('SimpleTextBad', 'Simple Text Field (bad)') |
82
|
|
|
->setError('This is an error message', 'bad'), |
83
|
|
|
NumericField::create('Number', 'Number Field'), |
84
|
|
|
EmailField::create('Email', "Email Field"), |
85
|
|
|
DropdownField::create('Dropdown', 'Normal dropdown', [ |
86
|
|
|
'1' => 'One option', |
87
|
|
|
'2' => 'Two option', |
88
|
|
|
]), |
89
|
|
|
CheckboxField::create('Checkbox', 'Checkbox'), |
90
|
|
|
CheckboxSetField::create('CheckboxSet', 'Checkbox set', [ |
91
|
|
|
'1' => 'One option', |
92
|
|
|
'2' => 'Two option', |
93
|
|
|
'3' => 'Three option', |
94
|
|
|
]), |
95
|
|
|
OptionsetField::create('Option', 'Option', [ |
96
|
|
|
'1' => 'One option', |
97
|
|
|
]), |
98
|
|
|
OptionsetField::create('OptionSet', 'Option set', [ |
99
|
|
|
'1' => 'One option', |
100
|
|
|
'2' => 'Two option', |
101
|
|
|
'3' => 'Three option', |
102
|
|
|
]), |
103
|
|
|
TextField::create('Text', 'Text') |
104
|
|
|
->setDescription('This is a description') |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$actions = FieldList::create( |
108
|
|
|
FormAction::create('doForm', 'Submit') |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$required = new RequiredFields( |
112
|
|
|
'SimpleText', |
113
|
|
|
'Email', |
114
|
|
|
'Checkbox', |
115
|
|
|
'Dropdown' |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$form = new Form($this, 'TestForm', $fields, $actions, $required); |
119
|
|
|
$form->setMessage('This is a form wide message. See the alerts component for site wide messages.', 'warning'); |
120
|
|
|
|
121
|
|
|
return $form; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Emulate an HTMLEditorField output useful for testing shortcodes and output extensions etc. |
126
|
|
|
* @return HTMLText |
127
|
|
|
*/ |
128
|
|
|
public function getContent() |
129
|
|
|
{ |
130
|
|
|
$content = ''; |
131
|
|
|
|
132
|
|
|
// add file link to html content |
133
|
|
|
$file = File::get()->filter('ClassName', 'File')->first(); |
134
|
|
|
if ($file) { |
135
|
|
|
$content .= '<p>This is an internal <a href="[file_link,id=' . $file->ID . ']">link to a file</a> inside content</p>'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// add external link to html content |
139
|
|
|
$content .= '<p>This is an external <a href="http://google.com">link to google</a> inside content.</p>'; |
140
|
|
|
|
141
|
|
|
return DBField::create_field('HTMLText', $content); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return ArrayList |
146
|
|
|
*/ |
147
|
|
|
public function getColorSwatches() |
148
|
|
|
{ |
149
|
|
|
$list = ArrayList::create(); |
150
|
|
|
$colors = $this->config()->color_swatches; |
151
|
|
|
|
152
|
|
|
if ($colors) { |
153
|
|
|
foreach ($colors as $color) { |
154
|
|
|
$list->push(ArrayData::create($color)); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $list; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.