Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
104 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
 
20
class OIDplusPagePublicObjects extends OIDplusPagePlugin {
21
        public function type() {
22
                return 'public';
23
        }
24
 
25
        public function priority() {
26
                return 0;
27
        }
28
 
29
        public function action(&$handled) {
30
        }
31
 
32
        public function init($html=true) {
33
        }
34
 
35
        public function cfgSetValue($name, $value) {
36
        }
37
 
38
        public function gui($id, &$out, &$handled) {
39
                if ($id === 'oidplus:system') {
40
                        $handled = true;
41
 
42
                        $out['title'] = OIDplus::config()->systemTitle(); // 'Object Database of ' . $_SERVER['SERVER_NAME'];
106 daniel-mar 43
                        $out['icon'] = 'plugins/publicPages/'.basename(__DIR__).'/system_big.png';
104 daniel-mar 44
                        $out['text'] = file_get_contents('welcome.html');
45
 
46
                        if (strpos($out['text'], '%%OBJECT_TYPE_LIST%%') !== false) {
47
                                $tmp = '<ul>';
48
                                foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
49
                                        $tmp .= '<li><a href="?goto='.urlencode($ot::root()).'" onclick="openOidInPanel('.js_escape($ot::root()).', true); return false;">'.htmlentities($ot::objectTypeTitle()).'</a></li>';
50
                                }
51
                                $tmp .= '</ul>';
52
                                $out['text'] = str_replace('%%OBJECT_TYPE_LIST%%', $tmp, $out['text']);
53
                        }
54
 
55
                        return $out;
56
                }
57
        }
58
 
106 daniel-mar 59
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
104 daniel-mar 60
                if ($nonjs) {
106 daniel-mar 61
                        $json[] = array('id' => 'oidplus:system', 'icon' => 'plugins/publicPages/'.basename(__DIR__).'/system.png', 'text' => 'System');
104 daniel-mar 62
 
63
                        $parent = '';
106 daniel-mar 64
                        $res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($req_goto)."'");
104 daniel-mar 65
                        while ($row = OIDplus::db()->fetch_object($res)) {
66
                                $parent = $row->parent;
67
                        }
68
 
69
                        $objTypesChildren = array();
70
                        foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
71
                                $icon = 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png';
72
                                $json[] = array('id' => $ot::root(), 'icon' => $icon, 'text' => $ot::objectTypeTitle());
73
 
74
                                try {
106 daniel-mar 75
                                        $tmp = OIDplusObject::parse($req_goto);
104 daniel-mar 76
                                } catch (Exception $e) {
77
                                        $tmp = null;
78
                                }
79
                                if (!is_null($tmp) && ($ot == get_class($tmp))) {
80
                                        // TODO: Instead of just having 3 levels (parent, this and children), it would be better if we'd had a full tree of all parents
81
                                        //       on the other hand, for giving search engines content, this is good enough
82
                                        $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where " .
106 daniel-mar 83
                                                           "parent = '".OIDplus::db()->real_escape_string($req_goto)."' or " .
84
                                                           "id = '".OIDplus::db()->real_escape_string($req_goto)."' " .
104 daniel-mar 85
                                                           ((!empty($parent)) ? " or id = '".OIDplus::db()->real_escape_string($parent)."' " : "") .
86
                                                           "order by ".OIDplus::db()->natOrder('id'));
87
                                        $z_used = 0;
88
                                        $y_used = 0;
89
                                        $x_used = 0;
90
                                        $stufe = 0;
91
                                        $menu_entries = array();
92
                                        $stufen = array();
93
                                        while ($row = OIDplus::db()->fetch_object($res)) {
94
                                                $obj = OIDplusObject::parse($row->id);
95
                                                if (is_null($obj)) continue; // might happen if the objectType is not available/loaded
96
                                                if (!$obj->userHasReadRights()) continue;
97
                                                $txt = $row->title == '' ? '' : ' -- '.htmlentities($row->title);
98
 
99
                                                if ($row->id == $parent) { $stufe=0; $z_used++; }
106 daniel-mar 100
                                                if ($row->id == $req_goto) { $stufe=1; $y_used++; }
101
                                                if ($row->parent == $req_goto) { $stufe=2; $x_used++; }
104 daniel-mar 102
 
103
                                                $menu_entry = array('id' => $row->id, 'icon' => '', 'text' => $txt, 'indent' => 0);
104
                                                $menu_entries[] = $menu_entry;
105
                                                $stufen[] = $stufe;
106
                                        }
107
                                        if ($x_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 2) $menu_entry['indent'] += 1;
108
                                        if ($y_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 1) $menu_entry['indent'] += 1;
109
                                        if ($z_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 0) $menu_entry['indent'] += 1;
110
                                        $json = array_merge($json, $menu_entries);
111
                                }
112
                        }
113
 
114
                        return true;
115
                } else {
116
                        if (isset($req_goto)) {
117
                                        $goto = $req_goto;
118
                                        $path = array();
119
                                        while (true) {
120
                                                $path[] = $goto;
121
                                                $res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($goto)."'");
122
                                                if (OIDplus::db()->num_rows($res) == 0) break;
123
                                                $row = OIDplus::db()->fetch_array($res);
124
                                                $goto = $row['parent'];
125
                                        }
126
 
127
                                        $goto_path = array_reverse($path);
128
                        } else {
129
                                $goto_path = null;
130
                        }
131
 
132
                        $objTypesChildren = array();
133
                        foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
134
                                $child = array(
135
                                                'id' => $ot::root(),
136
                                                'text' => $ot::objectTypeTitle(),
137
                                                'state' => array("opened" => true),
138
                                                'icon' => 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png',
139
                                                'children' => OIDplusTree::tree_populate($ot::root(), $goto_path)
140
                                        );
141
                                if (!file_exists($child['icon'])) $child['icon'] = null; // default icon (folder)
142
                                $objTypesChildren[] = $child;
143
                        }
144
 
145
                        $json[] = array(
146
                                'id' => "oidplus:system",
147
                                'text' => "Objects",
148
                                'state' => array(
149
                                        "opened" => true,
150
                                        // "selected" => true)  // "selected" ist buggy: 1) Das select-Event wird beim Laden nicht gefeuert 2) Die direkt untergeordneten Knoten lassen sich nicht öffnen (laden für ewig)
151
                                ),
106 daniel-mar 152
                                'icon' => 'plugins/publicPages/'.basename(__DIR__).'/system.png',
104 daniel-mar 153
                                'children' => $objTypesChildren
154
                        );
155
 
156
                        return true;
157
                }
158
        }
159
}
160
 
161
OIDplus::registerPagePlugin(new OIDplusPagePublicObjects());