Subversion Repositories oidplus

Rev

Rev 112 | 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
 
2 daniel-mar 22
class OIDplusOther extends OIDplusObject {
23
        private $other;
24
 
25
        public function __construct($other) {
16 daniel-mar 26
                // No syntax checks
2 daniel-mar 27
                $this->other = $other;
28
        }
29
 
30
        public static function parse($node_id) {
31
                @list($namespace, $other) = explode(':', $node_id, 2);
32
                if ($namespace !== 'other') return false;
33
                return new self($other);
34
        }
35
 
36
        public static function objectTypeTitle() {
37
                return "Other objects";
38
        }
39
 
40
        public static function objectTypeTitleShort() {
41
                return "Object";
42
        }
43
 
44
        public static function ns() {
45
                return 'other';
46
        }
47
 
48
        public static function root() {
49
                return 'other:';
50
        }
51
 
52
        public function isRoot() {
53
                return $this->other == '';
54
        }
55
 
56
        public function nodeId() {
57
                return 'other:'.$this->other;
58
        }
59
 
60
        public function addString($str) {
61
                if ($this->isRoot()) {
62
                        return 'other:'.$str;
63
                } else {
64
                        return $this->nodeId() . '\\' . $str;
65
                }
66
        }
67
 
68
        public function crudShowId(OIDplusObject $parent) {
69
                if ($parent->isRoot()) {
70
                        return substr($this->nodeId(), strlen($parent->nodeId()));
71
                } else {
72
                        return substr($this->nodeId(), strlen($parent->nodeId())+1);
73
                }
74
        }
75
 
76
        public function crudInsertPrefix() {
77
                return '';
78
        }
79
 
80
        public function jsTreeNodeName(OIDplusObject $parent = null) {
81
                if ($parent == null) return $this->objectTypeTitle();
82
                if ($parent->isRoot()) {
83
                        return substr($this->nodeId(), strlen($parent->nodeId()));
84
                } else {
85
                        return substr($this->nodeId(), strlen($parent->nodeId())+1);
86
                }
87
        }
88
 
89
        public function defaultTitle() {
90
                $ary = explode('\\', $this->other); // TODO: aber wenn ein arc ein "\" enthält, geht es nicht. besser von db ablesen?
91
                $ary = array_reverse($ary);
92
                return $ary[0];
93
        }
94
 
16 daniel-mar 95
        public function isLeafNode() {
96
                return false;
97
        }
98
 
34 daniel-mar 99
        public function getContentPage(&$title, &$content, &$icon) {
55 daniel-mar 100
                $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : '';
101
 
2 daniel-mar 102
                if ($this->isRoot()) {
103
                        $title = OIDplusOther::objectTypeTitle();
104
 
150 daniel-mar 105
                        $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = ?", array(self::root()));
2 daniel-mar 106
                        if (OIDplus::db()->num_rows($res) > 0) {
107
                                $content  = 'Please select an object in the tree view at the left to show its contents.';
108
                        } else {
109
                                $content  = 'Currently, no misc objects are registered in the system.';
110
                        }
111
 
16 daniel-mar 112
                        if (!$this->isLeafNode()) {
113
                                if (OIDplus::authUtils()::isAdminLoggedIn()) {
114
                                        $content .= '<h2>Manage root objects</h2>';
115
                                } else {
116
                                        $content .= '<h2>Available objects</h2>';
117
                                }
118
                                $content .= '%%CRUD%%';
2 daniel-mar 119
                        }
120
                } else {
11 daniel-mar 121
                        $content = '<h2>Description</h2>%%DESC%%'; // TODO: add more meta information about the object type
2 daniel-mar 122
 
16 daniel-mar 123
                        if (!$this->isLeafNode()) {
124
                                if ($this->userHasWriteRights()) {
125
                                        $content .= '<h2>Create or change subsequent objects</h2>';
126
                                } else {
127
                                        $content .= '<h2>Subsequent objects</h2>';
128
                                }
129
                                $content .= '%%CRUD%%';
2 daniel-mar 130
                        }
131
                }
132
        }
20 daniel-mar 133
 
134
        public function one_up() {
38 daniel-mar 135
                $oid = $this->other;
136
 
137
                $p = strrpos($oid, '\\');
138
                if ($p === false) return $oid;
139
                if ($p == 0) return '\\';
140
 
141
                $oid_up = substr($oid, 0, $p);
142
 
143
                return self::parse(self::ns().':'.$oid_up);
20 daniel-mar 144
        }
145
 
146
        public function distance($to) {
38 daniel-mar 147
                if (!is_object($to)) $to = OIDplusObject::parse($to);
148
                if (!($to instanceof $this)) return false;
149
 
150
                $a = $to->other;
151
                $b = $this->other;
152
 
153
                if (substr($a,0,1) == '\\') $a = substr($a,1);
154
                if (substr($b,0,1) == '\\') $b = substr($b,1);
155
 
156
                $ary = explode('\\', $a);
157
                $bry = explode('\\', $b);
158
 
159
                $min_len = min(count($ary), count($bry));
160
 
161
                for ($i=0; $i<$min_len; $i++) {
162
                        if ($ary[$i] != $bry[$i]) return false;
163
                }
164
 
165
                return count($ary) - count($bry);
20 daniel-mar 166
        }
2 daniel-mar 167
}
168
 
61 daniel-mar 169
OIDplus::registerObjectType('OIDplusOther');