Rev 1454 | 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 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 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 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
511 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
730 | daniel-mar | 26 | class OIDplusMenuUtils extends OIDplusBaseClass { |
2 | daniel-mar | 27 | |
1116 | daniel-mar | 28 | /** |
29 | * @return string |
||
30 | * @throws OIDplusConfigInitializationException |
||
31 | * @throws OIDplusException |
||
32 | */ |
||
1186 | daniel-mar | 33 | public function nonjs_menu(): string { |
2 | daniel-mar | 34 | $json = array(); |
35 | |||
1130 | daniel-mar | 36 | $static_node_id = $_REQUEST['goto'] ?? 'oidplus:system'; |
106 | daniel-mar | 37 | |
281 | daniel-mar | 38 | foreach (OIDplus::getPagePlugins() as $plugin) { |
331 | daniel-mar | 39 | // Note: The system (OIDplusMenuUtils) does only show the menu of |
40 | // publicPage plugins. Menu entries for RAs and Admins are |
||
41 | // handled by the tree() function of the plugin publicPages/090_login |
||
281 | daniel-mar | 42 | if (is_subclass_of($plugin, OIDplusPagePluginPublic::class)) { |
43 | $plugin->tree($json, null, true, $static_node_id); |
||
44 | } |
||
61 | daniel-mar | 45 | } |
2 | daniel-mar | 46 | |
1066 | daniel-mar | 47 | $out = ''; |
2 | daniel-mar | 48 | foreach ($json as $x) { |
1066 | daniel-mar | 49 | if ($static_node_id == $x['id']) $out .= '<b>'; |
50 | if (isset($x['indent'])) $out .= str_repeat(' ', $x['indent']*5); |
||
360 | daniel-mar | 51 | $cur_lang = OIDplus::getCurrentLang(); |
1041 | daniel-mar | 52 | if ($cur_lang != OIDplus::getDefaultLang()) { |
1066 | daniel-mar | 53 | $out .= '<a href="?lang='.$cur_lang.'&goto='.urlencode($x['id']).'">'; |
360 | daniel-mar | 54 | } else { |
1066 | daniel-mar | 55 | $out .= '<a href="?goto='.urlencode($x['id']).'">'; |
360 | daniel-mar | 56 | } |
1066 | daniel-mar | 57 | if (!empty($x['icon'])) $out .= '<img src="'.$x['icon'].'" alt=""> '; |
58 | $out .= htmlentities($x['id']).' | '.htmlentities($x['text']).'</a><br>'; |
||
59 | if ($static_node_id == $x['id']) $out .= '</b>'; |
||
2 | daniel-mar | 60 | } |
1066 | daniel-mar | 61 | return $out; |
2 | daniel-mar | 62 | } |
63 | |||
1116 | daniel-mar | 64 | /** |
65 | * @param string $req_id comes from jsTree via AJAX |
||
66 | * @param string $req_goto comes from the user (GET argument) |
||
67 | * @return string[] |
||
68 | */ |
||
1186 | daniel-mar | 69 | public function json_tree(string $req_id, string $req_goto): array { |
106 | daniel-mar | 70 | $json = array(); |
2 | daniel-mar | 71 | |
1116 | daniel-mar | 72 | if ($req_id === '#') { |
281 | daniel-mar | 73 | foreach (OIDplus::getPagePlugins() as $plugin) { |
331 | daniel-mar | 74 | // Note: The system (OIDplusMenuUtils) does only show the menu of |
75 | // publicPage plugins. Menu entries for RAs and Admins are |
||
76 | // handled by the tree() function of the plugin publicPages/090_login |
||
281 | daniel-mar | 77 | if (is_subclass_of($plugin, OIDplusPagePluginPublic::class)) { |
78 | $plugin->tree($json, null, false, $req_goto); |
||
79 | } |
||
61 | daniel-mar | 80 | } |
2 | daniel-mar | 81 | } else { |
1186 | daniel-mar | 82 | $json = $this->tree_populate($req_id); |
2 | daniel-mar | 83 | } |
84 | |||
1186 | daniel-mar | 85 | if (is_array($json)) $this->addHrefIfRequired($json); |
541 | daniel-mar | 86 | |
328 | daniel-mar | 87 | return $json; |
2 | daniel-mar | 88 | } |
89 | |||
1116 | daniel-mar | 90 | /** |
91 | * @param array $json |
||
92 | * @return void |
||
93 | */ |
||
1186 | daniel-mar | 94 | protected function addHrefIfRequired(array &$json) { |
541 | daniel-mar | 95 | foreach ($json as &$item) { |
542 | daniel-mar | 96 | if (isset($item['id'])) { |
97 | if (!isset($item['conditionalselect']) || ($item['conditionalselect'] != 'false')) { |
||
98 | if (!isset($item['a_attr'])) { |
||
99 | $item['a_attr'] = array("href" => "?goto=".urlencode($item['id'])); |
||
100 | } else if (!isset($item['a_attr']['href'])) { |
||
101 | $item['a_attr']['href'] = "?goto=".urlencode($item['id']); |
||
102 | } |
||
103 | } |
||
541 | daniel-mar | 104 | } |
542 | daniel-mar | 105 | |
541 | daniel-mar | 106 | if (isset($item['children'])) { |
1186 | daniel-mar | 107 | if (is_array($item['children'])) $this->addHrefIfRequired($item['children']); |
541 | daniel-mar | 108 | } |
109 | } |
||
1316 | daniel-mar | 110 | unset($item); |
541 | daniel-mar | 111 | } |
112 | |||
1121 | daniel-mar | 113 | /** |
1130 | daniel-mar | 114 | * @param string $parent |
115 | * @param array|true|null $goto_path |
||
1121 | daniel-mar | 116 | * @return array |
117 | * @throws OIDplusConfigInitializationException |
||
118 | * @throws OIDplusException |
||
119 | */ |
||
1186 | daniel-mar | 120 | public function tree_populate(string $parent, $goto_path=null): array { |
2 | daniel-mar | 121 | $children = array(); |
122 | |||
123 | $parentObj = OIDplusObject::parse($parent); |
||
124 | |||
281 | daniel-mar | 125 | if (is_array($goto_path)) array_shift($goto_path); |
331 | daniel-mar | 126 | |
1148 | daniel-mar | 127 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array($parent)); |
1156 | daniel-mar | 128 | $res->naturalSortByField('id'); |
1445 | daniel-mar | 129 | $max_ent = 0; |
236 | daniel-mar | 130 | while ($row = $res->fetch_array()) { |
1445 | daniel-mar | 131 | $max_ent++; |
1454 | daniel-mar | 132 | if ($max_ent > 1000) { // TODO: we need to find a solution for this!!! |
133 | // Note: We cannot use id=oidplus:system, otherwise the lazy-load-tree breaks |
||
1455 | daniel-mar | 134 | $children[] = array('id' => '', 'icon' => '', 'text' => _L('List truncated due to too many subordinate elements'), 'indent' => 0); |
1454 | daniel-mar | 135 | break; |
136 | } |
||
137 | |||
2 | daniel-mar | 138 | $obj = OIDplusObject::parse($row['id']); |
1121 | daniel-mar | 139 | if (!$obj) continue; // e.g. object-type plugin disabled |
2 | daniel-mar | 140 | |
141 | if (!$obj->userHasReadRights()) continue; |
||
142 | |||
143 | $child = array(); |
||
144 | $child['id'] = $row['id']; |
||
145 | |||
499 | daniel-mar | 146 | // Determine display name (relative OID) |
1121 | daniel-mar | 147 | $child['text'] = $parentObj ? $obj->jsTreeNodeName($parentObj) : ''; |
104 | daniel-mar | 148 | $child['text'] .= empty($row['title']) ? /*' -- <i>'.htmlentities('Title missing').'</i>'*/ '' : ' -- <b>' . htmlentities($row['title']) . '</b>'; |
2 | daniel-mar | 149 | |
1313 | daniel-mar | 150 | // Check if node is confidential, or if one of its parent was confidential |
151 | $is_confidential = $obj->isConfidential(); |
||
2 | daniel-mar | 152 | if ($is_confidential) { |
153 | $child['text'] = '<font color="gray"><i>'.$child['text'].'</i></font>'; |
||
154 | } |
||
155 | |||
499 | daniel-mar | 156 | // Determine icon |
2 | daniel-mar | 157 | $child['icon'] = $obj->getIcon($row); |
158 | |||
499 | daniel-mar | 159 | // Check if there are more sub OIDs |
281 | daniel-mar | 160 | if ($goto_path === true) { |
1186 | daniel-mar | 161 | $child['children'] = $this->tree_populate($row['id'], $goto_path); |
2 | daniel-mar | 162 | $child['state'] = array("opened" => true); |
281 | daniel-mar | 163 | } else if (!is_null($goto_path) && (count($goto_path) > 0) && ($goto_path[0] === $row['id'])) { |
1186 | daniel-mar | 164 | $child['children'] = $this->tree_populate($row['id'], $goto_path); |
281 | daniel-mar | 165 | $child['state'] = array("opened" => true); |
2 | daniel-mar | 166 | } else { |
145 | daniel-mar | 167 | |
168 | // Variant 1: Fast, but does not check for hidden OIDs |
||
1454 | daniel-mar | 169 | /* |
170 | $obj_children = $obj->getChildren(); |
||
171 | $child['children'] = count($obj_children) > 0; |
||
172 | */ |
||
145 | daniel-mar | 173 | |
174 | // variant 2 |
||
1454 | daniel-mar | 175 | /* |
176 | $obj_children = $obj->getChildren(); |
||
177 | $child['children'] = false; |
||
150 | daniel-mar | 178 | foreach ($obj_children as $obj_test) { |
145 | daniel-mar | 179 | if (!$obj_test->userHasReadRights()) continue; |
1454 | daniel-mar | 180 | $child['children'] = true; |
181 | break; |
||
145 | daniel-mar | 182 | } |
1454 | daniel-mar | 183 | */ |
145 | daniel-mar | 184 | |
1454 | daniel-mar | 185 | // variant 3: A bit faster than variant 2 |
186 | $child['children'] = false; |
||
187 | $res2 = OIDplus::db()->query("select id from ###objects where parent = ?", array($obj->nodeId())); |
||
188 | while ($row2 = $res2->fetch_array()) { |
||
189 | $obj_test = OIDplusObject::parse($row2['id']); |
||
190 | if (!$obj_test->userHasReadRights()) continue; |
||
191 | $child['children'] = true; |
||
192 | break; |
||
193 | } |
||
194 | |||
2 | daniel-mar | 195 | } |
196 | |||
197 | $children[] = $child; |
||
198 | } |
||
199 | |||
200 | return $children; |
||
201 | } |
||
366 | daniel-mar | 202 | } |