dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxContent_menu.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxMenu;
3
4use dbx\dbxContent\dbxContentLng;
5use dbx\dbxContent\dbxContentPageCache;
6
7require_once dirname(__DIR__, 2) . '/dbxContent/include/dbxContent_bootstrap.php';
8
10
11 Public $oTPL;
12
13 public function __construct() {
14 $this->oTPL=dbx()->get_system_obj('dbxTPL');
15 }
16
17
18 private function user_has_access($groups) {
19 $groups = trim((string)$groups);
20 if ($groups === '') $groups = '*';
21 return dbx()->can($groups);
22 }
23
24 private function resolve_folder_rights($db, $folder_id, array $visited = array()) {
25 $folder_id = (int)$folder_id;
26 if ($folder_id <= 0) return '*';
27 if (isset($visited[$folder_id])) return '*';
28 $visited[$folder_id] = 1;
29
30 $row = $db->select1(dbxContentLng::ddFolder(), $folder_id, '*', 0);
31 if (!is_array($row)) return '*';
32
33 $raw = trim((string)($row['group_read'] ?? ''));
34 if ($raw === '') return '*';
35
36 $parent_id = (int)($row['parent_id'] ?? 0);
37 $parts = preg_split('/\s*,\s*/', $raw, -1, PREG_SPLIT_NO_EMPTY);
38 $out = array();
39 $uses_parent = false;
40
41 foreach ($parts as $part) {
42 $part = trim((string)$part);
43 if ($part === '') continue;
44 if (strtolower($part) === 'parent') {
45 $uses_parent = true;
46 continue;
47 }
48 $out[$part] = 1;
49 }
50
51 if ($uses_parent) {
52 $parent_rights = $this->resolve_folder_rights($db, $parent_id, $visited);
53 foreach (preg_split('/\s*,\s*/', $parent_rights, -1, PREG_SPLIT_NO_EMPTY) as $part) {
54 $part = trim((string)$part);
55 if ($part !== '' && strtolower($part) !== 'parent') $out[$part] = 1;
56 }
57 }
58
59 return count($out) ? implode(',', array_keys($out)) : '*';
60 }
61
62 private function folder_allowed($db, $folder_id) {
63 return $this->user_has_access($this->resolve_folder_rights($db, $folder_id));
64 }
65
66 private function content_href(array $page) {
67 $permalink = trim((string)($page['permalink'] ?? ''));
68 if ($permalink !== '') {
69 if (preg_match('/^(https?:\/\/|\/|\?)/i', $permalink)) return $permalink;
70 return dbx()->get_base_url() . ltrim($permalink, '/');
71 }
72 return '?dbx_modul=dbxContent&dbx_run1=show&dbx_cid=' . (int)($page['id'] ?? 0);
73 }
74
75 private function cms_pages($db, $folder_id) {
76 $rows = $db->select(dbxContentLng::ddContent(), 'folder = ' . (int)$folder_id . ' AND activ = 1', 'id,title,permalink,sorter,folder', 'sorter,title,id', 'ASC', '', 0, 0, 0);
77 return is_array($rows) ? $rows : array();
78 }
79
80 private function cms_folders($db, $parent_id) {
81 $rows = $db->select(dbxContentLng::ddFolder(), 'parent_id = ' . (int)$parent_id, 'id,name,parent_id,group_read,sorter', 'sorter,name,id', 'ASC', '', 0, 0, 0);
82 return is_array($rows) ? $rows : array();
83 }
84
85 private function render_page_li(array $page) {
86 $title = trim((string)($page['title'] ?? ''));
87 if ($title === '') $title = 'Seite ' . (int)($page['id'] ?? 0);
88 return '<li class="dbx-cms-menu-page"><a href="' . dbx()->esc($this->content_href($page)) . '">' . dbx()->esc($title) . '</a></li>';
89 }
90
91 private function render_pages($db, $folder_id) {
92 $html = '';
93 foreach ($this->cms_pages($db, $folder_id) as $page) {
94 $html .= $this->render_page_li($page);
95 }
96 return $html;
97 }
98
99 private function render_folder_li($db, array $folder) {
100 $id = (int)($folder['id'] ?? 0);
101 if ($id <= 0 || !$this->folder_allowed($db, $id)) return '';
102
103 $inner = $this->render_children($db, $id);
104 if (trim($inner) === '') return '';
105
106 $name = trim((string)($folder['name'] ?? ''));
107 if ($name === '') $name = 'Ordner ' . $id;
108
109 return '<li class="dbx-cms-menu-folder"><a>' . dbx()->esc($name) . '</a><ul>' . $inner . '</ul></li>';
110 }
111
112 private function render_children($db, $folder_id, $include_pages = true) {
113 $folder_id = (int)$folder_id;
114 if (!$this->folder_allowed($db, $folder_id)) return '';
115
116 $html = '';
117 foreach ($this->cms_folders($db, $folder_id) as $folder) {
118 $html .= $this->render_folder_li($db, $folder);
119 }
120 if ($include_pages) {
121 $html .= $this->render_pages($db, $folder_id);
122 }
123 return $html;
124 }
125
126 public function render_flat_marker($root = 0, $flat = 1) {
127 $root = (int)$root;
128 $flat = (int)$flat;
129
130 if ($flat !== 1) return '';
131
132 $db = dbx()->get_system_obj('dbxDB');
133 if (!$this->folder_allowed($db, $root)) return '';
134
135 return $this->render_pages($db, $root);
136 }
137
138 public function render_placeholder($root = 0, $flat = 1, $split_flat = false) {
139 $root = (int)$root;
140 $flat = (int)$flat;
141 $split_flat = $split_flat && $flat === 1;
142 $lng = dbxContentPageCache::currentLng();
143 $variant = dbxContentPageCache::menuVariantFlat($flat);
144 if ($split_flat) {
145 $variant .= '-split';
146 }
147
148 if (dbxContentPageCache::isEnabled()) {
149 $cached = dbxContentPageCache::readMenu($root, $variant, $lng);
150 if ($cached !== null) {
151 return $cached;
152 }
153 }
154
155 $db = dbx()->get_system_obj('dbxDB');
156 $html = '';
157
158 if ($root <= 0) {
159 $inner = $this->render_children($db, 0, !$split_flat);
160 if ($flat) {
161 $html = $inner;
162 } elseif (trim($inner) !== '') {
163 $html = '<li class="dbx-cms-menu-root"><a>Content</a><ul>' . $inner . '</ul></li>';
164 }
165 } else {
166 if ($this->folder_allowed($db, $root)) {
167 if ($flat) {
168 $html = $this->render_children($db, $root, !$split_flat);
169 } else {
170 $folder = $db->select1(dbxContentLng::ddFolder(), $root, '*', 0);
171 if (is_array($folder)) {
172 $html = $this->render_folder_li($db, $folder);
173 }
174 }
175 }
176 }
177
178 if (dbxContentPageCache::isEnabled()) {
179 dbxContentPageCache::writeMenu($html, $root, $variant, $lng);
180 }
181
182 return $html;
183 }
184
185
186 private function has_entrys($root,$tab_folder,$tab_content,$count,$deepr=0) {
187 $deepr=($deepr + 1);
188 $deep=dbx()->get_modul_var('deep',9,'int');
189
190
191 //$count=($count + $this->has_files($root,$tab_content));
192
193 if ($deepr <= $deep) {
194 $count=($count + $this->has_files($root,$tab_content));
195 $where = "parent_id = $root";
196 $db = dbx()->get_system_obj('dbxDB');
197 $folder=$db->select($tab_folder,$where,'id,name,group_read,sorter','sorter,name,id');
198 //dbx_debug("##Check-Entrys Folder=($root) Count=($count) Deep=($deep) Deepr=($deepr)");
199 if (is_array($folder)) {
200 foreach ($folder as $no => $record) {
201 $access=$this->folder_allowed($db, (int)$record['id']);
202 if ($access) {
203 $root = $record['id'];
204 $name = $record['name'];
205 $count = $this->has_entrys($root,$tab_folder,$tab_content,$count,$deepr) ;
206 }
207 }
208 }
209 }
210 return $count;
211 }
212
213
214
215 private function has_files($root,$tab_content) {
216 $count = 0;
217 if ($root >= 0) {
218 $where = "folder = $root";
219 //dbx_debug("Content-menu ($tab_content) where=($where)");
220 $db = dbx()->get_system_obj('dbxDB');
221 if (!$this->folder_allowed($db, (int)$root)) return 0;
222 $files = $db->select($tab_content,$where,'id,title');
223 if (is_array($files)) {
224 $count = count($files);
225 }
226 }
227 //dbx_debug("has_files($root)=($count)");
228 return $count;
229 }
230
231 private function content_files($root,$tab_content,$content) {
232 $obj_files='';
233 $where = "folder = $root";
234 $db = dbx()->get_system_obj('dbxDB');
235 if (!$this->folder_allowed($db, (int)$root)) {
236 return str_replace('{obj:files}', '', $content);
237 }
238 $files = $db->select($tab_content,$where,'id,title,permalink,sorter','sorter,title,id');
239 if (is_array($files)) {
240 foreach ($files as $no => $record) {
241 $id =$record['id'];
242 $title=$record['title'];
243 $tpl=$this->oTPL->get_tpl('modul','menu-content-file');
244 if ($record['permalink']) {
245 $href=$record['permalink'];
246 } else {
247 $href='?dbx_Modul=dbxContent&dbx_run1=show&cid='.$id;
248 }
249 $tpl = (str_replace('{href}' ,$href ,$tpl));
250 $tpl = (str_replace('{title}',$title,$tpl));
251 $obj_files.=$tpl;
252 }
253 }
254 $content = (str_replace('{obj:files}',$obj_files,$content));
255 return $content;
256 }
257
258
259 private function content_folder($root,$tab_folder,$tab_content,$content,$deepr=0) {
260 $obj_folder='';
261 $where = "parent_id = $root";
262 $db = dbx()->get_system_obj('dbxDB');
263 $folder=$db->select($tab_folder,$where,'id,name,group_read,sorter','sorter,name,id');
264 if (is_array($folder)) {
265 $deep = dbx()->get_modul_var('deep',9,'int');
266 $deepr = ($deepr + 1);
267 //dbx()->set_modul_var('deep_run',$deepr);
268 if ($deepr <= $deep) {
269 foreach ($folder as $no => $record) {
270 $access=$this->folder_allowed($db, (int)$record['id']);
271 if ($access) {
272 $root = $record['id'];
273 $name = $record['name'];
274 $count = $this->has_entrys($root,$tab_folder,$tab_content,0,$deepr);
275 if ($count) {
276 $tpl = $this->oTPL->get_tpl('modul','menu-content-sub');
277 $tpl = (str_replace('{folder}',$name ,$tpl));
278 $tpl = (str_replace('{count}' ,$count,$tpl));
279 $tpl = (str_replace('{deep}' ,$deepr,$tpl));
280
281 $tpl=$this->content_files($root,$tab_content,$tpl);
282 if ($deepr < $deep) {
283 $tpl=$this->content_folder($root,$tab_folder,$tab_content,$tpl,$deepr);
284 } else {
285 $tpl='';
286 }
287 $obj_folder.=$tpl;
288 } // count
289 } // access
290 } // foreach
291 } // deepr
292 } // folder
293 $content = (str_replace('{obj:folders}',$obj_folder,$content));
294 return $content;
295 }
296
297
298
299 private function content_menu_load() {
300 $content='';
301 $root = dbx()->get_modul_var('root',0,'int');
302 $deep = dbx()->get_modul_var('deep',9,'int');
303 $label = dbx()->get_modul_var('label');
304 $mode = dbx()->get_modul_var('mode');
305 $lng = dbx()->get_modul_var('lng');
306 $tpl = 'menu-content';
307 if ($mode) $tpl=$tpl.'-'.$mode;
308
309 $tab_content = dbxContentLng::ddContent($lng);
310 $tab_folder = dbxContentLng::ddFolder($lng);
311 dbx()->set_modul_var('deep_run',0);
312 $entrys= $this->has_entrys($root,$tab_folder,$tab_content,0);
313 $files = $this->has_files($root,$tab_content);
314 if ($entrys) {
315 $content=$this->oTPL->get_tpl('modul',$tpl);
316 $content=str_replace('{label}',$label,$content);
317
318 $content=$this->content_files($root,$tab_content,$content);
319 $content=$this->content_folder($root,$tab_folder,$tab_content,$content);
320 }
321 //dbx_debug("### Content_menu ROOT=($entrys) TPL=($tpl) Mode=($mode) #####");
322
323 return $content;
324 }
325
326
327
328
329
330 public function run() {
331 $root = dbx()->get_modul_var('root', 0);
332 if (!dbx()->is_int_value($root)) {
333 #todo $root = folder name select get root
334 }
335
336 $lng = trim((string) dbx()->get_modul_var('lng', ''));
337 if ($lng === '') {
338 $lng = dbx()->get_system_var('dbx_lng', 'de');
339 }
340 dbx()->set_modul_var('lng', $lng);
341
342 $deep = (int) dbx()->get_modul_var('deep', 9, 'int');
343 $mode = trim((string) dbx()->get_modul_var('mode', ''));
344 $label = trim((string) dbx()->get_modul_var('label', ''));
345 $variant = dbxContentPageCache::menuVariantLoad($deep, $mode, $label);
346
347 if (dbxContentPageCache::isEnabled()) {
348 $cached = dbxContentPageCache::readMenu((int) $root, $variant, $lng);
349 if ($cached !== null) {
350 return $cached;
351 }
352 }
353
354 $content = $this->content_menu_load();
355
356 if (dbxContentPageCache::isEnabled()) {
357 dbxContentPageCache::writeMenu($content, (int) $root, $variant, $lng);
358 }
359
360 return $content;
361 } // run()
362
363} // class
364
365?>
render_placeholder($root=0, $flat=1, $split_flat=false)
DBX schema administration.