Rev 9 | 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 |
||
5 | * Copyright 2019 Daniel Marschall, ViaThinkSoft |
||
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 | |||
20 | class OIDplusTree { |
||
21 | |||
22 | public static function nonjs_menu($static_node_id) { |
||
23 | if ($static_node_id == 'oidplus:system') echo '<b>'; |
||
9 | daniel-mar | 24 | echo '<a href="?goto=oidplus:system"><img src="img/system.png" alt="System icon"> System</a><br>'; |
2 | daniel-mar | 25 | if ($static_node_id == 'oidplus:system') echo '</b>'; |
26 | |||
27 | $parent = ''; |
||
28 | $res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($static_node_id)."'"); |
||
29 | while ($row = OIDplus::db()->fetch_object($res)) { |
||
30 | $parent = $row->parent; |
||
31 | } |
||
32 | |||
6 | daniel-mar | 33 | $objTypesChildren = array(); |
34 | foreach (OIDplusObject::$registeredObjectTypes as $ot) { |
||
2 | daniel-mar | 35 | $icon = 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png'; |
36 | if (file_exists($icon)) { |
||
9 | daniel-mar | 37 | $icon = '<img src="'.$icon.'" alt="'.$ot::ns().' icon"> '; |
2 | daniel-mar | 38 | } |
39 | |||
40 | if ($ot::ns().':' == $static_node_id) echo '<b>'; |
||
41 | echo '<a href="?goto='.urlencode($ot::root()).'">'.$icon.htmlentities($ot::objectTypeTitle()).'</a><br>'; |
||
42 | if ($ot::ns().':' == $static_node_id) echo '</b>'; |
||
43 | |||
44 | $tmp = OIDplusObject::parse($static_node_id); |
||
45 | if (!is_null($tmp) && ($ot == get_class($tmp))) { |
||
46 | // TODO: Instead of just having 3 levels (parent, this and children), it would be better if we'd had a full tree of all parents |
||
47 | $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where " . |
||
48 | "parent = '".OIDplus::db()->real_escape_string($static_node_id)."' or " . |
||
49 | "id = '".OIDplus::db()->real_escape_string($static_node_id)."' " . |
||
50 | ((!empty($parent)) ? " or id = '".OIDplus::db()->real_escape_string($parent)."' " : "") . |
||
51 | "order by ".OIDplus::db()->natOrder('id')); |
||
52 | $z_used = 0; |
||
53 | $y_used = 0; |
||
54 | $x_used = 0; |
||
55 | $stufe = 0; |
||
56 | $menu_entires = array(); |
||
57 | $stufen = array(); |
||
58 | while ($row = OIDplus::db()->fetch_object($res)) { |
||
59 | $obj = OIDplusObject::parse($row->id); |
||
60 | if (is_null($obj)) continue; // might happen if the objectType is not available/loaded |
||
61 | if (!$obj->userHasReadRights()) continue; |
||
62 | $txt = $row->title == '' ? '' : ' -- '.htmlentities($row->title); |
||
63 | |||
64 | if ($row->id == $parent) { $stufe=0; $z_used++; } |
||
65 | if ($row->id == $static_node_id) { $stufe=1; $y_used++; } |
||
66 | if ($row->parent == $static_node_id) { $stufe=2; $x_used++; } |
||
67 | |||
68 | $menu_entry = '<a href="?goto='.htmlentities($row->id).'">'.htmlentities($row->id).'</a>'.$txt; |
||
69 | if ($row->id === $static_node_id) $menu_entry = '<b>'.$menu_entry.'</b>'; |
||
70 | $menu_entries[] = $menu_entry; |
||
71 | $stufen[] = $stufe; |
||
72 | } |
||
73 | if ($x_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 2) $menu_entry = str_repeat(' ', 5) . $menu_entry; |
||
74 | if ($y_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 1) $menu_entry = str_repeat(' ', 5) . $menu_entry; |
||
75 | if ($z_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 0) $menu_entry = str_repeat(' ', 5) . $menu_entry; |
||
76 | echo implode("<br>\n", $menu_entries)."<br>\n"; |
||
77 | } |
||
6 | daniel-mar | 78 | } |
2 | daniel-mar | 79 | |
80 | // === Plugins === |
||
81 | |||
82 | $json = array(); |
||
83 | |||
84 | $ary = glob(__DIR__ . '/../../plugins/publicPages/'.'*'.'/tree.inc.php'); |
||
85 | sort($ary); |
||
86 | foreach ($ary as $a) include $a; |
||
87 | |||
88 | foreach ($json as $x) { |
||
89 | if ($static_node_id == $x['id']) echo '<b>'; |
||
9 | daniel-mar | 90 | echo '<a href="?goto='.urlencode($x['id']).'"><img src="'.$x['icon'].'" alt="'.$x['id'].' icon"> '.htmlentities($x['text']).'</a><br>'; |
2 | daniel-mar | 91 | if ($static_node_id == $x['id']) echo '</b>'; |
92 | } |
||
93 | |||
94 | } |
||
95 | |||
96 | public static function json_tree($req_id, $req_goto) { |
||
97 | |||
98 | $lang = array(); |
||
99 | $lang['title_missing'] = 'Title missing'; |
||
100 | |||
101 | if (!isset($req_id) || ($req_id == '#')) { |
||
102 | $json = array(); |
||
103 | |||
104 | if (isset($req_goto)) { |
||
6 | daniel-mar | 105 | $goto = $req_goto; |
106 | $path = array(); |
||
107 | while (true) { |
||
108 | $path[] = $goto; |
||
109 | $res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($goto)."'"); |
||
110 | if (OIDplus::db()->num_rows($res) == 0) break; |
||
111 | $row = OIDplus::db()->fetch_array($res); |
||
2 | daniel-mar | 112 | $goto = $row['parent']; |
6 | daniel-mar | 113 | } |
2 | daniel-mar | 114 | |
6 | daniel-mar | 115 | $goto_path = array_reverse($path); |
2 | daniel-mar | 116 | } else { |
117 | $goto_path = null; |
||
118 | } |
||
119 | |||
120 | $objTypesChildren = array(); |
||
121 | foreach (OIDplusObject::$registeredObjectTypes as $ot) { |
||
122 | $child = array( |
||
123 | 'id' => $ot::root(), |
||
124 | 'text' => $ot::objectTypeTitle(), |
||
125 | 'state' => array("opened" => true), |
||
126 | 'icon' => 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png', |
||
127 | 'children' => self::tree_populate($ot::root(), $goto_path) |
||
128 | ); |
||
129 | if (!file_exists($child['icon'])) $child['icon'] = null; // default icon (folder) |
||
130 | $objTypesChildren[] = $child; |
||
131 | } |
||
132 | |||
133 | $json[] = array( |
||
134 | 'id' => "oidplus:system", |
||
135 | 'text' => "Objects", |
||
136 | 'state' => array( |
||
137 | "opened" => true, |
||
138 | // "selected" => true) // "selected" ist buggy: 1) Das select-Event wird beim Laden nicht gefeuert 2) Die direkt untergeordneten Knoten lassen sich nicht öffnen (laden für ewig) |
||
139 | ), |
||
140 | 'icon' => 'img/system.png', |
||
141 | 'children' => $objTypesChildren |
||
142 | ); |
||
143 | |||
144 | $ra_emails = OIDplus::authUtils()::loggedInRaList(); |
||
145 | |||
146 | $loginChildren = array(); |
||
147 | |||
148 | if (OIDplus::authUtils()::isAdminLoggedIn()) { |
||
149 | $ra_roots = array(); |
||
150 | |||
151 | $ary = glob(__DIR__ . '/../../plugins/adminPages/'.'*'.'/tree.inc.php'); |
||
152 | sort($ary); |
||
153 | foreach ($ary as $a) include $a; |
||
154 | |||
155 | $ra_roots[] = array( |
||
25 | daniel-mar | 156 | 'id' => 'oidplus:logout$admin', |
157 | 'icon' => 'img/logout.png', |
||
158 | 'text' => 'Log out' |
||
2 | daniel-mar | 159 | ); |
160 | $loginChildren[] = array( |
||
161 | 'id' => 'oidplus:dummy$admin', |
||
162 | 'text' => "Logged in as admin", |
||
163 | 'icon' => 'img/admin.png', |
||
164 | 'state' => array("opened" => true), |
||
165 | 'children' => $ra_roots |
||
166 | ); |
||
167 | } |
||
168 | foreach ($ra_emails as $ra_email) { |
||
169 | $ra_roots = array(); |
||
170 | |||
171 | $ary = glob(__DIR__ . '/../../plugins/raPages/'.'*'.'/tree.inc.php'); |
||
172 | sort($ary); |
||
173 | foreach ($ary as $a) include $a; |
||
174 | |||
175 | $ra_roots[] = array( |
||
25 | daniel-mar | 176 | 'id' => 'oidplus:logout$'.$ra_email, |
177 | 'icon' => 'img/logout.png', |
||
178 | 'text' => 'Log out' |
||
2 | daniel-mar | 179 | ); |
180 | foreach (OIDplusObject::getRaRoots($ra_email) as $loc_root) { |
||
5 | daniel-mar | 181 | $ico = $loc_root->getIcon(); |
2 | daniel-mar | 182 | $ra_roots[] = array( |
183 | 'id' => 'oidplus:raroot$'.$loc_root->nodeId(), |
||
184 | 'text' => 'Jump to RA root '.$loc_root->objectTypeTitleShort().' '.$loc_root->crudShowId(OIDplusObject::parse($loc_root::root())), |
||
5 | daniel-mar | 185 | 'icon' => !is_null($ico) ? $ico : 'img/link.png' |
2 | daniel-mar | 186 | ); |
187 | } |
||
188 | $ra_email_or_name = (new OIDplusRA($ra_email))->raName(); |
||
189 | if ($ra_email_or_name == '') $ra_email_or_name = $ra_email; |
||
190 | $loginChildren[] = array( |
||
191 | 'id' => 'oidplus:dummy$'.$ra_email, |
||
192 | 'text' => "Logged in as ".htmlentities($ra_email_or_name), |
||
193 | 'icon' => 'img/ra.png', |
||
194 | 'state' => array("opened" => true), |
||
195 | 'children' => $ra_roots |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | $json[] = array( |
||
25 | daniel-mar | 200 | 'id' => 'oidplus:login', |
201 | 'icon' => 'img/login.png', |
||
202 | 'text' => 'Login', |
||
2 | daniel-mar | 203 | 'state' => array("opened" => count($loginChildren)>0), |
204 | 'children' => $loginChildren |
||
205 | ); |
||
206 | |||
207 | $ary = glob(__DIR__ . '/../../plugins/publicPages/'.'*'.'/tree.inc.php'); |
||
208 | sort($ary); |
||
209 | foreach ($ary as $a) include $a; |
||
210 | } else { |
||
211 | $json = self::tree_populate($req_id); |
||
212 | } |
||
213 | |||
214 | return json_encode($json); |
||
215 | } |
||
216 | |||
217 | private static function tree_populate($parent, $goto_path=null) { |
||
218 | global $lang; |
||
219 | $children = array(); |
||
220 | |||
221 | $parentObj = OIDplusObject::parse($parent); |
||
222 | |||
223 | @list($namespace, $oid) = explode(':', $parent, 2); |
||
224 | if ($namespace == 'oid') $oid = substr($oid, 1); // führenden Punkt entfernen |
||
225 | |||
226 | if (!is_null($goto_path)) array_shift($goto_path); |
||
227 | |||
228 | $confidential_oids = array(); |
||
229 | |||
230 | $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where confidential = '1'"); |
||
231 | while ($row = OIDplus::db()->fetch_array($res)) { |
||
232 | $confidential_oids[] = $row['id']; |
||
233 | } |
||
234 | |||
235 | $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = '".OIDplus::db()->real_escape_string($parent)."' order by ".OIDplus::db()->natOrder('id')); |
||
236 | while ($row = OIDplus::db()->fetch_array($res)) { |
||
237 | $obj = OIDplusObject::parse($row['id']); |
||
238 | |||
239 | if (!$obj->userHasReadRights()) continue; |
||
240 | |||
241 | $child = array(); |
||
242 | $child['id'] = $row['id']; |
||
243 | |||
244 | // Anzeigenamen (relative OID) bestimmen |
||
245 | $child['text'] = $obj->jsTreeNodeName($parentObj); |
||
246 | $child['text'] .= empty($row['title']) ? /*' -- <i>'.htmlentities($lang['title_missing']).'</i>'*/ '' : ' -- <b>' . htmlentities($row['title']) . '</b>'; |
||
247 | |||
248 | $is_confidential = false; |
||
249 | foreach ($confidential_oids as $test) { |
||
250 | $is_confidential |= ($row['id'] === $test) || (strpos($row['id'],$test.'.') === 0); |
||
251 | } |
||
252 | if ($is_confidential) { |
||
253 | $child['text'] = '<font color="gray"><i>'.$child['text'].'</i></font>'; |
||
254 | } |
||
255 | |||
256 | // Icon bestimmen |
||
257 | $child['icon'] = $obj->getIcon($row); |
||
258 | |||
259 | // Feststellen, ob es weitere Unter-OIDs gibt |
||
260 | if (!is_null($goto_path) && (count($goto_path) > 0) && ($goto_path[0] === $row['id'])) { |
||
261 | $child['children'] = self::tree_populate($row['id'], $goto_path); |
||
262 | $child['state'] = array("opened" => true); |
||
263 | } else { |
||
264 | $res2 = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = '".OIDplus::db()->real_escape_string($row['id'])."'"); |
||
265 | $child['children'] = OIDplus::db()->num_rows($res2) > 0; |
||
266 | } |
||
267 | |||
268 | $children[] = $child; |
||
269 | } |
||
270 | |||
271 | return $children; |
||
272 | } |
||
273 | } |
||
274 |