Subversion Repositories oidplus

Rev

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