Subversion Repositories oidplus

Rev

Rev 511 | Rev 542 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
511 daniel-mar 5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
2 daniel-mar 6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
511 daniel-mar 20
if (!defined('INSIDE_OIDPLUS')) die();
21
 
250 daniel-mar 22
class OIDplusMenuUtils {
2 daniel-mar 23
 
104 daniel-mar 24
        public static function nonjs_menu() {
2 daniel-mar 25
                $json = array();
26
 
106 daniel-mar 27
                $static_node_id = isset($_REQUEST['goto']) ? $_REQUEST['goto'] : 'oidplus:system';
28
 
281 daniel-mar 29
                foreach (OIDplus::getPagePlugins() as $plugin) {
331 daniel-mar 30
                        // Note: The system (OIDplusMenuUtils) does only show the menu of
31
                        //       publicPage plugins. Menu entries for RAs and Admins are
32
                        //       handled by the tree() function of the plugin publicPages/090_login
281 daniel-mar 33
                        if (is_subclass_of($plugin, OIDplusPagePluginPublic::class)) {
34
                                $plugin->tree($json, null, true, $static_node_id);
35
                        }
61 daniel-mar 36
                }
2 daniel-mar 37
 
38
                foreach ($json as $x) {
39
                        if ($static_node_id == $x['id']) echo '<b>';
366 daniel-mar 40
                        if (isset($x['indent'])) echo str_repeat('&nbsp;', $x['indent']*5);
360 daniel-mar 41
                        $cur_lang = OIDplus::getCurrentLang();
42
                        if ($cur_lang != OIDplus::DEFAULT_LANGUAGE) {
43
                                echo '<a href="?lang='.$cur_lang.'&amp;goto='.urlencode($x['id']).'">';
44
                        } else {
45
                                echo '<a href="?goto='.urlencode($x['id']).'">';
46
                        }
127 daniel-mar 47
                        if (!empty($x['icon'])) echo '<img src="'.$x['icon'].'" alt=""> ';
423 daniel-mar 48
                        echo htmlentities($x['id']).' | '.htmlentities($x['text']).'</a><br>';
2 daniel-mar 49
                        if ($static_node_id == $x['id']) echo '</b>';
50
                }
51
 
52
        }
53
 
106 daniel-mar 54
        // req_id comes from jsTree via AJAX
55
        // req_goto comes from the user (GET argument)
2 daniel-mar 56
        public static function json_tree($req_id, $req_goto) {
106 daniel-mar 57
                $json = array();
2 daniel-mar 58
 
59
                if (!isset($req_id) || ($req_id == '#')) {
281 daniel-mar 60
                        foreach (OIDplus::getPagePlugins() as $plugin) {
331 daniel-mar 61
                                // Note: The system (OIDplusMenuUtils) does only show the menu of
62
                                //       publicPage plugins. Menu entries for RAs and Admins are
63
                                //       handled by the tree() function of the plugin publicPages/090_login
281 daniel-mar 64
                                if (is_subclass_of($plugin, OIDplusPagePluginPublic::class)) {
65
                                        $plugin->tree($json, null, false, $req_goto);
66
                                }
61 daniel-mar 67
                        }
2 daniel-mar 68
                } else {
106 daniel-mar 69
                        $json = self::tree_populate($req_id);
2 daniel-mar 70
                }
71
 
541 daniel-mar 72
                self::addHrefIfRequired($json);
73
 
328 daniel-mar 74
                return $json;
2 daniel-mar 75
        }
76
 
541 daniel-mar 77
        protected static function addHrefIfRequired(&$json) {
78
                if (!is_array($json)) return;
79
                foreach ($json as &$item) {
80
                        if (!isset($item['a_attr']) && isset($item['id'])) {
81
                                $item['a_attr'] = array("href" => "?goto=".urlencode($item['id']));
82
                        }
83
                        if (isset($item['children'])) {
84
                                self::addHrefIfRequired($item['children']);
85
                        }
86
                }
87
        }
88
 
104 daniel-mar 89
        public static function tree_populate($parent, $goto_path=null) {
2 daniel-mar 90
                $children = array();
91
 
92
                $parentObj = OIDplusObject::parse($parent);
93
 
94
                @list($namespace, $oid) = explode(':', $parent, 2);
499 daniel-mar 95
                if ($namespace == 'oid') $oid = substr($oid, 1); // Remove leading dot
2 daniel-mar 96
 
281 daniel-mar 97
                if (is_array($goto_path)) array_shift($goto_path);
331 daniel-mar 98
 
2 daniel-mar 99
                $confidential_oids = array();
100
 
443 daniel-mar 101
                $res = OIDplus::db()->query("select id from ###objects where confidential = ?", array(true));
236 daniel-mar 102
                while ($row = $res->fetch_array()) {
2 daniel-mar 103
                        $confidential_oids[] = $row['id'];
104
                }
105
 
261 daniel-mar 106
                $res = OIDplus::db()->query("select * from ###objects where parent = ? order by ".OIDplus::db()->natOrder('id'), array($parent));
236 daniel-mar 107
                while ($row = $res->fetch_array()) {
2 daniel-mar 108
                        $obj = OIDplusObject::parse($row['id']);
109
 
110
                        if (!$obj->userHasReadRights()) continue;
111
 
112
                        $child = array();
113
                        $child['id'] = $row['id'];
114
 
499 daniel-mar 115
                        // Determine display name (relative OID)
2 daniel-mar 116
                        $child['text'] = $obj->jsTreeNodeName($parentObj);
104 daniel-mar 117
                        $child['text'] .= empty($row['title']) ? /*' -- <i>'.htmlentities('Title missing').'</i>'*/ '' : ' -- <b>' . htmlentities($row['title']) . '</b>';
2 daniel-mar 118
 
119
                        $is_confidential = false;
120
                        foreach ($confidential_oids as $test) {
121
                                $is_confidential |= ($row['id'] === $test) || (strpos($row['id'],$test.'.') === 0);
122
                        }
123
                        if ($is_confidential) {
124
                                $child['text'] = '<font color="gray"><i>'.$child['text'].'</i></font>';
125
                        }
126
 
499 daniel-mar 127
                        // Determine icon
2 daniel-mar 128
                        $child['icon'] = $obj->getIcon($row);
129
 
499 daniel-mar 130
                        // Check if there are more sub OIDs
281 daniel-mar 131
                        if ($goto_path === true) {
2 daniel-mar 132
                                $child['children'] = self::tree_populate($row['id'], $goto_path);
133
                                $child['state'] = array("opened" => true);
281 daniel-mar 134
                        } else if (!is_null($goto_path) && (count($goto_path) > 0) && ($goto_path[0] === $row['id'])) {
135
                                $child['children'] = self::tree_populate($row['id'], $goto_path);
136
                                $child['state'] = array("opened" => true);
2 daniel-mar 137
                        } else {
150 daniel-mar 138
                                $obj_children = $obj->getChildren();
145 daniel-mar 139
 
140
                                // Variant 1: Fast, but does not check for hidden OIDs
150 daniel-mar 141
                                //$child_count = count($obj_children);
145 daniel-mar 142
 
143
                                // variant 2
144
                                $child_count = 0;
150 daniel-mar 145
                                foreach ($obj_children as $obj_test) {
145 daniel-mar 146
                                        if (!$obj_test->userHasReadRights()) continue;
147
                                        $child_count++;
148
                                }
149
 
150
                                $child['children'] = $child_count > 0;
2 daniel-mar 151
                        }
152
 
153
                        $children[] = $child;
154
                }
155
 
156
                return $children;
157
        }
366 daniel-mar 158
}