Subversion Repositories oidplus

Rev

Rev 1124 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1118 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
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
 
20
namespace ViaThinkSoft\OIDplus;
21
 
22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
26
class OIDplusPhp extends OIDplusObject {
1130 daniel-mar 27
        /**
28
         * @var string
29
         */
1118 daniel-mar 30
        private $php;
31
 
32
        /**
1130 daniel-mar 33
         * @param string $php
1118 daniel-mar 34
         */
1130 daniel-mar 35
        public function __construct(string $php) {
1118 daniel-mar 36
                // TODO: syntax checks
37
                $this->php = $php;
38
        }
39
 
40
        /**
41
         * @param string $node_id
42
         * @return OIDplusPhp|null
43
         */
44
        public static function parse(string $node_id)/*: ?OIDplusPhp*/ {
45
                @list($namespace, $php) = explode(':', $node_id, 2);
46
                if ($namespace !== self::ns()) return null;
47
                return new self($php);
48
        }
49
 
50
        /**
51
         * @return string
52
         */
53
        public static function objectTypeTitle(): string {
54
                return _L('PHP Namespaces');
55
        }
56
 
57
        /**
58
         * @return string
59
         */
60
        public static function objectTypeTitleShort(): string {
61
                return _L('Namespace');
62
        }
63
 
64
        /**
65
         * @return string
66
         */
67
        public static function ns(): string {
68
                return 'php';
69
        }
70
 
71
        /**
72
         * @return string
73
         */
74
        public static function root(): string {
75
                return self::ns().':';
76
        }
77
 
78
        /**
79
         * @return bool
80
         */
81
        public function isRoot(): bool {
82
                return $this->php == '';
83
        }
84
 
85
        /**
86
         * @param bool $with_ns
87
         * @return string
88
         */
89
        public function nodeId(bool $with_ns=true): string {
90
                return $with_ns ? self::root().$this->php : $this->php;
91
        }
92
 
93
        /**
94
         * @param string $str
95
         * @return string
96
         * @throws OIDplusException
97
         */
98
        public function addString(string $str): string {
99
                if ($this->isRoot()) {
100
                        $str = ltrim($str,'\\');
101
                        return self::root().$str;
102
                } else {
103
                        if (strpos($str,'\\') !== false) throw new OIDplusException(_L('Please only submit one arc.'));
104
                        return $this->nodeId() . '\\' . $str;
105
                }
106
        }
107
 
108
        /**
109
         * @param OIDplusObject $parent
110
         * @return string
111
         */
112
        public function crudShowId(OIDplusObject $parent): string {
113
                return $this->php;
114
        }
115
 
116
        /**
117
         * @return string
118
         * @throws OIDplusException
119
         */
120
        public function crudInsertPrefix(): string {
121
                return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
122
        }
123
 
124
        /**
125
         * @param OIDplusObject|null $parent
126
         * @return string
127
         */
128
        public function jsTreeNodeName(OIDplusObject $parent = null): string {
129
                if ($parent == null) return $this->objectTypeTitle();
130
                if ($parent->isRoot()) {
131
                        return substr($this->nodeId(), strlen($parent->nodeId()));
132
                } else {
133
                        return substr($this->nodeId(), strlen($parent->nodeId())+1);
134
                }
135
        }
136
 
137
        /**
138
         * @return string
139
         */
140
        public function defaultTitle(): string {
141
                return $this->php;
142
        }
143
 
144
        /**
145
         * @return bool
146
         */
147
        public function isLeafNode(): bool {
148
                return substr($this->php, -4) === '.php';
149
        }
150
 
151
        /**
152
         * @param string $title
153
         * @param string $content
154
         * @param string $icon
155
         * @return void
156
         * @throws OIDplusException
157
         */
158
        public function getContentPage(string &$title, string &$content, string &$icon) {
159
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
160
 
161
                if ($this->isRoot()) {
162
                        $title = OIDplusPhp::objectTypeTitle();
163
 
164
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
165
                        if ($res->any()) {
166
                                $content  = '<p>'._L('Please select a PHP Namespace in the tree view at the left to show its contents.').'</p>';
167
                        } else {
168
                                $content  = '<p>'._L('Currently, no PHP Namespace is registered in the system.').'</p>';
169
                        }
170
 
171
                        if (!$this->isLeafNode()) {
172
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
173
                                        $content .= '<h2>'._L('Manage root objects').'</h2>';
174
                                } else {
175
                                        $content .= '<h2>'._L('Available objects').'</h2>';
176
                                }
177
                                $content .= '%%CRUD%%';
178
                        }
179
                } else {
180
                        $title = $this->getTitle();
181
 
182
                        $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>';
183
 
184
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
185
 
186
                        if (OIDplus::baseConfig()->getValue('PLUGIN_PHP_TYPE_LINK_TO_WEBFAN', false)) {
187
                                $content .= '<p><strong><a webfan-php-class-link="'.$this->nodeId(false).'" href="https://webfan.de/install/?source='.urlencode($this->nodeId(false)).'" target="_blank">Soure Code</a></strong></p>';
188
                        }
189
 
190
                        if (!$this->isLeafNode()) {
191
                                if ($this->userHasWriteRights()) {
192
                                        $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
193
                                        $content .= '<p>'._L('Append ".php" at the end to mark a node as leaf node (i.e. Interface/Class rather than Namespace)').'</p>';
194
                                } else {
195
                                        $content .= '<h2>'._L('Subordinate objects').'</h2>';
196
                                }
197
                                $content .= '%%CRUD%%';
198
                        }
199
                }
200
        }
