dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
Loading...
Searching...
No Matches
dbxEdit_fd.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxAdmin;
3
11class dbxEdit_fd
12{
13 private $_admin_modul = 'dbxAdmin';
14 private $_fd_field = 'dbxAdmin|ddedit-field';
15 private $_texts = null;
16
25 private function texts()
26 {
27 if ($this->_texts instanceof \dbxForm) {
28 return $this->_texts;
29 }
30
31 dbx()->get_system_obj('dbxForm', 'use');
32 $this->_texts = new \dbxForm();
33 $this->_texts->set_form_help_enabled(false);
34 $this->_texts->_fd = $this->_fd_field;
35 $this->_texts->load_fd_messages();
36
37 return $this->_texts;
38 }
39
40 public function run()
41 {
42 $work = dbx()->get_modul_var('dbx_run2', '');
43
44 switch ($work) {
45 case 'create_form_fd':
46 return $this->create_form_fd();
47
48 case 'delete_field':
49 return $this->delete_field();
50
51 case 'save_field_order':
52 return $this->save_field_order();
53
54 case 'create_from_dd':
55 return $this->create_from_dd();
56
57 case '':
58 case 'editor':
59 default:
60 return $this->run_editor();
61 }
62 }
63
64 private function run_editor()
65 {
66 $texts = $this->texts();
67 list($modul, $fd) = $this->fd_params_from_request();
68
69 if (!$modul) {
70 return $this->alert('warning', $texts->get_fd_message('fd_module_missing'));
71 }
72
73 if (!$fd) {
74 return $this->create_from_dd();
75 }
76
77 $model = $this->load_model($modul, $fd);
78 if (!$model) {
79 return $this->alert(
80 'danger',
81 $texts->format_fd_message(
82 'fd_not_readable',
83 array('fd' => dbx()->esc($modul . '|' . $fd))
84 )
85 );
86 }
87
88 $instance_id = $this->instance_id($modul . '_' . $fd);
89 $work_target_id = 'dbx_fdedit_work_' . $instance_id;
90 $fields = array_values((array)($model['fields'] ?? array()));
91 $work_content = count($fields)
92 ? $this->create_form_fd($modul, $fd, '0', $model)
93 : $this->create_form_fd($modul, $fd, 'new', $model);
94
95 $data = array(
96 'i' => $instance_id,
97 'modul' => $modul,
98 'fd' => $fd,
99 'message' => '',
100 'work_target_id' => $work_target_id,
101 'create_from_dd_url' => $this->build_url('create_from_dd', $modul, $fd),
102 'work_content' => $work_content,
103 'fields_order_report'=> $this->create_fields_order_report($modul, $fd, $model, $work_target_id),
104 );
105
106 $help = dbx()->get_include_obj('dbxAdminHelp', 'dbxAdmin');
107 $oTPL = dbx()->get_system_obj('dbxTPL');
108 $reloadAction = $oTPL->get_tpl('dbx|button-bar-reload-ajax', array(
109 'bar_reload_href' => '?dbx_modul=dbxAdmin&dbx_run1=edit_fd&modul=' . rawurlencode($modul) . '&fd=' . rawurlencode($fd),
110 'bar_reload_target' => 'dbx_fdedit_' . $instance_id,
111 'bar_reload_replace' => 'target',
112 ));
113 $barData = $help->moduleBarTemplateData('edit_fd', $reloadAction);
114 $barData['bar_title'] = $texts->format_fd_message(
115 'fd_frame_title',
116 array('fd' => dbx()->esc($modul . '|' . $fd))
117 );
118 $barData['bar_class'] = 'dbx-module-bar dbx-ddedit-head';
119 $data = array_merge($data, $barData);
120
121 return $oTPL->get_tpl($this->_admin_modul . '|fdedit-frame', $data);
122 }
123
124 private function create_form_fd($modul = '', $fd = '', $field_pos = null, $model = array())
125 {
126 $texts = $this->texts();
127 if (!$modul || !$fd) {
128 list($modul, $fd) = $this->fd_params_from_request();
129 }
130
131 if ($field_pos === null) {
132 $field_pos = dbx()->get_modul_var('field_pos', 'new');
133 }
134
135 if (!$model) {
136 $model = $this->load_model($modul, $fd);
137 }
138
139 if (!$model) {
140 return $this->alert(
141 'danger',
142 $texts->format_fd_message(
143 'fd_not_found',
144 array('fd' => dbx()->esc($modul . '|' . $fd))
145 )
146 );
147 }
148
149 $fields = array_values((array)($model['fields'] ?? array()));
150 $is_new = ((string)$field_pos === 'new');
151
152 if ($is_new) {
153 $data = $this->default_field_record();
154 } else {
155 $pos = (int)$field_pos;
156 if (!isset($fields[$pos]) || !is_array($fields[$pos])) {
157 return $this->alert(
158 'warning',
159 $texts->format_fd_message(
160 'fd_position_not_found',
161 array('position' => dbx()->esc((string)$field_pos))
162 )
163 );
164 }
165 $data = $fields[$pos];
166 }
167
168 $data['modul'] = $modul;
169 $data['dd'] = $fd;
170 $data['field_pos'] = (string)$field_pos;
171 $data['old_name'] = (string)($data['name'] ?? '');
172
173 $oForm = dbx()->get_system_obj('dbxForm');
174 $oForm->init('fdedit_field_' . $this->safe_id($modul . '_' . $fd . '_' . (string)$field_pos), 'ddedit-field-form');
175 $oForm->_fd = $this->_fd_field;
176 $oForm->load_fd_messages();
177 $oForm->_data = $data;
178 $oForm->_action = $this->build_url('create_form_fd', $modul, $fd, array('field_pos' => (string)$field_pos));
179 $oForm->_msg_info = $is_new
180 ? $oForm->format_fd_message(
181 'fd_edit_new',
182 array('fd' => dbx()->esc($fd))
183 )
184 : $oForm->format_fd_message(
185 'fd_edit_existing',
186 array(
187 'field' => dbx()->esc(
188 (string)($data['name'] ?? $field_pos)
189 ),
190 )
191 );
192 $oForm->add_flds();
193
194 if ($oForm->submit()) {
195 if (!$oForm->errors()) {
196 $field = $this->merge_record($data, $oForm->_post, $this->field_keys());
197 $field = $this->strip_editor_keys($field);
198
199 $message = '';
200 if (!$this->validate_field_record($field, $fields, $is_new ? -1 : (int)$field_pos, $message, $oForm)) {
201 $oForm->_msg_error = $message;
202 return $oForm->run();
203 }
204
205 if ($is_new) {
206 $fields[] = $field;
207 $field_pos = count($fields) - 1;
208 $is_new = false;
209 } else {
210 $fields[(int)$field_pos] = $field;
211 }
212
213 $model['fields'] = array_values($fields);
214 $ok = $this->save_model($modul, $fd, $model);
215
216 if ($ok) {
217 $field['modul'] = $modul;
218 $field['dd'] = $fd;
219 $field['field_pos'] = (string)$field_pos;
220 $field['old_name'] = (string)($field['name'] ?? '');
221
222 $oForm->_data = $field;
223 $oForm->_action = $this->build_url('create_form_fd', $modul, $fd, array('field_pos' => (string)$field_pos));
224 $oForm->_msg_success = $oForm->format_fd_message(
225 'fd_field_saved',
226 array(
227 'field' => dbx()->esc(
228 (string)($field['name'] ?? $field_pos)
229 ),
230 'fd' => dbx()->esc($fd),
231 )
232 );
233 } else {
234 $oForm->_msg_error = $oForm->format_fd_message(
235 'fd_field_save_error',
236 array(
237 'field' => dbx()->esc(
238 (string)($field['name'] ?? $field_pos)
239 ),
240 'fd' => dbx()->esc($fd),
241 )
242 );
243 }
244 } else {
245 $oForm->_msg_error = $oForm->format_fd_message(
246 'fd_field_check',
247 array(
248 'field' => dbx()->esc(
249 (string)($data['name'] ?? $field_pos)
250 ),
251 )
252 );
253 }
254 }
255
256 $delete_url = $this->build_url('delete_field', $modul, $fd, array('field_pos' => (string)$field_pos));
257
258 return str_replace(
259 '&dbx_run2=delete_field&modul={modul}&dd={dd}&field_pos={field_pos}',
260 dbx()->esc($delete_url),
261 $oForm->run()
262 );
263 }
264
265 private function create_fields_order_report($modul, $fd, $model, $target_id = '')
266 {
267 $rows = array();
268 foreach (array_values((array)($model['fields'] ?? array())) as $pos => $field) {
269 if (!is_array($field)) {
270 continue;
271 }
272
273 $row = $this->field_row_defaults();
274 foreach ($field as $key => $value) {
275 $row[$key] = is_array($value) ? implode(',', $value) : (string)$value;
276 }
277
278 $row['modul'] = $modul;
279 $row['dd'] = $fd;
280 $row['fd'] = $fd;
281 $row['field_pos'] = (string)$pos;
282 $row['sort_no'] = (string)($pos + 1);
283 $row['target_id'] = $target_id;
284 $row['form_url'] = $this->build_url('create_form_fd', $modul, $fd, array('field_pos' => (string)$pos));
285 $rows[] = $row;
286 }
287
288 $data = array(
289 'modul' => $modul,
290 'fd' => $fd,
291 'dd' => $fd,
292 'count' => count($rows),
293 'target_id' => $target_id,
294 'new_field_url' => $this->build_url('create_form_fd', $modul, $fd, array('field_pos' => 'new')),
295 );
296
297 $oReport = dbx()->get_system_obj('dbxReport');
298 $oReport->init('fdedit_fields_order_' . $this->safe_id($modul . '_' . $fd), 'fdedit-fields-order-report');
299 $oReport->_mode = 'tpl';
300 $oReport->_data = $data;
301 $oReport->_replaces = $data;
302 $oReport->_rdata = $rows;
303 $oReport->_rcount = count($rows);
304 $oReport->_rrows = 'auto';
305 $oReport->_pages = false;
306
307 return $oReport->run();
308 }
309
310 private function delete_field()
311 {
312 $texts = $this->texts();
313 list($modul, $fd) = $this->fd_params_from_request();
314 $field_pos = (int)dbx()->get_modul_var('field_pos', -1);
315
316 $model = $this->load_model($modul, $fd);
317 if (!$model) {
318 return $this->alert(
319 'danger',
320 $texts->format_fd_message(
321 'fd_not_found',
322 array('fd' => dbx()->esc($modul . '|' . $fd))
323 )
324 );
325 }
326
327 $fields = array_values((array)($model['fields'] ?? array()));
328 if (!isset($fields[$field_pos])) {
329 return $this->alert('warning', $texts->get_fd_message('fd_field_not_found'));
330 }
331
332 $name = (string)($fields[$field_pos]['name'] ?? $field_pos);
333 unset($fields[$field_pos]);
334 $model['fields'] = array_values($fields);
335
336 if ($this->save_model($modul, $fd, $model)) {
337 return $this->alert(
338 'success',
339 $texts->format_fd_message(
340 'fd_field_deleted',
341 array('field' => dbx()->esc($name))
342 )
343 );
344 }
345
346 return $this->alert(
347 'danger',
348 $texts->format_fd_message(
349 'fd_field_delete_error',
350 array('field' => dbx()->esc($name))
351 )
352 );
353 }
354
355 private function save_field_order()
356 {
357 $texts = $this->texts();
358 list($modul, $fd) = $this->fd_params_from_request();
359 $order = $this->parse_order(dbx()->get_modul_var('order', array()));
360
361 $model = $this->load_model($modul, $fd);
362 if (!$model) {
363 dbx()->json_response(array(
364 'ok' => 0,
365 'msg' => $texts->format_fd_message(
366 'fd_not_found',
367 array('fd' => $modul . '|' . $fd)
368 ),
369 ));
370 }
371
372 $new = $this->reorder_records(array_values((array)($model['fields'] ?? array())), $order);
373 if ($new === false) {
374 dbx()->json_response(array(
375 'ok' => 0,
376 'msg' => $texts->get_fd_message('fd_invalid_order'),
377 ));
378 }
379
380 $model['fields'] = $new;
381 $ok = $this->save_model($modul, $fd, $model);
382
383 dbx()->json_response(array(
384 'ok' => $ok ? 1 : 0,
385 'msg' => $texts->get_fd_message(
386 $ok ? 'fd_order_saved' : 'fd_order_save_error'
387 ),
388 'count' => count($new),
389 ));
390
391 return '';
392 }
393
401 private function create_from_dd()
402 {
403 $texts = $this->texts();
404 list($modul, $fd) = $this->fd_params_from_request();
405
406 $source_modul = $this->sanitize_name(dbx()->get_modul_var('source_modul', $modul ?: 'dbx'));
407 $source_dd = $this->sanitize_name(dbx()->get_modul_var('source_dd', ''));
408 $target_modul = $this->sanitize_name(dbx()->get_modul_var('target_modul', $modul ?: 'dbx'));
409 $target_fd = $this->sanitize_name(dbx()->get_modul_var('target_fd', $fd));
410 $overwrite = (string)dbx()->get_modul_var('overwrite', '') === '1';
411
412 $oForm = dbx()->get_system_obj('dbxForm');
413 $oForm->init(
414 'fd-create-from-dd-' . $this->safe_id($target_modul . '-' . $target_fd),
415 'fdedit-create-from-dd'
416 );
417 $oForm->_fd = $this->_fd_field;
418 $oForm->load_fd_messages();
419 $oForm->_action = '?dbx_modul=' . $this->_admin_modul .
420 '&dbx_run1=edit_fd&dbx_run2=create_from_dd&modul=' . rawurlencode($target_modul) .
421 '&fd=' . rawurlencode($target_fd);
422 // init() legt den Security-Wert in _data ab; deshalb Vorgaben ergänzen
423 // und den von dbxForm erzeugten Wert nicht durch ein neues Array ersetzen.
424 $oForm->_data = array_merge($oForm->_data, array(
425 'source_modul' => $source_modul,
426 'source_dd' => $source_dd,
427 'target_modul' => $target_modul,
428 'target_fd' => $target_fd,
429 'overwrite' => $overwrite ? 1 : 0,
430 ));
431 $oForm->_msg_info = $oForm->get_fd_message('create_info');
432
433 $oForm->add_fld(
434 'source_modul',
435 'text-label',
436 label: $oForm->get_fd_message('label_source_module'),
437 rules: 'parameter|min=1|max=64',
438 errormsg: $oForm->get_fd_message('error_source_module'),
439 dd: ''
440 );
441 $oForm->add_fld(
442 'source_dd',
443 'text-label',
444 label: $oForm->get_fd_message('label_source_dd'),
445 rules: 'parameter|min=1|max=64',
446 errormsg: $oForm->get_fd_message('error_source_dd'),
447 dd: ''
448 );
449 $oForm->add_fld(
450 'target_modul',
451 'text-label',
452 label: $oForm->get_fd_message('label_target_module'),
453 rules: 'parameter|min=1|max=64',
454 errormsg: $oForm->get_fd_message('error_target_module'),
455 dd: ''
456 );
457 $oForm->add_fld(
458 'target_fd',
459 'text-label',
460 label: $oForm->get_fd_message('label_target_fd'),
461 rules: 'parameter|min=1|max=64',
462 errormsg: $oForm->get_fd_message('error_target_fd'),
463 dd: ''
464 );
465 $oForm->add_fld(
466 'overwrite',
467 'checkbox-label',
468 label: $oForm->get_fd_message('label_overwrite'),
469 rules: 'int',
470 dd: ''
471 );
472
473 if ($oForm->submit()) {
474 if ($oForm->errors()) {
475 $oForm->_msg_error = $oForm->get_fd_message('required_fields');
476 } else {
477 $source_modul = $this->sanitize_name($oForm->get_post('source_modul', '', 'parameter|min=1|max=64'));
478 $source_dd = $this->sanitize_name($oForm->get_post('source_dd', '', 'parameter|min=1|max=64'));
479 $target_modul = $this->sanitize_name($oForm->get_post('target_modul', '', 'parameter|min=1|max=64'));
480 $target_fd = $this->sanitize_name($oForm->get_post('target_fd', '', 'parameter|min=1|max=64'));
481 $overwrite = (int)$oForm->get_post('overwrite', 0, 'int') === 1;
482
483 if ($this->fd_file_exists($target_modul, $target_fd) && !$overwrite) {
484 $oForm->add_fld_error(
485 'overwrite',
486 $oForm->get_fd_message('target_exists_field')
487 );
488 $oForm->_msg_error = $oForm->get_fd_message('target_exists');
489 } else {
490 $oDD = dbx()->get_system_obj('dbxDD');
491 $dd_model = $oDD->get_dd_model($source_modul . '|' . $source_dd);
492 $fields = is_array($dd_model['fields'] ?? null) ? array_values($dd_model['fields']) : array();
493
494 if (!count($fields)) {
495 $oForm->add_fld_error(
496 'source_dd',
497 $oForm->get_fd_message('source_no_fields_field')
498 );
499 $oForm->_msg_error = $oForm->get_fd_message('source_invalid');
500 } elseif ($this->save_model($target_modul, $target_fd, array('fields' => $fields))) {
501 $url = '?dbx_modul=' . $this->_admin_modul .
502 '&dbx_run1=edit_fd&modul=' . rawurlencode($target_modul) .
503 '&fd=' . rawurlencode($target_fd);
504 $oForm->_msg_success =
505 $oForm->format_fd_message(
506 'fd_created',
507 array(
508 'fd' => dbx()->esc($target_modul . '|' . $target_fd),
509 'dd' => dbx()->esc($source_modul . '|' . $source_dd),
510 )
511 ) . ' ' .
512 '<a class="btn btn-sm btn-primary ms-2" href="' . dbx()->esc($url) .
513 '">' . $oForm->get_fd_message('edit_fd_action') . '</a>';
514 } else {
515 $oForm->_msg_error = $oForm->get_fd_message('fd_write_error');
516 $oForm->add_fld_error(
517 'target_fd',
518 $oForm->get_fd_message('fd_write_field_error')
519 );
520 }
521 }
522 }
523 }
524
525 return $oForm->run();
526 }
527
528 private function load_model($modul, $fd)
529 {
530 if (!$modul || !$fd) {
531 return array();
532 }
533
534 $file = $this->fd_file_path($modul, $fd);
535 if (!is_file($file) || !is_readable($file)) {
536 return array();
537 }
538
539 $fields = array();
540 $field = array();
541 $messages = array();
542 include $file;
543
544 return array(
545 'fields' => is_array($fields) ? array_values($fields) : array(),
546 'messages' => $this->normalize_fd_messages(
547 is_array($messages) ? $messages : array(),
548 $fd
549 ),
550 );
551 }
552
553 private function save_model($modul, $fd, $model)
554 {
555 if (!$modul || !$fd || !is_array($model)) {
556 return 0;
557 }
558
559 $fields = is_array($model['fields'] ?? null) ? array_values($model['fields']) : array();
560 $messages = $this->normalize_fd_messages(
561 is_array($model['messages'] ?? null) ? $model['messages'] : array(),
562 $fd
563 );
564 $dir = dbx()->get_base_dir() . 'dbx/modules/' . $modul . '/fd/';
565 $dir = dbx()->os_path($dir);
566
567 if (!is_dir($dir) && !mkdir($dir, 0775, true)) {
568 return 0;
569 }
570
571 $file = $this->fd_file_path($modul, $fd);
572 $this->backup_fd_file($modul, $fd);
573
574 $content = "<?php\n\n";
575 $content .= "\$messages = array();\n";
576 $content .= "\$messages['save_success'] = "
577 . var_export($messages['save_success'], true) . ";\n";
578 $content .= "\$messages['save_succeass'] = \$messages['save_success'];\n";
579 $content .= "\$messages['save_error'] = "
580 . var_export($messages['save_error'], true) . ";\n\n";
581 foreach ($messages as $messageKey => $messageValue) {
582 if (
583 in_array(
584 $messageKey,
585 array('save_success', 'save_succeass', 'save_error'),
586 true
587 ) ||
588 !is_scalar($messageValue)
589 ) {
590 continue;
591 }
592 $content .= "\$messages[" . var_export((string)$messageKey, true) . '] = '
593 . var_export((string)$messageValue, true) . ";\n";
594 }
595 if (count($messages) > 3) {
596 $content .= "\n";
597 }
598 foreach ($fields as $field) {
599 if (!is_array($field)) {
600 continue;
601 }
602 $field = $this->normalize_field_for_write($field);
603 $content .= "\$field = array();\n";
604 foreach ($field as $key => $value) {
605 $content .= "\$field[" . var_export((string)$key, true) . "]=" . var_export($value, true) . ";\n";
606 }
607 $content .= "\$fields[]=\$field;\n\n";
608 }
609
610 return file_put_contents($file, $content) !== false ? 1 : 0;
611 }
612
620 private function normalize_fd_messages(array $messages, string $fd): array
621 {
622 $language = 'de';
623 if (preg_match('/_en$/', $fd)) {
624 $language = 'en';
625 } elseif (preg_match('/_es$/', $fd)) {
626 $language = 'es';
627 }
628
629 $defaults = array(
630 'de' => array(
631 'save_success' => 'Daten wurden gespeichert',
632 'save_error' => 'Daten konnten nicht gespeichert werden',
633 ),
634 'en' => array(
635 'save_success' => 'Data was saved',
636 'save_error' => 'Data could not be saved',
637 ),
638 'es' => array(
639 'save_success' => 'Los datos se guardaron',
640 'save_error' => 'Los datos no se pudieron guardar',
641 ),
642 );
643
644 if (
645 !isset($messages['save_success']) &&
646 isset($messages['save_succeass'])
647 ) {
648 $messages['save_success'] = $messages['save_succeass'];
649 }
650
651 foreach ($defaults[$language] as $key => $value) {
652 if (!isset($messages[$key]) || !is_scalar($messages[$key])) {
653 $messages[$key] = $value;
654 } else {
655 $messages[$key] = (string)$messages[$key];
656 }
657 }
658 $messages['save_succeass'] = $messages['save_success'];
659
660 return $messages;
661 }
662
663 private function normalize_field_for_write($field)
664 {
665 $field = $this->strip_editor_keys($field);
666 $oDD = dbx()->get_system_obj('dbxDD');
667 if (is_object($oDD) && method_exists($oDD, 'normalize_dd_field')) {
668 return $oDD->normalize_dd_field($field);
669 }
670
671 $out = array();
672 foreach ($this->field_keys() as $key) {
673 if (array_key_exists($key, $field)) {
674 $out[$key] = $field[$key];
675 }
676 }
677 return $out;
678 }
679
680 private function backup_fd_file($modul, $fd)
681 {
682 $file = $this->fd_file_path($modul, $fd);
683 if (!is_file($file)) {
684 return;
685 }
686
687 $dir = dirname($file) . DIRECTORY_SEPARATOR . '_backup';
688 if (!is_dir($dir)) {
689 mkdir($dir, 0775, true);
690 }
691
692 if (is_dir($dir)) {
693 copy($file, $dir . DIRECTORY_SEPARATOR . $fd . '.' . date('Ymd-His') . '.fd.php');
694 }
695 }
696
697 private function fd_params_from_request()
698 {
699 $modul = $this->sanitize_name(dbx()->get_modul_var('modul', ''));
700 $fd = $this->sanitize_name(dbx()->get_modul_var('fd', ''));
701
702 if (!$fd) {
703 $fd = $this->sanitize_name(dbx()->get_modul_var('dd', ''));
704 }
705
706 if (!$modul) {
707 $modul = $this->sanitize_name(dbx()->get_modul_var('xmodul', ''));
708 }
709
710 if (!$modul) {
711 $modul = $this->sanitize_name($this->get_system_var('dbx_activ_modul', 'dbx'));
712 }
713
714 return array($modul, $fd);
715 }
716
717 private function fd_file_exists($modul, $fd)
718 {
719 return is_file($this->fd_file_path($modul, $fd));
720 }
721
722 private function fd_file_path($modul, $fd)
723 {
724 $file = dbx()->get_base_dir() . 'dbx/modules/' . $modul . '/fd/' . $fd . '.fd.php';
725 return dbx()->os_path($file);
726 }
727
728 private function build_url($run2, $modul, $fd, $extra = array())
729 {
730 $url = '?dbx_modul=' . $this->_admin_modul .
731 '&dbx_run1=edit_fd' .
732 '&dbx_run2=' . rawurlencode($run2) .
733 '&modul=' . rawurlencode($modul) .
734 '&fd=' . rawurlencode($fd);
735
736 foreach ((array)$extra as $key => $value) {
737 $url .= '&' . rawurlencode((string)$key) . '=' . rawurlencode((string)$value);
738 }
739
740 return $url;
741 }
742
743 private function get_system_var($name, $default = '')
744 {
745 if (function_exists('dbx')) {
746 $obj = dbx();
747 if (is_object($obj) && method_exists($obj, 'get_system_var')) {
748 $value = $obj->get_system_var($name);
749 if ($value !== null && $value !== '') {
750 return $value;
751 }
752 }
753 }
754 return $default;
755 }
756
757 private function merge_record($old, $post, $keys)
758 {
759 $record = is_array($old) ? $old : array();
760 foreach ((array)$keys as $key) {
761 if (array_key_exists($key, (array)$post)) {
762 $record[$key] = $this->normalize_value($post[$key]);
763 }
764 }
765 return $record;
766 }
767
768 private function strip_editor_keys($record)
769 {
770 unset($record['modul'], $record['dd'], $record['fd'], $record['field_pos'], $record['old_name']);
771 return $record;
772 }
773
774 private function normalize_value($value)
775 {
776 if (is_array($value)) {
777 return implode(',', array_map('trim', $value));
778 }
779 return trim((string)$value);
780 }
781
782 private function parse_order($raw)
783 {
784 if (is_array($raw)) {
785 return array_values(array_map('intval', $raw));
786 }
787
788 $raw = trim((string)$raw);
789 if ($raw === '') {
790 return array();
791 }
792
793 if (substr($raw, 0, 1) === '[') {
794 $decoded = json_decode($raw, true);
795 if (is_array($decoded)) {
796 return array_values(array_map('intval', $decoded));
797 }
798 }
799
800 $parts = preg_split('/[|,\s;]+/', $raw);
801 return is_array($parts) ? array_values(array_map('intval', $parts)) : array();
802 }
803
804 private function reorder_records($records, $order)
805 {
806 $records = array_values((array)$records);
807 $count = count($records);
808 if (!$count) {
809 return array();
810 }
811 if (count($order) !== $count) {
812 return false;
813 }
814
815 $seen = array();
816 $new = array();
817 foreach ($order as $pos) {
818 $pos = (int)$pos;
819 if ($pos < 0 || $pos >= $count || isset($seen[$pos])) {
820 return false;
821 }
822 $seen[$pos] = 1;
823 $new[] = $records[$pos];
824 }
825 return $new;
826 }
827
828 private function validate_field_record($field, $fields, $self_pos, &$message, $texts)
829 {
830 $name = trim((string)($field['name'] ?? ''));
831 if (!$this->is_identifier($name)) {
832 $message = $texts->format_fd_message(
833 'fd_invalid_field_name',
834 array('field' => dbx()->esc($name))
835 );
836 return false;
837 }
838
839 $names = array();
840 foreach (array_values((array)$fields) as $pos => $old) {
841 if ((int)$pos === (int)$self_pos) {
842 continue;
843 }
844
845 $old_name = strtolower(trim((string)($old['name'] ?? '')));
846 if ($old_name !== '') {
847 $names[$old_name] = 1;
848 }
849 }
850
851 if (isset($names[strtolower($name)])) {
852 $message = $texts->format_fd_message(
853 'fd_duplicate_field_name',
854 array('field' => dbx()->esc($name))
855 );
856 return false;
857 }
858
859 return true;
860 }
861
862 private function is_identifier($name)
863 {
864 return (bool)preg_match('/^[A-Za-z_][A-Za-z0-9_]*$/', (string)$name);
865 }
866
867 private function sanitize_name($name)
868 {
869 $name = trim((string)$name);
870 return preg_match('/^[A-Za-z0-9_-]+$/', $name) ? $name : '';
871 }
872
873 private function safe_id($value)
874 {
875 $value = preg_replace('/[^A-Za-z0-9_]+/', '_', (string)$value);
876 $value = trim($value, '_');
877 return $value ?: 'x';
878 }
879
880 private function instance_id($seed)
881 {
882 return $this->safe_id($seed . '_' . substr(md5((string)$seed), 0, 6));
883 }
884
885 private function alert($type, $msg)
886 {
887 $type = preg_replace('/[^a-z]/', '', (string)$type);
888 if (!$type) {
889 $type = 'info';
890 }
891 return '<div class="alert alert-' . $type . '">' . $msg . '</div>';
892 }
893
894 private function field_keys()
895 {
896 $oDD = dbx()->get_system_obj('dbxDD');
897 if (is_object($oDD) && method_exists($oDD, 'dd_field_schema_keys')) {
898 return array_merge(array('modul', 'dd', 'fd', 'field_pos', 'old_name'), $oDD->dd_field_schema_keys());
899 }
900
901 return array(
902 'modul',
903 'dd',
904 'fd',
905 'field_pos',
906 'old_name',
907 'name',
908 'type',
909 'index',
910 'length',
911 'default',
912 'label',
913 'rules',
914 'tooltip',
915 'errormsg',
916 'placeholder',
917 'convert',
918 'protect',
919 'group',
920 'mask',
921 'data',
922 'options',
923 'tpl',
924 'js',
925 'prompt',
926 );
927 }
928
929 private function default_field_record()
930 {
931 return array(
932 'name' => '',
933 'type' => 'varchar',
934 'index' => '',
935 'length' => '255',
936 'default' => '',
937 'label' => '',
938 'rules' => 'text',
939 'tooltip' => '',
940 'errormsg' => '',
941 'placeholder' => '',
942 'convert' => '',
943 'protect' => '0',
944 'group' => '',
945 'mask' => '',
946 'data' => '',
947 'options' => '',
948 'tpl' => 'text-label',
949 'js' => '',
950 'prompt' => '',
951 );
952 }
953
954 private function field_row_defaults()
955 {
956 return array(
957 'modul' => '',
958 'dd' => '',
959 'fd' => '',
960 'field_pos' => '',
961 'name' => '',
962 'type' => '',
963 'index' => '',
964 'length' => '',
965 'label' => '',
966 );
967 }
968}
969
970?>
$messages
Definition config.fd.php:2
dbx()
Liefert die zentrale dbXapp-API als Singleton.
Definition dbxApi.php:2805
esc($value)
Escaped einen Wert fuer HTML-Text und HTML-Attribute.
Definition dbxApi.php:2310
foreach(array('bootstrapRowColumns', 'setBootstrapColumnLayout', 'addBootstrapColumn', 'dissolveBootstrapColumns',) as $function) $keys
foreach( $valid as $email) foreach($invalid as $email) $fd