dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxKiWritingStyles.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxKi;
3
5
6 private const CONFIG_KEY = 'writing_styles';
7
8 public static function defaults(): array {
9 $path = dirname(__DIR__) . '/cfg/writing_styles.php';
10 $writing_styles = array();
11 if (is_file($path)) {
12 include $path;
13 }
14 return is_array($writing_styles) ? $writing_styles : array();
15 }
16
17 public static function all(): array {
18 $config = dbx()->get_config('dbxKi');
19 $stored = array();
20 if (is_array($config) && !empty($config[self::CONFIG_KEY])) {
21 $raw = $config[self::CONFIG_KEY];
22 if (is_string($raw)) {
23 $decoded = json_decode($raw, true);
24 $stored = is_array($decoded) ? $decoded : array();
25 } elseif (is_array($raw)) {
26 $stored = $raw;
27 }
28 }
29 if (!$stored) {
30 return self::defaults();
31 }
32 return self::normalize($stored);
33 }
34
35 public static function save(array $styles): void {
36 $normalized = self::normalize($styles);
37 if (!$normalized) {
38 throw new \InvalidArgumentException('Mindestens ein gueltiger Schreibstil erforderlich.');
39 }
40 $config = dbx()->get_config('dbxKi');
41 if (!is_array($config)) {
42 $config = array();
43 }
44 $config[self::CONFIG_KEY] = json_encode($normalized, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
45 dbx()->set_config('dbxKi', $config);
46 }
47
48 public static function resetToDefaults(): void {
49 $config = dbx()->get_config('dbxKi');
50 if (!is_array($config)) {
51 $config = array();
52 }
53 unset($config[self::CONFIG_KEY]);
54 dbx()->set_config('dbxKi', $config);
55 }
56
57 public static function prompt(string $key): string {
58 $all = self::all();
59 return (string) ($all[$key]['prompt'] ?? ($all['sachlich']['prompt'] ?? ''));
60 }
61
62 public static function label(string $key): string {
63 $all = self::all();
64 return (string) ($all[$key]['label'] ?? $key);
65 }
66
67 private static function normalize(array $styles): array {
68 $out = array();
69 foreach ($styles as $key => $meta) {
70 if (!is_array($meta)) {
71 continue;
72 }
73 $key = self::slugKey(is_string($key) ? $key : (string) ($meta['key'] ?? ''));
74 if ($key === '') {
75 continue;
76 }
77 $label = trim((string) ($meta['label'] ?? ''));
78 $prompt = trim((string) ($meta['prompt'] ?? ''));
79 if ($label === '' || $prompt === '') {
80 continue;
81 }
82 $out[$key] = array('label' => $label, 'prompt' => $prompt);
83 }
84 return $out;
85 }
86
87 public static function slugKey(string $key): string {
88 $key = strtolower(trim($key));
89 $key = preg_replace('/[^a-z0-9_-]+/', '_', $key);
90 return trim((string) $key, '_');
91 }
92
93 public static function parseFormRows(array $keys, array $labels, array $prompts): array {
94 $styles = array();
95 $count = max(count($keys), count($labels), count($prompts));
96 for ($i = 0; $i < $count; $i++) {
97 $styles[] = array(
98 'key' => (string) ($keys[$i] ?? ''),
99 'label' => (string) ($labels[$i] ?? ''),
100 'prompt' => (string) ($prompts[$i] ?? ''),
101 );
102 }
103 $map = array();
104 foreach ($styles as $row) {
105 $key = self::slugKey((string) ($row['key'] ?? ''));
106 if ($key === '') {
107 continue;
108 }
109 $map[$key] = array(
110 'label' => trim((string) ($row['label'] ?? '')),
111 'prompt' => trim((string) ($row['prompt'] ?? '')),
112 );
113 }
114 return self::normalize($map);
115 }
116}
static parseFormRows(array $keys, array $labels, array $prompts)
$config['version']
Definition config.php:2
DBX schema administration.
$writing_styles
Pflegbare Schreibstil-Vorgaben fuer KI-Auftraege (dbxKi Briefing).