-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathShortcodableController.php
More file actions
251 lines (222 loc) · 8.26 KB
/
ShortcodableController.php
File metadata and controls
251 lines (222 loc) · 8.26 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace Silverstripe\Shortcodable\Controller;
use Silverstripe\Shortcodable;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\Form;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Permission;
/**
* ShortcodableController.
*
* @author shea@livesource.co.nz
**/
class ShortcodableController extends LeftAndMain
{
private static $sc_url_segment = 'shortcodable';
private static $required_permission_codes = 'CMS_ACCESS_LeftAndMain';
/**
* @var array
*/
private static $allowed_actions = array(
'ShortcodeForm' => 'CMS_ACCESS_LeftAndMain',
'handleEdit' => 'CMS_ACCESS_LeftAndMain',
'shortcodePlaceHolder' => 'CMS_ACCESS_LeftAndMain'
);
/**
* @var array
*/
private static $url_handlers = array(
'edit/$ShortcodeType!/$Action//$ID/$OtherID' => 'handleEdit'
);
/**
* @var string
*/
protected $shortcodableclass;
/**
* @var boolean
*/
protected $isnew = true;
/**
* @var array
*/
protected $shortcodedata;
/**
* Get the shortcodable class by whatever means possible.
* Determine if this is a new shortcode, or editing an existing one.
*/
public function init()
{
parent::init();
if ($data = $this->getShortcodeData()) {
$this->isnew = false;
$this->shortcodableclass = $data['name'];
} elseif ($type = $this->request->requestVar('ShortcodeType')) {
$this->shortcodableclass = $type;
} else {
$this->shortcodableclass = $this->request->param('ShortcodeType');
}
}
/**
* Point to edit link, if shortcodable class exists.
*/
public function Link($action = null)
{
if ($this->shortcodableclass) {
return Controller::join_links(
$this->config()->url_base,
$this->config()->sc_url_segment,
'edit',
$this->shortcodableclass
);
}
return Controller::join_links($this->config()->url_base, $this->config()->sc_url_segment, $action);
}
/**
* handleEdit
*/
public function handleEdit(HTTPRequest $request)
{
$this->shortcodableclass = $request->param('ShortcodeType');
return $this->handleAction($request, $action = $request->param('Action'));
}
/**
* Get the shortcode data from the request.
* @return array shortcodedata
*/
protected function getShortcodeData()
{
if($this->shortcodedata){
return $this->shortcodedata;
}
$data = false;
if($shortcode = $this->request->requestVar('Shortcode')){
//remove BOM inside string on cursor position...
$shortcode = str_replace("\xEF\xBB\xBF", '', $shortcode);
$data = singleton('\Silverstripe\Shortcodable\ShortcodableParser')->the_shortcodes(array(), $shortcode);
if(isset($data[0])){
$this->shortcodedata = $data[0];
return $this->shortcodedata;
}
}
}
/**
* Provides a GUI for the insert/edit shortcode popup.
*
* @return Form
**/
public function ShortcodeForm()
{
Config::inst()->update('SSViewer', 'theme_enabled', false);
$classes = Shortcodable::get_shortcodable_classes_fordropdown();
$classname = $this->shortcodableclass;
if ($this->isnew) {
$headingText = _t('Shortcodable.EDITSHORTCODE', 'Edit Shortcode');
} else {
$headingText = sprintf(
_t('Shortcodable.EDITSHORTCODE', 'Edit %s Shortcode'),
singleton($this->shortcodableclass)->singular_name()
);
}
// essential fields
$fields = FieldList::create(array(
CompositeField::create(
LiteralField::create(
'Heading',
sprintf('<h3 class="htmleditorfield-shortcodeform-heading insert">%s</h3>', $headingText)
)
)->addExtraClass('CompositeField composite cms-content-header nolabel'),
LiteralField::create('shortcodablefields', '<div class="ss-shortcodable content">'),
DropdownField::create('ShortcodeType', _t('Shortcodable.SHORTCODETYPE', 'Shortcode type'), $classes, $classname)
->setHasEmptyDefault(true)
->addExtraClass('shortcode-type')
));
// attribute and object id fields
if ($classname && class_exists($classname)) {
$class = singleton($classname);
if (is_subclass_of($class, 'DataObject')) {
if (singleton($classname)->hasMethod('getShortcodableRecords')) {
$dataObjectSource = singleton($classname)->getShortcodableRecords();
} else {
$dataObjectSource = $classname::get()->map()->toArray();
}
$fields->push(
DropdownField::create('id', $class->singular_name(), $dataObjectSource)
->setHasEmptyDefault(true)
);
}
if (singleton($classname)->hasMethod('getShortcodeFields')) {
if ($attrFields = singleton($classname)->getShortcodeFields()) {
$fields->push(
CompositeField::create($attrFields)
->addExtraClass('attributes-composite')
->setName('AttributesCompositeField')
);
}
}
}
// actions
$actions = FieldList::create(array(
FormAction::create('insert', _t('Shortcodable.BUTTONINSERTSHORTCODE', 'Insert shortcode'))
->addExtraClass('ss-ui-action-constructive')
->setAttribute('data-icon', 'accept')
->setUseButtonTag(true)
));
// form
$form = Form::create($this, 'ShortcodeForm', $fields, $actions)
->loadDataFrom($this)
->addExtraClass('htmleditorfield-form htmleditorfield-shortcodable cms-dialog-content');
$this->extend('updateShortcodeForm', $form);
$fields->push(LiteralField::create('shortcodablefieldsend', '</div>'));
if ($data = $this->getShortcodeData()) {
$form->loadDataFrom($data['atts']);
// special treatment for setting value of UploadFields
foreach ($form->Fields()->dataFields() as $field) {
if (is_a($field, 'UploadField') && isset($data['atts'][$field->getName()])) {
$field->setValue(array('Files' => explode(',', $data['atts'][$field->getName()])));
}
}
}
return $form;
}
/**
* Generates shortcode placeholder to display inside TinyMCE instead of the shortcode.
*
* @return void
*/
public function shortcodePlaceHolder($request)
{
if (!Permission::check('CMS_ACCESS_CMSMain')) {
return;
}
$classname = $request->param('ID');
$id = $request->param('OtherID');
if (!class_exists($classname)) {
return;
}
if ($id && is_subclass_of($classname, DataObject::class)) {
$object = $classname::get()->byID($id);
} else {
$object = singleton($classname);
}
if ($object->hasMethod('getShortcodePlaceHolder')) {
$attributes = null;
if ($shortcode = $request->requestVar('Shortcode')) {
$shortcode = str_replace("\xEF\xBB\xBF", '', $shortcode); //remove BOM inside string on cursor position...
$shortcodeData = singleton('\Silverstripe\Shortcodable\ShortcodableParser')->the_shortcodes(array(), $shortcode);
if (isset($shortcodeData[0])) {
$attributes = $shortcodeData[0]['atts'];
}
}
$link = $object->getShortcodePlaceholder($attributes);
return $this->redirect($link);
}
}
}