Rev 1086 | Rev 1121 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | 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; |
635 | 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 | |||
635 | daniel-mar | 26 | class OIDplusOther extends OIDplusObject { |
27 | private $other; |
||
28 | |||
1116 | daniel-mar | 29 | /** |
30 | * @param $other |
||
31 | */ |
||
635 | daniel-mar | 32 | public function __construct($other) { |
33 | // No syntax checks |
||
34 | $this->other = $other; |
||
35 | } |
||
36 | |||
1116 | daniel-mar | 37 | /** |
38 | * @param string $node_id |
||
39 | * @return OIDplusOther|null |
||
40 | */ |
||
41 | public static function parse(string $node_id)/*: ?OIDplusOther*/ { |
||
635 | daniel-mar | 42 | @list($namespace, $other) = explode(':', $node_id, 2); |
1116 | daniel-mar | 43 | if ($namespace !== self::ns()) return null; |
635 | daniel-mar | 44 | return new self($other); |
45 | } |
||
46 | |||
1116 | daniel-mar | 47 | /** |
48 | * @return string |
||
49 | */ |
||
50 | public static function objectTypeTitle(): string { |
||
635 | daniel-mar | 51 | return _L('Other objects'); |
52 | } |
||
53 | |||
1116 | daniel-mar | 54 | /** |
55 | * @return string |
||
56 | */ |
||
57 | public static function objectTypeTitleShort(): string { |
||
635 | daniel-mar | 58 | return _L('Object'); |
59 | } |
||
60 | |||
1116 | daniel-mar | 61 | /** |
62 | * @return string |
||
63 | */ |
||
64 | public static function ns(): string { |
||
635 | daniel-mar | 65 | return 'other'; |
66 | } |
||
67 | |||
1116 | daniel-mar | 68 | /** |
69 | * @return string |
||
70 | */ |
||
71 | public static function root(): string { |
||
860 | daniel-mar | 72 | return self::ns().':'; |
635 | daniel-mar | 73 | } |
74 | |||
1116 | daniel-mar | 75 | /** |
76 | * @return bool |
||
77 | */ |
||
78 | public function isRoot(): bool { |
||
635 | daniel-mar | 79 | return $this->other == ''; |
80 | } |
||
81 | |||
1116 | daniel-mar | 82 | /** |
83 | * @param bool $with_ns |
||
84 | * @return string |
||
85 | */ |
||
86 | public function nodeId(bool $with_ns=true): string { |
||
859 | daniel-mar | 87 | return $with_ns ? self::root().$this->other : $this->other; |
635 | daniel-mar | 88 | } |
89 | |||
1116 | daniel-mar | 90 | /** |
91 | * @param string $str |
||
92 | * @return string |
||
93 | */ |
||
94 | public function addString(string $str): string { |
||
635 | daniel-mar | 95 | if ($this->isRoot()) { |
858 | daniel-mar | 96 | return self::root() . $str; |
635 | daniel-mar | 97 | } else { |
98 | return $this->nodeId() . '\\' . $str; |
||
99 | } |
||
100 | } |
||
101 | |||
1116 | daniel-mar | 102 | /** |
103 | * @param OIDplusObject $parent |
||
104 | * @return string |
||
105 | */ |
||
106 | public function crudShowId(OIDplusObject $parent): string { |
||
635 | daniel-mar | 107 | if ($parent->isRoot()) { |
108 | return substr($this->nodeId(), strlen($parent->nodeId())); |
||
109 | } else { |
||
110 | return substr($this->nodeId(), strlen($parent->nodeId())+1); |
||
111 | } |
||
112 | } |
||
113 | |||
1116 | daniel-mar | 114 | /** |
115 | * @param OIDplusObject|null $parent |
||
116 | * @return string |
||
117 | */ |
||
118 | public function jsTreeNodeName(OIDplusObject $parent = null): string { |
||
635 | daniel-mar | 119 | if ($parent == null) return $this->objectTypeTitle(); |
120 | if ($parent->isRoot()) { |
||
121 | return substr($this->nodeId(), strlen($parent->nodeId())); |
||
122 | } else { |
||
123 | return substr($this->nodeId(), strlen($parent->nodeId())+1); |
||
124 | } |
||
125 | } |
||
126 | |||
1116 | daniel-mar | 127 | /** |
128 | * @return string |
||
129 | */ |
||
130 | public function defaultTitle(): string { |
||
635 | daniel-mar | 131 | $ary = explode('\\', $this->other); // TODO: but if an arc contains "\", this does not work. better read from db? |
132 | $ary = array_reverse($ary); |
||
133 | return $ary[0]; |
||
134 | } |
||
135 | |||
1116 | daniel-mar | 136 | /** |
137 | * @return bool |
||
138 | */ |
||
139 | public function isLeafNode(): bool { |
||
635 | daniel-mar | 140 | return false; |
141 | } |
||
142 | |||
1116 | daniel-mar | 143 | /** |
144 | * @param string $title |
||
145 | * @param string $content |
||
146 | * @param string $icon |
||
147 | * @return void |
||
148 | * @throws OIDplusException |
||
149 | */ |
||
150 | public function getContentPage(string &$title, string &$content, string &$icon) { |
||
801 | daniel-mar | 151 | $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 152 | |
153 | if ($this->isRoot()) { |
||
154 | $title = OIDplusOther::objectTypeTitle(); |
||
155 | |||
156 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
790 | daniel-mar | 157 | if ($res->any()) { |
962 | daniel-mar | 158 | $content = '<p>'._L('Please select an object in the tree view at the left to show its contents.').'</p>'; |
635 | daniel-mar | 159 | } else { |
962 | daniel-mar | 160 | $content = '<p>'._L('Currently, no misc. objects are registered in the system.').'</p>'; |
635 | daniel-mar | 161 | } |
162 | |||
163 | if (!$this->isLeafNode()) { |
||
164 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
165 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
||
166 | } else { |
||
167 | $content .= '<h2>'._L('Available objects').'</h2>'; |
||
168 | } |
||
169 | $content .= '%%CRUD%%'; |
||
170 | } |
||
171 | } else { |
||
172 | $title = $this->getTitle(); |
||
173 | |||
174 | $content = '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
||
175 | |||
176 | if (!$this->isLeafNode()) { |
||
177 | if ($this->userHasWriteRights()) { |
||
928 | daniel-mar | 178 | $content .= '<h2>'._L('Create or change subordinate objects').'</h2>'; |
635 | daniel-mar | 179 | } else { |
928 | daniel-mar | 180 | $content .= '<h2>'._L('Subordinate objects').'</h2>'; |
635 | daniel-mar | 181 | } |
182 | $content .= '%%CRUD%%'; |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
1116 | daniel-mar | 187 | /** |
188 | * @return OIDplusOther|null |
||
189 | */ |
||
190 | public function one_up()/*: ?OIDplusOther*/ { |
||
635 | daniel-mar | 191 | $oid = $this->other; |
192 | |||
193 | $p = strrpos($oid, '\\'); |
||
713 | daniel-mar | 194 | if ($p === false) return self::parse($oid); |
195 | if ($p == 0) return self::parse('\\'); |
||
635 | daniel-mar | 196 | |
197 | $oid_up = substr($oid, 0, $p); |
||
198 | |||
199 | return self::parse(self::ns().':'.$oid_up); |
||
200 | } |
||
201 | |||
1116 | daniel-mar | 202 | /** |
203 | * @param $to |
||
204 | * @return int|null |
||
205 | */ |
||
635 | daniel-mar | 206 | public function distance($to) { |
207 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
1116 | daniel-mar | 208 | if (!($to instanceof $this)) return null; |
635 | daniel-mar | 209 | |
210 | $a = $to->other; |
||
211 | $b = $this->other; |
||
212 | |||
213 | if (substr($a,0,1) == '\\') $a = substr($a,1); |
||
214 | if (substr($b,0,1) == '\\') $b = substr($b,1); |
||
215 | |||
216 | $ary = explode('\\', $a); |
||
217 | $bry = explode('\\', $b); |
||
218 | |||
219 | $min_len = min(count($ary), count($bry)); |
||
220 | |||
221 | for ($i=0; $i<$min_len; $i++) { |
||
1116 | daniel-mar | 222 | if ($ary[$i] != $bry[$i]) return null; |
635 | daniel-mar | 223 | } |
224 | |||
225 | return count($ary) - count($bry); |
||
226 | } |
||
227 | |||
1116 | daniel-mar | 228 | /** |
229 | * @return string |
||
230 | */ |
||
231 | public function getDirectoryName(): string { |
||
635 | daniel-mar | 232 | if ($this->isRoot()) return $this->ns(); |
233 | return $this->ns().'_'.md5($this->nodeId(false)); |
||
234 | } |
||
800 | daniel-mar | 235 | |
1116 | daniel-mar | 236 | /** |
237 | * @param string $mode |
||
238 | * @return string |
||
239 | */ |
||
240 | public static function treeIconFilename(string $mode): string { |
||
800 | daniel-mar | 241 | return 'img/'.$mode.'_icon16.png'; |
242 | } |
||
635 | daniel-mar | 243 | } |