dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxContent_content.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxContent;
3
4require_once __DIR__ . '/dbxContent_bootstrap.php';
5
7
8 Public $oTPL;
9
10 public function __construct() {
11 $this->oTPL = dbx()->get_system_obj('dbxTPL');
12 }
13
14
15
16
17
18 private function get_content_tpl_from_folder($folder=0) {
19 // #ToDO Recursive get TPL from Folder
20 $tpl='c-content';
21 $db=$db=dbx()->get_system_obj('dbxDB');
22 $rec=$db->select1(dbxContentLng::ddFolder(),$folder);
23 if (is_array($rec)) {
24 if ($rec['template']) {
25 $tpl=$rec['template'];
26 } else {
27 $rec=$db->select1(dbxContentLng::ddFolder(),$rec['parent_id']);
28 if (is_array($rec)) {
29 if ($rec['template']) {
30 $tpl=$rec['template'];
31 }
32 }
33 }
34 }
35 return $tpl;
36 }
37
38
39 private function get_cols_content($cols,$content,$split) {
40
41 $content_cols=array();
42 for ($c = 0; $c < $cols; $c++) { $content_cols[$c]=''; }
43 //dbx_debug("get_cols=($split)");
44
45
46
47 $content_tmp = explode($split, $content);
48 if (is_array($content_tmp)) {
49 $count_tmp = count($content_tmp);
50 //dbx_debug("br count=($count_tmp)");
51 if ($count_tmp >= $cols) {
52 $a=0; // Start
53 $per_col= (int) ($count_tmp / $cols +1);
54 //dbx_debug("br per col=($per_col) ");
55 for ($c = 0; $c < $cols; $c++) {
56 //dbx_debug("use cols=($cols) col=($c) A=($a)");
57 for ($p = $a; $p < ($per_col * ($c+1)) ; $p++) {
58 //dbx_debug("br add col=($c) part=($p)");
59 if (isset($content_tmp[$p])) {
60 $content_cols[$c].=$content_tmp[$p].$split;
61 // check for multiline elements ul img ....
62 if (strpos($content_tmp[$p],'</ul>')) $p=($p +3);
63 if (strpos($content_tmp[$p],'<img')) $p=($p +3);
64 }
65 $a++;
66 }
67 }
68 }
69 }
70 return $content_cols;
71 }
72
73 private function check_cols($content_cols,$cols) {
74 $ok=0;
75 if (is_array($content_cols)) {
76 if (isset($content_cols[$cols-1])) {
77 if (trim($content_cols[$cols-1]) > '') $ok=1;
78 }
79 }
80 return $ok;
81 }
82
83
84 private function get_col_content($cols,$content) {
85
86 $content = preg_replace('/<hr\b[^>]*class="[^"]*\bdbx_split\b[^"]*"[^>]*>/i', '<hr class="dbx_split">', (string)$content);
87 $content_cols = explode('<hr class="dbx_split">', $content);
88 if ($cols == count($content_cols)) return $content_cols;
89
90 $content = trim($content);
91 $content = (str_replace('<hr class="dbx_split">','<br>',$content));
92 $content = (str_replace('<br />','<br>',$content));
93 $content = (str_replace('<br/>' ,'<br>',$content));
94 $length = strlen($content);
95
96 if (!$length) return ''; // Content is empty
97 $content_cols=array();
98 if (!$this->check_cols($content_cols,$cols)) $content_cols=$this->get_cols_content($cols,$content,'</p>');
99 if (!$this->check_cols($content_cols,$cols)) $content_cols=$this->get_cols_content($cols,$content,'<br>');
100 if (!$this->check_cols($content_cols,$cols)) $content_cols=$this->get_cols_content($cols,$content,'.');
101 if (!$this->check_cols($content_cols,$cols)) $content_cols=$this->get_cols_content($cols,$content,' ');
102
103 return $content_cols;
104 }
105
106 private function split_content_sections($content) {
107 $content = (string)$content;
108 $sections = array(
109 'hero' => '',
110 'main' => $content,
111 'thesar' => '',
112 'footer' => '',
113 );
114
115 $tokens = array('hero', 'main', 'thesar', 'footer');
116 $pattern = '/(?:<!--\s*dbx:(hero|main|thesar|footer)\s*-->|<span[^>]*class="[^"]*dbx-cms-marker[^"]*"[^>]*>\s*dbx:(hero|main|thesar|footer)\s*<\/span>)/i';
117
118 if (!preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE)) {
119 return $sections;
120 }
121
122 $parts = array();
123 foreach ($matches[0] as $idx => $match) {
124 $name = strtolower($matches[1][$idx][0] ?: $matches[2][$idx][0]);
125 if (!in_array($name, $tokens, true)) continue;
126 $parts[] = array('name' => $name, 'pos' => $match[1], 'len' => strlen($match[0]));
127 }
128
129 if (!$parts) return $sections;
130
131 for ($i = 0; $i < count($parts); $i++) {
132 $start = $parts[$i]['pos'] + $parts[$i]['len'];
133 $end = isset($parts[$i + 1]) ? $parts[$i + 1]['pos'] : strlen($content);
134 $sections[$parts[$i]['name']] = trim(substr($content, $start, max(0, $end - $start)));
135 }
136
137 if (trim($sections['main']) === $content) {
138 $first = $parts[0]['pos'];
139 $sections['main'] = trim(substr($content, 0, $first));
140 }
141
142 return $sections;
143 }
144
145 private function adminEditorBarHtml($cid = 0) {
146 if (!dbx()->can('admin')) {
147 return '';
148 }
149
150 $url = $this->appUrl() . '?dbx_modul=dbxContent_admin&dbx_run1=cms&cid=' . (int)$cid;
151 return $this->oTPL->get_tpl('dbxContent|content-view-bar-admin-win', array(
152 'admin_url' => dbx()->esc($url),
153 ));
154 }
155
156 private function appUrl() {
157 $script = str_replace('\\', '/', (string)($_SERVER['SCRIPT_NAME'] ?? ''));
158 if ($script === '') return '';
159 $dir = str_replace('\\', '/', dirname($script));
160 if ($dir === '.' || $dir === '/' || $dir === '\\') return '/';
161 return rtrim($dir, '/') . '/';
162 }
163
164 private function wrapContentPage($pageContent, $cid) {
165 $i = dbx()->next_id();
166 $title = trim((string)dbx()->get_system_var('dbx_title', ''));
167 if ($title === '') {
168 $db = dbx()->get_system_obj('dbxDB');
169 $row = $db->select1(dbxContentLng::ddContent(), (int)$cid, 'title', 0);
170 if (is_array($row)) {
171 $title = trim((string)($row['title'] ?? ''));
172 }
173 }
174 if ($title === '') {
175 $title = 'Seite #' . (int)$cid;
176 }
177
178 return $this->oTPL->get_tpl('dbxContent|content-page-frontend', array(
179 'frame_id' => 'dbx_content_page_' . $i,
180 'cid' => (string)(int)$cid,
181 'frontend_head' => $this->oTPL->get_tpl('dbxContent|content-frontend-head', array(
182 'bar_title' => $title,
183 'bar_title_pre' => $this->adminEditorBarHtml((int)$cid),
184 'bar_title_heading_attrs' => ' data-cms-page-title',
185 )),
186 'page_content' => $pageContent,
187 ));
188 }
189
190 private function get_media_merge($cid) {
191 $merge = array('media_hero' => '', 'media_gallery' => '', 'media_teaser' => '', 'media_footer' => '');
192 $cid = (int)$cid;
193 if ($cid <= 0) return $merge;
194
195 $db = dbx()->get_system_obj('dbxDB');
196 $usage_rows = $db->select('dbxMediaUsage', 'content_id = ' . $cid . ' AND active = 1', '*', 'slot,sorter,id', 'ASC', '', 0, 0, 0);
197 $rows = array();
198 if (is_array($usage_rows) && !empty($usage_rows)) {
199 foreach ($usage_rows as $usage) {
200 if (!is_array($usage)) continue;
201 $row = $db->select1('dbxMedia', (int)($usage['media_id'] ?? 0));
202 if (!is_array($row) || (int)($row['active'] ?? 0) !== 1) continue;
203 $row['slot'] = (string)($usage['slot'] ?? 'gallery');
204 $rows[] = $row;
205 }
206 } else {
207 $rows = $db->select('dbxMedia', 'content_id = ' . $cid . ' AND active = 1', '*', 'slot,title', 'ASC', '', 0, 0, 0);
208 }
209 if (!is_array($rows)) return $merge;
210
211 $gallery = '';
212
213 foreach ($rows as $row) {
214 if (!is_array($row)) continue;
215 $slot = trim((string)($row['slot'] ?? 'gallery')) ?: 'gallery';
216 $url = 'index.php?dbx_modul=dbxContent&dbx_run1=media&dbx_mid=' . (int)($row['id'] ?? 0);
217 $alt = htmlspecialchars((string)($row['alt'] ?? $row['title'] ?? ''), ENT_QUOTES, 'UTF-8');
218 $img = '<img class="dbx-content-media dbx-content-media-' . htmlspecialchars($slot, ENT_QUOTES, 'UTF-8') . '" src="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '" alt="' . $alt . '">';
219
220 if ($slot === 'hero' && !$merge['media_hero']) $merge['media_hero'] = $img;
221 if ($slot === 'teaser' && !$merge['media_teaser']) $merge['media_teaser'] = $img;
222 if ($slot === 'footer' && !$merge['media_footer']) $merge['media_footer'] = $img;
223 if ($slot === 'gallery') $gallery .= $img;
224 }
225
226 $merge['media_gallery'] = $gallery ? '<div class="dbx-content-gallery">' . $gallery . '</div>' : '';
227 return $merge;
228 }
229
230
231 public function renderPage(int $cid, array $options = array()): string {
232 $cid = (int) $cid;
233 if ($cid <= 0) {
234 return '';
235 }
236
237 require_once __DIR__ . '/dbxContent_bootstrap.php';
238 $renderer = dbx()->get_include_obj('dbxContentRenderer', 'dbxContent');
240 $renderOptions = array(
241 'skip_hits' => !empty($options['skip_hits']),
242 'admin_help' => !empty($options['admin_help']),
243 );
244 $static = $renderer->renderStatic($cid, $renderOptions);
245 return $this->wrapContentPage($static, $cid);
246 }
247
248 public function run() {
249 $cid = (int) dbx()->get_modul_var('dbx_cid', 0, 'int');
250 if ($cid <= 0) {
251 $cid = (int) dbx()->get_modul_var('cid', 0, 'int');
252 }
253 if ($cid <= 0) {
254 $cid = (int) dbx()->get_system_var('dbx_cid', 0, 'int');
255 }
256 if ($cid <= 0) {
257 require_once __DIR__ . '/dbxContent_bootstrap.php';
259 }
260 if (!$cid > 0) {
261 return "show cid=($cid) nicht gefunden! ";
262 }
263
264 return $this->renderPage($cid);
265 } // run()
266
267
268} // class
269
270?>
renderPage(int $cid, array $options=array())
static resolveCid(string $lng='')
Startseiten-CID fuer die aktive (oder uebergebene) Sprache.
static ddFolder(string $lng='')
static ddContent(string $lng='')
DBX schema administration.
$_SERVER['REQUEST_URI']