/
toolbars.php
501 lines (457 loc) · 17.4 KB
/
toolbars.php
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
/**
* @package midcom.services
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
/**
* On-site toolbar service.
*
* This service manages the toolbars used for the on-site administration system.
* For each context, it provides the following set of toolbars:
*
* 1. The <i>Node</i> toolbar is applicable to the current
* node, which is usually a topic. MidCOM places the topic management operations
* into this toolbar, where applicable.
*
* 2. The <i>View</i> toolbar is applicable to the specific view ("url"). Usually
* this maps to a single displayed object (see also the bind_to_object() member
* function). MidCOM places the object-specific management operations (like
* Metadata controls) into this toolbar, if it is bound to an object. Otherwise,
* this toolbar is not touched by MidCOM.
*
* It is important to understand that the default toolbars made available through this
* service are completely specific to a given request context. If you have a dynamic_load
* running on a given site, it will have its own set of toolbars for each instance.
*
* In addition, components may retrieve a third kind of toolbars, which are not under
* the general control of MidCOM, the <i>Object</i> toolbars. They apply to a single
* database object (like a bound <i>View</i> toolbar). The usage of this kind of
* toolbars is completely component-specific.
*
* <b>Implementation notes</b>
*
* It has yet to prove if the toolbar system is needed for a dynamic_load environments.
* The main reason for this is that dl'ed stuff is often quite tight in space and thus cannot
* display any toolbars in a sane way. Usually, the administrative tasks will be bound to the
* main request.
*
* This could be different for portal applications, which display several components on the
* welcome page, each with its own management options.
*
* <b>Configuration</b>
* See midcom_config for configuration options.
*
* @package midcom.services
*/
class midcom_services_toolbars
{
/**
* The toolbars currently available.
*
* This array is indexed by context id; each value consists of a flat array
* of two toolbars, the first object being the Node toolbar, the second
* View toolbar. The toolbars are created on-demand.
*
* @var array
*/
private $_toolbars = array();
/**
* midcom.services.toolbars has two modes, it can either display one centralized toolbar
* for authenticated users, or the node and view toolbars separately for others. This
* property controls whether centralized mode is enabled.
*
* @var boolean
*/
private $_enable_centralized = false;
/**
* Whether we're in centralized mode, i.e. centralized toolbar has been shown
*
* @var boolean
*/
private $_centralized_mode = false;
/**
* Simple constructor
*/
public function __construct()
{
static $initialized = false;
if ($initialized)
{
// This is auth service looping because it instantiates classes for magic privileges!
return;
}
$initialized = true;
if ( !midcom::get()->auth->user
|| !midcom::get()->config->get('toolbars_enable_centralized')
|| !midcom::get()->auth->can_user_do('midcom:centralized_toolbar', null, $this))
{
return;
}
if (midcom::get()->auth->can_user_do('midcom:ajax', null, $this))
{
midcom::get()->head->enable_jquery();
midcom::get()->head->add_jsfile(MIDCOM_JQUERY_UI_URL . '/ui/core.min.js');
midcom::get()->head->add_jsfile(MIDCOM_JQUERY_UI_URL . '/ui/widget.min.js');
midcom::get()->head->add_jsfile(MIDCOM_JQUERY_UI_URL . '/ui/mouse.min.js');
midcom::get()->head->add_jsfile(MIDCOM_JQUERY_UI_URL . '/ui/draggable.min.js');
midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.services.toolbars/jquery.midcom_services_toolbars.js');
midcom::get()->head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.services.toolbars/fancy.css', 'screen');
$script = "jQuery('body div.midcom_services_toolbars_fancy').midcom_services_toolbar();";
midcom::get()->head->add_jquery_state_script($script);
}
else
{
midcom::get()->head->add_stylesheet(midcom::get()->config->get('toolbars_simple_css_path'), 'screen');
}
// We've included CSS and JS, path is clear for centralized mode
$this->_enable_centralized = true;
}
public function get_class_magic_default_privileges()
{
return array
(
'EVERYONE' => array(),
'ANONYMOUS' => array(),
'USERS' => array()
);
}
/**
* Returns the host toolbar of the specified context. The toolbars
* will be created if this is the first request.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @return midcom_helper_toolbar_host
*/
function get_host_toolbar($context_id = null)
{
return $this->_get_toolbar($context_id, MIDCOM_TOOLBAR_HOST);
}
/**
* Returns the node toolbar of the specified context. The toolbars
* will be created if this is the first request.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @return midcom_helper_toolbar_node
*/
function get_node_toolbar($context_id = null)
{
return $this->_get_toolbar($context_id, MIDCOM_TOOLBAR_NODE);
}
/**
* Returns the view toolbar of the specified context. The toolbars
* will be created if this is the first request.
*
* @param int $context_id The context to retrieve the view toolbar for, this
* defaults to the current context.
* @return midcom_helper_toolbar_view
*/
function get_view_toolbar($context_id = null)
{
return $this->_get_toolbar($context_id, MIDCOM_TOOLBAR_VIEW);
}
/**
* Returns the help toolbar of the specified context. The toolbars
* will be created if this is the first request.
*
* @param int $context_id The context to retrieve the help toolbar for, this
* defaults to the current context.
* @return midcom_helper_toolbar_help
*/
function get_help_toolbar($context_id = null)
{
return $this->_get_toolbar($context_id, MIDCOM_TOOLBAR_HELP);
}
/**
*
* @param integer $context_id
* @param string $identifier
* @return midcom_helper_toolbar
*/
private function _get_toolbar($context_id, $identifier)
{
if ($context_id === null)
{
$context_id = midcom_core_context::get()->id;
}
if (! array_key_exists($context_id, $this->_toolbars))
{
$this->_create_toolbars($context_id);
}
return $this->_toolbars[$context_id][$identifier];
}
/**
* Creates the node and view toolbars for a given context ID.
*
* @param int $context_id The context ID for whicht the toolbars should be created.
*/
private function _create_toolbars ($context_id)
{
$component = midcom_core_context::get($context_id)->get_key(MIDCOM_CONTEXT_COMPONENT);
$topic = midcom_core_context::get($context_id)->get_key(MIDCOM_CONTEXT_CONTENTTOPIC);
$this->_toolbars[$context_id][MIDCOM_TOOLBAR_HELP] = new midcom_helper_toolbar_help($component);
$this->_toolbars[$context_id][MIDCOM_TOOLBAR_HOST] = new midcom_helper_toolbar_host;
$this->_toolbars[$context_id][MIDCOM_TOOLBAR_NODE] = new midcom_helper_toolbar_node($topic);
$this->_toolbars[$context_id][MIDCOM_TOOLBAR_VIEW] = new midcom_helper_toolbar_view;
}
/**
* Add a toolbar
*
* @param string $identifier
* @param midcom_helper_toolbar $toolbar
* @param int $context_id The context to retrieve the help toolbar for, this
* defaults to the current context.
*/
function add_toolbar($identifier, midcom_helper_toolbar $toolbar, $context_id = null)
{
if ($context_id === null)
{
$context_id = midcom_core_context::get()->id;
}
$this->_toolbars[$context_id][$identifier] = $toolbar;
}
/**
* Binds the a toolbar to a DBA object. This will append a number of globally available
* toolbar options. For example, expect Metadata- and Version Control-related options
* to be added.
*
* This call is available through convenience functions throughout the framework: The
* toolbar main class has a mapping for it (midcom_helper_toolbar::bind_to($object))
* and object toolbars created by this service will automatically be bound to the
* specified object.
*
* Repeated bind calls are intercepted, you can only bind a toolbar to a single object.
*
* @see midcom_helper_toolbar::bind_to()
* @see create_object_toolbar()
* @param midcom_helper_toolbar $toolbar
*/
function bind_toolbar_to_object(midcom_helper_toolbar $toolbar, $object)
{
if (!midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX))
{
debug_add("Toolbar for object {$object->guid} was called before topic prefix was available, skipping global items.", MIDCOM_LOG_WARN);
return;
}
if (array_key_exists('midcom_services_toolbars_bound_to_object', $toolbar->customdata))
{
// We already processed this toolbar, skipping further adds.
return;
}
$toolbar->customdata['midcom_services_toolbars_bound_to_object'] = true;
$reflector = new midcom_helper_reflector($object);
$toolbar->set_label($reflector->get_class_label());
$toolbar->bind_object($object);
}
/**
* Renders the specified toolbar for the indicated context.
*
* If the toolbar is undefined, an empty string is returned.
*
* @param int $toolbar_identifier The toolbar identifier constant (one of
* MIDCOM_TOOLBAR_NODE or MIDCOM_TOOLBAR_VIEW etc.)
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @return string The rendered toolbar
* @see midcom_helper_toolbar::render()
*/
function _render_toolbar($toolbar_identifier, $context_id = null)
{
if ($context_id === null)
{
$context_id = midcom_core_context::get()->id;
}
if (! array_key_exists($context_id, $this->_toolbars))
{
return '';
}
return $this->_toolbars[$context_id][$toolbar_identifier]->render();
}
/**
* Renders the node toolbar for the indicated context. If the toolbar is undefined,
* an empty string is returned. If you want to show the toolbar directly, look for
* the show_xxx_toolbar methods.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @return string The rendered toolbar
* @see midcom_helper_toolbar::render()
*/
function render_node_toolbar($context_id = null)
{
return $this->_render_toolbar(MIDCOM_TOOLBAR_NODE, $context_id);
}
/**
* Renders the view toolbar for the indicated context. If the toolbar is undefined,
* an empty string is returned. If you want to show the toolbar directly, look for
* the show_xxx_toolbar methods.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @return string The rendered toolbar
* @see midcom_helper_toolbar::render()
*/
function render_view_toolbar($context_id = null)
{
return $this->_render_toolbar(MIDCOM_TOOLBAR_VIEW, $context_id);
}
/**
* Renders the host toolbar for the indicated context. If the toolbar is undefined,
* an empty string is returned. If you want to show the toolbar directly, look for
* the show_xxx_toolbar methods.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @return string The rendered toolbar
* @see midcom_helper_toolbar::render()
*/
function render_host_toolbar($context_id = null)
{
return $this->_render_toolbar(MIDCOM_TOOLBAR_HOST, $context_id);
}
/**
* Renders the help toolbar for the indicated context. If the toolbar is undefined,
* an empty string is returned. If you want to show the toolbar directly, look for
* the show_xxx_toolbar methods.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @return string The rendered toolbar
* @see midcom_helper_toolbar::render()
*/
function render_help_toolbar($context_id = null)
{
return $this->_render_toolbar(MIDCOM_TOOLBAR_HELP, $context_id);
}
/**
* Displays the node toolbar for the indicated context. If the toolbar is undefined,
* an empty string is returned.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @see midcom_helper_toolbar::render()
*/
function show_node_toolbar($context_id = null)
{
if (!$this->_centralized_mode)
{
echo $this->render_node_toolbar($context_id);
}
}
/**
* Displays the host toolbar for the indicated context. If the toolbar is undefined,
* an empty string is returned.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @see midcom_helper_toolbar::render()
*/
function show_host_toolbar($context_id = null)
{
if (!$this->_centralized_mode)
{
echo $this->render_host_toolbar($context_id);
}
}
/**
* Displays the view toolbar for the indicated context. If the toolbar is undefined,
* an empty string is returned.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @see midcom_helper_toolbar::render()
*/
function show_view_toolbar($context_id = null)
{
if (!$this->_centralized_mode)
{
echo $this->render_view_toolbar($context_id);
}
}
/**
* Displays the help toolbar for the indicated context. If the toolbar is undefined,
* an empty string is returned.
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @see midcom_helper_toolbar::render()
*/
function show_help_toolbar($context_id = null)
{
if (!$this->_centralized_mode)
{
echo $this->render_help_toolbar($context_id);
}
}
/**
* Displays the combined MidCOM toolbar system
*
* @param int $context_id The context to retrieve the node toolbar for, this
* defaults to the current context.
* @see midcom_helper_toolbar::render()
*/
function show($context_id = null)
{
if (!$this->_enable_centralized)
{
return;
}
if (null === $context_id)
{
$context_id = midcom_core_context::get()->id;
}
$this->_centralized_mode = true;
$enable_drag = false;
$toolbar_style = "";
$toolbar_class = "midcom_services_toolbars_simple";
if (midcom::get()->auth->can_user_do('midcom:ajax', null, 'midcom_services_toolbars'))
{
$enable_drag = true;
$toolbar_class = "midcom_services_toolbars_fancy";
$toolbar_style = "display: none;";
}
echo "<div class=\"{$toolbar_class}\" style=\"{$toolbar_style}\">\n";
echo " <div class=\"minimizer\">\n";
echo " </div>\n";
echo " <div class=\"items\">\n";
foreach (array_reverse($this->_toolbars[$context_id], true) as $identifier => $toolbar)
{
if (count($toolbar->items) == 0)
{
continue;
}
switch ($identifier)
{
case MIDCOM_TOOLBAR_VIEW:
$id = $class = 'page';
break;
case MIDCOM_TOOLBAR_NODE:
$id = $class = 'folder';
break;
case MIDCOM_TOOLBAR_HOST:
$id = $class = 'host';
break;
case MIDCOM_TOOLBAR_HELP:
$id = $class = 'help';
break;
default:
$id = 'custom-' . $identifier;
$class = 'custom';
break;
}
echo " <div id=\"midcom_services_toolbars_topic-{$id}\" class=\"item\">\n";
echo " <span class=\"midcom_services_toolbars_topic_title {$class}\">" . $toolbar->get_label() . "</span>\n";
echo $toolbar->render();
echo " </div>\n";
}
echo "</div>\n";
if ($enable_drag)
{
echo " <div class=\"dragbar\"></div>\n";
}
echo "</div>\n";
}
}