201
 
202
        /**
203
         * @return OIDplusPhp|null
204
         */
205
        public function one_up()/*: ?OIDplusPhp*/ {
206
                $oid = $this->php;
207
 
208
                $p = strrpos($oid, '\\');
209
                if ($p === false) return self::parse($oid);
210
                if ($p == 0) return self::parse('\\');
211
 
212
                $oid_up = substr($oid, 0, $p);
213
 
214
                return self::parse(self::ns().':'.$oid_up);
215
        }
216
 
217
        /**
1130 daniel-mar 218
         * @param OIDplusObject|string $to
1118 daniel-mar 219
         * @return int|null
220
         */
221
        public function distance($to) {
222
                if (!is_object($to)) $to = OIDplusObject::parse($to);
1121 daniel-mar 223
                if (!$to) return null;
1118 daniel-mar 224
                if (!($to instanceof $this)) return null;
225
 
226
                $a = $to->php;
227
                $b = $this->php;
228
 
229
                if (substr($a,0,1) == '\\') $a = substr($a,1);
230
                if (substr($b,0,1) == '\\') $b = substr($b,1);
231
 
232
                $ary = explode('\\', $a);
233
                $bry = explode('\\', $b);
234
 
235
                $min_len = min(count($ary), count($bry));
236
 
237
                for ($i=0; $i<$min_len; $i++) {
238
                        if ($ary[$i] != $bry[$i]) return null;
239
                }
240
 
241
                return count($ary) - count($bry);
242
        }
243
 
244
        /**
245
         * @return string
1130 daniel-mar 246
         * @throws OIDplusException
1118 daniel-mar 247
         */
248
        public function getDirectoryName(): string {
249
                if ($this->isRoot()) return $this->ns();
250
                if (OIDplus::baseConfig()->getValue('PLUGIN_PHP_TYPE_LINK_TO_WEBFAN', false)) {
251
                        return $this->ns().str_replace(['\\', "/"], [\DIRECTORY_SEPARATOR, \DIRECTORY_SEPARATOR], $this->nodeId(false));
252
                } else {
253
                        return $this->ns().'_'.md5($this->nodeId(false));
254
                }
255
        }
256
 
257
        /**
258
         * @param string $mode
259
         * @return string
260
         */
261
        public static function treeIconFilename(string $mode): string {
262
                return 'img/'.$mode.'_icon16.png';
263
        }
264
}