Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1121 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1086 Rev 1116
Line 24... Line 24...
24
// phpcs:enable PSR1.Files.SideEffects
24
// phpcs:enable PSR1.Files.SideEffects
25
 
25
 
26
class OIDplusOther extends OIDplusObject {
26
class OIDplusOther extends OIDplusObject {
27
        private $other;
27
        private $other;
28
 
28
 
-
 
29
        /**
-
 
30
         * @param $other
-
 
31
         */
29
        public function __construct($other) {
32
        public function __construct($other) {
30
                // No syntax checks
33
                // No syntax checks
31
                $this->other = $other;
34
                $this->other = $other;
32
        }
35
        }
33
 
36
 
-
 
37
        /**
-
 
38
         * @param string $node_id
-
 
39
         * @return OIDplusOther|null
-
 
40
         */
34
        public static function parse($node_id) {
41
        public static function parse(string $node_id)/*: ?OIDplusOther*/ {
35
                @list($namespace, $other) = explode(':', $node_id, 2);
42
                @list($namespace, $other) = explode(':', $node_id, 2);
36
                if ($namespace !== self::ns()) return false;
43
                if ($namespace !== self::ns()) return null;
37
                return new self($other);
44
                return new self($other);
38
        }
45
        }
39
 
46
 
-
 
47
        /**
-
 
48
         * @return string
-
 
49
         */
40
        public static function objectTypeTitle() {
50
        public static function objectTypeTitle(): string {
41
                return _L('Other objects');
51
                return _L('Other objects');
42
        }
52
        }
43
 
53
 
-
 
54
        /**
-
 
55
         * @return string
-
 
56
         */
44
        public static function objectTypeTitleShort() {
57
        public static function objectTypeTitleShort(): string {
45
                return _L('Object');
58
                return _L('Object');
46
        }
59
        }
47
 
60
 
-
 
61
        /**
-
 
62
         * @return string
-
 
63
         */
48
        public static function ns() {
64
        public static function ns(): string {
49
                return 'other';
65
                return 'other';
50
        }
66
        }
51
 
67
 
-
 
68
        /**
-
 
69
         * @return string
-
 
70
         */
52
        public static function root() {
71
        public static function root(): string {
53
                return self::ns().':';
72
                return self::ns().':';
54
        }
73
        }
55
 
74
 
-
 
75
        /**
-
 
76
         * @return bool
-
 
77
         */
56
        public function isRoot() {
78
        public function isRoot(): bool {
57
                return $this->other == '';
79
                return $this->other == '';
58
        }
80
        }
59
 
81
 
-
 
82
        /**
-
 
83
         * @param bool $with_ns
-
 
84
         * @return string
-
 
85
         */
60
        public function nodeId($with_ns=true) {
86
        public function nodeId(bool $with_ns=true): string {
61
                return $with_ns ? self::root().$this->other : $this->other;
87
                return $with_ns ? self::root().$this->other : $this->other;
62
        }
88
        }
63
 
89
 
-
 
90
        /**
-
 
91
         * @param string $str
-
 
92
         * @return string
-
 
93
         */
64
        public function addString($str) {
94
        public function addString(string $str): string {
65
                if ($this->isRoot()) {
95
                if ($this->isRoot()) {
66
                        return self::root() . $str;
96
                        return self::root() . $str;
67
                } else {
97
                } else {
68
                        return $this->nodeId() . '\\' . $str;
98
                        return $this->nodeId() . '\\' . $str;
69
                }
99
                }
70
        }
100
        }
71
 
101
 
-
 
102
        /**
-
 
103
         * @param OIDplusObject $parent
-
 
104
         * @return string
-
 
105
         */
72
        public function crudShowId(OIDplusObject $parent) {
106
        public function crudShowId(OIDplusObject $parent): string {
73
                if ($parent->isRoot()) {
107
                if ($parent->isRoot()) {
74
                        return substr($this->nodeId(), strlen($parent->nodeId()));
108
                        return substr($this->nodeId(), strlen($parent->nodeId()));
75
                } else {
109
                } else {
76
                        return substr($this->nodeId(), strlen($parent->nodeId())+1);
110
                        return substr($this->nodeId(), strlen($parent->nodeId())+1);
77
                }
111
                }
78
        }
112
        }
79
 
113
 
-
 
114
        /**
-
 
115
         * @param OIDplusObject|null $parent
-
 
116
         * @return string
-
 
117
         */
80
        public function jsTreeNodeName(OIDplusObject $parent = null) {
118
        public function jsTreeNodeName(OIDplusObject $parent = null): string {
81
                if ($parent == null) return $this->objectTypeTitle();
119
                if ($parent == null) return $this->objectTypeTitle();
82
                if ($parent->isRoot()) {
120
                if ($parent->isRoot()) {
83
                        return substr($this->nodeId(), strlen($parent->nodeId()));
121
                        return substr($this->nodeId(), strlen($parent->nodeId()));
84
                } else {
122
                } else {
85
                        return substr($this->nodeId(), strlen($parent->nodeId())+1);
123
                        return substr($this->nodeId(), strlen($parent->nodeId())+1);
86
                }
124
                }
87
        }
125
        }
88
 
126
 
-
 
127
        /**
-
 
128
         * @return string
-
 
129
         */
89
        public function defaultTitle() {
130
        public function defaultTitle(): string {
90
                $ary = explode('\\', $this->other); // TODO: but if an arc contains "\", this does not work. better read from db?
131
                $ary = explode('\\', $this->other); // TODO: but if an arc contains "\", this does not work. better read from db?
91
                $ary = array_reverse($ary);
132
                $ary = array_reverse($ary);
92
                return $ary[0];
133
                return $ary[0];
93
        }
134
        }
94
 
135
 
-
 
136
        /**
-
 
137
         * @return bool
-
 
138
         */
95
        public function isLeafNode() {
139
        public function isLeafNode(): bool {
96
                return false;
140
                return false;
97
        }
141
        }
98
 
142
 
-
 
143
        /**
-
 
144
         * @param string $title
-
 
145
         * @param string $content
-
 
146
         * @param string $icon
-
 
147
         * @return void
-
 
148
         * @throws OIDplusException
-
 
149
         */
99
        public function getContentPage(&$title, &$content, &$icon) {
150
        public function getContentPage(string &$title, string &$content, string &$icon) {
100
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
151
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
101
 
152
 
102
                if ($this->isRoot()) {
153
                if ($this->isRoot()) {
103
                        $title = OIDplusOther::objectTypeTitle();
154
                        $title = OIDplusOther::objectTypeTitle();
104
 
155
 
Line 131... Line 182...
131
                                $content .= '%%CRUD%%';
182
                                $content .= '%%CRUD%%';
132
                        }
183
                        }
133
                }
184
                }
134
        }
185
        }
