dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
Loading...
Searching...
No Matches
dbxKiCmsHelpProvision.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxKi;
3
4use dbx\dbxContent\dbxContentLng;
5use dbx\dbxContent\dbxContentLngSync;
6use dbx\dbxContent\dbxContentPageCache;
7use dbx\dbxContent\dbxContentPermalinkIndex;
8
9require_once dirname(__DIR__, 2) . '/dbxContent/include/dbxContent_bootstrap_sync.php';
10
12
13 public const PERMALINK = 'dbxki-anleitung-content-seite-mit-ki';
14 public const TITLE = 'Content-Seite mit ChatGPT oder DeepSeek erstellen';
15 public const TPL = 'cms-anleitung-ki-content-seite';
16 public const CONFIG_KEY = 'cms_anleitung_provision_version';
17 public const PROVISION_VERSION = 2;
18
19 public static function run(): void {
20 $version = (int) dbx()->get_config('dbxKi', self::CONFIG_KEY, 0);
21 if ($version >= self::PROVISION_VERSION) {
22 return;
23 }
25 if (empty($result['errors'])) {
26 self::markProvisioned();
27 }
28 }
29
30 public static function provision(): array {
31 $result = array('created' => 0, 'updated' => 0, 'folder_id' => 0, 'page_id' => 0, 'permalink' => self::PERMALINK, 'errors' => array());
32
33 $db = dbx()->get_system_obj('dbxDB');
34 if (!is_object($db) || !$db->connect_db_server('dbx|dbxContent.db3')) {
35 $result['errors'][] = 'CMS-Datenbank nicht erreichbar.';
36 return $result;
37 }
38
39 dbxContentLngSync::ensureSchema($db);
40
41 $folderId = self::ensureHelpFolder($db, $result);
42 if ($folderId <= 0) {
43 return $result;
44 }
45 $result['folder_id'] = $folderId;
46
47 $content = self::loadTemplateHtml();
48 if ($content === '') {
49 $result['errors'][] = 'Anleitungs-Template fehlt: ' . self::TPL . '.htm';
50 return $result;
51 }
52
53 $cms = dbx()->get_include_obj('dbxKiCmsService', 'dbxKi');
54 $lng = dbxContentLng::current();
55 $dd = dbxContentLng::ddContent($lng);
56 $existing = $db->select1($dd, array('permalink' => self::PERMALINK), 'id', 0);
57 $existingId = is_array($existing) ? (int) ($existing['id'] ?? 0) : 0;
58
59 try {
60 if ($existingId > 0) {
61 $params = array(
62 'lng' => $lng,
63 'id' => $existingId,
64 'content' => $content,
65 'title' => self::TITLE,
66 'group_read' => 'admin',
67 );
68 $plan = $cms->bundleBuildPlan('page.update', $params);
69 $exec = $cms->bundleExecutePlan('page.update', $params, $plan);
70 $result['updated'] = 1;
71 $result['page_id'] = (int) ($exec['id'] ?? $existingId);
72 } else {
73 $params = array(
74 'lng' => $lng,
75 'folder_id' => $folderId,
76 'title' => self::TITLE,
77 'permalink' => self::PERMALINK,
78 'template' => 'parent',
79 'activ' => 1,
80 'group_read' => 'admin',
81 'description' => 'Schritt-fuer-Schritt-Anleitung: Neue CMS-Content-Seite mit ChatGPT, DeepSeek und dbxKi-Bundle erstellen.',
82 'keywords' => 'dbxKi, ChatGPT, DeepSeek, CMS, Bundle, KI',
83 'content' => $content,
84 );
85 $plan = $cms->bundleBuildPlan('page.create', $params);
86 $exec = $cms->bundleExecutePlan('page.create', $params, $plan);
87 $result['created'] = 1;
88 $result['page_id'] = (int) ($exec['id'] ?? 0);
89 }
90 } catch (\Throwable $e) {
91 $result['errors'][] = $e->getMessage();
92 return $result;
93 }
94
95 if ($result['page_id'] > 0) {
96 dbxContentPermalinkIndex::upsertPage($result['page_id'], self::PERMALINK, 'admin', 1, $lng);
97 }
98
99 dbxContentPageCache::invalidateAll();
100
101 return $result;
102 }
103
104 public static function pageUrl(): string {
105 return self::PERMALINK;
106 }
107
108 private static function markProvisioned(): void {
109 $config = dbx()->get_config('dbxKi');
110 if (!is_array($config)) {
111 $config = array();
112 }
113 $config[self::CONFIG_KEY] = self::PROVISION_VERSION;
114 dbx()->set_config('dbxKi', $config);
115 }
116
117 private static function loadTemplateHtml(): string {
118 $path = dirname(__DIR__) . '/tpl/htm/' . self::TPL . '.htm';
119 if (!is_file($path) || !is_readable($path)) {
120 return '';
121 }
122 $html = file_get_contents($path);
123 return is_string($html) ? trim($html) : '';
124 }
125
126 private static function ensureHelpFolder($db, array &$result): int {
127 $outsideId = self::findFolderByName($db, 'outside', 0);
128 if ($outsideId <= 0) {
129 $outsideId = self::insertFolder($db, 'outside', 0, 'admin');
130 }
131 if ($outsideId <= 0) {
132 $result['errors'][] = 'Ordner outside konnte nicht angelegt werden.';
133 return 0;
134 }
135
136 $helpId = self::findFolderByName($db, 'help', $outsideId);
137 if ($helpId <= 0) {
138 $helpId = self::insertFolder($db, 'help', $outsideId, 'admin');
139 }
140 if ($helpId <= 0) {
141 $result['errors'][] = 'Ordner help konnte nicht angelegt werden.';
142 return 0;
143 }
144
145 return $helpId;
146 }
147
148 private static function findFolderByName($db, string $name, int $parentId): int {
149 $dd = dbxContentLng::ddFolder();
150 $where = "name = '" . str_replace("'", "''", trim($name)) . "' AND parent_id = " . (int) $parentId;
151 $rows = $db->select($dd, $where, 'id', 'id', 'ASC', '', 1, 0, 0);
152 if (!is_array($rows) || !isset($rows[0]['id'])) {
153 return 0;
154 }
155 return (int) $rows[0]['id'];
156 }
157
158 private static function insertFolder($db, string $name, int $parentId, string $groupRead): int {
159 $cms = dbx()->get_include_obj('dbxKiCmsService', 'dbxKi');
160 $params = array(
161 'lng' => dbxContentLng::current(),
162 'name' => $name,
163 'parent_id' => $parentId,
164 'group_read' => $groupRead,
165 'template' => $parentId > 0 ? 'parent' : 'c-content',
166 );
167 $plan = $cms->bundleBuildPlan('folder.create', $params);
168 $exec = $cms->bundleExecutePlan('folder.create', $params, $plan);
169 return (int) ($exec['id'] ?? 0);
170 }
171}
if($resolved !==$expectedBase . 'files/test/') $config
DBX schema administration.