Subversion Repositories oidplus

Rev

Rev 644 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2021 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
if (!defined('INSIDE_OIDPLUS')) die();
21
 
22
class OIDplusGuid extends OIDplusObject {
23
        private $guid;
24
 
25
        public function __construct($guid) {
26
                if (uuid_valid($guid)) {
27
                        $this->guid = uuid_canonize($guid); // It is a real GUID (leaf node)
28
                } else {
29
                        $this->guid = $guid; // It is a category name
30
                }
31
        }
32
 
33
        public static function parse($node_id) {
34
                @list($namespace, $guid) = explode(':', $node_id, 2);
35
                if ($namespace !== 'guid') return false;
36
                return new self($guid);
37
        }
38
 
39
        public static function objectTypeTitle() {
40
                return _L('Globally Unique Identifier (GUID)');
41
        }
42
 
43
        public static function objectTypeTitleShort() {
44
                return _L('GUID');
45
        }
46
 
47
        public static function ns() {
48
                return 'guid';
49
        }
50
 
51
        public static function root() {
52
                return 'guid:';
53
        }
54
 
55
        public function isRoot() {
56
                return $this->guid == '';
57
        }
58
 
59
        public function nodeId($with_ns=true) {
60
                return $with_ns ? 'guid:'.$this->guid : $this->guid;
61
        }
62
 
63
        public function addString($str) {
64
                if (uuid_valid($str)) {
65
                        // real GUID
66
                        return 'guid:'.uuid_canonize($str);
67
                } else {
68
                        // just a category
69
                        return 'guid:'.$this->guid.'/'.$str;
70
                }
71
        }
72
 
73
        public function crudShowId(OIDplusObject $parent) {
74
                $tmp = explode('/',$this->guid);
75
                return end($tmp);
76
        }
77
 
78
        public function crudInsertPrefix() {
79
                return '';
80
        }
81
 
82
        public function jsTreeNodeName(OIDplusObject $parent = null) {
83
                if ($parent == null) return $this->objectTypeTitle();
84
                return $this->crudShowId($parent);
85
        }
86
 
87
        public function defaultTitle() {
88
                return $this->guid;
89
        }
90
 
91
        public function isLeafNode() {
92
                return uuid_valid($this->guid);
93
        }
94
 
95
        public function getContentPage(&$title, &$content, &$icon) {
96
                $icon = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webPath(__DIR__,true).'/icon_big.png' : '';
97
 
98
                if ($this->isRoot()) {
99
                        $title = OIDplusGuid::objectTypeTitle();
100
 
101
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
102
                        if ($res->num_rows() > 0) {
103
                                $content  = _L('Please select a GUID in the tree view at the left to show its contents.');
104
                        } else {
105
                                $content  = _L('Currently, no GUID is registered in the system.');
106
                        }
107
 
108
                        if (!$this->isLeafNode()) {
109
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
110
                                        $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
111
                                } else {
112
                                        $content .= '<h2>'._L('Available objects / categories').'</h2>';
113
                                }
114
                                $content .= '%%CRUD%%';
115
                        }
116
                } else {
117
                        $title = $this->getTitle();
118
 
119
                        if ($this->isLeafNode()) {
120
                                ob_start();
121
                                uuid_info($this->guid);
122
                                $info = ob_get_contents();
123
                                ob_end_clean();
124
                                $info = preg_replace('@:\s*(.+)\n@ismU', ": <code>\\1</code><br>", $info);
125
 
126
                                $content = '<h2>'._L('Technical information').'</h2>' .
127
                                       '<p>'._L('UUID').': <code>' . uuid_canonize($this->guid) . '</code><br>' .
128
                                       ''._L('C++ notation').': <code>' . uuid_c_syntax($this->guid) . '</code><br>' .
129
                                       "$info";
130
                                //      "<a href=\"https://misc.daniel-marschall.de/tools/uuid_mac_decoder/interprete_uuid.php?uuid=".urlencode($this->guid)."\">More technical information</a></p>";
131
                        } else {
132
                                $content = '';
133
                        }
134
 
135
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
136
 
137
                        if (!$this->isLeafNode()) {
138
                                if ($this->userHasWriteRights()) {
139
                                        $content .= '<h2>'._L('Create or change subsequent objects / categories').'</h2>';
140
                                } else {
141
                                        $content .= '<h2>'._L('Subsequent objects / categories').'</h2>';
142
                                }
143
                                $content .= '%%CRUD%%';
144
                        }
145
                }
146
        }
147
 
148
        // TODO: It would be nice if category and leaf items could have different pictures.
149
        //       But the problem is, that the RA link should have a orange "GUID" icon, not a folder icon
150
        /*
151
        public function getIcon($row) {
152
                if (!$this->isLeafNode()) return null; // foldericon
153
                return parent::getIcon($row);
154
        }
155
        */
156
 
157
        public function one_up() {
158
                // A GUID is a GUID, there is no hierarchy
159
                return false;
160
        }
161
 
162
        public function distance($to) {
163
                // Distance between GUIDs is not possible
164
                return null;
165
        }
166
 
167
        public function getAltIds() {
168
                if ($this->isRoot()) return array();
169
                if (!$this->isLeafNode()) return array();
170
                $ids = parent::getAltIds();
171
                $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID'));
172
                return $ids;
173
        }
174
 
175
        public function getDirectoryName() {
176
                if ($this->isLeafNode()) {
177
                        // Leaf (UUID)
178
                        // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582"
179
                        $str = $this->nodeId(false);
180
                        $str = str_replace('-', '_', $str);
181
                        $str = strtolower($str);
182
                        return $this->ns().'_'.$str;
183
                } else {
184
                        // Category
185
                        return parent::getDirectoryName();
186
                }
187
        }
188
}