dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxEdit_dd.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxAdmin;
3
44{
49 private $_admin_modul = 'dbxAdmin';
50
55 private $_fd_table = 'dbxAdmin|ddedit-table';
56
61 private $_fd_field = 'dbxAdmin|ddedit-field';
62
67 private $_fd_index = 'dbxAdmin|ddedit-index';
68
74 public function run()
75 {
76 $work = dbx()->get_modul_request_var('dbx_run2', '');
77
78 switch ($work) {
79
80 case 'create_form_table':
81 return $this->create_form_table();
82
83 case 'create_form_dd':
84 return $this->create_form_dd();
85
86 case 'create_form_index':
87 return $this->create_form_index();
88
89 case 'delete_field':
90 return $this->delete_field();
91
92 case 'delete_index':
93 return $this->delete_index();
94
95 case 'save_field_order':
96 return $this->save_field_order();
97
98 case 'save_index_order':
99 return $this->save_index_order();
100
101 case '':
102 case 'editor':
103 default:
104 return $this->run_editor();
105 }
106 }
107
108
114 private function run_editor()
115 {
116 list($modul, $dd) = $this->dd_params_from_request();
117
118 if (!$modul || !$dd) {
119 return $this->alert('warning', 'Kein DD angegeben. Erwartet: modul und dd.');
120 }
121
122 $model = $this->load_model($modul, $dd);
123 if (!$model) {
124 return $this->alert('danger', 'DD nicht gefunden oder nicht lesbar: ' . dbx()->esc($modul . '|' . $dd));
125 }
126
127 $instance_id = $this->instance_id($modul . '_' . $dd);
128 $work_target_id = 'dbx_ddedit_work_' . $instance_id;
129 $work_content = $this->create_form_table($modul, $dd, $model);
130 $fields_order_report = $this->create_fields_order_report($modul, $dd, $model, $work_target_id);
131 $indexes_report = $this->create_indexes_report($modul, $dd, $model, $work_target_id);
132
133 $data = array(
134 'i' => $instance_id,
135 'modul' => $modul,
136 'dd' => $dd,
137 'path' => $this->dd_file_path($modul, $dd),
138 'message' => '',
139 'work_target_id' => $work_target_id,
140 'table_url' => $this->build_url('create_form_table', $modul, $dd),
141 'new_field_url' => $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => 'new')),
142 'new_index_url' => $this->build_url('create_form_index', $modul, $dd, array('index_pos' => 'new')),
143 'work_content' => $work_content,
144 'fields_order_report' => $fields_order_report,
145 'indexes_report' => $indexes_report,
146 );
147
148 $help = dbx()->get_include_obj('dbxAdminHelp', 'dbxAdmin');
149 $oTPL = dbx()->get_system_obj('dbxTPL');
150 $reloadAction = $oTPL->get_tpl('dbx|button-bar-reload-ajax', array(
151 'bar_reload_href' => '?dbx_modul=dbxAdmin&dbx_run1=edit_dd&modul=' . rawurlencode($modul) . '&dd=' . rawurlencode($dd),
152 'bar_reload_target' => 'dbx_ddedit_' . $instance_id,
153 'bar_reload_replace' => 'target',
154 ));
155 $barData = $help->moduleBarTemplateData('edit_dd', $reloadAction);
156 $barData['bar_title'] = 'DD bearbeiten: ' . $modul . '|' . $dd;
157 $barData['bar_class'] = 'dbx-module-bar dbx-ddedit-head';
158 $data = array_merge($data, $barData);
159
160 return $oTPL->get_tpl($this->_admin_modul . '|ddedit-frame', $data);
161 }
162
172 private function create_form_table($modul = '', $dd = '', $model = array())
173 {
174 if (!$modul || !$dd) {
175 list($modul, $dd) = $this->dd_params_from_request();
176 }
177
178 if (!$model) {
179 $model = $this->load_model($modul, $dd);
180 }
181
182 if (!$model) {
183 return $this->alert('danger', 'DD nicht gefunden: ' . dbx()->esc($modul . '|' . $dd));
184 }
185
186 $data = is_array($model['table'] ?? null) ? $model['table'] : array();
187 $data['modul'] = $modul;
188 $data['dd'] = $dd;
189
190 $oForm = dbx()->get_system_obj('dbxForm');
191 $oForm->init('ddedit_table_' . $this->safe_id($modul . '_' . $dd), 'ddedit-table-form');
192 $oForm->_fd = $this->_fd_table;
193 $oForm->_data = $data;
194 $oForm->_action = $this->build_url('create_form_table', $modul, $dd);
195 $oForm->_msg_info = 'Bearbeite Table Daten von DD ' . $dd . '.';
196 $oForm->add_flds();
197 $this->apply_table_right_fields($oForm, $data);
198
199 if ($oForm->submit()) {
200
201 if (!$oForm->errors()) {
202
203 $table = $this->merge_record($data, $oForm->_post, $this->table_keys());
204 $table['datadic'] = $dd;
205
206 $model['table'] = $table;
207
208 $ok = $this->save_model($modul, $dd, $model);
209
210 if ($ok) {
211 $oForm->_data = array_merge($table, array('modul' => $modul, 'dd' => $dd));
212 $oForm->_msg_success = 'Table Daten von DD ' . $dd . ' gespeichert.';
213 } else {
214 $oForm->_msg_error = 'Table Daten von DD ' . $dd . ' konnten nicht gespeichert werden.';
215 }
216
217 } else {
218 $oForm->_msg_error = 'Table Daten von DD ' . $dd . ' bitte pruefen.';
219 }
220 }
221
222 return $oForm->run();
223 }
224
235 private function create_form_dd($modul = '', $dd = '', $field_pos = null, $model = array())
236 {
237 if (!$modul || !$dd) {
238 list($modul, $dd) = $this->dd_params_from_request();
239 }
240
241 if ($field_pos === null) {
242 $field_pos = dbx()->get_modul_request_var('field_pos', 'new');
243 }
244
245 if (!$model) {
246 $model = $this->load_model($modul, $dd);
247 }
248
249 if (!$model) {
250 return $this->alert('danger', 'DD nicht gefunden: ' . dbx()->esc($modul . '|' . $dd));
251 }
252
253 $fields = array_values((array)($model['fields'] ?? array()));
254 $is_new = ((string)$field_pos === 'new');
255
256 if ($is_new) {
257 $data = $this->default_field_record();
258 } else {
259 $pos = (int)$field_pos;
260 if (!isset($fields[$pos]) || !is_array($fields[$pos])) {
261 return $this->alert('warning', 'Feldposition nicht gefunden: ' . dbx()->esc((string)$field_pos));
262 }
263
264 $data = $fields[$pos];
265 }
266
267 $data['modul'] = $modul;
268 $data['dd'] = $dd;
269 $data['field_pos'] = (string)$field_pos;
270 $data['old_name'] = (string)($data['name'] ?? '');
271
272 $form_id = 'ddedit_field_' . $this->safe_id($modul . '_' . $dd . '_' . (string)$field_pos);
273
274 $oForm = dbx()->get_system_obj('dbxForm');
275 $oForm->init($form_id, 'ddedit-field-form');
276 $oForm->_fd = $this->_fd_field;
277 $oForm->_data = $data;
278 $oForm->_action = $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => (string)$field_pos));
279 $oForm->_msg_info = $is_new
280 ? 'Bearbeite neues Feld fuer DD ' . $dd . '.'
281 : 'Bearbeite Feld ' . (string)($data['name'] ?? $field_pos) . '.';
282 $oForm->add_flds();
283
284 if ($oForm->submit()) {
285
286 if (!$oForm->errors()) {
287
288 $field = $this->merge_record($data, $oForm->_post, $this->field_keys());
289 $field = $this->strip_editor_keys($field);
290
291 $message = '';
292 if (!$this->validate_field_record($field, $fields, $is_new ? -1 : (int)$field_pos, $message)) {
293 $oForm->_msg_error = $message;
294 return $oForm->run();
295 }
296
297 if ($is_new) {
298 $fields[] = $field;
299 $field_pos = count($fields) - 1;
300 $is_new = false;
301 } else {
302 $fields[(int)$field_pos] = $field;
303 }
304
305 $model['fields'] = array_values($fields);
306
307 $ok = $this->save_model($modul, $dd, $model);
308
309 if ($ok) {
310 $field['modul'] = $modul;
311 $field['dd'] = $dd;
312 $field['field_pos'] = (string)$field_pos;
313 $field['old_name'] = (string)($field['name'] ?? '');
314
315 $oForm->_data = $field;
316 $oForm->_action = $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => (string)$field_pos));
317 $oForm->_msg_success = 'Feld ' . (string)($field['name'] ?? $field_pos) . ' gespeichert.';
318 } else {
319 $oForm->_msg_error = 'Feld ' . (string)($field['name'] ?? $field_pos) . ' konnte nicht gespeichert werden.';
320 }
321
322 } else {
323 $oForm->_msg_error = 'Feld ' . (string)($data['name'] ?? $field_pos) . ' bitte pruefen.';
324 }
325 }
326
327 $delete_url = $this->build_url('delete_field', $modul, $dd, array('field_pos' => (string)$field_pos));
328
329 return str_replace(
330 '&dbx_run2=delete_field&modul={modul}&dd={dd}&field_pos={field_pos}',
331 dbx()->esc($delete_url),
332 $oForm->run()
333 );
334 }
335
346 private function create_form_index($modul = '', $dd = '', $index_pos = null, $model = array())
347 {
348 if (!$modul || !$dd) {
349 list($modul, $dd) = $this->dd_params_from_request();
350 }
351
352 if ($index_pos === null) {
353 $index_pos = dbx()->get_modul_request_var('index_pos', 'new');
354 }
355
356 if (!$model) {
357 $model = $this->load_model($modul, $dd);
358 }
359
360 if (!$model) {
361 return $this->alert('danger', 'DD nicht gefunden: ' . dbx()->esc($modul . '|' . $dd));
362 }
363
364 $indexes = array_values((array)($model['indexes'] ?? array()));
365 $is_new = ((string)$index_pos === 'new');
366
367 if ($is_new) {
368 $data = $this->default_index_record();
369 } else {
370 $pos = (int)$index_pos;
371 if (!isset($indexes[$pos]) || !is_array($indexes[$pos])) {
372 return $this->alert('warning', 'Indexposition nicht gefunden: ' . dbx()->esc((string)$index_pos));
373 }
374
375 $data = $indexes[$pos];
376 }
377
378 $data['modul'] = $modul;
379 $data['dd'] = $dd;
380 $data['index_pos'] = (string)$index_pos;
381 $data['old_name'] = (string)($data['name'] ?? '');
382
383 $form_id = 'ddedit_index_' . $this->safe_id($modul . '_' . $dd . '_' . (string)$index_pos);
384
385 $oForm = dbx()->get_system_obj('dbxForm');
386 $oForm->init($form_id, 'ddedit-index-form');
387 $oForm->_fd = $this->_fd_index;
388 $oForm->_data = $data;
389 $oForm->_action = $this->build_url('create_form_index', $modul, $dd, array('index_pos' => (string)$index_pos));
390 $oForm->_msg_info = $is_new
391 ? 'Bearbeite neuen Index fuer DD ' . $dd . '.'
392 : 'Bearbeite Index ' . (string)($data['name'] ?? $index_pos) . '.';
393 $oForm->add_flds();
394
395 if ($oForm->submit()) {
396
397 if (!$oForm->errors()) {
398
399 $index = $this->merge_record($data, $oForm->_post, $this->index_keys());
400 $index = $this->strip_editor_keys($index);
401
402 $message = '';
403 if (!$this->validate_index_record($index, $indexes, $is_new ? -1 : (int)$index_pos, $message)) {
404 $oForm->_msg_error = $message;
405 return $oForm->run();
406 }
407
408 if ($is_new) {
409 $indexes[] = $index;
410 $index_pos = count($indexes) - 1;
411 $is_new = false;
412 } else {
413 $indexes[(int)$index_pos] = $index;
414 }
415
416 $model['indexes'] = array_values($indexes);
417
418 $ok = $this->save_model($modul, $dd, $model);
419
420 if ($ok) {
421 $index['modul'] = $modul;
422 $index['dd'] = $dd;
423 $index['index_pos'] = (string)$index_pos;
424 $index['old_name'] = (string)($index['name'] ?? '');
425
426 $oForm->_data = $index;
427 $oForm->_action = $this->build_url('create_form_index', $modul, $dd, array('index_pos' => (string)$index_pos));
428 $oForm->_msg_success = 'Index ' . (string)($index['name'] ?? $index_pos) . ' gespeichert.';
429 } else {
430 $oForm->_msg_error = 'Index ' . (string)($index['name'] ?? $index_pos) . ' konnte nicht gespeichert werden.';
431 }
432
433 } else {
434 $oForm->_msg_error = 'Index ' . (string)($data['name'] ?? $index_pos) . ' bitte pruefen.';
435 }
436 }
437
438 $delete_url = $this->build_url('delete_index', $modul, $dd, array('index_pos' => (string)$index_pos));
439
440 return str_replace(
441 '&dbx_run2=delete_index&modul={modul}&dd={dd}&index_pos={index_pos}',
442 dbx()->esc($delete_url),
443 $oForm->run()
444 );
445 }
446
456 private function create_fields_report($modul, $dd, $model)
457 {
458 $fields = array_values((array)($model['fields'] ?? array()));
459 $rows = array();
460
461 foreach ($fields as $pos => $field) {
462 if (!is_array($field)) {
463 continue;
464 }
465
466 $row = $this->field_row_defaults();
467 foreach ($field as $key => $value) {
468 $row[$key] = is_array($value) ? implode(',', $value) : (string)$value;
469 }
470
471 $row['modul'] = $modul;
472 $row['dd'] = $dd;
473 $row['field_pos'] = (string)$pos;
474
475 $rows[] = $row;
476 }
477
478 $data = array(
479 'modul' => $modul,
480 'dd' => $dd,
481 'count' => count($rows),
482 );
483
484 $oReport = dbx()->get_system_obj('dbxReport');
485 $oReport->init('ddedit_fields_' . $this->safe_id($modul . '_' . $dd), 'ddedit-fields-report');
486 $oReport->_mode = 'tpl';
487 $oReport->_data = $data;
488 $oReport->_replaces = $data;
489 $oReport->_rdata = $rows;
490 $oReport->_rcount = count($rows);
491 $oReport->_rrows = 'auto';
492 $oReport->_pages = false;
493
494 $oReport->add_obj(
495 'new_field_form',
496 'obj-value',
497 '[modul=dbxAdmin]dbx_run1=edit_dd&dbx_run2=create_form_dd&modul=' . $modul . '&dd=' . $dd . '&field_pos=new[/modul]'
498 );
499
500 $oReport->add_obj(
501 'field_form',
502 'obj-value',
503 '[modul=dbxAdmin]dbx_run1=edit_dd&dbx_run2=create_form_dd&modul={modul}&dd={dd}&field_pos={field_pos}[/modul]'
504 );
505
506 return $oReport->run();
507 }
508
518 private function create_fields_order_report($modul, $dd, $model, $target_id = '')
519 {
520 $fields = array_values((array)($model['fields'] ?? array()));
521 $rows = array();
522
523 foreach ($fields as $pos => $field) {
524 if (!is_array($field)) {
525 continue;
526 }
527
528 $row = $this->field_row_defaults();
529 foreach ($field as $key => $value) {
530 $row[$key] = is_array($value) ? implode(',', $value) : (string)$value;
531 }
532
533 $row['modul'] = $modul;
534 $row['dd'] = $dd;
535 $row['field_pos'] = (string)$pos;
536 $row['sort_no'] = (string)($pos + 1);
537 $row['target_id'] = $target_id;
538 $row['form_url'] = $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => (string)$pos));
539
540 $rows[] = $row;
541 }
542
543 $data = array(
544 'modul' => $modul,
545 'dd' => $dd,
546 'count' => count($rows),
547 'target_id' => $target_id,
548 'new_field_url' => $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => 'new')),
549 );
550
551 $oReport = dbx()->get_system_obj('dbxReport');
552 $oReport->init('ddedit_fields_order_' . $this->safe_id($modul . '_' . $dd), 'ddedit-fields-order-report');
553 $oReport->_mode = 'tpl';
554 $oReport->_data = $data;
555 $oReport->_replaces = $data;
556 $oReport->_rdata = $rows;
557 $oReport->_rcount = count($rows);
558 $oReport->_rrows = 'auto';
559 $oReport->_pages = false;
560
561 return $oReport->run();
562 }
563
573 private function create_indexes_report($modul, $dd, $model, $target_id = '')
574 {
575 $indexes = array_values((array)($model['indexes'] ?? array()));
576 $rows = array();
577
578 foreach ($indexes as $pos => $index) {
579 if (!is_array($index)) {
580 continue;
581 }
582
583 $row = $this->index_row_defaults();
584 foreach ($index as $key => $value) {
585 $row[$key] = is_array($value) ? implode(',', $value) : (string)$value;
586 }
587
588 $row['modul'] = $modul;
589 $row['dd'] = $dd;
590 $row['index_pos'] = (string)$pos;
591 $row['target_id'] = $target_id;
592 $row['form_url'] = $this->build_url('create_form_index', $modul, $dd, array('index_pos' => (string)$pos));
593
594 $rows[] = $row;
595 }
596
597 $data = array(
598 'modul' => $modul,
599 'dd' => $dd,
600 'count' => count($rows),
601 'target_id' => $target_id,
602 'new_index_url' => $this->build_url('create_form_index', $modul, $dd, array('index_pos' => 'new')),
603 );
604
605 $oReport = dbx()->get_system_obj('dbxReport');
606 $oReport->init('ddedit_indexes_' . $this->safe_id($modul . '_' . $dd), 'ddedit-indexes-report');
607 $oReport->_mode = 'tpl';
608 $oReport->_data = $data;
609 $oReport->_replaces = $data;
610 $oReport->_rdata = $rows;
611 $oReport->_rcount = count($rows);
612 $oReport->_rrows = 'auto';
613 $oReport->_pages = false;
614
615 return $oReport->run();
616 }
617
623 private function delete_field()
624 {
625 list($modul, $dd) = $this->dd_params_from_request();
626 $field_pos = (int)dbx()->get_modul_request_var('field_pos', -1);
627
628 $model = $this->load_model($modul, $dd);
629 if (!$model) {
630 return $this->alert('danger', 'DD nicht gefunden.');
631 }
632
633 $fields = array_values((array)($model['fields'] ?? array()));
634 if (!isset($fields[$field_pos])) {
635 return $this->alert('warning', 'Feld nicht gefunden.');
636 }
637
638 $name = (string)($fields[$field_pos]['name'] ?? $field_pos);
639 unset($fields[$field_pos]);
640
641 $model['fields'] = array_values($fields);
642
643 $ok = $this->save_model($modul, $dd, $model);
644
645 if ($ok) {
646 return $this->alert('success', 'Feld gelöscht: ' . dbx()->esc($name));
647 }
648
649 return $this->alert('danger', 'Feld konnte nicht gelöscht werden: ' . dbx()->esc($name));
650 }
651
657 private function delete_index()
658 {
659 list($modul, $dd) = $this->dd_params_from_request();
660 $index_pos = (int)dbx()->get_modul_request_var('index_pos', -1);
661
662 $model = $this->load_model($modul, $dd);
663 if (!$model) {
664 return $this->alert('danger', 'DD nicht gefunden.');
665 }
666
667 $indexes = array_values((array)($model['indexes'] ?? array()));
668 if (!isset($indexes[$index_pos])) {
669 return $this->alert('warning', 'Index nicht gefunden.');
670 }
671
672 $name = (string)($indexes[$index_pos]['name'] ?? $index_pos);
673 unset($indexes[$index_pos]);
674
675 $model['indexes'] = array_values($indexes);
676
677 $ok = $this->save_model($modul, $dd, $model);
678
679 if ($ok) {
680 return $this->alert('success', 'Index gelöscht: ' . dbx()->esc($name));
681 }
682
683 return $this->alert('danger', 'Index konnte nicht gelöscht werden: ' . dbx()->esc($name));
684 }
685
691 private function save_field_order()
692 {
693 list($modul, $dd) = $this->dd_params_from_request();
694 $order = $this->parse_order(dbx()->get_modul_request_var('order', array()));
695
696 $model = $this->load_model($modul, $dd);
697 if (!$model) {
698 dbx()->json_response(array('ok' => 0, 'msg' => 'DD nicht gefunden.'));
699 }
700
701 $fields = array_values((array)($model['fields'] ?? array()));
702 $new = $this->reorder_records($fields, $order);
703
704 if ($new === false) {
705 dbx()->json_response(array('ok' => 0, 'msg' => 'Ungültige Reihenfolge.'));
706 }
707
708 $model['fields'] = $new;
709 $ok = $this->save_model($modul, $dd, $model);
710
711 dbx()->json_response(array(
712 'ok' => $ok ? 1 : 0,
713 'msg' => $ok ? 'Feldreihenfolge gespeichert.' : 'Feldreihenfolge konnte nicht gespeichert werden.',
714 'count' => count($new),
715 ));
716
717 return '';
718 }
719
725 private function save_index_order()
726 {
727 list($modul, $dd) = $this->dd_params_from_request();
728 $order = $this->parse_order(dbx()->get_modul_request_var('order', array()));
729
730 $model = $this->load_model($modul, $dd);
731 if (!$model) {
732 dbx()->json_response(array('ok' => 0, 'msg' => 'DD nicht gefunden.'));
733 }
734
735 $indexes = array_values((array)($model['indexes'] ?? array()));
736 $new = $this->reorder_records($indexes, $order);
737
738 if ($new === false) {
739 dbx()->json_response(array('ok' => 0, 'msg' => 'Ungültige Reihenfolge.'));
740 }
741
742 $model['indexes'] = $new;
743 $ok = $this->save_model($modul, $dd, $model);
744
745 dbx()->json_response(array(
746 'ok' => $ok ? 1 : 0,
747 'msg' => $ok ? 'Indexreihenfolge gespeichert.' : 'Indexreihenfolge konnte nicht gespeichert werden.',
748 'count' => count($new),
749 ));
750
751 return '';
752 }
753
762 private function load_model($modul, $dd)
763 {
764 if (!$modul || !$dd) {
765 return array();
766 }
767
768 $oDD = dbx()->get_system_obj('dbxDD');
769 $model = $oDD->get_dd_model($this->dd_ref($modul, $dd));
770
771 if (!is_array($model)) {
772 return array();
773 }
774
775 if (!isset($model['table']) || !is_array($model['table'])) {
776 $model['table'] = array();
777 }
778
779 if (!isset($model['fields']) || !is_array($model['fields'])) {
780 $model['fields'] = array();
781 }
782
783 if (!isset($model['indexes']) || !is_array($model['indexes'])) {
784 $model['indexes'] = array();
785 }
786
787 return $model;
788 }
789
799 private function save_model($modul, $dd, $model)
800 {
801 if (!$modul || !$dd || !is_array($model)) {
802 return 0;
803 }
804
805 $this->backup_dd_file($modul, $dd);
806
807 $table = is_array($model['table'] ?? null) ? $model['table'] : array();
808 $fields = is_array($model['fields'] ?? null) ? array_values($model['fields']) : array();
809 $indexes = is_array($model['indexes'] ?? null) ? array_values($model['indexes']) : array();
810
811 $oDD = dbx()->get_system_obj('dbxDD');
812 return $oDD->save_dd($modul, $dd, $table, $fields, $indexes);
813 }
814
823 private function apply_table_right_fields($oForm, $data)
824 {
825 if (!is_object($oForm) || !method_exists($oForm, 'add_fld')) {
826 return;
827 }
828
829 foreach (array('read', 'create', 'update', 'delete') as $name) {
830 $oForm->add_fld(
831 $name,
832 'dbxAdmin|ddedit-rights-select1',
833 rules: 'array|parameter+*',
834 options: $this->table_right_options($data[$name] ?? '', false)
835 );
836 }
837
838 foreach (array('read_owner', 'create_owner', 'update_owner', 'delete_owner') as $name) {
839 $oForm->add_fld(
840 $name,
841 'dbxAdmin|ddedit-rights-select1',
842 rules: 'array|parameter+*',
843 options: $this->table_right_options($data[$name] ?? '', true)
844 );
845 }
846 }
847
856 private function table_right_options($current = '', $owner = false)
857 {
858 $options = array();
859
860 if ($owner) {
861 $options['owner'] = 'owner';
862 } else {
863 $options['*'] = '*Alle*';
864 }
865
866 $db = dbx()->get_system_obj('dbxDB');
867 if (is_object($db) && method_exists($db, 'select')) {
868 $rows = $db->select('dbxUser_groups', '', '*', 'name');
869 if (is_array($rows)) {
870 foreach ($rows as $row) {
871 if (!is_array($row)) {
872 continue;
873 }
874
875 $name = trim((string)($row['name'] ?? ''));
876 if ($name === '') {
877 continue;
878 }
879
880 $label = trim((string)($row['description'] ?? ''));
881 $options[$name] = $label !== '' ? $label : $name;
882 }
883 }
884 }
885
886 foreach ($this->csv_values($current) as $value) {
887 if (!isset($options[$value])) {
888 $options[$value] = $value;
889 }
890 }
891
892 return $options;
893 }
894
902 private function csv_values($value)
903 {
904 if (is_array($value)) {
905 $values = $value;
906 } else {
907 $values = explode(',', (string)$value);
908 }
909
910 $out = array();
911 foreach ($values as $item) {
912 $item = trim((string)$item);
913 if ($item !== '') {
914 $out[] = $item;
915 }
916 }
917
918 return array_values(array_unique($out));
919 }
920
929 private function backup_dd_file($modul, $dd)
930 {
931 $file = $this->dd_file_path($modul, $dd);
932 if (!$file || !file_exists($file)) {
933 return 0;
934 }
935
936 $dir = dirname($file) . '/_backup';
937 if (!is_dir($dir)) {
938 @mkdir($dir, 0777, true);
939 }
940
941 if (!is_dir($dir)) {
942 return 0;
943 }
944
945 $backup = $dir . '/' . $dd . '.' . date('Ymd-His') . '.dd.php';
946 return @copy($file, $backup) ? 1 : 0;
947 }
948
954 private function dd_params_from_request()
955 {
956 $modul = $this->sanitize_name(dbx()->get_modul_request_var('modul', ''));
957 $dd = $this->sanitize_name(dbx()->get_modul_request_var('dd', ''));
958
959 if (!$modul) {
960 $modul = $this->sanitize_name(dbx()->get_modul_request_var('xmodul', ''));
961 }
962
963 if (!$modul) {
964 $modul = $this->sanitize_name($this->get_system_var('dbx_activ_modul', 'dbx'));
965 }
966
967 return array($modul, $dd);
968 }
969
978 private function dd_ref($modul, $dd)
979 {
980 return $modul . '|' . $dd;
981 }
982
991 private function dd_file_path($modul, $dd)
992 {
993 $file = dbx_get_base_dir() . 'dbx/modules/' . $modul . '/dd/' . $dd . '.dd.php';
994
995 if (function_exists('dbx_os_path_file')) {
996 return dbx_os_path_file($file);
997 }
998
999 return $file;
1000 }
1001
1012 private function build_url($run2, $modul, $dd, $extra = array())
1013 {
1014 $url = '?dbx_modul=' . $this->_admin_modul .
1015 '&dbx_run1=edit_dd' .
1016 '&dbx_run2=' . rawurlencode($run2) .
1017 '&modul=' . rawurlencode($modul) .
1018 '&dd=' . rawurlencode($dd);
1019
1020 foreach ((array)$extra as $key => $value) {
1021 $url .= '&' . rawurlencode((string)$key) . '=' . rawurlencode((string)$value);
1022 }
1023
1024 return $url;
1025 }
1026
1035 private function get_system_var($name, $default = '')
1036 {
1037 if (function_exists('dbx')) {
1038 $obj = dbx();
1039
1040 if (is_object($obj) && method_exists($obj, 'get_system_var')) {
1041 $value = $obj->get_system_var($name);
1042 if ($value !== null && $value !== '') {
1043 return $value;
1044 }
1045 }
1046 }
1047
1048 return $default;
1049 }
1050
1060 private function merge_record($old, $post, $keys)
1061 {
1062 $record = is_array($old) ? $old : array();
1063
1064 foreach ((array)$keys as $key) {
1065 if (array_key_exists($key, (array)$post)) {
1066 $record[$key] = $this->normalize_value($post[$key]);
1067 }
1068 }
1069
1070 return $record;
1071 }
1072
1080 private function strip_editor_keys($record)
1081 {
1082 unset($record['modul']);
1083 unset($record['dd']);
1084 unset($record['field_pos']);
1085 unset($record['index_pos']);
1086 unset($record['old_name']);
1087
1088 return $record;
1089 }
1090
1098 private function normalize_value($value)
1099 {
1100 if (is_array($value)) {
1101 return implode(',', array_map('trim', $value));
1102 }
1103
1104 return trim((string)$value);
1105 }
1106
1114 private function parse_order($raw)
1115 {
1116 if (is_array($raw)) {
1117 return array_values(array_map('intval', $raw));
1118 }
1119
1120 $raw = trim((string)$raw);
1121 if ($raw === '') {
1122 return array();
1123 }
1124
1125 if (substr($raw, 0, 1) === '[') {
1126 $decoded = json_decode($raw, true);
1127 if (is_array($decoded)) {
1128 return array_values(array_map('intval', $decoded));
1129 }
1130 }
1131
1132 $parts = preg_split('/[|,\s;]+/', $raw);
1133 if (!is_array($parts)) {
1134 return array();
1135 }
1136
1137 return array_values(array_map('intval', $parts));
1138 }
1139
1148 private function reorder_records($records, $order)
1149 {
1150 $records = array_values((array)$records);
1151 $count = count($records);
1152
1153 if (!$count) {
1154 return array();
1155 }
1156
1157 if (count($order) !== $count) {
1158 return false;
1159 }
1160
1161 $seen = array();
1162 $new = array();
1163
1164 foreach ($order as $pos) {
1165 $pos = (int)$pos;
1166
1167 if ($pos < 0 || $pos >= $count || isset($seen[$pos])) {
1168 return false;
1169 }
1170
1171 $seen[$pos] = 1;
1172 $new[] = $records[$pos];
1173 }
1174
1175 return $new;
1176 }
1177
1188 private function validate_field_record($field, $fields, $self_pos, &$message)
1189 {
1190 $name = trim((string)($field['name'] ?? ''));
1191
1192 if (!$this->is_identifier($name)) {
1193 $message = 'Ungültiger Feldname: ' . $name;
1194 return false;
1195 }
1196
1197 $names = array();
1198 foreach (array_values((array)$fields) as $pos => $old) {
1199 if ((int)$pos === (int)$self_pos) {
1200 continue;
1201 }
1202
1203 $old_name = strtolower(trim((string)($old['name'] ?? '')));
1204 if ($old_name !== '') {
1205 $names[$old_name] = 1;
1206 }
1207 }
1208
1209 if (isset($names[strtolower($name)])) {
1210 $message = 'Feldname doppelt: ' . $name;
1211 return false;
1212 }
1213
1214 return true;
1215 }
1216
1227 private function validate_index_record($index, $indexes, $self_pos, &$message)
1228 {
1229 $name = trim((string)($index['name'] ?? ''));
1230 $type = strtoupper(trim((string)($index['type'] ?? 'INDEX')));
1231 $fields = trim((string)($index['fields'] ?? ''));
1232
1233 if (!$this->is_identifier($name)) {
1234 $message = 'Ungültiger Indexname: ' . $name;
1235 return false;
1236 }
1237
1238 if (!$fields) {
1239 $message = 'Index benötigt mindestens ein Feld.';
1240 return false;
1241 }
1242
1243 if (!in_array($type, array('PRIMARY', 'INDEX', 'UNIQUE', 'FULLTEXT'), true)) {
1244 $message = 'Ungültiger Indextyp: ' . $type;
1245 return false;
1246 }
1247
1248 $names = array();
1249 foreach (array_values((array)$indexes) as $pos => $old) {
1250 if ((int)$pos === (int)$self_pos) {
1251 continue;
1252 }
1253
1254 $old_name = strtolower(trim((string)($old['name'] ?? '')));
1255 if ($old_name !== '') {
1256 $names[$old_name] = 1;
1257 }
1258 }
1259
1260 if (isset($names[strtolower($name)])) {
1261 $message = 'Indexname doppelt: ' . $name;
1262 return false;
1263 }
1264
1265 return true;
1266 }
1267
1275 private function is_identifier($name)
1276 {
1277 return (bool)preg_match('/^[A-Za-z_][A-Za-z0-9_]*$/', (string)$name);
1278 }
1279
1287 private function sanitize_name($name)
1288 {
1289 $name = trim((string)$name);
1290
1291 if (!preg_match('/^[A-Za-z0-9_]+$/', $name)) {
1292 return '';
1293 }
1294
1295 return $name;
1296 }
1297
1305 private function safe_id($value)
1306 {
1307 $value = preg_replace('/[^A-Za-z0-9_]+/', '_', (string)$value);
1308 $value = trim($value, '_');
1309
1310 return $value ?: 'x';
1311 }
1312
1320 private function instance_id($seed)
1321 {
1322 return $this->safe_id($seed . '_' . substr(md5((string)$seed), 0, 6));
1323 }
1324
1333 private function alert($type, $msg)
1334 {
1335 $type = preg_replace('/[^a-z]/', '', (string)$type);
1336 if (!$type) {
1337 $type = 'info';
1338 }
1339
1340 return '<div class="alert alert-' . $type . '">' . $msg . '</div>';
1341 }
1342
1348 private function table_keys()
1349 {
1350 $oDD = dbx()->get_system_obj('dbxDD');
1351
1352 if (is_object($oDD) && method_exists($oDD, 'dd_table_schema_keys')) {
1353 return $oDD->dd_table_schema_keys();
1354 }
1355
1356 return array(
1357 'server',
1358 'table',
1359 'datadic',
1360 'primary',
1361 'language',
1362 'version',
1363 'autosync',
1364 'cache',
1365 'trash',
1366 'trace',
1367 'update_sql',
1368 'default_sort',
1369 'form-dd-table',
1370 'read',
1371 'create',
1372 'update',
1373 'delete',
1374 'read_owner',
1375 'create_owner',
1376 'update_owner',
1377 'delete_owner',
1378 );
1379 }
1380
1386 private function field_keys()
1387 {
1388 $oDD = dbx()->get_system_obj('dbxDD');
1389
1390 if (is_object($oDD) && method_exists($oDD, 'dd_field_schema_keys')) {
1391 return array_merge(array('modul', 'dd', 'field_pos', 'old_name'), $oDD->dd_field_schema_keys());
1392 }
1393
1394 return array(
1395 'modul',
1396 'dd',
1397 'field_pos',
1398 'old_name',
1399 'name',
1400 'type',
1401 'index',
1402 'length',
1403 'default',
1404 'label',
1405 'rules',
1406 'tooltip',
1407 'errormsg',
1408 'placeholder',
1409 'convert',
1410 'protect',
1411 'group',
1412 'mask',
1413 'data',
1414 'options',
1415 'tpl',
1416 'js',
1417 'prompt',
1418 );
1419 }
1420
1426 private function index_keys()
1427 {
1428 $oDD = dbx()->get_system_obj('dbxDD');
1429
1430 if (is_object($oDD) && method_exists($oDD, 'dd_index_schema_keys')) {
1431 return array_merge(array('modul', 'dd', 'index_pos', 'old_name'), $oDD->dd_index_schema_keys());
1432 }
1433
1434 return array(
1435 'modul',
1436 'dd',
1437 'index_pos',
1438 'old_name',
1439 'name',
1440 'type',
1441 'fields',
1442 'unique',
1443 'comment',
1444 );
1445 }
1446
1452 private function default_field_record()
1453 {
1454 return array(
1455 'name' => '',
1456 'type' => 'varchar',
1457 'index' => '',
1458 'length' => '255',
1459 'default' => '',
1460 'label' => '',
1461 'rules' => 'text',
1462 'tooltip' => '',
1463 'errormsg' => '',
1464 'placeholder' => '',
1465 'convert' => '',
1466 'protect' => '0',
1467 'group' => '',
1468 'mask' => '',
1469 'data' => '',
1470 'options' => '',
1471 'tpl' => 'text-label',
1472 'js' => '',
1473 'prompt' => '',
1474 );
1475 }
1476
1482 private function default_index_record()
1483 {
1484 return array(
1485 'name' => '',
1486 'type' => 'INDEX',
1487 'fields' => '',
1488 'unique' => '0',
1489 'comment' => '',
1490 );
1491 }
1492
1498 private function field_row_defaults()
1499 {
1500 return array(
1501 'modul' => '',
1502 'dd' => '',
1503 'field_pos' => '',
1504 'name' => '',
1505 'type' => '',
1506 'index' => '',
1507 'length' => '',
1508 'label' => '',
1509 );
1510 }
1511
1517 private function index_row_defaults()
1518 {
1519 return array(
1520 'modul' => '',
1521 'dd' => '',
1522 'index_pos' => '',
1523 'name' => '',
1524 'type' => '',
1525 'fields' => '',
1526 'unique' => '',
1527 'comment' => '',
1528 );
1529 }
1530}
1531
1532?>
$table['server']
Definition .dd.php:6
$indexes[]
Definition .dd.php:167
$index['name']
Definition .dd.php:162
$fields[]
Definition config.dd.php:16
$field
Definition config.dd.php:4
dbx_os_path_file($path_file)
Definition index.php:164
dbx_get_base_dir($cut_Data=0)
Definition index.php:157
DBX schema administration.