dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxContentContextHelpProvision.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxContent;
3
4require_once __DIR__ . '/dbxContent_bootstrap_sync.php';
5require_once __DIR__ . '/dbxContentContextHelp.class.php';
6require_once dirname(__DIR__, 2) . '/dbxAdmin/include/dbxAdminHelp.class.php';
7
9
10 private const PROVISION_VERSION = 5;
11 private const CONFIG_KEY = 'context_help_provision_version';
12
13 public static function run(): void {
14 $version = (int) dbx()->get_config('dbxContent', self::CONFIG_KEY);
15 if ($version >= self::PROVISION_VERSION) {
16 return;
17 }
18
19 $db = dbx()->get_system_obj('dbxDB');
20 if (!is_object($db)) {
21 return;
22 }
23
24 $server = 'dbx|dbxContent.db3';
25 if (!$db->connect_db_server($server)) {
26 return;
27 }
28
30 if (empty($result['errors']) && ((int) ($result['created'] ?? 0) + (int) ($result['updated'] ?? 0)) > 0) {
31 self::markProvisioned();
32 }
33 }
34
35 public static function provisionAll($db): array {
36 $result = array('folders' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'errors' => array());
37 if (!is_object($db)) {
38 $result['errors'][] = 'Keine DB-Verbindung.';
39 return $result;
40 }
41
42 $helpFolderId = self::ensureHelpFolder($db, $result);
43 if ($helpFolderId <= 0) {
44 return $result;
45 }
46
47 $help = new \dbx\dbxAdmin\dbxAdminHelp();
48
49 $contextHelp = new dbxContentContextHelp();
50 $tplDir = dirname(__DIR__, 2) . '/dbxAdmin/tpl/htm/';
52 $sorter = self::nextContentSorter($db, $helpFolderId);
53
54 foreach ($help->topics() as $topic => $meta) {
55 if (!is_array($meta)) {
56 continue;
57 }
58
59 $permalink = $contextHelp->permalinkForTopic((string) $topic);
60 if ($permalink === '') {
61 $result['errors'][] = 'Permalink fuer Topic ' . $topic . ' leer.';
62 continue;
63 }
64
65 $tplName = trim((string) ($meta['tpl'] ?? ''));
66 $title = trim((string) ($meta['title'] ?? $topic));
67 $content = self::loadTemplateHtml($tplDir, $tplName);
68 if ($content === '') {
69 $result['errors'][] = 'Template fehlt: ' . $tplName;
70 continue;
71 }
72
73 $existing = $db->select1($dd, array('permalink' => $permalink), 'id,content', 0);
74 $groupRead = in_array($topic, array('consent_privacy', 'impressum'), true) ? '*' : 'admin';
75 $pageData = self::pageData($helpFolderId, $title, $permalink, $content, $sorter, $groupRead);
76
77 if (is_array($existing) && (int) ($existing['id'] ?? 0) > 0) {
78 $id = (int) $existing['id'];
79 unset($pageData['sorter']);
80 if ($db->update($dd, $pageData, $id, 0, 1, 1, 0) === 1) {
82 self::syncPermalinkIndex($db, $id, $permalink, $groupRead);
83 $result['updated']++;
84 } else {
85 $result['errors'][] = 'Update fehlgeschlagen: ' . $permalink;
86 }
87 continue;
88 }
89
90 if ($db->insert($dd, $pageData, 0, 1, 0, 0) !== 1) {
91 $result['errors'][] = 'Insert fehlgeschlagen: ' . $permalink;
92 continue;
93 }
94
95 $id = (int) $db->get_insert_id();
96 if ($id <= 0) {
97 $result['errors'][] = 'Keine ID nach Insert: ' . $permalink;
98 continue;
99 }
100
102 self::syncPermalinkIndex($db, $id, $permalink, $groupRead);
103 $result['created']++;
104 $sorter = self::nextSorterAfter($sorter);
105 }
106
109 }
110
111 return $result;
112 }
113
114 private static function markProvisioned(): void {
115 $config = dbx()->get_config('dbxContent');
116 if (!is_array($config)) {
117 $config = array();
118 }
119 $config[self::CONFIG_KEY] = self::PROVISION_VERSION;
120 dbx()->set_config('dbxContent', $config);
121 }
122
123 private static function ensureHelpFolder($db, array &$result): int {
124 $outsideId = self::findFolderByName($db, 'outside', 0);
125 if ($outsideId <= 0) {
126 $outsideId = self::insertFolder($db, 'outside', 0, 'admin', $result);
127 }
128 if ($outsideId <= 0) {
129 $result['errors'][] = 'Ordner outside konnte nicht angelegt werden.';
130 return 0;
131 }
132
133 $helpId = self::findFolderByName($db, 'help', $outsideId);
134 if ($helpId <= 0) {
135 $helpId = self::insertFolder($db, 'help', $outsideId, 'parent', $result);
136 }
137 if ($helpId <= 0) {
138 $result['errors'][] = 'Ordner help konnte nicht angelegt werden.';
139 return 0;
140 }
141
142 return $helpId;
143 }
144
145 private static function findFolderByName($db, string $name, int $parentId): int {
146 $dd = dbxContentLng::ddFolder();
147 $name = trim($name);
148 $parentId = (int) $parentId;
149 $where = "name = '" . str_replace("'", "''", $name) . "' AND parent_id = " . $parentId;
150 $rows = $db->select($dd, $where, 'id', 'id', 'ASC', '', 1, 0, 0);
151 if (!is_array($rows) || !isset($rows[0]['id'])) {
152 return 0;
153 }
154 return (int) $rows[0]['id'];
155 }
156
157 private static function insertFolder($db, string $name, int $parentId, string $groupRead, array &$result): int {
158 $parentId = (int) $parentId;
159 $sorter = self::nextFolderSorter($db, $parentId);
160 $data = array(
161 'name' => $name,
162 'parent_id' => $parentId,
163 'sorter' => $sorter,
164 'group_read' => $groupRead,
165 'template' => $parentId > 0 ? 'parent' : 'c-content',
166 'hero_template' => $parentId > 0 ? 'parent' : 'image-hero',
167 'hero_image_id' => 'parent',
168 'hero_margin_top' => 'parent',
169 'hero_height' => 'parent',
170 'hero_variant' => 'parent',
171 'hero_sticky' => 'parent',
172 'hero_scroll_layer' => 'parent',
173 );
174
175 if ($db->insert(dbxContentLng::ddFolder(), $data, 0, 1, 0, 0) !== 1) {
176 return 0;
177 }
178
179 $id = (int) $db->get_insert_id();
180 if ($id > 0) {
181 dbxContentLngSync::afterFolderSave($db, $id, true);
182 $result['folders']++;
183 }
184 return $id;
185 }
186
187 private static function pageData(int $folderId, string $title, string $permalink, string $content, string $sorter, string $groupRead = 'admin'): array {
188 return array(
189 'activ' => 1,
190 'folder' => $folderId,
191 'title' => substr($title, 0, 254),
192 'permalink' => substr($permalink, 0, 254),
193 'description' => '',
194 'keywords' => '',
195 'group_read' => $groupRead,
196 'sorter' => $sorter,
197 'template' => 'parent',
198 'hero_template' => 'parent',
199 'hero_image_id' => 'parent',
200 'hero_margin_top' => 'parent',
201 'hero_height' => 'parent',
202 'hero_variant' => 'parent',
203 'hero_sticky' => 'parent',
204 'hero_scroll_layer' => 'parent',
205 'gallery_template' => 'image-gallery',
206 'gallery_visible_count' => '3',
207 'gallery_image_size' => 'original',
208 'gallery_lightbox_width' => '100vw',
209 'gallery_overflow' => 'grid',
210 'gallery_click_behavior' => 'lightbox',
211 'content' => $content,
212 );
213 }
214
215 private static function loadTemplateHtml(string $tplDir, string $tplName): string {
216 $tplName = trim($tplName);
217 if ($tplName === '') {
218 return '';
219 }
220 $path = rtrim($tplDir, '/\\') . DIRECTORY_SEPARATOR . $tplName . '.htm';
221 if (!is_file($path) || !is_readable($path)) {
222 return '';
223 }
224 $html = file_get_contents($path);
225 return is_string($html) ? trim($html) : '';
226 }
227
228 private static function syncPermalinkIndex($db, int $cid, string $permalink, string $rights = 'admin'): void {
229 $cid = (int) $cid;
230 $permalink = dbxContentPermalinkIndex::normalizePermalink($permalink);
231 if ($cid <= 0 || $permalink === '') {
232 return;
233 }
234 dbxContentPermalinkIndex::upsertPage($cid, $permalink, $rights, 1);
235 }
236
237 private static function nextContentSorter($db, int $folderId): string {
238 $folderId = (int) $folderId;
239 $rows = $db->select(dbxContentLng::ddContent(), 'folder = ' . $folderId, 'sorter', 'sorter,id', 'DESC', '', 1, 0, 0);
240 $max = 0;
241 if (is_array($rows) && isset($rows[0]) && is_array($rows[0])) {
242 $max = (int) ($rows[0]['sorter'] ?? 0);
243 }
244 return sprintf('%04d', $max + 10);
245 }
246
247 private static function nextFolderSorter($db, int $parentId): string {
248 $parentId = (int) $parentId;
249 $rows = $db->select(dbxContentLng::ddFolder(), 'parent_id = ' . $parentId, 'sorter', 'sorter,id', 'DESC', '', 1, 0, 0);
250 $max = 0;
251 if (is_array($rows) && isset($rows[0]) && is_array($rows[0])) {
252 $max = (int) ($rows[0]['sorter'] ?? 0);
253 }
254 return sprintf('%04d', $max + 10);
255 }
256
257 private static function nextSorterAfter(string $sorter): string {
258 $num = (int) $sorter;
259 return sprintf('%04d', $num + 10);
260 }
261}
static ddContent(string $lng='')
static afterPageSave($db, int $id, bool $isNew=false)
$config['version']
Definition config.php:2
DBX schema administration.
if(! $db->connect_db_server($server)) $result