Subversion Repositories oidplus

Rev

Rev 1118 | 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 OIDplusJava extends OIDplusObject {
27
        private $java;
28
 
1116 daniel-mar 29
        /**
30
         * @param $java
31
         */
635 daniel-mar 32
        public function __construct($java) {
33
                // TODO: syntax checks
34
                $this->java = $java;
35
        }
36
 
1116 daniel-mar 37
        /**
38
         * @param string $node_id
39
         * @return OIDplusJava|null
40
         */
41
        public static function parse(string $node_id)/*: ?OIDplusJava*/ {
635 daniel-mar 42
                @list($namespace, $java) = explode(':', $node_id, 2);
1116 daniel-mar 43
                if ($namespace !== self::ns()) return null;
635 daniel-mar 44
                return new self($java);
45
        }
46
 
1116 daniel-mar 47
        /**
48
         * @return string
49
         */
50
        public static function objectTypeTitle(): string {
635 daniel-mar 51
                return _L('Java Package Names');
52
        }
53
 
1116 daniel-mar 54
        /**
55
         * @return string
56
         */
57
        public static function objectTypeTitleShort(): string {
635 daniel-mar 58
                return _L('Package');
59
        }
60
 
1116 daniel-mar 61
        /**
62
         * @return string
63
         */
64
        public static function ns(): string {
635 daniel-mar 65
                return 'java';
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->java == '';
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->java : $this->java;
635 daniel-mar 88
        }
89
 
1116 daniel-mar 90
        /**
91
         * @param string $str
92
         * @return string
93
         * @throws OIDplusException
94
         */
95
        public function addString(string $str): string {
635 daniel-mar 96
                if ($this->isRoot()) {
859 daniel-mar 97
                        return self::root().$str;
635 daniel-mar 98
                } else {
99
                        if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc.'));
100
                        return $this->nodeId() . '.' . $str;
101
                }
102
        }
103
 
1116 daniel-mar 104
        /**
105
         * @param OIDplusObject $parent
106
         * @return string
107
         */
108
        public function crudShowId(OIDplusObject $parent): string {
635 daniel-mar 109
                return $this->java;
110
        }
111
 
1116 daniel-mar 112
        /**
113
         * @return string
114
         * @throws OIDplusException
115
         */
116
        public function crudInsertPrefix(): string {
635 daniel-mar 117
                return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
118
        }
119
 
1116 daniel-mar 120
        /**
121
         * @param OIDplusObject|null $parent
122
         * @return string
123
         */
124
        public function jsTreeNodeName(OIDplusObject $parent = null): string {
635 daniel-mar 125
                if ($parent == null) return $this->objectTypeTitle();
1118 daniel-mar 126
                if ($parent->isRoot()) {
127
                        return substr($this->nodeId(), strlen($parent->nodeId()));
128
                } else {
129
                        return substr($this->nodeId(), strlen($parent->nodeId())+1);
130
                }
635 daniel-mar 131
        }
132
 
1116 daniel-mar 133
        /**
134
         * @return string
135
         */
136
        public function defaultTitle(): string {
635 daniel-mar 137
                return $this->java;
138
        }
139
 
1116 daniel-mar 140
        /**
141
         * @return bool
142
         */
143
        public function isLeafNode(): bool {
635 daniel-mar 144
                return false;
145
        }
146
 
1116 daniel-mar 147
        /**
148
         * @param string $title
149
         * @param string $content
150
         * @param string $icon
151
         * @return void
152
         * @throws OIDplusException
153
         */
154
        public function getContentPage(string &$title, string &$content, string &$icon) {
801 daniel-mar 155
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 156
 
157
                if ($this->isRoot()) {
158
                        $title = OIDplusJava::objectTypeTitle();
159
 
160
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
790 daniel-mar 161
                        if ($res->any()) {
962 daniel-mar 162
                                $content  = '<p>'._L('Please select a Java Package Name in the tree view at the left to show its contents.').'</p>';
635 daniel-mar 163
                        } else {
962 daniel-mar 164
                                $content  = '<p>'._L('Currently, no Java Package Name is registered in the system.').'</p>';
635 daniel-mar 165
                        }
166
 
167
                        if (!$this->isLeafNode()) {
168
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
169
                                        $content .= '<h2>'._L('Manage root objects').'</h2>';
170
                                } else {
171
                                        $content .= '<h2>'._L('Available objects').'</h2>';
172
                                }
173
                                $content .= '%%CRUD%%';
174
                        }
175
                } else {
176
                        $title = $this->getTitle();
177
 
178
                        $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>';
179
 
180
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
181
 
182
                        if (!$this->isLeafNode()) {
183
                                if ($this->userHasWriteRights()) {
928 daniel-mar 184
                                        $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
635 daniel-mar 185
                                } else {
928 daniel-mar 186
                                        $content .= '<h2>'._L('Subordinate objects').'</h2>';
635 daniel-mar 187
                                }
188
                                $content .= '%%CRUD%%';
189
                        }
190
                }
191
        }
192
 
1116 daniel-mar 193
        /**
194
         * @return OIDplusJava|null
195
         */
196
        public function one_up()/*: ?OIDplusJava*/ {
635 daniel-mar 197
                $oid = $this->java;
198
 
199
                $p = strrpos($oid, '.');
713 daniel-mar 200
                if ($p === false) return self::parse($oid);
201
                if ($p == 0) return self::parse('.');
635 daniel-mar 202
 
203
                $oid_up = substr($oid, 0, $p);
204
 
205
                return self::parse(self::ns().':'.$oid_up);
206
        }
207
 
1116 daniel-mar 208
        /**
209
         * @param $to
210
         * @return int|null
211
         */
635 daniel-mar 212
        public function distance($to) {
213
                if (!is_object($to)) $to = OIDplusObject::parse($to);
1121 daniel-mar 214
                if (!$to) return null;
1116 daniel-mar 215
                if (!($to instanceof $this)) return null;
635 daniel-mar 216
 
217
                $a = $to->java;
218
                $b = $this->java;
219
 
220
                if (substr($a,0,1) == '.') $a = substr($a,1);
221
                if (substr($b,0,1) == '.') $b = substr($b,1);
222
 
223
                $ary = explode('.', $a);
224
                $bry = explode('.', $b);
225
 
226
                $min_len = min(count($ary), count($bry));
227
 
228
                for ($i=0; $i<$min_len; $i++) {
1116 daniel-mar 229
                        if ($ary[$i] != $bry[$i]) return null;
635 daniel-mar 230
                }
231
 
232
                return count($ary) - count($bry);
233
        }
234
 
1116 daniel-mar 235
        /**
236
         * @return string
237
         */
238
        public function getDirectoryName(): string {
635 daniel-mar 239
                if ($this->isRoot()) return $this->ns();
240
                return $this->ns().'_'.md5($this->nodeId(false));
241
        }
800 daniel-mar 242
 
1116 daniel-mar 243
        /**
244
         * @param string $mode
245
         * @return string
246
         */
247
        public static function treeIconFilename(string $mode): string {
800 daniel-mar 248
                return 'img/'.$mode.'_icon16.png';
249
        }
713 daniel-mar 250
}