Rev 1050 | Rev 1116 | 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 | |||
29 | public function __construct($other) { |
||
30 | // No syntax checks |
||
31 | $this->other = $other; |
||
32 | } |
||
33 | |||
34 | public static function parse($node_id) { |
||
35 | @list($namespace, $other) = explode(':', $node_id, 2); |
||
860 | daniel-mar | 36 | if ($namespace !== self::ns()) return false; |
635 | daniel-mar | 37 | return new self($other); |
38 | } |
||
39 | |||
40 | public static function objectTypeTitle() { |
||
41 | return _L('Other objects'); |
||
42 | } |
||
43 | |||
44 | public static function objectTypeTitleShort() { |
||
45 | return _L('Object'); |
||
46 | } |
||
47 | |||
48 | public static function ns() { |
||
49 | return 'other'; |
||
50 | } |
||
51 | |||
52 | public static function root() { |
||
860 | daniel-mar | 53 | return self::ns().':'; |
635 | daniel-mar | 54 | } |
55 | |||
56 | public function isRoot() { |
||
57 | return $this->other == ''; |
||
58 | } |
||
59 | |||
60 | public function nodeId($with_ns=true) { |
||
859 | daniel-mar | 61 | return $with_ns ? self::root().$this->other : $this->other; |
635 | daniel-mar | 62 | } |
63 | |||
64 | public function addString($str) { |
||
65 | if ($this->isRoot()) { |
||
858 | daniel-mar | 66 | return self::root() . $str; |
635 | daniel-mar | 67 | } else { |
68 | return $this->nodeId() . '\\' . $str; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | public function crudShowId(OIDplusObject $parent) { |
||
73 | if ($parent->isRoot()) { |
||
74 | return substr($this->nodeId(), strlen($parent->nodeId())); |
||
75 | } else { |
||
76 | return substr($this->nodeId(), strlen($parent->nodeId())+1); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
||
81 | if ($parent == null) return $this->objectTypeTitle(); |
||
82 | if ($parent->isRoot()) { |
||
83 | return substr($this->nodeId(), strlen($parent->nodeId())); |
||
84 | } else { |
||
85 | return substr($this->nodeId(), strlen($parent->nodeId())+1); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | public function defaultTitle() { |
||
90 | $ary = explode('\\', $this->other); // TODO: but if an arc contains "\", this does not work. better read from db? |
||
91 | $ary = array_reverse($ary); |
||
92 | return $ary[0]; |
||
93 | } |
||
94 | |||
95 | public function isLeafNode() { |
||
96 | return false; |
||
97 | } |
||
98 | |||
99 | public function getContentPage(&$title, &$content, &$icon) { |
||
801 | daniel-mar | 100 | $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 101 | |
102 | if ($this->isRoot()) { |
||
103 | $title = OIDplusOther::objectTypeTitle(); |
||
104 | |||
105 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
790 | daniel-mar | 106 | if ($res->any()) { |
962 | daniel-mar | 107 | $content = '<p>'._L('Please select an object in the tree view at the left to show its contents.').'</p>'; |
635 | daniel-mar | 108 | } else { |
962 | daniel-mar | 109 | $content = '<p>'._L('Currently, no misc. objects are registered in the system.').'</p>'; |
635 | daniel-mar | 110 | } |
111 | |||
112 | if (!$this->isLeafNode()) { |
||
113 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
114 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
||
115 | } else { |
||
116 | $content .= '<h2>'._L('Available objects').'</h2>'; |
||
117 | } |
||
118 | $content .= '%%CRUD%%'; |
||
119 | } |
||
120 | } else { |
||
121 | $title = $this->getTitle(); |
||
122 | |||
123 | $content = '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
||
124 | |||
125 | if (!$this->isLeafNode()) { |
||
126 | if ($this->userHasWriteRights()) { |
||
928 | daniel-mar | 127 | $content .= '<h2>'._L('Create or change subordinate objects').'</h2>'; |
635 | daniel-mar | 128 | } else { |
928 | daniel-mar | 129 | $content .= '<h2>'._L('Subordinate objects').'</h2>'; |
635 | daniel-mar | 130 | } |
131 | $content .= '%%CRUD%%'; |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | public function one_up() { |
||
137 | $oid = $this->other; |
||
138 | |||
139 | $p = strrpos($oid, '\\'); |
||
713 | daniel-mar | 140 | if ($p === false) return self::parse($oid); |
141 | if ($p == 0) return self::parse('\\'); |
||
635 | daniel-mar | 142 | |
143 | $oid_up = substr($oid, 0, $p); |
||
144 | |||
145 | return self::parse(self::ns().':'.$oid_up); |
||
146 | } |
||
147 | |||
148 | public function distance($to) { |
||
149 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
150 | if (!($to instanceof $this)) return false; |
||
151 | |||
152 | $a = $to->other; |
||
153 | $b = $this->other; |
||
154 | |||
155 | if (substr($a,0,1) == '\\') $a = substr($a,1); |
||
156 | if (substr($b,0,1) == '\\') $b = substr($b,1); |
||
157 | |||
158 | $ary = explode('\\', $a); |
||
159 | $bry = explode('\\', $b); |
||
160 | |||
161 | $min_len = min(count($ary), count($bry)); |
||
162 | |||
163 | for ($i=0; $i<$min_len; $i++) { |
||
164 | if ($ary[$i] != $bry[$i]) return false; |
||
165 | } |
||
166 | |||
167 | return count($ary) - count($bry); |
||
168 | } |
||
169 | |||
170 | public function getDirectoryName() { |
||
171 | if ($this->isRoot()) return $this->ns(); |
||
172 | return $this->ns().'_'.md5($this->nodeId(false)); |
||
173 | } |
||
800 | daniel-mar | 174 | |
805 | daniel-mar | 175 | public static function treeIconFilename($mode) { |
800 | daniel-mar | 176 | return 'img/'.$mode.'_icon16.png'; |
177 | } |
||
635 | daniel-mar | 178 | } |