-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHannaCodeDialog.module
More file actions
382 lines (340 loc) · 13.5 KB
/
HannaCodeDialog.module
File metadata and controls
382 lines (340 loc) · 13.5 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php namespace ProcessWire;
class HannaCodeDialog extends WireData implements Module, ConfigurableModule {
/**
* Module information
*/
public static function getModuleInfo() {
return array(
'title' => 'Hanna Code Dialog',
'version' => '0.5.2',
'summary' => 'Enhances the use of Hanna tags in CKEditor fields, including the dialog-based editing of Hanna tags.',
'author' => 'Robin Sallis',
'icon' => 'clone',
'autoload' => 'template=admin',
'singular' => true,
'requires' => 'ProcessWire>=3.0.0, TextformatterHannaCode>=0.3.0, PHP>=5.4.0',
);
}
/**
* Array of Hanna tags that have no attributes
*/
protected $tags_no_attributes = [];
/**
* Ready
*/
public function ready() {
$this->addHookAfter('ProcessPageEdit::execute', $this, 'beforeEditProcess');
$this->addHookAfter('ProcessProfile::execute', $this, 'beforeEditProcess');
$this->addHookBefore('ProcessLogin::executeLogout', $this, 'renderForm');
$this->addHookAfter('InputfieldCKEditor::renderReadyHook', $this, 'afterCkeRenderReady');
$this->addHookBefore('InputfieldForm::render', $this, 'addCheatSheet');
}
/**
* Before edit Process executes
* ProcessPageEdit, ProcessUser, ProcessProfile
*
* @param HookEvent $event
*/
protected function beforeEditProcess(HookEvent $event) {
$config = $this->wire()->config;
$process = $event->wire()->process;
// Get edited page
if($process instanceof ProcessProfile) {
$edited_page = $event->wire()->user;
$process_name = 'ProcessProfile';
} else {
$edited_page = $event->object->getPage();
$process_name = $process->className();
}
// To config JS
$settings = [];
$data = $this->wire()->modules->getModuleConfigData('TextformatterHannaCode');
$settings['plugins_url'] = $config->urls->{$this} . 'plugins/';
$settings['iframe_path'] = "{$config->urls->admin}login/logout/?pid={$edited_page->id}&process=$process_name&modal=1";
$settings['open_tag'] = !empty($data['openTag']) ? $data['openTag'] : '[[';
$settings['close_tag'] = !empty($data['closeTag']) ? $data['closeTag'] : ']]';
$settings['hanna_tags'] = $this->getDropdownTags($edited_page);
$settings['tags_no_attributes'] = $this->tags_no_attributes;
$settings['widget_colour'] = $this->widget_colour ?: '#d3eaf2';
$settings['dialog_width'] = $this->dialog_width ?: 360;
$settings['dialog_height'] = $this->dialog_height ?: 420;
$settings['dropdown_title'] = $this->_('Insert Hanna tag');
$settings['dialog_title'] = $this->_('Hanna Code Dialog');
$config->js($this->className, $settings);
}
/**
* Render dialog form
*
* @param HookEvent $event
*/
protected function renderForm(HookEvent $event) {
$config = $this->wire()->config;
$input = $this->wire()->input;
$admin_theme = $this->wire()->adminTheme;
$hanna = $this->wire()->modules->get('TextformatterHannaCode');
// Required parameters must be present
$tag = $input->get->textarea('tag');
$process_name = $input->get->text('process');
$id = (int) $input->get('pid');
$inputfield_name = $input->get->fieldName('field_name');
if(!$tag || !$process_name || !$id || !$inputfield_name) return;
// Replace the method
$event->replace = true;
// Get edited page
if($process_name === 'ProcessProfile') {
$edited_page = $event->wire()->user;
} else {
$edited_page = $this->wire()->pages->get($id);
// Return early if the page is not editable
if(!$edited_page->id || !$edited_page->editable) {
$event->return = $this->_("You don't have permission to edit this page.");
return;
}
}
// Add class to body tag
$admin_theme->addBodyClass('HannaCodeDialog');
// Get current attributes
$current_attributes = $hanna->getAttributes($tag);
if(!isset($current_attributes['name'])) $current_attributes['name'] = trim($tag);
// Render form
$vars = [
'current_attributes' => $current_attributes,
'hanna_tags' => $this->getHannaTags(),
'edited_page' => $edited_page,
'inputfield_name' => $inputfield_name,
];
$options = ['defaultPath' => $config->paths->{$this}];
$event->return = $this->wire()->files->render('iframe_form', $vars, $options);
}
/**
* Get Hanna Code tags from database
*
* @param bool $name_only
* @param bool $filter_tags
* @return array
*/
public function getHannaTags($name_only = false, $filter_tags = false) {
$hanna = $this->wire()->modules->get('TextformatterHannaCode');
$tags = [];
$tags_no_attributes = [];
$hanna_codes = $hanna->hannaCodes()->getAll();
foreach($hanna_codes as $hanna_code) {
// If this tag has no attributes then add it to $tags_no_attributes
if(empty($hanna_code->attrs)) $tags_no_attributes[] = $hanna_code->name;
if($name_only) {
if($filter_tags) {
$exclude_prefix = $this->exclude_prefix ?: '_';
if((is_array($this->exclude_selection) && in_array($hanna_code->name, $this->exclude_selection)) || strpos($hanna_code->name, $exclude_prefix) === 0) continue;
}
$tags[] = $hanna_code->name;
} else {
$tags[$hanna_code->name] = $hanna_code->attrs;
}
}
$this->tags_no_attributes = $tags_no_attributes;
return $tags;
}
/**
* Get Hanna Code tags for toolbar dropdown
*
* @param Page $page The page being edited
* @return array Array of tag names
*
*/
public function ___getDropdownTags($page) {
return $this->getHannaTags(true, true);
}
/**
* Build dialog form
*
* @param string $tag_name
* @param Page $edited_page
* @param array $current_attributes
* @param array $default_attributes
* @return InputfieldForm $form
*/
public function ___buildForm($tag_name, $edited_page, $current_attributes, $default_attributes) {
/* @var InputfieldForm $form */
$form = $this->modules->get('InputfieldForm');
$form->attr('id+name', 'hanna-form');
$form->attr('data-name', $tag_name);
return $form;
}
/**
* Prepare options for use in dialog inputfield
*
* @param string $options_string String of options delimited with a pipe character
* @param string $attribute_name The name of the attribute the options are for
* @param string $tag_name The name of the Hanna tag
* @param object $page The page being edited
* @return array A regular array of option values, or an associative array $value => $label
*
*/
public function ___prepareOptions($options_string, $attribute_name, $tag_name, $page) {
$options = explode('|', $options_string);
return $options;
}
/**
* After InputfieldCKEditor::renderReadyHook
* Add extra CKEditor plugins
*
* @param HookEvent $event
*/
protected function afterCkeRenderReady(HookEvent $event) {
/** @var InputfieldCKEditor $inputfield */
$inputfield = $event->object;
$field = $inputfield->hasField;
if(!$field) return;
$config = $this->wire()->config;
if(!$this->force_plugins) {
$textformatters = $field->textformatters;
if(!is_array($textformatters) || !in_array('TextformatterHannaCode', $textformatters)) return;
}
$config_name = "InputfieldCKEditor_{$field->name}";
$config_name_matrix = "InputfieldCKEditor_{$field->name}_matrix";
$config_name_repeater_context = "InputfieldCKEditor_{$field->name}_in_";
$js_config = $config->js();
foreach($js_config as $key => $value) {
if(
$key === $config_name ||
strpos($key, $config_name_matrix) === 0 ||
strpos($key, $config_name_repeater_context) === 0 ||
isset($value['extraPlugins']) // ProFields Table
) {
if(strpos($js_config[$key]['extraPlugins'], 'hannadropdown,hannadialog') === false) {
$js_config[$key]['extraPlugins'] .= ',hannadropdown,hannadialog';
}
$config->js($key, $js_config[$key]);
}
}
// Add assets
$info = $this->getModuleInfo();
$version = $info['version'];
$config->styles->add($config->urls->{$this} . "{$this}.css?v={$version}");
$config->scripts->add($config->urls->{$this} . "{$this}.js?v={$version}");
// CKEditor JS settings
$settings = $config->js('InputfieldCKEditor');
// Modify CKEditor timestamp property for cache busting of plugin files
$settings['timestamp'] .= '.' . $version;
$config->js('InputfieldCKEditor', $settings);
}
/**
* Add cheatsheet info to Hanna Code edit
*
* @param HookEvent $event
*/
public function addCheatSheet(HookEvent $event) {
/* @var InputfieldForm $form */
$form = $event->object;
if($form->id !== 'HannaCodeEdit') return;
$hc_attr = $form->getChildByName('hc_attr');
$f = $this->wire()->modules->get('InputfieldMarkup');
$f->name = 'hcd_info';
$f->label = $this->_('Attributes cheatsheet for HannaCodeDialog');
$f->value = <<<EOT
<style>
.hcd-cheatsheet { padding:15px 0; }
.hcd-cheatsheet p { margin:10px 0; }
.hcd-cheatsheet h3 { font-size:18px; color:#686868; margin:20px 0 10px; }
.hcd-cheatsheet .mono { font-family:monospace; font-size:15px; background:#ececec; display:inline-block; padding:0 5px; border-radius:3px; margin:0 3px; color:#000; }
.hcd-cheatsheet .mono-block { padding:5px 10px; margin:0; display:block; }
</style>
<div class='hcd-cheatsheet'>
<p>Use a double underscore to add <span class='mono'>type</span>, <span class='mono'>options</span>, <span class='mono'>description</span> or <span class='mono'>notes</span> settings for an attribute.</p>
<p><strong>For an attributed named 'vegetables'...</strong></p>
<h3>Inputfield type</h3>
<p class='mono mono-block'>vegetables__type=asmselect</p>
<p>The default type for text attributes is <span class='mono'>text</span>. The default type for attributes with options specified is <span class='mono'>select</span>.</p>
<p>Inputfield types are case insensitive. Supported types are <span class='mono'>text</span> <span class='mono'>textarea</span> <span class='mono'>select</span> <span class='mono'>radios</span> <span class='mono'>selectmultiple</span> <span class='mono'>asmselect</span> <span class='mono'>checkboxes</span> <span class='mono'>checkbox</span> <span class='mono'>pagelistselect</span> <span class='mono'>pagelistselectmultiple</span></p>
<p>The <span class='mono'>checkbox</span> type allows for an integer value of 1 or 0.</p>
<h3>Options: static</h3>
<p class='mono mono-block'>vegetables__options=Spinach|Pumpkin|Celery|Tomato|Brussels Sprout|Potato</p>
<p>Use the pipe character as a delimiter between options.</p>
<h3>Options: dynamically generated by another Hanna tag</h3>
<p class='mono mono-block'>vegetables__options=[[_generate_vegetable_options]]</p>
<p>You can use one Hanna tag to generate options for another Hanna tag. The tag that generates the options should <strong>echo</strong> a string of options delimited by pipe characters (i.e. the same format as a static options string).</p>
<p>You will probably want to name the Hanna tag that generates the options so that it starts with an underscore, to avoid it appearing as an insertable tag in the HannaCodeDialog dropdown menu.</p>
<h3>Inputfield description</h3>
<p class='mono mono-block'>vegetables__description=Please select vegetables for your soup.</p>
<h3>Inputfield notes</h3>
<p class='mono mono-block'>vegetables__notes=Pumpkin and celery is a delicious combination.</p>
</div>
EOT;
$f->icon = 'question-circle';
$f->collapsed = Inputfield::collapsedYes;
$form->insertAfter($f, $hc_attr);
}
/**
* Config inputfields
*
* @param InputfieldWrapper $inputfields
*/
public function getModuleConfigInputfields(InputfieldWrapper $inputfields) {
$modules = $this->wire()->modules;
/* @var InputfieldText $f */
$f = $modules->InputfieldText;
$f_name = 'exclude_prefix';
$f->name = $f_name;
$f->label = $this->_('Exclude prefix');
$f->description = $this->_('Tags that have this prefix will not appear in the toolbar dropdown.');
$f->columnWidth = 50;
$f->value = $this->$f_name ?: '_';
$inputfields->add($f);
/* @var InputfieldAsmSelect $f */
$f = $modules->InputfieldAsmSelect;
$f_name = 'exclude_selection';
$f->name = $f_name;
$f->label = $this->_('Exclude Hanna tags');
$f->description = $this->_('Tags that are selected here will not appear in the toolbar dropdown.');
$f->columnWidth = 50;
foreach($this->getHannaTags(true) as $select_option) {
$f->addOption($select_option);
}
$f->value = $this->$f_name;
$inputfields->add($f);
/* @var InputfieldText $f */
$f = $modules->InputfieldText;
$f_name = 'widget_colour';
$f->name = $f_name;
$f->label = $this->_('Background colour of tag widgets');
$f->columnWidth = 33;
$f->value = $this->$f_name ?: '#d3eaf2';
$inputfields->add($f);
/* @var InputfieldInteger $f */
$f = $modules->InputfieldInteger;
$f_name = 'dialog_width';
$f->name = $f_name;
$f->label = $this->_('Dialog width');
$f->columnWidth = 33;
$f->inputType = 'number';
$f->min = 0;
$f->value = $this->$f_name ?: 360;
$inputfields->add($f);
/* @var InputfieldInteger $f */
$f = $modules->InputfieldInteger;
$f_name = 'dialog_height';
$f->name = $f_name;
$f->label = $this->_('Dialog height');
$f->columnWidth = 34;
$f->inputType = 'number';
$f->min = 0;
$f->value = $this->$f_name ?: 420;
$inputfields->add($f);
/* @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f_name = 'force_plugins';
$f->name = $f_name;
$f->label = $this->_('Load HannaCodeDialog CKEditor plugins even if TextformatterHannaCode is not applied to field');
$f->checked = $this->$f_name === 1 ? 'checked' : '';
$inputfields->add($f);
if($modules->isInstalled('TracyDebugger')) {
/* @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f_name = 'show_tracy';
$f->name = $f_name;
$f->label = $this->_('Allow Tracy Debugger bar in dialog');
$f->checked = $this->$f_name === 1 ? 'checked' : '';
$inputfields->add($f);
}
}
}