1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package midcom.admin.help |
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Michelf\MarkdownExtra; |
10
|
|
|
use midgard\portable\storage\connection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Online help display |
14
|
|
|
* |
15
|
|
|
* @package midcom.admin.help |
16
|
|
|
*/ |
17
|
|
|
class midcom_admin_help_help extends midcom_baseclasses_components_plugin |
18
|
|
|
{ |
19
|
|
|
private $mgdtypes = [ |
20
|
|
|
MGD_TYPE_STRING => "string", |
21
|
|
|
MGD_TYPE_INT => "integer", |
22
|
|
|
MGD_TYPE_UINT => "unsigned integer", |
23
|
|
|
MGD_TYPE_FLOAT => "float", |
24
|
|
|
MGD_TYPE_BOOLEAN => "boolean", |
25
|
|
|
MGD_TYPE_TIMESTAMP => "datetime", |
26
|
|
|
MGD_TYPE_LONGTEXT => "longtext", |
27
|
|
|
MGD_TYPE_GUID => "guid", |
28
|
|
|
MGD_TYPE_NONE => 'none' |
29
|
|
|
]; |
30
|
|
|
|
31
|
3 |
|
public function _on_initialize() |
32
|
|
|
{ |
33
|
3 |
|
midcom::get()->auth->require_valid_user(); |
34
|
|
|
|
35
|
3 |
|
midcom::get()->skip_page_style = true; |
36
|
|
|
// doing this here as this component most probably will not be called by itself. |
37
|
3 |
|
midcom::get()->style->prepend_component_styledir('midcom.admin.help'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get component's documentation directory path |
42
|
|
|
*/ |
43
|
8 |
|
private static function get_documentation_dir(string $component) : string |
44
|
|
|
{ |
45
|
8 |
|
return midcom::get()->componentloader->path_to_snippetpath($component) . '/documentation/'; |
46
|
|
|
} |
47
|
|
|
|
48
|
8 |
|
public static function generate_file_path(string $help_id, string $component, string $language = null) : ?string |
49
|
|
|
{ |
50
|
8 |
|
if ($language === null) { |
51
|
8 |
|
$language = midcom::get()->i18n->get_current_language(); |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
$file = self::get_documentation_dir($component) . "{$help_id}.{$language}.txt"; |
55
|
8 |
|
if (!file_exists($file)) { |
56
|
8 |
|
if ($language != midcom::get()->i18n->get_fallback_language()) { |
57
|
|
|
// Try MidCOM's default fallback language |
58
|
1 |
|
return self::generate_file_path($help_id, $component, midcom::get()->i18n->get_fallback_language()); |
59
|
|
|
} |
60
|
8 |
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
return $file; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
private function get_help_title(string $help_id, string $component) : string |
67
|
|
|
{ |
68
|
4 |
|
if ($path = self::generate_file_path($help_id, $component)) { |
69
|
4 |
|
$file_contents = file($path); |
70
|
4 |
|
if (trim($file_contents[0])) { |
71
|
4 |
|
return trim($file_contents[0]); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return $this->_l10n->get("help_" . $help_id); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Load the file from the component's documentation directory. |
80
|
|
|
*/ |
81
|
5 |
|
private function _load_file(string $help_id, string $component) : ?string |
82
|
|
|
{ |
83
|
|
|
// Try loading the file |
84
|
5 |
|
$file = self::generate_file_path($help_id, $component); |
85
|
5 |
|
if (!$file) { |
86
|
4 |
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Load the contents |
90
|
1 |
|
$help_contents = file_get_contents($file); |
91
|
|
|
|
92
|
|
|
// Replace static URLs (URLs for screenshots etc) |
93
|
1 |
|
return str_replace('MIDCOM_STATIC_URL', MIDCOM_STATIC_URL, $help_contents); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Load a help file and markdownize it |
98
|
|
|
*/ |
99
|
5 |
|
public function get_help_contents(string $help_id, string $component) : ?string |
100
|
|
|
{ |
101
|
5 |
|
$text = $this->_load_file($help_id, $component); |
102
|
5 |
|
if (!$text) { |
103
|
4 |
|
return null; |
104
|
|
|
} |
105
|
1 |
|
return MarkdownExtra::defaultTransform($text); |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
public function list_files(string $component, bool $with_index = false) : array |
109
|
|
|
{ |
110
|
5 |
|
$files = $this->_list_physical_files($component); |
111
|
5 |
|
$files = $this->_add_virtual_files($files, $component); |
112
|
|
|
|
113
|
5 |
|
ksort($files); |
114
|
|
|
// prepend 'index' URL if required |
115
|
5 |
|
if ($with_index) { |
116
|
1 |
|
$files = array_merge([ |
117
|
|
|
'index' => [ |
118
|
1 |
|
'path' => '/', |
119
|
1 |
|
'subject' => $this->_l10n->get('help_index'), |
120
|
|
|
'lang' => 'en', |
121
|
|
|
]], |
122
|
|
|
$files |
123
|
|
|
); |
124
|
|
|
} |
125
|
5 |
|
return $files; |
126
|
|
|
} |
127
|
|
|
|
128
|
5 |
|
private function _add_virtual_files(array $files, string $component) : array |
129
|
|
|
{ |
130
|
|
|
// Schemas |
131
|
5 |
|
$this->_request_data['mgdschemas'] = midcom::get()->dbclassloader->get_component_classes($component); |
132
|
5 |
|
if (!empty($this->_request_data['mgdschemas'])) { |
133
|
3 |
|
$files['mgdschemas'] = [ |
134
|
|
|
'path' => '/mgdschemas', |
135
|
3 |
|
'subject' => $this->_l10n->get('help_mgdschemas'), |
136
|
|
|
'lang' => 'en', |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// URL Methods |
141
|
5 |
|
$this->_request_data['urlmethods'] = $this->read_url_methods($component); |
142
|
5 |
|
if (!empty($this->_request_data['urlmethods'])) { |
143
|
4 |
|
$files['urlmethods'] = [ |
144
|
|
|
'path' => '/urlmethods', |
145
|
4 |
|
'subject' => $this->_l10n->get('help_urlmethods'), |
146
|
|
|
'lang' => 'en', |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Break if dealing with MidCOM Core docs |
151
|
5 |
|
if ($component == 'midcom') { |
152
|
|
|
return $files; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// handlers |
156
|
5 |
|
$this->_request_data['request_switch_info'] = $this->read_component_handlers($component); |
157
|
5 |
|
if (!empty($this->_request_data['request_switch_info'])) { |
158
|
4 |
|
$files['handlers'] = [ |
159
|
|
|
'path' => '/handlers', |
160
|
4 |
|
'subject' => $this->_l10n->get('help_handlers'), |
161
|
|
|
'lang' => 'en', |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
|
165
|
5 |
|
return $files; |
166
|
|
|
} |
167
|
|
|
|
168
|
5 |
|
private function _list_physical_files(string $component) : array |
169
|
|
|
{ |
170
|
5 |
|
$component_dir = self::get_documentation_dir($component); |
171
|
5 |
|
if (!is_dir($component_dir)) { |
172
|
1 |
|
return []; |
173
|
|
|
} |
174
|
|
|
|
175
|
4 |
|
$files = []; |
176
|
4 |
|
$pattern = $component_dir . '*.{' . $this->_i18n->get_current_language() . ',' . $this->_i18n->get_fallback_language() . '}.txt'; |
177
|
|
|
|
178
|
4 |
|
foreach (glob($pattern, GLOB_NOSORT|GLOB_BRACE) as $path) { |
179
|
4 |
|
$entry = basename($path); |
180
|
4 |
|
if ( str_starts_with($entry, 'index') |
181
|
4 |
|
|| str_starts_with($entry, 'handler') |
182
|
4 |
|
|| str_starts_with($entry, 'urlmethod')) { |
183
|
|
|
// Ignore dotfiles, handlers & index.lang.txt |
184
|
1 |
|
continue; |
185
|
|
|
} |
186
|
|
|
|
187
|
4 |
|
$filename_parts = explode('.', $entry); |
188
|
|
|
|
189
|
4 |
|
$files[$filename_parts[0]] = [ |
190
|
|
|
'path' => $path, |
191
|
4 |
|
'subject' => $this->get_help_title($filename_parts[0], $component), |
192
|
4 |
|
'lang' => $filename_parts[1], |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
|
196
|
4 |
|
return $files; |
197
|
|
|
} |
198
|
|
|
|
199
|
5 |
|
private function read_component_handlers(string $component) : array |
200
|
|
|
{ |
201
|
5 |
|
$data = []; |
202
|
|
|
|
203
|
5 |
|
$handler = midcom::get()->componentloader->get_interface_class($component); |
204
|
5 |
|
$viewer = $handler->get_viewer(new midcom_db_topic); |
205
|
5 |
|
$routes = $viewer->get_router()->getRouteCollection()->all(); |
206
|
5 |
|
foreach ($routes as $request_handler_id => $route) { |
207
|
|
|
$details = [ |
208
|
4 |
|
'route' => preg_replace('/args_(\d+)/', 'args[\1]', $route->getPath()) |
209
|
|
|
]; |
210
|
|
|
|
211
|
4 |
|
list ($details['controller'], $details['action']) = explode('::', $route->getDefault('_controller'), 2); |
|
|
|
|
212
|
|
|
|
213
|
4 |
|
if (self::generate_file_path('handlers_' . $request_handler_id, $component)) { |
214
|
1 |
|
$details['info'] = $this->get_help_contents('handlers_' . $request_handler_id, $component); |
215
|
1 |
|
$details['handler_help_url'] = 'handlers_' . $request_handler_id; |
216
|
|
|
} |
217
|
4 |
|
$data[$request_handler_id] = $details; |
218
|
|
|
} |
219
|
|
|
|
220
|
5 |
|
return $data; |
221
|
|
|
} |
222
|
|
|
|
223
|
5 |
|
private function read_url_methods(string $component) : array |
224
|
|
|
{ |
225
|
5 |
|
$data = []; |
226
|
|
|
|
227
|
5 |
|
$exec_path = midcom::get()->componentloader->path_to_snippetpath($component) . '/exec/'; |
228
|
5 |
|
if ( !is_dir($exec_path) |
229
|
5 |
|
|| !is_readable($exec_path)) { |
230
|
|
|
// Directory not accessible, skip loading it |
231
|
1 |
|
return $data; |
232
|
|
|
} |
233
|
|
|
|
234
|
4 |
|
foreach (glob($exec_path . '/*.php', GLOB_NOSORT) as $path) { |
235
|
4 |
|
$file = basename($path); |
236
|
4 |
|
$info_id = "urlmethod_" . str_replace('.php', '', $file); |
237
|
|
|
|
238
|
4 |
|
$data[$file] = [ |
239
|
4 |
|
'url' => '/midcom-exec-' . $component . '/' . $file, |
240
|
|
|
]; |
241
|
|
|
|
242
|
4 |
|
if (self::generate_file_path($info_id, $component)) { |
243
|
|
|
$data[$file]['handler_help_url'] = $info_id; |
244
|
|
|
$data[$file]['description'] = $this->get_help_contents($info_id, $component); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
4 |
|
return $data; |
249
|
|
|
} |
250
|
|
|
|
251
|
1 |
|
private function read_schema_properties() |
252
|
|
|
{ |
253
|
1 |
|
foreach (array_keys($this->_request_data['mgdschemas']) as $mgdschema_class) { |
254
|
1 |
|
$mrp = new midgard_reflection_property($mgdschema_class); |
255
|
1 |
|
$class_props = connection::get_em()->getClassMetadata($mgdschema_class)->get_schema_properties(); |
|
|
|
|
256
|
|
|
|
257
|
1 |
|
unset($class_props['metadata']); |
258
|
1 |
|
$default_properties = []; |
259
|
1 |
|
$additional_properties = []; |
260
|
|
|
|
261
|
1 |
|
foreach ($class_props as $prop) { |
262
|
1 |
|
switch ($prop) { |
263
|
1 |
|
case 'action': |
264
|
|
|
// Midgard-internal properties, skip |
265
|
|
|
break; |
266
|
1 |
|
case 'guid': |
267
|
1 |
|
case 'id': |
268
|
1 |
|
$default_properties[$prop] = $this->_get_property_data($mrp, $prop); |
269
|
1 |
|
break; |
270
|
|
|
default: |
271
|
1 |
|
$additional_properties[$prop] = $this->_get_property_data($mrp, $prop); |
272
|
1 |
|
break; |
273
|
|
|
} |
274
|
|
|
} |
275
|
1 |
|
ksort($default_properties); |
276
|
1 |
|
ksort($additional_properties); |
277
|
|
|
|
278
|
1 |
|
$this->_request_data['properties'][$mgdschema_class] = array_merge($default_properties, $additional_properties); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
1 |
|
private function _get_property_data(midgard_reflection_property $mrp, string $prop) : array |
283
|
|
|
{ |
284
|
|
|
return [ |
285
|
1 |
|
'value' => $mrp->description($prop) ?? '', |
286
|
1 |
|
'link' => $mrp->is_link($prop), |
287
|
1 |
|
'link_name' => $mrp->get_link_name($prop), |
288
|
1 |
|
'link_target' => $mrp->get_link_target($prop), |
289
|
1 |
|
'midgard_type' => $this->mgdtypes[$mrp->get_midgard_type($prop)] |
290
|
|
|
]; |
291
|
|
|
} |
292
|
|
|
|
293
|
3 |
|
private function _prepare_breadcrumb(string $handler_id) |
294
|
|
|
{ |
295
|
3 |
|
$this->add_breadcrumb($this->router->generate('welcome'), $this->_l10n->get('help')); |
296
|
|
|
|
297
|
3 |
|
if (in_array($handler_id, ['help', 'component'])) { |
298
|
2 |
|
$this->add_breadcrumb( |
299
|
2 |
|
$this->router->generate('component', ['component' => $this->_request_data['component']]), |
300
|
2 |
|
$this->_i18n->get_string($this->_request_data['component'], $this->_request_data['component']) |
301
|
|
|
); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
1 |
|
public function _handler_welcome(string $handler_id, array &$data) |
306
|
|
|
{ |
307
|
1 |
|
$data['view_title'] = $this->_l10n->get($this->_component); |
308
|
1 |
|
midcom::get()->head->set_pagetitle($data['view_title']); |
309
|
|
|
|
310
|
1 |
|
$data['components'] = []; |
311
|
1 |
|
$data['libraries'] = []; |
312
|
|
|
|
313
|
1 |
|
foreach (midcom::get()->componentloader->get_manifests() as $manifest) { |
314
|
1 |
|
$type = $manifest->purecode ? 'libraries' : 'components'; |
315
|
1 |
|
$title = $manifest->get_name_translated(); |
316
|
1 |
|
if ($title == $manifest->name) { |
317
|
1 |
|
$title = ''; |
318
|
|
|
} |
319
|
|
|
|
320
|
1 |
|
$data[$type][$manifest->name] = [ |
321
|
1 |
|
'name' => $manifest->name, |
322
|
|
|
'title' => $title, |
323
|
1 |
|
'icon' => midcom::get()->componentloader->get_component_icon($manifest->name), |
324
|
1 |
|
'description' => $manifest->description, |
325
|
|
|
]; |
326
|
|
|
} |
327
|
|
|
|
328
|
1 |
|
asort($data['components']); |
329
|
1 |
|
asort($data['libraries']); |
330
|
|
|
|
331
|
1 |
|
$this->_prepare_breadcrumb($handler_id); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Shows the help system main screen |
336
|
|
|
*/ |
337
|
1 |
|
public function _show_welcome(string $handler_id, array &$data) |
338
|
|
|
{ |
339
|
1 |
|
midcom_show_style('midcom_admin_help_header'); |
340
|
1 |
|
midcom_show_style('midcom_admin_help_about'); |
341
|
1 |
|
$list_types = ['components', 'libraries']; |
342
|
|
|
|
343
|
1 |
|
foreach ($list_types as $list_type) { |
344
|
1 |
|
$data['list_type'] = $list_type; |
345
|
1 |
|
midcom_show_style('midcom_admin_help_list_header'); |
346
|
1 |
|
foreach ($data[$list_type] as $component_data) { |
347
|
1 |
|
$data['component_data'] = $component_data; |
348
|
1 |
|
midcom_show_style('midcom_admin_help_list_item'); |
349
|
|
|
} |
350
|
1 |
|
midcom_show_style('midcom_admin_help_list_footer'); |
351
|
|
|
} |
352
|
|
|
|
353
|
1 |
|
midcom_show_style('midcom_admin_help_footer'); |
354
|
|
|
} |
355
|
|
|
|
356
|
1 |
|
public function _handler_component(string $handler_id, string $component, array &$data) |
357
|
|
|
{ |
358
|
1 |
|
$data['component'] = $component; |
359
|
1 |
|
$data['view_title'] = sprintf($this->_l10n->get('help for %s'), $this->_i18n->get_string($component, $component)); |
360
|
1 |
|
midcom::get()->head->set_pagetitle($data['view_title']); |
361
|
|
|
|
362
|
1 |
|
$data['help_files'] = $this->list_files($component); |
363
|
1 |
|
$data['html'] = $this->get_help_contents('index', $component); |
364
|
1 |
|
$this->_prepare_breadcrumb($handler_id); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Shows the component help ToC. |
369
|
|
|
*/ |
370
|
1 |
|
public function _show_component(string $handler_id, array &$data) |
371
|
|
|
{ |
372
|
1 |
|
midcom_show_style('midcom_admin_help_header'); |
373
|
|
|
|
374
|
1 |
|
midcom_show_style('midcom_admin_help_show'); |
375
|
1 |
|
midcom_show_style('midcom_admin_help_component'); |
376
|
|
|
|
377
|
1 |
|
midcom_show_style('midcom_admin_help_footer'); |
378
|
|
|
} |
379
|
|
|
|
380
|
1 |
|
public function _handler_help(string $handler_id, string $component, string $help_id, array &$data) |
381
|
|
|
{ |
382
|
1 |
|
$data['help_id'] = $help_id; |
383
|
1 |
|
$data['component'] = $component; |
384
|
1 |
|
$data['help_files'] = $this->list_files($component); |
385
|
|
|
|
386
|
1 |
|
if ($help_id == 'mgdschemas') { |
387
|
1 |
|
$this->read_schema_properties(); |
388
|
|
|
} |
389
|
1 |
|
$data['html'] = $this->get_help_contents($help_id, $component); |
390
|
|
|
|
391
|
1 |
|
midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.admin.help/twisty.js'); |
392
|
1 |
|
$this->add_stylesheet(MIDCOM_STATIC_URL . '/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css'); |
393
|
|
|
|
394
|
|
|
// Table of contents navi |
395
|
1 |
|
$data['view_title'] = sprintf( |
396
|
1 |
|
$this->_l10n->get('help for %s in %s'), |
397
|
1 |
|
$this->get_help_title($help_id, $component), |
398
|
1 |
|
$this->_i18n->get_string($component, $component) |
399
|
|
|
); |
400
|
1 |
|
midcom::get()->head->set_pagetitle($data['view_title']); |
401
|
1 |
|
$this->_prepare_breadcrumb($handler_id); |
402
|
1 |
|
if (in_array($help_id, ['handlers', 'urlmethods', 'mgdschemas'])) { |
403
|
1 |
|
$this->add_breadcrumb("", $this->_l10n->get($help_id)); |
404
|
|
|
} else { |
405
|
|
|
$this->add_breadcrumb("", $this->get_help_title($help_id, $component)); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Shows the help page. |
411
|
|
|
*/ |
412
|
1 |
|
public function _show_help(string $handler_id, array &$data) |
413
|
|
|
{ |
414
|
1 |
|
midcom_show_style('midcom_admin_help_header'); |
415
|
1 |
|
midcom_show_style('midcom_admin_help_show'); |
416
|
1 |
|
if (in_array($data['help_id'], ['handlers', 'mgdschemas', 'urlmethods'])) { |
417
|
1 |
|
midcom_show_style('midcom_admin_help_' . $data['help_id']); |
418
|
|
|
} elseif (!$data['html']) { |
419
|
|
|
$data['html'] = $this->get_help_contents('notfound', $this->_component); |
420
|
|
|
midcom_show_style('midcom_admin_help_show'); |
421
|
|
|
midcom_show_style('midcom_admin_help_component'); |
422
|
|
|
} |
423
|
1 |
|
midcom_show_style('midcom_admin_help_footer'); |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|