-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAdminCustomFiles.module
More file actions
448 lines (367 loc) · 15.1 KB
/
AdminCustomFiles.module
File metadata and controls
448 lines (367 loc) · 15.1 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
/**
* Admin Custom Files
*
* This module enables you to add custom scripts and files to the admin.
*
* Copyright (C) 2014 by Martijn Geerts
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class AdminCustomFiles extends WireData implements Module, ConfigurableModule {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Admin Custom Files',
'version' => '0.8.7',
'author' => 'Martijn Geerts',
'summary' => 'Add custom scripts & styles to the admin with optional dependencies',
'singular' => true,
'autoload' => "template=admin",
'icon' => 'file-code-o'
);
}
/**
* init() needed for PW2.4 and below
*
*/
public function init() {
}
// (bool) Current Page process is selected (Module configuration)
private $filter = null;
// (string) Name of the admin theme
private $admin_theme = null;
// (string) Name of the current Page process
private $process_name = null;
// (Page) Page object if the process is ProcessPageEdit
private $edit_page = null;
// (string) Name of the template if the process is ProcessPageEdit
private $template_name = null;
// (array) Converted textarea Dependencies to array (Module configuration)
private $dependency_array = array();
// (array) Gobal files without Process dependencies (e.g. site/scripts/lib/library.js)
private $global_files = array();
// (array) Admin theme files (e.g. AdminThemeReno.js and/or AdminThemeReno.js)
private $theme_based_files = array();
// (array) Gobal files with Process dependencies (e.g ProcessPageEdit site/scripts/lib/jquery-plug-in.js)
private $dependency_files = array();
// (array) Files depending on admin process (i.e. ProcessPageEdit.js)
private $process_files = array();
// (array) On ProcessPageEdit process (template-name.css and or template-name.js)
private $template_files = array();
// (array) Admin Custom Files array
private $AdminCustomFiles = array();
/**
* Factory settings
*
*/
private static $factorySettings = array(
'files_folder' => 'AdminCustomFiles',
);
/**
* Set defaults for getConfig()
*
*/
public function __construct() {
$this->set('files_folder', $this->factorySettings['files_folder']);
}
/**
* Helper function to convert newlines to array.
*
* @return array
*/
private function explode($string) {
return array_map('trim', explode("\n", $string));
}
/**
* Helper function thats returns a valid relative URL or false.
*
* @return mixed
*/
private function file($string) {
$url = $this->config->urls->templates;
$path = $this->config->paths->templates;
return file_exists($path . $string) ? $url . $string : false;
}
/**
* Include admintheme based files
*
* @return mixed
*/
private function getAdminCustomFiles() {
$path = $this->config->paths->templates . $this->files_folder;
if (!$this->files_folder || !file_exists($path)) return false;
foreach (scandir($path) as $file) {
if (strpos($file, '.') === 0) continue;
$this->AdminCustomFiles[] = $file;
}
}
/**
* On API ready, controller & set the hook
*
*/
public function ready() {
// Current processname
$this->process_name = (string) $this->page->process;
// Processes enabled ?
$this->filter = is_array($this->process_filter) ? in_array($this->process_name, $this->process_filter) : false;
// Page object if the process is ProcessPageEdit
$this->edit_page = $this->process_name == 'ProcessPageEdit' ? $this->pages->get($this->input->id) : new NullPage;
// Is the ProcessPageEdit && are the filters set
$this->template_name = $this->filter && $this->edit_page->id ? $this->edit_page->template->name : null;
// All dependencies
$this->dependency_array = $this->explode($this->dependencies);
// Create AdminCustomFiles array
$this->getAdminCustomFiles();
// Create dependencies array
$this->createDependencies();
// Get the template name if process is ProcessPageEdit
$this->createProcessFiles();
// Insert Theme based files
$this->createThemeFiles();
// Create js config
$this->addToJsConfig();
// Hook it (please help find me to find the last hook)
$this->addHookAfter('ProcessController::execute', $this, 'injectFiles');
}
/**
* Most likely these are plug-ins the admin custom files rely on. (Dependencies Settings)
*
*/
private function createDependencies() {
if (!count($this->dependency_array)) return false;
foreach ($this->dependency_array as $dependency) {
$type = strpos($dependency, ' ') > 0 ? 'dependency' : 'global';
if ($type === 'dependency' && !$this->filter) continue;
if ($type === 'dependency' && strpos($dependency, $this->process_name) !== 0) continue;
if ($type === 'dependency') { $dependency = str_replace($this->process_name, '', $dependency); }
$file = $this->file(trim($dependency));
if (!$file) continue;
if ($type === 'dependency') {
$this->dependency_files[] = $file;
} else {
$this->global_files[] = $file;
}
}
}
/**
* Scripts & files inside the AdminCustomFiles triggered on the running page process.
*
*/
private function createProcessFiles() {
if (!$this->filter) return false;
if (!count($this->AdminCustomFiles)) return false;
foreach ($this->AdminCustomFiles as $file) {
if (strpos($file, $this->process_name) === 0) {
$this->process_files[] = $this->file($this->files_folder . "/" . $file);
} else if ($this->template_name && strpos($file, $this->template_name) === 0) {
$this->template_files[] = $this->file($this->files_folder . "/" . $file);
}
}
}
/**
* Scripts & files inside the AdminCustomFiles triggered on the running page process.
*
*/
private function createThemeFiles() {
$this->admin_theme = !$this->user->admin_theme ? 'AdminThemeDefault' : $this->user->admin_theme;
if (!$this->theme_files) return false;
if (!$this->admin_theme) return false;
if (!count($this->AdminCustomFiles)) return false;
foreach ($this->AdminCustomFiles as $file) {
if (strpos($file, $this->admin_theme) === 0) {
$this->theme_based_files[] = $this->file($this->files_folder . "/" . $file);
}
}
}
/**
* Populate the js config data
*
*/
private function addToJsConfig() {
if (!$this->js_config) return;
$user = $this->user;
$roles = explode("|", $user->roles->implode('|', 'name'));
$data = array(
'process' => $this->process_name,
'host' => $this->config->httpHost,
'adminTheme' => $this->admin_theme,
'user' => array(
'id' => $user->id,
'name' => $user->name,
'email' => $user->email,
'roles' => $roles,
)
);
if ($this->process_name == 'ProcessPageEdit') {
$page = $this->edit_page;
$data['page'] = array(
'id' => $page->id,
'name' => $page->name,
'path' => $page->path,
'parentID' => $page->parentID,
'numChildren' => $page->numChildren,
'created' => $page->created,
'modified' => $page->modified,
'createdUser' => $page->createdUser->name,
'modifiedUser' => $page->modifiedUser->name,
);
$data['template'] = $this->template_name;
if ($page->id) {
$data['fields'] = explode("|", $page->fields->implode('|', 'name'));
}
}
$this->config->js($this->className(), $data);
}
/**
* Append scripts & styles to the filenamearray
*
*/
protected function injectFiles() {
$files = array_merge(
$this->global_files,
$this->theme_based_files,
$this->dependency_files,
$this->process_files,
$this->template_files
);
foreach ($files as $file) {
if (strpos($file, '.css') !== false) {
$this->config->styles->append($file);
} else if (strpos($file, '.js') !== false) {
$this->config->scripts->append($file);
}
}
}
/**
* Create the AdminCustomFiles folder
*
*/
public function ___install() {
$AdminCustomFiles = $this->config->paths->templates . self::$factorySettings['files_folder'];
if (!is_dir($AdminCustomFiles)) {
wireMkdir($AdminCustomFiles);
$this->message(sprintf($this->_("We created the folder /site/templates/%s/"), self::$factorySettings['files_folder']));
}
}
/**
* Uninstall
*
*/
public function ___uninstall() {
if (is_dir($this->config->paths->templates . $this->files_folder)) {
$this->message(sprintf($this->_("You may remove the folder /site/templates/%s/ manually."), $this->files_folder));
}
}
/**
* Module configuration
*
*/
public static function getModuleConfigInputfields(array $data) {
return wire('modules')->get('AdminCustomFiles')->getConfig();
}
/**
* Convenience method escaping the static visibility for getModuleConfigInputfields
*
*/
private function getConfig() {
// If scripts & styles folder is blank use factory settings
$files_folder = trim($this->files_folder);
$empty_config_data = array();
foreach (self::$factorySettings as $key => $value) {
if ($this->data[$key]) continue;
$empty_config_data[$key] = $value;
}
if (count($empty_config_data)) { $this->modules->saveModuleConfigData($this->className(), $empty_config_data); }
// Show include based files for 2.5 and higher
$version = ProcessWire::versionMajor . ProcessWire::versionMinor . 0;
$version = (int) $version;
$theme = ($version < 250) ? false : true;
$inputfields = new InputfieldWrapper();
// process_filter
$field = $this->modules->get('InputfieldAsmSelect');
$field->attr('name', 'process_filter');
$field->label = $this->_('Enable for process');
foreach ($this->modules as $module) {
$name = (string) $module;
if (strpos($name, 'Process') !== 0 ) continue;
$info = $this->wire('modules')->getModuleInfo($name);
$label = !empty($info['title']) ? $info['title'] : $name;
$field->addOption($name, "$name, ($label)");
}
$field->attr('value', is_array($this->process_filter) ? $this->process_filter : array());
$field->columnWidth = ($theme ? 33 : 50);
$inputfields->add($field);
// js_config
$field = $this->modules->get('InputfieldCheckbox');
$field->attr('name', 'js_config');
$field->set('skipLabel', Inputfield::skipLabelFor);
$field->label = $this->_('JSON');
$field->label2 = $this->_('Additional javascript config data');
$field->notes = $this->_("Tip: console.log(config.AdminCustomFiles)");
$field->attr('value', $this->js_config);
$field->attr('checked', $this->js_config);
$field->columnWidth = ($theme ? 33 : 50);
$inputfields->add($field);
// theme_files
if ($theme) {
$field = $this->modules->get('InputfieldCheckbox');
$field->attr('name', 'theme_files');
$field->set('skipLabel', Inputfield::skipLabelFor);
$field->label = $this->_('Theme based files');
$field->label2 = $this->_('Include theme based files');
$field->notes = $this->_("e.g. AdminThemeReno.js and/or AdminThemeReno.css");
$field->attr('value', $this->theme_files);
$field->attr('checked', $this->theme_files);
$field->columnWidth = 34;
$inputfields->add($field);
}
// files_folder
$field = $this->modules->get('InputfieldText');
$field->attr('name', 'files_folder');
$field->label = $this->_('Place dynamic styles & scripts in this folder');
$description = $this->_('- Start the filenames with the “running process name” and use .css or .js as extension.');
$description .= "\n";
$description .= $this->_('- For the ProcessPageEdit process you could additionally add files starting with the template-name ending with .js and/or .css');
$description .= "\n";
$description .= $this->_('- The process must be enabled here in the settings.');
$description .= "\n";
$description .= $this->_('- Only files matching the running process name are included.');
$field->description = $description; // Columns description
$field->value = $files_folder ? $files_folder : self::$factorySettings['files_folder'];
if (file_exists($this->config->paths->templates . $files_folder)) {
$field->notes = $this->config->urls->templates . $field->value . '/';
} else {
$field->notes = $this->_('Warning:') . ' ' . $this->config->urls->templates . $files_folder . "/ " . $this->_('doesn’t exist !');
}
$inputfields->add($field);
// dependencies
$field = $this->modules->get('InputfieldTextarea');
$field->attr('name', 'dependencies');
$field->label = $this->_('Dependencies, use this for plug-ins and other requirements.');
$description = $this->_('- Specify just an URL and this file will be included on every page, regardless if the process is enabled.');
$description .= "\n";
$description .= $this->_('- Files gets included when the admin process matches the process name proceding the URL. (The process must be enabled.)');
$description .= "\n";
$description .= $this->_('- Every dependency on it’s own line, URL’s relative to the processwire templates folder.');
$description .= "\n";
$description .= $this->_('- Multiple dependencies are possible.');
$field->description = $description;
$notes = 'e.g. ProcessPageEdit scripts/plug-ins/jquery.validate/jquery.validate.js';
$field->notes = $notes;
$field->value = trim($this->dependencies);
$inputfields->add($field);
return $inputfields;
}
}