dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
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
70 private $_text_forms = array();
71
78 private function texts($fd)
79 {
80 if (isset($this->_text_forms[$fd])) {
81 return $this->_text_forms[$fd];
82 }
83
84 dbx()->get_system_obj('dbxForm', 'use');
85 $form = new \dbxForm();
86 $form->init('ddedit-texts-' . $this->safe_id($fd));
87 $form->_fd = $fd;
88 $form->load_fd_messages();
89 $form->set_form_help_enabled(false);
90 $this->_text_forms[$fd] = $form;
91
92 return $form;
93 }
94
100 public function run()
101 {
102 $work = dbx()->get_modul_var('dbx_run2', '');
103
104 switch ($work) {
105
106 case 'create_form_table':
107 return $this->create_form_table();
108
109 case 'create_form_dd':
110 return $this->create_form_dd();
111
112 case 'create_form_index':
113 return $this->create_form_index();
114
115 case 'delete_field':
116 return $this->delete_field();
117
118 case 'delete_index':
119 return $this->delete_index();
120
121 case 'save_field_order':
122 return $this->save_field_order();
123
124 case 'save_index_order':
125 return $this->save_index_order();
126
127 case '':
128 case 'editor':
129 default:
130 return $this->run_editor();
131 }
132 }
133
134
140 private function run_editor()
141 {
142 list($modul, $dd) = $this->dd_params_from_request();
143 $texts = $this->texts($this->_fd_table);
144
145 if (!$modul || !$dd) {
146 return $this->alert('warning', $texts->get_fd_message('missing_dd_params'));
147 }
148
149 $model = $this->load_model($modul, $dd);
150 if (!$model) {
151 return $this->alert('danger', $texts->format_fd_message(
152 'dd_unreadable',
153 array('dd' => dbx()->esc($modul . '|' . $dd))
154 ));
155 }
156
157 $instance_id = $this->instance_id($modul . '_' . $dd);
158 $work_target_id = 'dbx_ddedit_work_' . $instance_id;
159 $work_content = $this->create_form_table($modul, $dd, $model);
160 $fields_order_report = $this->create_fields_order_report($modul, $dd, $model, $work_target_id);
161 $indexes_report = $this->create_indexes_report($modul, $dd, $model, $work_target_id);
162
163 $data = array(
164 'i' => $instance_id,
165 'modul' => $modul,
166 'dd' => $dd,
167 'path' => $this->dd_file_path($modul, $dd),
168 'message' => '',
169 'work_target_id' => $work_target_id,
170 'table_url' => $this->build_url('create_form_table', $modul, $dd),
171 'new_field_url' => $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => 'new')),
172 'new_index_url' => $this->build_url('create_form_index', $modul, $dd, array('index_pos' => 'new')),
173 'work_content' => $work_content,
174 'fields_order_report' => $fields_order_report,
175 'indexes_report' => $indexes_report,
176 );
177
178 $help = dbx()->get_include_obj('dbxAdminHelp', 'dbxAdmin');
179 $oTPL = dbx()->get_system_obj('dbxTPL');
180 $reloadAction = $oTPL->get_tpl('dbx|button-bar-reload-ajax', array(
181 'bar_reload_href' => '?dbx_modul=dbxAdmin&dbx_run1=edit_dd&modul=' . rawurlencode($modul) . '&dd=' . rawurlencode($dd),
182 'bar_reload_target' => 'dbx_ddedit_' . $instance_id,
183 'bar_reload_replace' => 'target',
184 ));
185 $barData = $help->moduleBarTemplateData('edit_dd', $reloadAction);
186 $barData['bar_title'] = $texts->format_fd_message(
187 'bar_title',
188 array('dd' => dbx()->esc($modul . '|' . $dd))
189 );
190 $barData['bar_subtitle'] = $texts->get_fd_message('bar_subtitle');
191 $barData['bar_class'] = 'dbx-module-bar dbx-ddedit-head';
192 $data = array_merge($data, $barData);
193
194 return $oTPL->get_tpl($this->_admin_modul . '|ddedit-frame', $data);
195 }
196
206 private function create_form_table($modul = '', $dd = '', $model = array())
207 {
208 $texts = $this->texts($this->_fd_table);
209 if (!$modul || !$dd) {
210 list($modul, $dd) = $this->dd_params_from_request();
211 }
212
213 if (!$model) {
214 $model = $this->load_model($modul, $dd);
215 }
216
217 if (!$model) {
218 return $this->alert('danger', $texts->format_fd_message(
219 'dd_not_found',
220 array('dd' => dbx()->esc($modul . '|' . $dd))
221 ));
222 }
223
224 $data = is_array($model['table'] ?? null) ? $model['table'] : array();
225 $data['modul'] = $modul;
226 $data['dd'] = $dd;
227
228 $oForm = dbx()->get_system_obj('dbxForm');
229 $oForm->init('ddedit_table_' . $this->safe_id($modul . '_' . $dd), 'ddedit-table-form');
230 $oForm->_fd = $this->_fd_table;
231 $oForm->load_fd_messages();
232 $oForm->set_form_help_enabled(false);
233 $oForm->_data = $data;
234 $oForm->_action = $this->build_url('create_form_table', $modul, $dd);
235 $oForm->_msg_info = $texts->format_fd_message('edit_info', array('dd' => dbx()->esc($dd)));
236 $oForm->add_flds();
237 $this->apply_table_right_fields($oForm, $data);
238
239 if ($oForm->submit()) {
240
241 if (!$oForm->errors()) {
242
243 $table = $this->merge_record($data, $oForm->_post, $this->table_keys());
244 $table['datadic'] = $dd;
245
246 $model['table'] = $table;
247
248 $ok = $this->save_model($modul, $dd, $model);
249
250 if ($ok) {
251 $oForm->_data = array_merge($table, array('modul' => $modul, 'dd' => $dd));
252 $oForm->_msg_success = $texts->format_fd_message('table_saved', array('dd' => dbx()->esc($dd)));
253 } else {
254 $oForm->_msg_error = $texts->format_fd_message('table_save_error', array('dd' => dbx()->esc($dd)));
255 }
256
257 } else {
258 $oForm->_msg_error = $texts->format_fd_message('table_check', array('dd' => dbx()->esc($dd)));
259 }
260 }
261
262 return $oForm->run();
263 }
264
275 private function create_form_dd($modul = '', $dd = '', $field_pos = null, $model = array())
276 {
277 $texts = $this->texts($this->_fd_field);
278 if (!$modul || !$dd) {
279 list($modul, $dd) = $this->dd_params_from_request();
280 }
281
282 if ($field_pos === null) {
283 $field_pos = dbx()->get_modul_var('field_pos', 'new');
284 }
285
286 if (!$model) {
287 $model = $this->load_model($modul, $dd);
288 }
289
290 if (!$model) {
291 return $this->alert('danger', $texts->format_fd_message(
292 'dd_not_found',
293 array('dd' => dbx()->esc($modul . '|' . $dd))
294 ));
295 }
296
297 $fields = array_values((array)($model['fields'] ?? array()));
298 $is_new = ((string)$field_pos === 'new');
299
300 if ($is_new) {
301 $data = $this->default_field_record();
302 } else {
303 $pos = (int)$field_pos;
304 if (!isset($fields[$pos]) || !is_array($fields[$pos])) {
305 return $this->alert('warning', $texts->format_fd_message(
306 'position_not_found',
307 array('position' => dbx()->esc((string)$field_pos))
308 ));
309 }
310
311 $data = $fields[$pos];
312 }
313
314 $data['modul'] = $modul;
315 $data['dd'] = $dd;
316 $data['field_pos'] = (string)$field_pos;
317 $data['old_name'] = (string)($data['name'] ?? '');
318
319 $form_id = 'ddedit_field_' . $this->safe_id($modul . '_' . $dd . '_' . (string)$field_pos);
320
321 $oForm = dbx()->get_system_obj('dbxForm');
322 $oForm->init($form_id, 'ddedit-field-form');
323 $oForm->_fd = $this->_fd_field;
324 $oForm->load_fd_messages();
325 $oForm->set_form_help_enabled(false);
326 $oForm->_data = $data;
327 $oForm->_action = $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => (string)$field_pos));
328 $oForm->_msg_info = $is_new
329 ? $texts->format_fd_message('edit_new', array('dd' => dbx()->esc($dd)))
330 : $texts->format_fd_message('edit_existing', array('field' => dbx()->esc((string)($data['name'] ?? $field_pos))));
331 $oForm->add_flds();
332
333 if ($oForm->submit()) {
334
335 if (!$oForm->errors()) {
336
337 $field = $this->merge_record($data, $oForm->_post, $this->field_keys());
338 $field = $this->strip_editor_keys($field);
339
340 $message = '';
341 if (!$this->validate_field_record($field, $fields, $is_new ? -1 : (int)$field_pos, $message)) {
342 $oForm->_msg_error = $message;
343 return $oForm->run();
344 }
345
346 if ($is_new) {
347 $fields[] = $field;
348 $field_pos = count($fields) - 1;
349 $is_new = false;
350 } else {
351 $fields[(int)$field_pos] = $field;
352 }
353
354 $model['fields'] = array_values($fields);
355
356 $ok = $this->save_model($modul, $dd, $model);
357
358 if ($ok) {
359 $field['modul'] = $modul;
360 $field['dd'] = $dd;
361 $field['field_pos'] = (string)$field_pos;
362 $field['old_name'] = (string)($field['name'] ?? '');
363
364 $oForm->_data = $field;
365 $oForm->_action = $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => (string)$field_pos));
366 $oForm->_msg_success = $texts->format_fd_message(
367 'field_saved',
368 array('field' => dbx()->esc((string)($field['name'] ?? $field_pos)))
369 );
370 } else {
371 $oForm->_msg_error = $texts->format_fd_message(
372 'field_save_error',
373 array('field' => dbx()->esc((string)($field['name'] ?? $field_pos)))
374 );
375 }
376
377 } else {
378 $oForm->_msg_error = $texts->format_fd_message(
379 'field_check',
380 array('field' => dbx()->esc((string)($data['name'] ?? $field_pos)))
381 );
382 }
383 }
384
385 $delete_url = $this->build_url('delete_field', $modul, $dd, array('field_pos' => (string)$field_pos));
386
387 return str_replace(
388 '&dbx_run2=delete_field&modul={modul}&dd={dd}&field_pos={field_pos}',
389 dbx()->esc($delete_url),
390 $oForm->run()
391 );
392 }
393
404 private function create_form_index($modul = '', $dd = '', $index_pos = null, $model = array())
405 {
406 $texts = $this->texts($this->_fd_index);
407 if (!$modul || !$dd) {
408 list($modul, $dd) = $this->dd_params_from_request();
409 }
410
411 if ($index_pos === null) {
412 $index_pos = dbx()->get_modul_var('index_pos', 'new');
413 }
414
415 if (!$model) {
416 $model = $this->load_model($modul, $dd);
417 }
418
419 if (!$model) {
420 return $this->alert('danger', $texts->format_fd_message(
421 'dd_not_found',
422 array('dd' => dbx()->esc($modul . '|' . $dd))
423 ));
424 }
425
426 $indexes = array_values((array)($model['indexes'] ?? array()));
427 $is_new = ((string)$index_pos === 'new');
428
429 if ($is_new) {
430 $data = $this->default_index_record();
431 } else {
432 $pos = (int)$index_pos;
433 if (!isset($indexes[$pos]) || !is_array($indexes[$pos])) {
434 return $this->alert('warning', $texts->format_fd_message(
435 'position_not_found',
436 array('position' => dbx()->esc((string)$index_pos))
437 ));
438 }
439
440 $data = $indexes[$pos];
441 }
442
443 $data['modul'] = $modul;
444 $data['dd'] = $dd;
445 $data['index_pos'] = (string)$index_pos;
446 $data['old_name'] = (string)($data['name'] ?? '');
447
448 $form_id = 'ddedit_index_' . $this->safe_id($modul . '_' . $dd . '_' . (string)$index_pos);
449
450 $oForm = dbx()->get_system_obj('dbxForm');
451 $oForm->init($form_id, 'ddedit-index-form');
452 $oForm->_fd = $this->_fd_index;
453 $oForm->load_fd_messages();
454 $oForm->set_form_help_enabled(false);
455 $oForm->_data = $data;
456 $oForm->_action = $this->build_url('create_form_index', $modul, $dd, array('index_pos' => (string)$index_pos));
457 $oForm->_msg_info = $is_new
458 ? $texts->format_fd_message('edit_new', array('dd' => dbx()->esc($dd)))
459 : $texts->format_fd_message('edit_existing', array('index' => dbx()->esc((string)($data['name'] ?? $index_pos))));
460 $oForm->add_flds();
461
462 if ($oForm->submit()) {
463
464 if (!$oForm->errors()) {
465
466 $index = $this->merge_record($data, $oForm->_post, $this->index_keys());
467 $index = $this->strip_editor_keys($index);
468
469 $message = '';
470 if (!$this->validate_index_record($index, $indexes, $is_new ? -1 : (int)$index_pos, $message)) {
471 $oForm->_msg_error = $message;
472 return $oForm->run();
473 }
474
475 if ($is_new) {
476 $indexes[] = $index;
477 $index_pos = count($indexes) - 1;
478 $is_new = false;
479 } else {
480 $indexes[(int)$index_pos] = $index;
481 }
482
483 $model['indexes'] = array_values($indexes);
484
485 $ok = $this->save_model($modul, $dd, $model);
486
487 if ($ok) {
488 $index['modul'] = $modul;
489 $index['dd'] = $dd;
490 $index['index_pos'] = (string)$index_pos;
491 $index['old_name'] = (string)($index['name'] ?? '');
492
493 $oForm->_data = $index;
494 $oForm->_action = $this->build_url('create_form_index', $modul, $dd, array('index_pos' => (string)$index_pos));
495 $oForm->_msg_success = $texts->format_fd_message(
496 'index_saved',
497 array('index' => dbx()->esc((string)($index['name'] ?? $index_pos)))
498 );
499 } else {
500 $oForm->_msg_error = $texts->format_fd_message(
501 'index_save_error',
502 array('index' => dbx()->esc((string)($index['name'] ?? $index_pos)))
503 );
504 }
505
506 } else {
507 $oForm->_msg_error = $texts->format_fd_message(
508 'index_check',
509 array('index' => dbx()->esc((string)($data['name'] ?? $index_pos)))
510 );
511 }
512 }
513
514 $delete_url = $this->build_url('delete_index', $modul, $dd, array('index_pos' => (string)$index_pos));
515
516 return str_replace(
517 '&dbx_run2=delete_index&modul={modul}&dd={dd}&index_pos={index_pos}',
518 dbx()->esc($delete_url),
519 $oForm->run()
520 );
521 }
522
532 private function create_fields_report($modul, $dd, $model)
533 {
534 $fields = array_values((array)($model['fields'] ?? array()));
535 $rows = array();
536
537 foreach ($fields as $pos => $field) {
538 if (!is_array($field)) {
539 continue;
540 }
541
542 $row = $this->field_row_defaults();
543 foreach ($field as $key => $value) {
544 $row[$key] = is_array($value) ? implode(',', $value) : (string)$value;
545 }
546
547 $row['modul'] = $modul;
548 $row['dd'] = $dd;
549 $row['field_pos'] = (string)$pos;
550
551 $rows[] = $row;
552 }
553
554 $data = array(
555 'modul' => $modul,
556 'dd' => $dd,
557 'count' => count($rows),
558 );
559
560 $oReport = dbx()->get_system_obj('dbxReport');
561 $oReport->init('ddedit_fields_' . $this->safe_id($modul . '_' . $dd), 'ddedit-fields-report');
562 $oReport->_mode = 'tpl';
563 $oReport->_data = $data;
564 $oReport->_replaces = $data;
565 $oReport->_rdata = $rows;
566 $oReport->_rcount = count($rows);
567 $oReport->_rrows = 'auto';
568 $oReport->_pages = false;
569
570 $oReport->add_obj(
571 'new_field_form',
572 'obj-value',
573 '[modul=dbxAdmin]dbx_run1=edit_dd&dbx_run2=create_form_dd&modul=' . $modul . '&dd=' . $dd . '&field_pos=new[/modul]'
574 );
575
576 $oReport->add_obj(
577 'field_form',
578 'obj-value',
579 '[modul=dbxAdmin]dbx_run1=edit_dd&dbx_run2=create_form_dd&modul={modul}&dd={dd}&field_pos={field_pos}[/modul]'
580 );
581
582 return $oReport->run();
583 }
584
594 private function create_fields_order_report($modul, $dd, $model, $target_id = '')
595 {
596 $fields = array_values((array)($model['fields'] ?? array()));
597 $rows = array();
598
599 foreach ($fields as $pos => $field) {
600 if (!is_array($field)) {
601 continue;
602 }
603
604 $row = $this->field_row_defaults();
605 foreach ($field as $key => $value) {
606 $row[$key] = is_array($value) ? implode(',', $value) : (string)$value;
607 }
608
609 $row['modul'] = $modul;
610 $row['dd'] = $dd;
611 $row['field_pos'] = (string)$pos;
612 $row['sort_no'] = (string)($pos + 1);
613 $row['target_id'] = $target_id;
614 $row['form_url'] = $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => (string)$pos));
615
616 $rows[] = $row;
617 }
618
619 $data = array(
620 'modul' => $modul,
621 'dd' => $dd,
622 'count' => count($rows),
623 'target_id' => $target_id,
624 'new_field_url' => $this->build_url('create_form_dd', $modul, $dd, array('field_pos' => 'new')),
625 );
626
627 $oReport = dbx()->get_system_obj('dbxReport');
628 $oReport->init('ddedit_fields_order_' . $this->safe_id($modul . '_' . $dd), 'ddedit-fields-order-report');
629 $oReport->_mode = 'tpl';
630 $oReport->_data = $data;
631 $oReport->_replaces = $data;
632 $oReport->_rdata = $rows;
633 $oReport->_rcount = count($rows);
634 $oReport->_rrows = 'auto';
635 $oReport->_pages = false;
636
637 return $oReport->run();
638 }
639
649 private function create_indexes_report($modul, $dd, $model, $target_id = '')
650 {
651 $indexes = array_values((array)($model['indexes'] ?? array()));
652 $rows = array();
653
654 foreach ($indexes as $pos => $index) {
655 if (!is_array($index)) {
656 continue;
657 }
658
659 $row = $this->index_row_defaults();
660 foreach ($index as $key => $value) {
661 $row[$key] = is_array($value) ? implode(',', $value) : (string)$value;
662 }
663
664 $row['modul'] = $modul;
665 $row['dd'] = $dd;
666 $row['index_pos'] = (string)$pos;
667 $row['target_id'] = $target_id;
668 $row['form_url'] = $this->build_url('create_form_index', $modul, $dd, array('index_pos' => (string)$pos));
669
670 $rows[] = $row;
671 }
672
673 $data = array(
674 'modul' => $modul,
675 'dd' => $dd,
676 'count' => count($rows),
677 'target_id' => $target_id,
678 'new_index_url' => $this->build_url('create_form_index', $modul, $dd, array('index_pos' => 'new')),
679 );
680
681 $oReport = dbx()->get_system_obj('dbxReport');
682 $oReport->init('ddedit_indexes_' . $this->safe_id($modul . '_' . $dd), 'ddedit-indexes-report');
683 $oReport->_mode = 'tpl';
684 $oReport->_data = $data;
685 $oReport->_replaces = $data;
686 $oReport->_rdata = $rows;
687 $oReport->_rcount = count($rows);
688 $oReport->_rrows = 'auto';
689 $oReport->_pages = false;
690
691 return $oReport->run();
692 }
693
699 private function delete_field()
700 {
701 list($modul, $dd) = $this->dd_params_from_request();
702 $field_pos = (int)dbx()->get_modul_var('field_pos', -1);
703 $texts = $this->texts($this->_fd_field);
704
705 $model = $this->load_model($modul, $dd);
706 if (!$model) {
707 return $this->alert('danger', $texts->format_fd_message(
708 'dd_not_found',
709 array('dd' => dbx()->esc($modul . '|' . $dd))
710 ));
711 }
712
713 $fields = array_values((array)($model['fields'] ?? array()));
714 if (!isset($fields[$field_pos])) {
715 return $this->alert('warning', $texts->get_fd_message('field_not_found'));
716 }
717
718 $name = (string)($fields[$field_pos]['name'] ?? $field_pos);
719 unset($fields[$field_pos]);
720
721 $model['fields'] = array_values($fields);
722
723 $ok = $this->save_model($modul, $dd, $model);
724
725 if ($ok) {
726 return $this->alert('success', $texts->format_fd_message(
727 'field_deleted',
728 array('field' => dbx()->esc($name))
729 ));
730 }
731
732 return $this->alert('danger', $texts->format_fd_message(
733 'field_delete_error',
734 array('field' => dbx()->esc($name))
735 ));
736 }
737
743 private function delete_index()
744 {
745 list($modul, $dd) = $this->dd_params_from_request();
746 $index_pos = (int)dbx()->get_modul_var('index_pos', -1);
747 $texts = $this->texts($this->_fd_index);
748
749 $model = $this->load_model($modul, $dd);
750 if (!$model) {
751 return $this->alert('danger', $texts->format_fd_message(
752 'dd_not_found',
753 array('dd' => dbx()->esc($modul . '|' . $dd))
754 ));
755 }
756
757 $indexes = array_values((array)($model['indexes'] ?? array()));
758 if (!isset($indexes[$index_pos])) {
759 return $this->alert('warning', $texts->get_fd_message('index_not_found'));
760 }
761
762 $name = (string)($indexes[$index_pos]['name'] ?? $index_pos);
763 unset($indexes[$index_pos]);
764
765 $model['indexes'] = array_values($indexes);
766
767 $ok = $this->save_model($modul, $dd, $model);
768
769 if ($ok) {
770 return $this->alert('success', $texts->format_fd_message(
771 'index_deleted',
772 array('index' => dbx()->esc($name))
773 ));
774 }
775
776 return $this->alert('danger', $texts->format_fd_message(
777 'index_delete_error',
778 array('index' => dbx()->esc($name))
779 ));
780 }
781
787 private function save_field_order()
788 {
789 list($modul, $dd) = $this->dd_params_from_request();
790 $order = $this->parse_order(dbx()->get_modul_var('order', array()));
791 $texts = $this->texts($this->_fd_field);
792
793 $model = $this->load_model($modul, $dd);
794 if (!$model) {
795 dbx()->json_response(array(
796 'ok' => 0,
797 'msg' => $texts->format_fd_message('dd_not_found', array('dd' => $modul . '|' . $dd)),
798 ));
799 }
800
801 $fields = array_values((array)($model['fields'] ?? array()));
802 $new = $this->reorder_records($fields, $order);
803
804 if ($new === false) {
805 dbx()->json_response(array('ok' => 0, 'msg' => $texts->get_fd_message('invalid_order')));
806 }
807
808 $model['fields'] = $new;
809 $ok = $this->save_model($modul, $dd, $model);
810
811 dbx()->json_response(array(
812 'ok' => $ok ? 1 : 0,
813 'msg' => $ok ? $texts->get_fd_message('order_saved') : $texts->get_fd_message('order_save_error'),
814 'count' => count($new),
815 ));
816
817 return '';
818 }
819
825 private function save_index_order()
826 {
827 list($modul, $dd) = $this->dd_params_from_request();
828 $order = $this->parse_order(dbx()->get_modul_var('order', array()));
829 $texts = $this->texts($this->_fd_index);
830
831 $model = $this->load_model($modul, $dd);
832 if (!$model) {
833 dbx()->json_response(array(
834 'ok' => 0,
835 'msg' => $texts->format_fd_message('dd_not_found', array('dd' => $modul . '|' . $dd)),
836 ));
837 }
838
839 $indexes = array_values((array)($model['indexes'] ?? array()));
840 $new = $this->reorder_records($indexes, $order);
841
842 if ($new === false) {
843 dbx()->json_response(array('ok' => 0, 'msg' => $texts->get_fd_message('invalid_order')));
844 }
845
846 $model['indexes'] = $new;
847 $ok = $this->save_model($modul, $dd, $model);
848
849 dbx()->json_response(array(
850 'ok' => $ok ? 1 : 0,
851 'msg' => $ok ? $texts->get_fd_message('order_saved') : $texts->get_fd_message('order_save_error'),
852 'count' => count($new),
853 ));
854
855 return '';
856 }
857
866 private function load_model($modul, $dd)
867 {
868 if (!$modul || !$dd) {
869 return array();
870 }
871
872 $oDD = dbx()->get_system_obj('dbxDD');
873 $model = $oDD->get_dd_model($this->dd_ref($modul, $dd));
874
875 if (!is_array($model)) {
876 return array();
877 }
878
879 if (!isset($model['table']) || !is_array($model['table'])) {
880 $model['table'] = array();
881 }
882
883 if (!isset($model['fields']) || !is_array($model['fields'])) {
884 $model['fields'] = array();
885 }
886
887 if (!isset($model['indexes']) || !is_array($model['indexes'])) {
888 $model['indexes'] = array();
889 }
890
891 return $model;
892 }
893
903 private function save_model($modul, $dd, $model)
904 {
905 if (!$modul || !$dd || !is_array($model)) {
906 return 0;
907 }
908
909 $this->backup_dd_file($modul, $dd);
910
911 $table = is_array($model['table'] ?? null) ? $model['table'] : array();
912 $fields = is_array($model['fields'] ?? null) ? array_values($model['fields']) : array();
913 $indexes = is_array($model['indexes'] ?? null) ? array_values($model['indexes']) : array();
914
915 $oDD = dbx()->get_system_obj('dbxDD');
916 return $oDD->save_dd($modul, $dd, $table, $fields, $indexes);
917 }
918
927 private function apply_table_right_fields($oForm, $data)
928 {
929 if (!is_object($oForm) || !method_exists($oForm, 'add_fld')) {
930 return;
931 }
932
933 foreach (array('read', 'create', 'update', 'delete') as $name) {
934 $oForm->add_fld(
935 $name,
936 'dbxAdmin|ddedit-rights-select1',
937 rules: 'array|parameter+*',
938 options: $this->table_right_options($data[$name] ?? '', false)
939 );
940 }
941
942 foreach (array('read_owner', 'create_owner', 'update_owner', 'delete_owner') as $name) {
943 $oForm->add_fld(
944 $name,
945 'dbxAdmin|ddedit-rights-select1',
946 rules: 'array|parameter+*',
947 options: $this->table_right_options($data[$name] ?? '', true)
948 );
949 }
950 }
951
960 private function table_right_options($current = '', $owner = false)
961 {
962 $options = array();
963
964 if ($owner) {
965 $options['owner'] = 'owner';
966 } else {
967 $options['*'] = $this->texts($this->_fd_table)->get_fd_message('rights_all');
968 }
969
970 $db = dbx()->get_system_obj('dbxDB');
971 if (is_object($db) && method_exists($db, 'select')) {
972 $rows = $db->select('dbxUser_groups', '', '*', 'name');
973 if (is_array($rows)) {
974 foreach ($rows as $row) {
975 if (!is_array($row)) {
976 continue;
977 }
978
979 $name = trim((string)($row['name'] ?? ''));
980 if ($name === '') {
981 continue;
982 }
983
984 $label = trim((string)($row['description'] ?? ''));
985 $options[$name] = $label !== '' ? $label : $name;
986 }
987 }
988 }
989
990 foreach ($this->csv_values($current) as $value) {
991 if (!isset($options[$value])) {
993 }
994 }
995
996 return $options;
997 }
998
1006 private function csv_values($value)
1007 {
1008 if (is_array($value)) {
1009 $values = $value;
1010 } else {
1011 $values = explode(',', (string)$value);
1012 }
1013
1014 $out = array();
1015 foreach ($values as $item) {
1016 $item = trim((string)$item);
1017 if ($item !== '') {
1018 $out[] = $item;
1019 }
1020 }
1021
1022 return array_values(array_unique($out));
1023 }
1024
1033 private function backup_dd_file($modul, $dd)
1034 {
1035 $file = $this->dd_file_path($modul, $dd);
1036 if (!$file || !file_exists($file)) {
1037 return 0;
1038 }
1039
1040 $dir = dirname($file) . '/_backup';
1041 if (!is_dir($dir)) {
1042 @mkdir($dir, 0777, true);
1043 }
1044
1045 if (!is_dir($dir)) {
1046 return 0;
1047 }
1048
1049 $backup = $dir . '/' . $dd . '.' . date('Ymd-His') . '.dd.php';
1050 return @copy($file, $backup) ? 1 : 0;
1051 }
1052
1058 private function dd_params_from_request()
1059 {
1060 $modul = $this->sanitize_name(dbx()->get_modul_var('modul', ''));
1061 $dd = $this->sanitize_name(dbx()->get_modul_var('dd', ''));
1062
1063 if (!$modul) {
1064 $modul = $this->sanitize_name(dbx()->get_modul_var('xmodul', ''));
1065 }
1066
1067 if (!$modul) {
1068 $modul = $this->sanitize_name($this->get_system_var('dbx_activ_modul', 'dbx'));
1069 }
1070
1071 return array($modul, $dd);
1072 }
1073
1082 private function dd_ref($modul, $dd)
1083 {
1084 return $modul . '|' . $dd;
1085 }
1086
1095 private function dd_file_path($modul, $dd)
1096 {
1097 $file = dbx()->get_base_dir() . 'dbx/modules/' . $modul . '/dd/' . $dd . '.dd.php';
1098
1099 return dbx()->os_path($file);
1100 }
1101
1112 private function build_url($run2, $modul, $dd, $extra = array())
1113 {
1114 $url = '?dbx_modul=' . $this->_admin_modul .
1115 '&dbx_run1=edit_dd' .
1116 '&dbx_run2=' . rawurlencode($run2) .
1117 '&modul=' . rawurlencode($modul) .
1118 '&dd=' . rawurlencode($dd);
1119
1120 foreach ((array)$extra as $key => $value) {
1121 $url .= '&' . rawurlencode((string)$key) . '=' . rawurlencode((string)$value);
1122 }
1123
1124 return $url;
1125 }
1126
1135 private function get_system_var($name, $default = '')
1136 {
1137 if (function_exists('dbx')) {
1138 $obj = dbx();
1139
1140 if (is_object($obj) && method_exists($obj, 'get_system_var')) {
1141 $value = $obj->get_system_var($name);
1142 if ($value !== null && $value !== '') {
1143 return $value;
1144 }
1145 }
1146 }
1147
1148 return $default;
1149 }
1150
1160 private function merge_record($old, $post, $keys)
1161 {
1162 $record = is_array($old) ? $old : array();
1163
1164 foreach ((array)$keys as $key) {
1165 if (array_key_exists($key, (array)$post)) {
1166 $record[$key] = $this->normalize_value($post[$key]);
1167 }
1168 }
1169
1170 return $record;
1171 }
1172
1180 private function strip_editor_keys($record)
1181 {
1182 unset($record['modul']);
1183 unset($record['dd']);
1184 unset($record['field_pos']);
1185 unset($record['index_pos']);
1186 unset($record['old_name']);
1187
1188 return $record;
1189 }
1190
1198 private function normalize_value($value)
1199 {
1200 if (is_array($value)) {
1201 return implode(',', array_map('trim', $value));
1202 }
1203
1204 return trim((string)$value);
1205 }
1206
1214 private function parse_order($raw)
1215 {
1216 if (is_array($raw)) {
1217 return array_values(array_map('intval', $raw));
1218 }
1219
1220 $raw = trim((string)$raw);
1221 if ($raw === '') {
1222 return array();
1223 }
1224
1225 if (substr($raw, 0, 1) === '[') {
1226 $decoded = json_decode($raw, true);
1227 if (is_array($decoded)) {
1228 return array_values(array_map('intval', $decoded));
1229 }
1230 }
1231
1232 $parts = preg_split('/[|,\s;]+/', $raw);
1233 if (!is_array($parts)) {
1234 return array();
1235 }
1236
1237 return array_values(array_map('intval', $parts));
1238 }
1239
1248 private function reorder_records($records, $order)
1249 {
1250 $records = array_values((array)$records);
1251 $count = count($records);
1252
1253 if (!$count) {
1254 return array();
1255 }
1256
1257 if (count($order) !== $count) {
1258 return false;
1259 }
1260
1261 $seen = array();
1262 $new = array();
1263
1264 foreach ($order as $pos) {
1265 $pos = (int)$pos;
1266
1267 if ($pos < 0 || $pos >= $count || isset($seen[$pos])) {
1268 return false;
1269 }
1270
1271 $seen[$pos] = 1;
1272 $new[] = $records[$pos];
1273 }
1274
1275 return $new;
1276 }
1277
1288 private function validate_field_record($field, $fields, $self_pos, &$message)
1289 {
1290 $name = trim((string)($field['name'] ?? ''));
1291 $texts = $this->texts($this->_fd_field);
1292
1293 if (!$this->is_identifier($name)) {
1294 $message = $texts->format_fd_message('invalid_field_name', array('field' => dbx()->esc($name)));
1295 return false;
1296 }
1297
1298 $names = array();
1299 foreach (array_values((array)$fields) as $pos => $old) {
1300 if ((int)$pos === (int)$self_pos) {
1301 continue;
1302 }
1303
1304 $old_name = strtolower(trim((string)($old['name'] ?? '')));
1305 if ($old_name !== '') {
1306 $names[$old_name] = 1;
1307 }
1308 }
1309
1310 if (isset($names[strtolower($name)])) {
1311 $message = $texts->format_fd_message('duplicate_field_name', array('field' => dbx()->esc($name)));
1312 return false;
1313 }
1314
1315 return true;
1316 }
1317
1328 private function validate_index_record($index, $indexes, $self_pos, &$message)
1329 {
1330 $name = trim((string)($index['name'] ?? ''));
1331 $type = strtoupper(trim((string)($index['type'] ?? 'INDEX')));
1332 $fields = trim((string)($index['fields'] ?? ''));
1333 $texts = $this->texts($this->_fd_index);
1334
1335 if (!$this->is_identifier($name)) {
1336 $message = $texts->format_fd_message('invalid_index_name', array('index' => dbx()->esc($name)));
1337 return false;
1338 }
1339
1340 if (!$fields) {
1341 $message = $texts->get_fd_message('missing_index_field');
1342 return false;
1343 }
1344
1345 if (!in_array($type, array('PRIMARY', 'INDEX', 'UNIQUE', 'FULLTEXT'), true)) {
1346 $message = $texts->format_fd_message('invalid_index_type', array('type' => dbx()->esc($type)));
1347 return false;
1348 }
1349
1350 $names = array();
1351 foreach (array_values((array)$indexes) as $pos => $old) {
1352 if ((int)$pos === (int)$self_pos) {
1353 continue;
1354 }
1355
1356 $old_name = strtolower(trim((string)($old['name'] ?? '')));
1357 if ($old_name !== '') {
1358 $names[$old_name] = 1;
1359 }
1360 }
1361
1362 if (isset($names[strtolower($name)])) {
1363 $message = $texts->format_fd_message('duplicate_index_name', array('index' => dbx()->esc($name)));
1364 return false;
1365 }
1366
1367 return true;
1368 }
1369
1377 private function is_identifier($name)
1378 {
1379 return (bool)preg_match('/^[A-Za-z_][A-Za-z0-9_]*$/', (string)$name);
1380 }
1381
1389 private function sanitize_name($name)
1390 {
1391 $name = trim((string)$name);
1392
1393 if (!preg_match('/^[A-Za-z0-9_]+$/', $name)) {
1394 return '';
1395 }
1396
1397 return $name;
1398 }
1399
1407 private function safe_id($value)
1408 {
1409 $value = preg_replace('/[^A-Za-z0-9_]+/', '_', (string)$value);
1410 $value = trim($value, '_');
1411
1412 return $value ?: 'x';
1413 }
1414
1422 private function instance_id($seed)
1423 {
1424 return $this->safe_id($seed . '_' . substr(md5((string)$seed), 0, 6));
1425 }
1426
1435 private function alert($type, $msg)
1436 {
1437 $type = preg_replace('/[^a-z]/', '', (string)$type);
1438 if (!$type) {
1439 $type = 'info';
1440 }
1441
1442 return '<div class="alert alert-' . $type . '">' . $msg . '</div>';
1443 }
1444
1450 private function table_keys()
1451 {
1452 $oDD = dbx()->get_system_obj('dbxDD');
1453
1454 if (is_object($oDD) && method_exists($oDD, 'dd_table_schema_keys')) {
1455 return $oDD->dd_table_schema_keys();
1456 }
1457
1458 return array(
1459 'server',
1460 'table',
1461 'datadic',
1462 'primary',
1463 'language',
1464 'version',
1465 'autosync',
1466 'cache',
1467 'trash',
1468 'trace',
1469 'update_sql',
1470 'default_sort',
1471 'form-dd-table',
1472 'read',
1473 'create',
1474 'update',
1475 'delete',
1476 'read_owner',
1477 'create_owner',
1478 'update_owner',
1479 'delete_owner',
1480 );
1481 }
1482
1488 private function field_keys()
1489 {
1490 $oDD = dbx()->get_system_obj('dbxDD');
1491
1492 if (is_object($oDD) && method_exists($oDD, 'dd_field_schema_keys')) {
1493 return array_merge(array('modul', 'dd', 'field_pos', 'old_name'), $oDD->dd_field_schema_keys());
1494 }
1495
1496 return array(
1497 'modul',
1498 'dd',
1499 'field_pos',
1500 'old_name',
1501 'name',
1502 'type',
1503 'index',
1504 'length',
1505 'default',
1506 'label',
1507 'rules',
1508 'tooltip',
1509 'errormsg',
1510 'placeholder',
1511 'convert',
1512 'protect',
1513 'group',
1514 'mask',
1515 'data',
1516 'options',
1517 'tpl',
1518 'js',
1519 'prompt',
1520 );
1521 }
1522
1528 private function index_keys()
1529 {
1530 $oDD = dbx()->get_system_obj('dbxDD');
1531
1532 if (is_object($oDD) && method_exists($oDD, 'dd_index_schema_keys')) {
1533 return array_merge(array('modul', 'dd', 'index_pos', 'old_name'), $oDD->dd_index_schema_keys());
1534 }
1535
1536 return array(
1537 'modul',
1538 'dd',
1539 'index_pos',
1540 'old_name',
1541 'name',
1542 'type',
1543 'fields',
1544 'unique',
1545 'comment',
1546 );
1547 }
1548
1554 private function default_field_record()
1555 {
1556 return array(
1557 'name' => '',
1558 'type' => 'varchar',
1559 'index' => '',
1560 'length' => '255',
1561 'default' => '',
1562 'label' => '',
1563 'rules' => 'text',
1564 'tooltip' => '',
1565 'errormsg' => '',
1566 'placeholder' => '',
1567 'convert' => '',
1568 'protect' => '0',
1569 'group' => '',
1570 'mask' => '',
1571 'data' => '',
1572 'options' => '',
1573 'tpl' => 'text-label',
1574 'js' => '',
1575 'prompt' => '',
1576 );
1577 }
1578
1584 private function default_index_record()
1585 {
1586 return array(
1587 'name' => '',
1588 'type' => 'INDEX',
1589 'fields' => '',
1590 'unique' => '0',
1591 'comment' => '',
1592 );
1593 }
1594
1600 private function field_row_defaults()
1601 {
1602 return array(
1603 'modul' => '',
1604 'dd' => '',
1605 'field_pos' => '',
1606 'name' => '',
1607 'type' => '',
1608 'index' => '',
1609 'length' => '',
1610 'label' => '',
1611 );
1612 }
1613
1619 private function index_row_defaults()
1620 {
1621 return array(
1622 'modul' => '',
1623 'dd' => '',
1624 'index_pos' => '',
1625 'name' => '',
1626 'type' => '',
1627 'fields' => '',
1628 'unique' => '',
1629 'comment' => '',
1630 );
1631 }
1632}
1633
1634?>
$table['server']
Definition .dd.php:6
$indexes[]
Definition .dd.php:167
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
DBX schema administration.