Subversion Repositories oidplus

Rev

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

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