dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
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,menu_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['menu_title'] ?? ''));
87 if ($title === '') $title = trim((string)($page['title'] ?? ''));
88 if ($title === '') $title = 'Seite ' . (int)($page['id'] ?? 0);
89 return '<li class="dbx-cms-menu-page"><a href="' . dbx()->esc($this->content_href($page)) . '"><span>' . dbx()->esc($title) . '</span></a></li>';
90 }
91
92 private function resolve_folder_reference($db, $root) {
93 if (is_int($root) || (is_string($root) && ctype_digit(trim($root)))) {
94 return (int)$root;
95 }
96
97 $path = trim(rawurldecode((string)$root), " \t\n\r\0\x0B/");
98 if ($path === '') return 0;
99
100 $parentId = 0;
101 foreach (preg_split('#\s*/\s*#', $path, -1, PREG_SPLIT_NO_EMPTY) as $name) {
102 $folder = $db->select1(
103 dbxContentLng::ddFolder(),
104 array('parent_id' => $parentId, 'name' => trim((string)$name)),
105 array('id', 'name'),
106 0
107 );
108 $parentId = (int)($folder['id'] ?? 0);
109 if ($parentId <= 0) return 0;
110 }
111
112 return $parentId;
113 }
114
115 private function render_pages($db, $folder_id) {
116 $html = '';
117 foreach ($this->cms_pages($db, $folder_id) as $page) {
118 $html .= $this->render_page_li($page);
119 }
120 return $html;
121 }
122
123 private function render_folder_li($db, array $folder) {
124 $id = (int)($folder['id'] ?? 0);
125 if ($id <= 0 || !$this->folder_allowed($db, $id)) return '';
126
127 $inner = $this->render_children($db, $id);
128 if (trim($inner) === '') return '';
129
130 $name = trim((string)($folder['name'] ?? ''));
131 if ($name === '') $name = 'Ordner ' . $id;
132
133 return '<li class="dbx-cms-menu-folder">'
134 . '<a role="button" tabindex="0" aria-haspopup="true">'
135 . '<i class="bi bi-folder2-open" aria-hidden="true"></i>'
136 . '<span>' . dbx()->esc($name) . '</span>'
137 . '</a><ul>' . $inner . '</ul></li>';
138 }
139
140 private function render_children($db, $folder_id, $include_pages = true) {
141 $folder_id = (int)$folder_id;
142 if (!$this->folder_allowed($db, $folder_id)) return '';
143
144 $html = '';
145 foreach ($this->cms_folders($db, $folder_id) as $folder) {
146 $html .= $this->render_folder_li($db, $folder);
147 }
148 if ($include_pages) {
149 $html .= $this->render_pages($db, $folder_id);
150 }
151 return $html;
152 }
153
154 public function render_flat_marker($root = 0, $flat = 1) {
155 $flat = (int)$flat;
156
157 if ($flat !== 1) return '';
158
159 $db = dbx()->get_system_obj('dbxDB');
160 $root = $this->resolve_folder_reference($db, $root);
161 if (!$this->folder_allowed($db, $root)) return '';
162
163 return $this->render_pages($db, $root);
164 }
165
166 public function render_placeholder($root = 0, $flat = 1, $split_flat = false) {
167 $flat = (int)$flat;
168 $split_flat = $split_flat && $flat === 1;
169 $db = dbx()->get_system_obj('dbxDB');
170 $root = $this->resolve_folder_reference($db, $root);
171 $lng = dbxContentPageCache::currentLng();
172 $variant = dbxContentPageCache::menuVariantFlat($flat);
173 if ($split_flat) {
174 $variant .= '-split';
175 }
176
177 if (dbxContentPageCache::isReadEnabled()) {
178 $cached = dbxContentPageCache::readMenu($root, $variant, $lng);
179 if ($cached !== null) {
180 return $cached;
181 }
182 }
183
184 $html = '';
185
186 if ($root <= 0) {
187 $inner = $this->render_children($db, 0, !$split_flat);
188 if ($flat) {
189 $html = $inner;
190 } elseif (trim($inner) !== '') {
191 $html = '<li class="dbx-cms-menu-root"><a>Content</a><ul>' . $inner . '</ul></li>';
192 }
193 } else {
194 if ($this->folder_allowed($db, $root)) {
195 if ($flat) {
196 $html = $this->render_children($db, $root, !$split_flat);
197 } else {
198 $folder = $db->select1(dbxContentLng::ddFolder(), $root, '*', 0);
199 if (is_array($folder)) {
200 $html = $this->render_folder_li($db, $folder);
201 }
202 }
203 }
204 }
205
206 if (dbxContentPageCache::isWriteEnabled()) {
207 dbxContentPageCache::writeMenu($html, $root, $variant, $lng);
208 }
209
210 return $html;
211 }
212
213
214 private function has_entrys($root,$tab_folder,$tab_content,$count,$deepr=0) {
215 $deepr=($deepr + 1);
216 $deep=dbx()->get_modul_var('deep',9,'int');
217
218
219 //$count=($count + $this->has_files($root,$tab_content));
220
221 if ($deepr <= $deep) {
222 $count=($count + $this->has_files($root,$tab_content));
223 $where = "parent_id = $root";
224 $db = dbx()->get_system_obj('dbxDB');
225 $folder=$db->select($tab_folder,$where,'id,name,group_read,sorter','sorter,name,id');
226 //dbx_debug("##Check-Entrys Folder=($root) Count=($count) Deep=($deep) Deepr=($deepr)");
227 if (is_array($folder)) {
228 foreach ($folder as $no => $record) {
229 $access=$this->folder_allowed($db, (int)$record['id']);
230 if ($access) {
231 $root = $record['id'];
232 $name = $record['name'];
233 $count = $this->has_entrys($root,$tab_folder,$tab_content,$count,$deepr) ;
234 }
235 }
236 }
237 }
238 return $count;
239 }
240
241
242
243 private function has_files($root,$tab_content) {
244 $count = 0;
245 if ($root >= 0) {
246 $where = "folder = $root";
247 //dbx_debug("Content-menu ($tab_content) where=($where)");
248 $db = dbx()->get_system_obj('dbxDB');
249 if (!$this->folder_allowed($db, (int)$root)) return 0;
250 $files = $db->select($tab_content,$where,'id,title');
251 if (is_array($files)) {
252 $count = count($files);
253 }
254 }
255 //dbx_debug("has_files($root)=($count)");
256 return $count;
257 }
258
259 private function content_files($root,$tab_content,$content) {
260 $obj_files='';
261 $where = "folder = $root";
262 $db = dbx()->get_system_obj('dbxDB');
263 if (!$this->folder_allowed($db, (int)$root)) {
264 return str_replace('{obj:files}', '', $content);
265 }
266 $files = $db->select($tab_content,$where,'id,title,permalink,sorter','sorter,title,id');
267 if (is_array($files)) {
268 foreach ($files as $no => $record) {
269 $id =$record['id'];
270 $title=$record['title'];
271 $tpl=$this->oTPL->get_tpl('modul','menu-content-file');
272 if ($record['permalink']) {
273 $href=$record['permalink'];
274 } else {
275 $href='?dbx_Modul=dbxContent&dbx_run1=show&cid='.$id;
276 }
277 $tpl = (str_replace('{href}' ,$href ,$tpl));
278 $tpl = (str_replace('{title}',$title,$tpl));
279 $obj_files.=$tpl;
280 }
281 }
282 $content = (str_replace('{obj:files}',$obj_files,$content));
283 return $content;
284 }
285
286
287 private function content_folder($root,$tab_folder,$tab_content,$content,$deepr=0) {
288 $obj_folder='';
289 $where = "parent_id = $root";
290 $db = dbx()->get_system_obj('dbxDB');
291 $folder=$db->select($tab_folder,$where,'id,name,group_read,sorter','sorter,name,id');
292 if (is_array($folder)) {
293 $deep = dbx()->get_modul_var('deep',9,'int');
294 $deepr = ($deepr + 1);
295 //dbx()->set_modul_var('deep_run',$deepr);
296 if ($deepr <= $deep) {
297 foreach ($folder as $no => $record) {
298 $access=$this->folder_allowed($db, (int)$record['id']);
299 if ($access) {
300 $root = $record['id'];
301 $name = $record['name'];
302 $count = $this->has_entrys($root,$tab_folder,$tab_content,0,$deepr);
303 if ($count) {
304 $tpl = $this->oTPL->get_tpl('modul','menu-content-sub');
305 $tpl = (str_replace('{folder}',$name ,$tpl));
306 $tpl = (str_replace('{count}' ,$count,$tpl));
307 $tpl = (str_replace('{deep}' ,$deepr,$tpl));
308
309 $tpl=$this->content_files($root,$tab_content,$tpl);
310 if ($deepr < $deep) {
311 $tpl=$this->content_folder($root,$tab_folder,$tab_content,$tpl,$deepr);
312 } else {
313 $tpl='';
314 }
315 $obj_folder.=$tpl;
316 } // count
317 } // access
318 } // foreach
319 } // deepr
320 } // folder
321 $content = (str_replace('{obj:folders}',$obj_folder,$content));
322 return $content;
323 }
324
325
326
327 private function content_menu_load() {
328 $content='';
329 $root = dbx()->get_modul_var('root',0,'int');
330 $deep = dbx()->get_modul_var('deep',9,'int');
331 $label = dbx()->get_modul_var('label');
332 $mode = dbx()->get_modul_var('mode');
333 $lng = dbx()->get_modul_var('lng');
334 $tpl = 'menu-content';
335 if ($mode) $tpl=$tpl.'-'.$mode;
336
337 $tab_content = dbxContentLng::ddContent($lng);
338 $tab_folder = dbxContentLng::ddFolder($lng);
339 dbx()->set_modul_var('deep_run',0);
340 $entrys= $this->has_entrys($root,$tab_folder,$tab_content,0);
341 $files = $this->has_files($root,$tab_content);
342 if ($entrys) {
343 $content=$this->oTPL->get_tpl('modul',$tpl);
344 $content=str_replace('{label}',$label,$content);
345
346 $content=$this->content_files($root,$tab_content,$content);
347 $content=$this->content_folder($root,$tab_folder,$tab_content,$content);
348 }
349 //dbx_debug("### Content_menu ROOT=($entrys) TPL=($tpl) Mode=($mode) #####");
350
351 return $content;
352 }
353
354
355
356
357
358 public function run() {
359 $root = dbx()->get_modul_var('root', 0);
360 if (!dbx()->is_int_value($root)) {
361 #todo $root = folder name select get root
362 }
363
364 $lng = trim((string) dbx()->get_modul_var('lng', ''));
365 if ($lng === '') {
366 $lng = dbx()->get_system_var('dbx_lng', 'de');
367 }
368 dbx()->set_modul_var('lng', $lng);
369
370 $deep = (int) dbx()->get_modul_var('deep', 9, 'int');
371 $mode = trim((string) dbx()->get_modul_var('mode', ''));
372 $label = trim((string) dbx()->get_modul_var('label', ''));
373 $variant = dbxContentPageCache::menuVariantLoad($deep, $mode, $label);
374
375 if (dbxContentPageCache::isReadEnabled()) {
376 $cached = dbxContentPageCache::readMenu((int) $root, $variant, $lng);
377 if ($cached !== null) {
378 return $cached;
379 }
380 }
381
382 $content = $this->content_menu_load();
383
384 if (dbxContentPageCache::isWriteEnabled()) {
385 dbxContentPageCache::writeMenu($content, (int) $root, $variant, $lng);
386 }
387
388 return $content;
389 } // run()
390
391} // class
392
393?>
render_placeholder($root=0, $flat=1, $split_flat=false)
dbx()
Liefert die zentrale dbXapp-API als Singleton.
Definition dbxApi.php:2805
is_int_value($value)
Prueft, ob ein Wert als Integer verwendbar ist.
Definition dbxApi.php:2435
if($web->content_permalink_redirect_target() !=='') $tpl
if(trim($first) !=='first') $cached
DBX schema administration.
if($cinematic===''||!str_contains($cinematic, 'data-dbx-cinema')) $page