Subversion Repositories oidplus

Rev

Rev 644 | Rev 753 | 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
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 jsTreeNodeName(OIDplusObject $parent = null) {
79
                if ($parent == null) return $this->objectTypeTitle();
80
                return $this->crudShowId($parent);
81
        }
82
 
83
        public function defaultTitle() {
84
                return $this->guid;
85
        }
86
 
87
        public function isLeafNode() {
88
                return uuid_valid($this->guid);
89
        }
90
 
91
        public function getContentPage(&$title, &$content, &$icon) {
644 daniel-mar 92
                $icon = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webPath(__DIR__,true).'icon_big.png' : '';
635 daniel-mar 93
 
94
                if ($this->isRoot()) {
95
                        $title = OIDplusGuid::objectTypeTitle();
96
 
97
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
98
                        if ($res->num_rows() > 0) {
99
                                $content  = _L('Please select a GUID in the tree view at the left to show its contents.');
100
                        } else {
101
                                $content  = _L('Currently, no GUID is registered in the system.');
102
                        }
103
 
104
                        if (!$this->isLeafNode()) {
105
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
106
                                        $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
107
                                } else {
108
                                        $content .= '<h2>'._L('Available objects / categories').'</h2>';
109
                                }
110
                                $content .= '%%CRUD%%';
111
                        }
112
                } else {
113
                        $title = $this->getTitle();
114
 
115
                        if ($this->isLeafNode()) {
116
                                ob_start();
117
                                uuid_info($this->guid);
118
                                $info = ob_get_contents();
119
                                ob_end_clean();
120
                                $info = preg_replace('@:\s*(.+)\n@ismU', ": <code>\\1</code><br>", $info);
121
 
122
                                $content = '<h2>'._L('Technical information').'</h2>' .
123
                                       '<p>'._L('UUID').': <code>' . uuid_canonize($this->guid) . '</code><br>' .
124
                                       ''._L('C++ notation').': <code>' . uuid_c_syntax($this->guid) . '</code><br>' .
125
                                       "$info";
126
                                //      "<a href=\"https://misc.daniel-marschall.de/tools/uuid_mac_decoder/interprete_uuid.php?uuid=".urlencode($this->guid)."\">More technical information</a></p>";
127
                        } else {
128
                                $content = '';
129
                        }
130
 
131
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
132
 
133
                        if (!$this->isLeafNode()) {
134
                                if ($this->userHasWriteRights()) {
135
                                        $content .= '<h2>'._L('Create or change subsequent objects / categories').'</h2>';
136
                                } else {
137
                                        $content .= '<h2>'._L('Subsequent objects / categories').'</h2>';
138
                                }
139
                                $content .= '%%CRUD%%';
140
                        }
141
                }
142
        }
143
 
144
        // TODO: It would be nice if category and leaf items could have different pictures.
145
        //       But the problem is, that the RA link should have a orange "GUID" icon, not a folder icon
146
        /*
147
        public function getIcon($row) {
148
                if (!$this->isLeafNode()) return null; // foldericon
149
                return parent::getIcon($row);
150
        }
151
        */
152
 
153
        public function one_up() {
154
                // A GUID is a GUID, there is no hierarchy
155
                return false;
156
        }
157
 
158
        public function distance($to) {
159
                // Distance between GUIDs is not possible
160
                return null;
161
        }
162
 
163
        public function getAltIds() {
164
                if ($this->isRoot()) return array();
165
                if (!$this->isLeafNode()) return array();
166
                $ids = parent::getAltIds();
167
                $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID'));
168
                return $ids;
169
        }
170
 
171
        public function getDirectoryName() {
172
                if ($this->isLeafNode()) {
173
                        // Leaf (UUID)
174
                        // Example output: "guid_adb0b042_5b57_11eb_b0d9_3c4a92df8582"
175
                        $str = $this->nodeId(false);
176
                        $str = str_replace('-', '_', $str);
177
                        $str = strtolower($str);
178
                        return $this->ns().'_'.$str;
179
                } else {
180
                        // Category
181
                        return parent::getDirectoryName();
182
                }
183
        }
707 daniel-mar 184
}