Subversion Repositories oidplus

Rev

Rev 1148 | Rev 1186 | 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
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
         */
33
        public static 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('&nbsp;', $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.'&amp;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
         */
69
        public static 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 {
106 daniel-mar 82
                        $json = self::tree_populate($req_id);
2 daniel-mar 83
                }
84
 
1116 daniel-mar 85
                if (is_array($json)) self::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
         */
94
        protected static 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'])) {
1116 daniel-mar 107
                                if (is_array($item['children'])) self::addHrefIfRequired($item['children']);
541 daniel-mar 108
                        }
109
                }
110
        }
111
 
1121 daniel-mar 112
        /**
1130 daniel-mar 113
         * @param string $parent
114
         * @param array|true|null $goto_path
1121 daniel-mar 115
         * @return array
116
         * @throws OIDplusConfigInitializationException
117
         * @throws OIDplusException
118
         */
1130 daniel-mar 119
        public static function tree_populate(string $parent, $goto_path=null): array {
2 daniel-mar 120
                $children = array();
121
 
122
                $parentObj = OIDplusObject::parse($parent);
123
 
124
                @list($namespace, $oid) = explode(':', $parent, 2);
499 daniel-mar 125
                if ($namespace == 'oid') $oid = substr($oid, 1); // Remove leading dot
2 daniel-mar 126
 
281 daniel-mar 127
                if (is_array($goto_path)) array_shift($goto_path);
331 daniel-mar 128
 
2 daniel-mar 129
                $confidential_oids = array();
130
 
443 daniel-mar 131
                $res = OIDplus::db()->query("select id from ###objects where confidential = ?", array(true));
236 daniel-mar 132
                while ($row = $res->fetch_array()) {
2 daniel-mar 133
                        $confidential_oids[] = $row['id'];
134
                }
135
 
1148 daniel-mar 136
                $res = OIDplus::db()->query("select * from ###objects where parent = ?", array($parent));
1156 daniel-mar 137
                $res->naturalSortByField('id');
236 daniel-mar 138
                while ($row = $res->fetch_array()) {
2 daniel-mar 139
                        $obj = OIDplusObject::parse($row['id']);
1121 daniel-mar 140
                        if (!$obj) continue; // e.g. object-type plugin disabled
2 daniel-mar 141
 
142
                        if (!$obj->userHasReadRights()) continue;
143
 
144
                        $child = array();
145
                        $child['id'] = $row['id'];
146
 
499 daniel-mar 147
                        // Determine display name (relative OID)
1121 daniel-mar 148
                        $child['text'] = $parentObj ? $obj->jsTreeNodeName($parentObj) : '';
104 daniel-mar 149
                        $child['text'] .= empty($row['title']) ? /*' -- <i>'.htmlentities('Title missing').'</i>'*/ '' : ' -- <b>' . htmlentities($row['title']) . '</b>';
2 daniel-mar 150
 
151
                        $is_confidential = false;
152
                        foreach ($confidential_oids as $test) {
153
                                $is_confidential |= ($row['id'] === $test) || (strpos($row['id'],$test.'.') === 0);
154
                        }
155
                        if ($is_confidential) {
156
                                $child['text'] = '<font color="gray"><i>'.$child['text'].'</i></font>';
157
                        }
158
 
499 daniel-mar 159
                        // Determine icon
2 daniel-mar 160
                        $child['icon'] = $obj->getIcon($row);
161
 
499 daniel-mar 162
                        // Check if there are more sub OIDs
281 daniel-mar 163
                        if ($goto_path === true) {
2 daniel-mar 164
                                $child['children'] = self::tree_populate($row['id'], $goto_path);
165
                                $child['state'] = array("opened" => true);
281 daniel-mar 166
                        } else if (!is_null($goto_path) && (count($goto_path) > 0) && ($goto_path[0] === $row['id'])) {
167
                                $child['children'] = self::tree_populate($row['id'], $goto_path);
168
                                $child['state'] = array("opened" => true);
2 daniel-mar 169
                        } else {
150 daniel-mar 170
                                $obj_children = $obj->getChildren();
145 daniel-mar 171
 
172
                                // Variant 1: Fast, but does not check for hidden OIDs
150 daniel-mar 173
                                //$child_count = count($obj_children);
145 daniel-mar 174
 
175
                                // variant 2
176
                                $child_count = 0;
150 daniel-mar 177
                                foreach ($obj_children as $obj_test) {
145 daniel-mar 178
                                        if (!$obj_test->userHasReadRights()) continue;
179
                                        $child_count++;
180
                                }
181
 
182
                                $child['children'] = $child_count > 0;
2 daniel-mar 183
                        }
184
 
185
                        $children[] = $child;
186
                }
187
 
188
                return $children;
189
        }
366 daniel-mar 190
}