135
 
186
 
-
 
187
        /**
-
 
188
         * @return OIDplusOther|null
-
 
189
         */
136
        public function one_up() {
190
        public function one_up()/*: ?OIDplusOther*/ {
137
                $oid = $this->other;
191
                $oid = $this->other;
138
 
192
 
139
                $p = strrpos($oid, '\\');
193
                $p = strrpos($oid, '\\');
140
                if ($p === false) return self::parse($oid);
194
                if ($p === false) return self::parse($oid);
141
                if ($p == 0) return self::parse('\\');
195
                if ($p == 0) return self::parse('\\');
Line 143... Line 197...
143
                $oid_up = substr($oid, 0, $p);
197
                $oid_up = substr($oid, 0, $p);
144
 
198
 
145
                return self::parse(self::ns().':'.$oid_up);
199
                return self::parse(self::ns().':'.$oid_up);
146
        }
200
        }
147
 
201
 
-
 
202
        /**
-
 
203
         * @param $to
-
 
204
         * @return int|null
-
 
205
         */
148
        public function distance($to) {
206
        public function distance($to) {
149
                if (!is_object($to)) $to = OIDplusObject::parse($to);
207
                if (!is_object($to)) $to = OIDplusObject::parse($to);
150
                if (!($to instanceof $this)) return false;
208
                if (!($to instanceof $this)) return null;
151
 
209
 
152
                $a = $to->other;
210
                $a = $to->other;
153
                $b = $this->other;
211
                $b = $this->other;
154
 
212
 
155
                if (substr($a,0,1) == '\\') $a = substr($a,1);
213
                if (substr($a,0,1) == '\\') $a = substr($a,1);
Line 159... Line 217...
159
                $bry = explode('\\', $b);
217
                $bry = explode('\\', $b);
160
 
218
 
161
                $min_len = min(count($ary), count($bry));
219
                $min_len = min(count($ary), count($bry));
162
 
220
 
163
                for ($i=0; $i<$min_len; $i++) {
221
                for ($i=0; $i<$min_len; $i++) {
164
                        if ($ary[$i] != $bry[$i]) return false;
222
                        if ($ary[$i] != $bry[$i]) return null;
165
                }
223
                }
166
 
224
 
167
                return count($ary) - count($bry);
225
                return count($ary) - count($bry);
168
        }
226
        }
169
 
227
 
-
 
228
        /**
-
 
229
         * @return string
-
 
230
         */
170
        public function getDirectoryName() {
231
        public function getDirectoryName(): string {
171
                if ($this->isRoot()) return $this->ns();
232
                if ($this->isRoot()) return $this->ns();
172
                return $this->ns().'_'.md5($this->nodeId(false));
233
                return $this->ns().'_'.md5($this->nodeId(false));
173
        }
234
        }
174
 
235
 
-
 
236
        /**
-
 
237
         * @param string $mode
-
 
238
         * @return string
-
 
239
         */
175
        public static function treeIconFilename($mode) {
240
        public static function treeIconFilename(string $mode): string {
176
                return 'img/'.$mode.'_icon16.png';
241
                return 'img/'.$mode.'_icon16.png';
177
        }
242
        }
178
}
243
}