Completed
Push — master ( 55c49d...8b3df6 )
by Andreas
25:17
created

midgard_admin_asgard_navigation::_is_collapsed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midgard.admin.asgard
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
/**
10
 * Navigation class for Asgard
11
 *
12
 * @package midgard.admin.asgard
13
 */
14
class midgard_admin_asgard_navigation extends midcom_baseclasses_components_purecode
15
{
16
    /**
17
     * Root types
18
     *
19
     * @var array
20
     */
21
    public $root_types = [];
22
23
    /**
24
     * Some object
25
     *
26
     * @var midgard\portable\api\mgdobject
27
     */
28
    protected $_object;
29
30
    /**
31
     * Object path to the current object.
32
     *
33
     * @var Array
34
     */
35
    private $_object_path = [];
36
37
    private $_reflectors = [];
38
    private $_request_data = [];
39
    private $expanded_root_types = [];
40
    protected $shown_objects = [];
41
42 2
    public function __construct($object, &$request_data)
43
    {
44 2
        parent::__construct();
45
46 2
        $this->_object = $object;
47 2
        $this->get_object_path();
48 2
        $this->_request_data =& $request_data;
49
50 2
        $this->root_types = midcom_helper_reflector_tree::get_root_classes();
51
52 2
        if (array_key_exists('current_type', $this->_request_data)) {
53
            $expanded_type = $this->_request_data['current_type'];
54
            if (!in_array($expanded_type, $this->root_types)) {
55
                $expanded_type = midcom_helper_reflector_tree::get($expanded_type)->get_parent_class();
56
            }
57
            $this->expanded_root_types[] = $expanded_type;
58 2
        } elseif (isset($this->_object)) {
59
            // we go through the path bottom up and show the first root type we find
60 2
            foreach (array_reverse($this->_object_path) as $node) {
61 2
                foreach ($this->root_types as $root_type) {
62 2
                    if (    is_a($node, $root_type)
63 2
                        || midcom_helper_reflector::is_same_class($root_type, $node->__midcom_class_name__)) {
64 2
                        $this->expanded_root_types[] = $root_type;
65 2
                        break;
66
                    }
67
                }
68
            }
69
        }
70 2
    }
71
72
    /**
73
     *
74
     * @param midgard\portable\api\mgdobject $object
75
     * @return midcom_helper_reflector_tree
76
     */
77 2
    protected function _get_reflector($object)
78
    {
79 2
        if (is_string($object)) {
0 ignored issues
show
introduced by
The condition is_string($object) is always false.
Loading history...
80
            $classname = $object;
81
        } else {
82 2
            $classname = get_class($object);
83
        }
84 2
        if (!isset($this->_reflectors[$classname])) {
85 2
            $this->_reflectors[$classname] = midcom_helper_reflector_tree::get($object);
86
        }
87
88 2
        return $this->_reflectors[$classname];
89
    }
90
91 2
    private function get_object_path()
92
    {
93 2
        if (is_object($this->_object)) {
94 2
            foreach (midcom_helper_reflector_tree::resolve_path_parts($this->_object) as $part) {
95 2
                $this->_object_path[] = $part['object'];
96
            }
97
        }
98 2
    }
99
100
    protected function _is_collapsed($type, $total)
101
    {
102
        return (   $total > $this->_config->get('max_navigation_entries')
103
                && empty($_GET['show_all_' . $type]));
104
    }
105
106 2
    protected function _list_child_elements($object, $level = 0)
107
    {
108 2
        if ($level > 25) {
109
            debug_add('Recursion level 25 exceeded, aborting', MIDCOM_LOG_ERROR);
110
            return;
111
        }
112 2
        $ref = $this->_get_reflector($object);
113
114 2
        $child_types = [];
115 2
        foreach ($ref->get_child_classes() as $class) {
116 2
            $qb = $ref->_child_objects_type_qb($class, $object, false);
117
118 2
            if (   !$qb
119 2
                || !($count = $qb->count_unchecked())) {
120 2
                continue;
121
            }
122 1
            midcom_helper_reflector_tree::add_schema_sorts_to_qb($qb, $class);
123 1
            if ($this->_is_collapsed($class, $count)) {
124
                $qb->set_limit($this->_config->get('max_navigation_entries'));
125
            }
126 1
            $child_types[$class] = ['total' => $count, 'qb' => $qb];
127
        }
128
129 2
        if (!empty($child_types)) {
130 1
            echo "<ul>\n";
131 1
            foreach ($child_types as $type => $data) {
132 1
                $children = $data['qb']->execute();
133 1
                $label_mapping = [];
134 1
                foreach ($children as $i => $child) {
135 1
                    if (isset($this->shown_objects[$child->guid])) {
136
                        continue;
137
                    }
138
139 1
                    $ref = $this->_get_reflector($child);
140 1
                    $label_mapping[$i] = htmlspecialchars($ref->get_object_label($child));
141
                }
142
143 1
                asort($label_mapping);
144
145 1
                foreach ($label_mapping as $index => $label) {
146 1
                    $child = $children[$index];
147 1
                    $this->_draw_element($child, $label, $level);
148
                }
149 1
                if ($this->_is_collapsed($type, $data['total'])) {
150 1
                    $this->_draw_collapsed_element($level, $type, $data['total']);
151
                }
152
            }
153 1
            echo "</ul>\n";
154
        }
155 2
    }
156
157
    /**
158
     * Renders the given root objects to HTML and calls _list_child_elements()
159
     *
160
     * @param midcom_helper_reflector_tree $ref Reflector singleton
161
     */
162
    private function _list_root_elements(midcom_helper_reflector_tree $ref)
163
    {
164
        $qb = $ref->_root_objects_qb(false);
165
166
        if (   !$qb
167
            || !($total = $qb->count_unchecked())) {
168
            return;
169
        }
170
        midcom_helper_reflector_tree::add_schema_sorts_to_qb($qb, $ref->mgdschema_class);
171
        if ($this->_is_collapsed($ref->mgdschema_class, $total)) {
172
            $qb->set_limit($this->_config->get('max_navigation_entries'));
173
        }
174
175
        echo "<ul class=\"midgard_admin_asgard_navigation\">\n";
176
177
        $root_objects = $qb->execute();
178
179
        $label_mapping = [];
180
        foreach ($root_objects as $i => $object) {
181
            $label_mapping[$i] = htmlspecialchars($ref->get_object_label($object));
182
        }
183
184
        asort($label_mapping);
185
        $autoexpand = (count($root_objects) == 1);
186
        foreach ($label_mapping as $index => $label) {
187
            $object = $root_objects[$index];
188
            $this->_draw_element($object, $label, 1, $autoexpand);
189
        }
190
        if ($this->_is_collapsed($ref->mgdschema_class, $total)) {
191
            $this->_draw_collapsed_element(0, $ref->mgdschema_class, $total);
192
        }
193
194
        echo "</ul>\n";
195
    }
196
197
    private function _draw_collapsed_element($level, $type, $total)
198
    {
199
        $ref = midcom_helper_reflector::get($type);
200
        if (!empty($this->_object_path[$level])) {
201
            if ($this->_object_path[$level]->__mgdschema_class_name__ == $type) {
202
                $object = $this->_object_path[$level];
203
            } elseif ($level == 0) {
204
                // this is the case where our object has parents, but we're in its type view directly
205
                foreach ($this->_object_path as $candidate) {
206
                    if ($candidate->__mgdschema_class_name__ == $type) {
207
                        $object = $candidate;
208
                        break;
209
                    }
210
                }
211
            }
212
            if (!empty($object)) {
213
                $label = htmlspecialchars($ref->get_object_label($object));
214
                $this->_draw_element($object, $label, $level);
215
            }
216
        }
217
        $icon = midcom_helper_reflector::get_object_icon(new $type);
218
        echo '<li><a class="expand-type-children" href="?show_all_' . $type . '=1">' . $icon . ' ' . sprintf($this->_l10n->get('show all %s %s entries'), $total, $ref->get_class_label()) . '</a></li>';
219
    }
220
221
    protected function _draw_element($object, $label, $level, $autoexpand = false)
222
    {
223
        $ref = $this->_get_reflector($object);
224
225
        $selected = $this->_is_selected($object);
226
        $css_class = get_class($object);
227
        $this->_common_css_classes($object, $ref, $css_class);
228
229
        $mode = $this->_request_data['default_mode'];
230
        if (strpos($css_class, 'readonly')) {
231
            $mode = 'view';
232
        }
233
234
        $this->shown_objects[$object->guid] = true;
235
236
        echo "    <li class=\"{$css_class}\">";
237
238
        $icon = $ref->get_object_icon($object);
239
240
        if (trim($label) == '') {
241
            $label = $ref->get_class_label() . ' #' . $object->id;
242
        }
243
        $label = html_entity_decode($label);
244
245
        echo "<a href=\"" . midcom_connection::get_url('self') . "__mfa/asgard/object/{$mode}/{$object->guid}/\" title=\"GUID: {$object->guid}, ID: {$object->id}\">{$icon}{$label}</a>\n";
246
        if (   $selected
247
            || $autoexpand) {
248
            $this->_list_child_elements($object, $level + 1);
249
        }
250
        echo "    </li>\n";
251
    }
252
253
    private function _draw_plugins()
254
    {
255
        $customdata = midcom::get()->componentloader->get_all_manifest_customdata('asgard_plugin');
256
        foreach ($customdata as $component => $plugin_config) {
257
            $this->_request_data['section_url'] = midcom_connection::get_url('self') . "__mfa/asgard_{$component}/";
258
            $this->_request_data['section_name'] = $this->_i18n->get_string($component, $component);
259
            $class = $plugin_config['class'];
260
261
            if (!midcom::get()->auth->can_user_do("{$component}:access", null, $class)) {
262
                // Disabled plugin
263
                continue;
264
            }
265
266
            if (   method_exists($class, 'navigation')
267
                && ($this->_request_data['plugin_name'] == "asgard_{$component}")) {
268
                $this->_request_data['expanded'] = true;
269
                midcom_show_style('midgard_admin_asgard_navigation_section_header');
270
                call_user_func([$class, 'navigation']);
271
            } else {
272
                $this->_request_data['expanded'] = false;
273
                midcom_show_style('midgard_admin_asgard_navigation_section_header');
274
            }
275
276
            midcom_show_style('midgard_admin_asgard_navigation_section_footer');
277
        }
278
    }
279
280 1
    private function _is_selected($object)
281
    {
282 1
        foreach ($this->_object_path as $path_object) {
283 1
            if ($object->guid == $path_object->guid) {
284 1
                return true;
285
            }
286
        }
287 1
        return false;
288
    }
289
290 1
    protected function _common_css_classes($object, $ref, &$css_class)
291
    {
292 1
        $css_class .= " {$ref->mgdschema_class}";
293
294
        // Populate common properties
295 1
        $css_class = midcom::get()->metadata->get_object_classes($object, $css_class);
296
297 1
        if ($this->_is_selected($object)) {
298
            $css_class .= ' selected';
299
        }
300 1
        if (   is_object($this->_object)
301 1
            && (   $object->guid == $this->_object->guid
302 1
                || (   is_a($this->_object, midcom_db_parameter::class)
303 1
                    && $object->guid == $this->_object->parentguid))) {
304
            $css_class .= ' current';
305
        }
306 1
        if ( !$object->can_do('midgard:update')) {
307
            $css_class .= ' readonly';
308
        }
309 1
    }
310
311
    /**
312
     * Appliy visibility restrictions from various sources
313
     *
314
     * @return array Alphabetically sorted list of class => title pairs
315
     */
316
    private function _process_root_types()
317
    {
318
        // Included or excluded types
319
        $types = [];
320
321
        // Get the types that might have special display conditions
322
        if (   $this->_config->get('midgard_types')
323
            && preg_match_all('/\|([a-z0-9\.\-_]+)/', $this->_config->get('midgard_types'), $regs)) {
324
            $types = $regs[1];
325
        }
326
327
        // Override with user selected
328
        // @TODO: Should this just include to the configuration selection, although it would break the consistency
329
        // of other similar preference sets, which simply override the global settings?
330
        if (   midgard_admin_asgard_plugin::get_preference('midgard_types')
331
            && preg_match_all('/\|([a-z0-9\.\-_]+)/', midgard_admin_asgard_plugin::get_preference('midgard_types'), $regs)) {
332
            $types = $regs[1];
333
        }
334
335
        // Get the inclusion/exclusion model
336
        $model = $this->_config->get('midgard_types_model');
337
        if (midgard_admin_asgard_plugin::get_preference('midgard_types_model')) {
338
            $model = midgard_admin_asgard_plugin::get_preference('midgard_types_model');
339
        }
340
        $exclude = ($model == 'exclude');
341
342
        // Get the possible regular expression
343
        $regexp = $this->_config->get('midgard_types_regexp');
344
        if (midgard_admin_asgard_plugin::get_preference('midgard_types_regexp')) {
345
            $regexp = midgard_admin_asgard_plugin::get_preference('midgard_types_regexp');
346
        }
347
348
        // "Convert" quickly to PERL regular expression
349
        if (!preg_match('/^[\/|]/', $regexp)) {
350
            $regexp = "/{$regexp}/";
351
        }
352
353
        if ($exclude) {
354
            $types = array_diff($this->root_types, $types);
355
        } elseif (!empty($types)) {
356
            $types = array_intersect($this->root_types, $types);
357
        }
358
359
        $label_mapping = [];
360
        foreach ($types as $root_type) {
361
            // If the regular expression has been set, check which types should be shown
362
            if (   $regexp !== '//'
363
                && (boolean) preg_match($regexp, $root_type) == $exclude) {
364
                continue;
365
            }
366
367
            $ref = $this->_get_reflector($root_type);
368
            $label_mapping[$root_type] = $ref->get_class_label();
369
        }
370
        asort($label_mapping);
371
372
        return $label_mapping;
373
    }
374
375
    public function draw()
376
    {
377
        $this->_request_data['chapter_name'] = midcom::get()->config->get('midcom_site_title');
378
        midcom_show_style('midgard_admin_asgard_navigation_chapter');
379
380
        $this->_draw_plugins();
381
382
        if (!midcom::get()->auth->can_user_do('midgard.admin.asgard:manage_objects', null, 'midgard_admin_asgard_plugin')) {
383
            return;
384
        }
385
386
        $label_mapping = $this->_process_root_types();
387
388
        $expanded_types = array_intersect(array_keys($label_mapping), $this->expanded_root_types);
389
390
        /*
391
         * Use a dropdown for displaying the navigation if at least one type is expanded
392
         * and the user has the corresponding preference set. That way, you expanded types
393
         * can take up the maximum available space while all types are still accessible with one
394
         * click if nothing is expanded
395
         */
396
        $types_shown = false;
397
        if (    !empty($expanded_types)
398
             && midgard_admin_asgard_plugin::get_preference('navigation_type') === 'dropdown') {
399
            $this->_draw_select_navigation();
400
            $types_shown = true;
401
        }
402
403
        foreach ($expanded_types as $root_type) {
404
            $this->_request_data['section_url'] = midcom_connection::get_url('self') . "__mfa/asgard/{$root_type}";
405
            $this->_request_data['section_name'] = $label_mapping[$root_type];
406
            $this->_request_data['expanded'] = true;
407
            midcom_show_style('midgard_admin_asgard_navigation_section_header');
408
            $ref = $this->_get_reflector($root_type);
409
            $this->_list_root_elements($ref);
410
411
            midcom_show_style('midgard_admin_asgard_navigation_section_footer');
412
        }
413
414
        if (!$types_shown) {
415
            $this->_request_data['section_name'] = $this->_l10n->get('midgard objects');
416
            $this->_request_data['section_url'] = null;
417
            $this->_request_data['expanded'] = true;
418
            midcom_show_style('midgard_admin_asgard_navigation_section_header');
419
            $collapsed_types = array_diff_key($label_mapping, array_flip($expanded_types));
420
421
            $this->_draw_type_list($collapsed_types);
422
423
            midcom_show_style('midgard_admin_asgard_navigation_section_footer');
424
        }
425
    }
426
427
    private function _draw_type_list(array $types)
428
    {
429
        echo "<ul class=\"midgard_admin_asgard_navigation\">\n";
430
431
        foreach ($types as $type => $label) {
432
            $url = midcom_connection::get_url('self') . "__mfa/asgard/{$type}/";
433
            echo "    <li class=\"mgdschema-type\">";
434
435
            $dbaclass = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($type);
436
            if (   $dbaclass
437
                && class_exists($dbaclass)) {
438
                $object = new $dbaclass;
439
            } else {
440
                $object = new $type;
441
            }
442
            $icon = midcom_helper_reflector::get_object_icon($object);
443
444
            echo "<a href=\"" . $url . "\" title=\"{$label}\">{$icon}{$label}</a>\n";
445
446
            echo "    </li>\n";
447
        }
448
449
        echo "</ul>\n";
450
    }
451
452
    private function _draw_select_navigation()
453
    {
454
        if (!empty($this->_object_path)) {
455
            $this->_request_data['root_object'] = $this->_object_path[0];
456
            $this->_request_data['navigation_type'] = $this->_object_path[0]->__mgdschema_class_name__;
457
        } elseif (isset($this->expanded_root_types[0])) {
458
            $this->_request_data['navigation_type'] = $this->expanded_root_types[0];
459
        } else {
460
            $this->_request_data['navigation_type'] = '';
461
        }
462
463
        $label_mapping = [];
464
465
        foreach ($this->root_types as $root_type) {
466
            $ref = $this->_get_reflector($root_type);
467
            $label_mapping[$root_type] = $ref->get_class_label();
468
        }
469
        asort($label_mapping);
470
471
        $this->_request_data['label_mapping'] = $label_mapping;
472
        $this->_request_data['expanded_root_types'] = $this->expanded_root_types;
473
474
        midcom_show_style('midgard_admin_asgard_navigation_sections');
475
    }
476
}
477