-
Notifications
You must be signed in to change notification settings - Fork 2
/
AdminHelpTab.module
324 lines (263 loc) · 10.7 KB
/
AdminHelpTab.module
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
<?php
/**
* ProcessWire Admin Help Tab
*
* A little addon to display help docs relevant to the page being edited.
*
* @copyright Copyright (c) 2015, Macrura/Outflux3
*
*/
class AdminHelpTab extends WireData implements Module, ConfigurableModule {
/**
* @var Page Page being edited
*
*/
private $editedPage;
/**
* Return information about this module (required)
*
* @return array
*
*/
static public function getModuleInfo() {
return array(
'title' => 'Admin Help Tab',
'summary' => 'Adds a tab to the editor with related documentation.',
'version' => 108,
'author' => 'Marc W. | @Macrura(community), @outflux3 (Github), http://nibiri.com',
'autoload' => true,
'requires' => array('AdminHelp', 'FieldtypeTemplates'),
'icon' => 'life-ring'
);
}
protected static $configDefaults = array(
'tab_title' => '', // default Help
'tab_icon' => '', // default life-ring
'tab_color' => '',
'use_modal' => 0,
'modal_width' => '',
'no_icon' => 0,
);
/**
* __construct() is the right place to init config vars before they'll get populated
*
*/
public function __construct() {
$this->mainClass = 'AdminHelp';
$this->enabledTemplates = array();
$this->parentConfig = $this->wire('modules')->getModuleConfigData($this->mainClass);
foreach(self::$configDefaults as $key => $value) {
$this->set($key, $value);
}
}
/**
* ready() is called when both PW's API is ready and $page object is initialized (for autoload modules!)
* Add the hook here to be able to skip it based on the template of the edited page.
*
*/
public function ready() {
// we're interested in page editor only
if(wire('page')->process != 'ProcessPageEdit') return;
// skip changing templates (only target the actual edit form)
$id = (int)$this->input->get('id');
if(!$id) return;
// wire('page') would be the page with ProcessPageEdit
// GET parameter id tells the page that's being edited
$this->editedPage = wire('pages')->get($id);
// don't even consider system templates
if($this->editedPage->template->flags & Template::flagSystem) return;
// hook only if
// 1) no templates have been chosen (=all enabled) OR
// 2) the template of the edited page has been chosen
if(count($this->enabledTemplates) == 0 or
in_array($this->editedPage->template->name, $this->enabledTemplates)) {
// add the content as a tab
$this->addHookAfter('ProcessPageEdit::buildForm', $this, 'hookBuildForm');
// legacy - prev version had the setting on main module
//if($this->parentConfig['use_modal']) $this->use_modal == 1;
if($this->use_modal) {
$this->addHookAfter('ProcessPageEdit::getTabs', $this, 'addHelpLinkModal');
$this->modules->get('JqueryMagnific');
$this->config->styles->add($this->config->urls->siteModules . $this->mainClass . '/AdminHelpTabModal.css?v=' . time());
$this->config->scripts->add($this->config->urls->siteModules . $this->mainClass . '/AdminHelpTabModal.js?v=' . time());
} else {
$this->config->scripts->add($this->config->urls->siteModules . $this->mainClass . '/AdminHelpTab.js?v=' . time());
}
}
}
/**
* Add References tab to page edit form
*
*/
public function hookBuildForm(HookEvent $event) {
// get the InputFieldForm object from the event (return value of buildForm())
$savedPageID = (int) $this->parentConfig['helpRoot'];
$form = $event->return;
// create the tab
$docTab = new InputfieldWrapper();
$docTab->attr('id', $this->className() . 'Help');
// configure title/icon
//$docTab->attr('title', $this->_('Help'));
$icon = $this->tab_icon ? "<i class='fa fa-{$this->tab_icon}'></i> " : "<i class='fa fa-life-ring'></i> ";
if($this->no_icon) $icon = '';
$label = $this->tab_title ?: $this->_('Help');
$color = $this->tab_color ?: '5CC7B2';
$docTab->attr('title', $icon . $label);
$docTab->attr('data-color', $color);
// tab content
$field = $this->modules->get("InputfieldMarkup");
// tab intro
$intro = $this->modules->get("InputfieldMarkup");
$intro->attr('id+name', $this->className() . 'Intro');
// find docs
$docs = wire('pages')->find("has_parent=$savedPageID, template_select={$this->editedPage->template->id}, sort=sort");
if(count($docs)) {
$docLink = wire('config')->urls->admin . 'page/edit/?id=';
$introMarkup = '';
$body = '<div id="docs">';
foreach($docs as $doc) {
$body .= "<div id='doc{$doc->id}' class='help-doc'>";
$body .= "<h1><i class='fa fa-file-text-o'></i> {$doc->title}";
$body .= " <span><a href='{$docLink}{$doc->id}' target='_blank' title='Edit'><i class='fa fa-pencil'></i></a></span></h1>";
$body .= $doc->body;
// Uncomment below to enable edit link:
//$body .= '<div class="doc-edit">';
//$body .= "<a href='{$docLink}{$doc->id}' target='_blank'><i class='fa fa-pencil'></i> Edit: {$doc->title}</a></div>";
$body .= '</div>';
if($doc->children) {
foreach($doc->children as $childDoc) {
$body .= "<div id='doc{$childDoc->id}' class='help-doc'>";
$body .= "<h1><i class='fa fa-file-text-o'></i> {$childDoc->title}";
$body .= " <span><a href='{$docLink}{$childDoc->id}' target='_blank' title='Edit'><i class='fa fa-pencil'></i></a></span></h1>";
$body .= $childDoc->body;
// Uncomment below to enable edit link:
//$body .= '<div class="doc-edit">';
//$body .= "<a href='{$docLink}{$childDoc->id}' target='_blank'><i class='fa fa-pencil'></i> Edit: {$childDoc->title}</a></div>";
$body .= '</div>';
}
}
if($doc->help_header != '') {
$introMarkup .= $doc->help_header;
}
}
$body .= '</div>';
$field->markupText = $body;
// custom intro - places at the beginning of the first tab content
if($introMarkup) {
$intro->markupText = $introMarkup;
$form->prepend($intro);
}
} else {
$docsAdd = wire('config')->urls->admin . 'page/add/?parent_id=' . $savedPageID;
$field->markupText = "<h2>No applicable help docs found</h2>";
$field->markupText .= "<br> <a href='$docsAdd'>";
$field->markupText .= '<button class="ui-button ui-widget ui-corner-all ui-state-default" name="button" value="Add New" type="button"><span class="ui-button-text"><i class="fa fa-plus-circle"></i> Add New Doc</span></button>';
$field->markupText .= '</a>';
}
// append the markup to the tab and the tab to the form
$docTab->append($field);
$form->prepend($docTab);
$this->config->styles->add($this->config->urls->siteModules . $this->mainClass . '/' . $this->mainClass . '.css?v=' . time());
}
/**
* Add Modal Help Link to Page Editor
*
*/
public function addHelpLinkModal(HookEvent $event) {
$tabs = $event->return;
$event->replace = true;
// SETTINGS
$icon = $this->tab_icon ? "<i class='fa fa-{$this->tab_icon}'></i> " : "<i class='fa fa-life-ring'></i> ";
if($this->no_icon) $icon = '';
$label = $this->tab_title ?: $this->_('Help');
$style = $this->tab_color ? " style='color:#{$this->tab_color}'" : " style='color:#5CC7B2'";
$width = $this->modal_width ? " data-mfp-width='{$this->modal_width}'" : '';
$id = $this->className() . 'HelpModal';
$url = '#AdminHelpTabHelp';
$a = "<a id='_ProcessPageEditHelpModal' href='$url' title='Open help in modal'{$style}{$width}>{$icon}{$label}</a>";
$tabs[$id] = $a;
$event->return = $tabs;
}
/**
* Return an InputfieldsWrapper of Inputfields used to configure the class
*
* @param array $data Array of config values indexed by field name
* @return InputfieldsWrapper
*
*/
public static function getModuleConfigInputfields(array $data) {
$wrapper = new InputfieldWrapper();
// ---- ENABLED TEMPLATES ---- //
$fieldEnabledTemplates = wire('modules')->get('InputfieldAsmSelect');
$fieldEnabledTemplates->attr('name+id', 'enabledTemplates');
$fieldEnabledTemplates->label = __('Enabled templates', __FILE__);
$fieldEnabledTemplates->description = __('"Help" tab will only be shown for chosen templates. If no template is chosen, "Help" tab will be shown for all templates.', __FILE__);
$fieldEnabledTemplates->attr('title', __('Enable template', __FILE__));
$fieldEnabledTemplates->setAsmSelectOption('sortable', false);
// populate with all available templates
foreach(wire('templates') as $t) {
// filter out system templates
if(!($t->flags & Template::flagSystem)) $fieldEnabledTemplates->addOption($t->name);
}
if(isset($data['enabledTemplates'])) $fieldEnabledTemplates->value = $data['enabledTemplates'];
$wrapper->add($fieldEnabledTemplates);
// ---- TAB TITLE ---- //
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'tab_title');
$f->attr('value', $data['tab_title']);
$f->attr('size', 20);
$f->attr('maxlength', 21);
$f->attr('placeholder', 'Help');
$f->label = __('Help Tab Title');
$f->description = __('You can override the tab title.');
$f->columnWidth = 50;
$wrapper->add($f);
// ---- TAB COLOR ---- //
// @todo - check for color fieldtype?
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'tab_color');
$f->attr('value', $data['tab_color']);
$f->attr('placeholder', '5CC7B2');
$f->label = __('Hex Color Value');
$f->description = __('The color to use for the tab text.');
$f->columnWidth = 50;
$wrapper->add($f);
// ---- TAB ICON ---- //
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'tab_icon');
$f->attr('value', $data['tab_icon']);
$f->attr('placeholder', 'life-ring');
$f->label = __('FontAwesome Icon Name');
$f->description = __('You can enter a valid FontAwesome icon to use next to the tab.');
$f->columnWidth = 50;
$wrapper->add($f);
// ---- TAB ICON ---- //
$f = wire('modules')->get('InputfieldCheckbox');
$f->attr('name', 'no_icon');
$f->attr('value', 1);
if($data['no_icon']) $f->attr('checked', 'checked');
$f->label = __('Disable Tab Icon');
$f->description = __('By befault an icon is shown. Check this box to disable the display of the icon.');
$f->columnWidth = 50;
$wrapper->add($f);
// ---- TAB IN POPUP ---- //
$f = wire('modules')->get('InputfieldCheckbox');
$f->attr('name', 'use_modal');
$f->label2 = 'Pop It Up!';
$f->label = __('Display Help in Popup?', __FILE__);
$f->description = __('Check to show help in a popup (lightbox) instead of inside tab content.', __FILE__);
$f->value = (isset($data['use_modal'])) ? $data['use_modal'] : 1;
$f->checked = ($f->value == 1) ? 'checked' : '';
$f->columnWidth = 50;
$wrapper->add($f);
// ---- POPUP WIDTH ---- //
$f = wire('modules')->get('InputfieldInteger');
$f->attr('name', 'modal_width');
$f->attr('value', $data['modal_width']);
$f->label = __('Popup Max Width');
$f->description = __('Max width in px for the popup/lightbox content. Leave blank for full width.');
$f->columnWidth = 50;
$wrapper->add($f);
return $wrapper;
}
}