PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 2.0.13
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v2.0.13
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / WPDataProjects / Project / WPDP_Project_Table_Form.php

WPDP_Project_Table_Form.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 2.0.13, at WPDataProjects/Project/WPDP_Project_Table_Form.php

1,049 lines 36.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
4 *
5 * @package WPDataProjects
6 */
7
8 namespace WPDataProjects\Project {
9
10 use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist;
11 use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists;
12 use WPDataProjects\Data_Dictionary\WPDP_List_Columns_Cache;
13 use WPDataAccess\Utilities\WPDA_Message_Box;
14
15 /**
16 * Class WPDP_Project_Table_Form
17 *
18 * @package WPDataProjects\Project
19 * @author Peter Schulz
20 * @since 2.0.0
21 */
22 class WPDP_Project_Table_Form {
23
24 /**
25 * @var null
26 */
27 protected $page = null;
28
29 /**
30 * @var null
31 */
32 protected $wpda_table_name = null;
33 /**
34 * @var array|null
35 */
36 protected $table_structure = null;
37
38 /**
39 * @var null|WPDA_Dictionary_Exist
40 */
41 protected $wpda_data_dictionary = null;
42 /**
43 * @var null|WPDP_List_Columns
44 */
45 protected $wpda_list_columns = null;
46
47 /**
48 * @var null
49 */
50 protected $action = null;
51 /**
52 * @var null|string
53 */
54 protected $action2 = null;
55
56 /**
57 * @var null|boolean
58 */
59 protected $has_primary_key = null;
60
61 /**
62 * WPDP_Project_Table_Form constructor.
63 */
64 public function __construct() {
65 if ( isset( $_REQUEST['page'] ) ) {
66 $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // input var okay.
67 } else {
68 wp_die( __( 'ERROR: Wrong arguments', 'wp-data-access' ) );
69 }
70
71 if ( isset( $_REQUEST['action'] ) ) {
72 $this->action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) );
73 }
74
75 if ( isset( $_REQUEST['action2'] ) ) {
76 $this->action2 = sanitize_text_field( wp_unslash( $_REQUEST['action2'] ) );
77 }
78
79 if ( isset( $_REQUEST['wpda_table_name'] ) && ! is_array( $_REQUEST['wpda_table_name'] ) ) {
80 $wpda_project_design_table_model = new WPDP_Project_Design_Table_Model();
81 if ( 'reconcile' === $this->action ) {
82 $wpda_table_name_re = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); // input var okay.
83 $wpda_reverse_engineering = new \WPDataAccess\Utilities\WPDA_Reverse_Engineering( $wpda_table_name_re );
84 $table_structure = $wpda_reverse_engineering->get_designer_format( 'advanced' );
85 if ( isset( $_REQUEST['keep_options'] ) ) {
86 $param_keep_options = sanitize_text_field( wp_unslash( $_REQUEST['keep_options'] ) );
87 } else {
88 $param_keep_options = 'off';
89 }
90 $result_update = $wpda_project_design_table_model->reconcile( $table_structure, $param_keep_options );
91 if ( false === $result_update ) {
92 $msg = new WPDA_Message_Box(
93 [
94 'message_text' => __( 'Update failed', 'wp-data-access' ),
95 'message_type' => 'error',
96 'message_is_dismissible' => false,
97 ]
98 );
99 $msg->box();
100 }
101 if ( 0 === $result_update ) {
102 $msg = new WPDA_Message_Box(
103 [
104 'message_text' => __( 'Nothing to save', 'wp-data-access' ),
105 ]
106 );
107 $msg->box();
108 } else {
109 $msg = new WPDA_Message_Box(
110 [
111 'message_text' => __( 'Succesfully saved changes to database', 'wp-data-access' ),
112 ]
113 );
114 $msg->box();
115 }
116 } elseif ( 'reverse_engineering' === $this->action ) {
117 if ( isset( $_REQUEST['wpda_table_name'] ) ) {
118 $wpda_table_name_re = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); // input var okay.
119 $wpda_reverse_engineering = new \WPDataAccess\Utilities\WPDA_Reverse_Engineering( $wpda_table_name_re );
120 $table_structure = $wpda_reverse_engineering->get_designer_format( 'advanced' );
121 if ( count( $table_structure ) > 0 ) {
122 $this->wpda_table_name = $wpda_table_name_re;
123 $this->wpda_table_design = $table_structure;
124 } else {
125 wp_die( __( 'ERROR: Reverse engineering table failed', 'wp-data-access' ) );
126 }
127 if ( ! WPDP_Project_Design_Table_Model::insert_reverse_engineered( $this->wpda_table_name, $this->wpda_table_design ) ) {
128 wp_die( __( 'ERROR: Reverse engineering table failed', 'wp-data-access' ) );
129 } else {
130 // Convert named array to object (needed to display structure).
131 $this->wpda_table_design = json_decode( json_encode( $table_structure ) );
132 }
133 $this->action2 = 'edit';
134 $msg = new WPDA_Message_Box(
135 [
136 'message_text' => __( 'Table added to respository', 'wp-data-access' ),
137 ]
138 );
139 $msg->box();
140 } else {
141 wp_die( __( 'ERROR: Wrong arguments', 'wp-data-access' ) );
142 }
143 } elseif ( null !== $this->action2 ) {
144 $result_update = $wpda_project_design_table_model->update();
145 if ( false === $result_update ) {
146 $msg = new WPDA_Message_Box(
147 [
148 'message_text' => __( 'Update failed', 'wp-data-access' ),
149 'message_type' => 'error',
150 'message_is_dismissible' => false,
151 ]
152 );
153 $msg->box();
154 }
155 if ( 0 === $result_update ) {
156 $msg = new WPDA_Message_Box(
157 [
158 'message_text' => __( 'Nothing to save', 'wp-data-access' ),
159 ]
160 );
161 $msg->box();
162 } else {
163 $msg = new WPDA_Message_Box(
164 [
165 'message_text' => __( 'Succesfully saved changes to database', 'wp-data-access' ),
166 ]
167 );
168 $msg->box();
169 }
170 }
171
172 $wpda_project_design_table_model->query();
173 $structure_messages = $wpda_project_design_table_model->validate();
174
175 foreach ( $structure_messages as $messages ) {
176 if ( 'ERR' === $messages[0] ) {
177 $msg = new WPDA_Message_Box(
178 [
179 'message_text' => $messages[1],
180 'message_type' => 'error',
181 'message_is_dismissible' => false,
182 ]
183 );
184 $msg->box();
185 } else {
186 $msg = new WPDA_Message_Box(
187 [
188 'message_text' => $messages[1],
189 ]
190 );
191 $msg->box();
192 }
193 }
194
195 $this->table_structure = $wpda_project_design_table_model->get_table_design();
196 $this->wpda_table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) );
197 $this->wpda_data_dictionary = new WPDA_Dictionary_Exist( '', $this->wpda_table_name );
198
199 if ( $this->wpda_data_dictionary->table_exists() ) {
200 $this->wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( '', $this->wpda_table_name, 'tableform' );
201 if ( 0 === count( $this->wpda_list_columns->get_table_primary_key() ) ) {
202 $this->has_primary_key = false;
203 } else {
204 $this->has_primary_key = true;
205 }
206 } else {
207 wp_die( __( 'ERROR: Invalid table name or not authorized', 'wp-data-access' ) );
208 }
209 } else {
210 wp_die( __( 'ERROR: Argument wpda_table_name not found', 'wp-data-access' ) );
211 }
212 }
213
214 /**
215 *
216 */
217 public function show() {
218 ?>
219 <div class="wrap">
220 <h1>
221 <a
222 href="javascript:void(0)"
223 onclick="javascript:location.href='?page=wpdp&tab=tables'"
224 style="display: inline-block; vertical-align: unset;"
225 class="dashicons dashicons-arrow-left-alt"
226 title="<?php echo __( 'Back to table list', 'wp-data-access' ); ?>"
227 ></a>&nbsp;
228 <?php echo __( 'Back to table list', 'wp-data-access' ); ?>
229 </h1>
230 <form
231 method="post"
232 action="?page=<?php echo esc_attr( $this->page ); ?>&tab=tables"
233 style="display: inline-block; vertical-align: unset;"
234 >
235 <table cellspacing="0" cellpadding="0" border="0">
236 <tr>
237 <td>
238 <input type="hidden" name="action" value="reconcile">
239 <input type="hidden" name="wpda_table_name"
240 value="<?php echo esc_attr( $this->wpda_table_name ); ?>">
241 <input type="submit"
242 value="<?php echo esc_html__( 'Reconcile Table', 'wp-data-access' ); ?>"
243 class="page-title-action">
244 &nbsp;
245 </td>
246 <td style="vertical-align: top;">
247 <label><input type="checkbox" name="keep_options">Keep options?</label>
248 </td>
249 </tr>
250 </table>
251 </form>
252 </div>
253 <?php
254 $this->show_table_info();
255 if ( true === $this->has_primary_key ) {
256 echo '<br/>';
257 $this->show_relations();
258 }
259 echo '<br/>';
260 $this->show_list_table();
261 if ( true === $this->has_primary_key ) {
262 echo '<br/>';
263 $this->show_table_form();
264 }
265 ?>
266 <script language="JavaScript">
267 jQuery(document).ready(function () {
268 jQuery('.dashicons-arrow-down').click(function (event) {
269 var curr_id = event.target.parentNode.parentNode.id;
270 jQuery('#' + curr_id).closest('tr').next().insertBefore(jQuery("#" + curr_id));
271 });
272 jQuery('.dashicons-arrow-up').click(function (event) {
273 var curr_id = event.target.parentNode.parentNode.id;
274 jQuery('#' + curr_id).closest('tr').prev().insertAfter(jQuery("#" + curr_id));
275 });
276 });
277 </script>
278 <?php
279 }
280
281 /**
282 *
283 */
284 protected function show_table_info() {
285 if ( isset( $this->table_structure->tableinfo ) && isset( $this->table_structure->tableinfo->tab_label ) ) {
286 $tab_label = $this->table_structure->tableinfo->tab_label;
287 } else {
288 $tab_label = '';
289 }
290 ?>
291 <form id="wpdp_form_table_info" method="post">
292 <table class="wpda-table-structure">
293 <thead>
294 <tr>
295 <td colspan="2" class="wpda-table-structure-first-column-left" style="text-align:left;">
296 <label style="font-weight: normal;">
297 <?php echo __( 'Manage info for table', 'wp-data-access' ); ?>
298 </label>
299 <label>
300 <?php echo esc_attr( $this->wpda_table_name ); ?>
301 </label>
302 </td>
303 </tr>
304 </thead>
305 <tbody id="wpda_table_info">
306 <tr>
307 <td style="text-align: right; padding-right: 5px; width: 120px;">
308 <label>
309 <?php echo __( 'Tab label', 'wp-data-access' ) ?>
310 </label>
311 </td>
312 <td>
313 <input type="text" name="tab_label" value="<?php echo esc_attr( $tab_label ); ?>"/>
314 </td>
315 </tr>
316 </tbody>
317 <tfoot>
318 <tr>
319 <td colspan="2">
320 <input type="hidden" name="tab" value="tables"/>
321 <input type="hidden" name="wpda_table_name"
322 value="<?php echo esc_attr( $this->wpda_table_name ); ?>"/>
323 <input type="hidden" name="action" value="edit"/>
324 <input type="hidden" name="action2" value="tableinfo"/>
325 <input type="submit"
326 class="button button-primary"
327 value="<?php echo __( 'Save table info', 'wp-data-access' ); ?>"/>
328 </td>
329 </tr>
330 </tfoot>
331 </table>
332 </form>
333 <?php
334 }
335
336 /**
337 *
338 */
339 protected function show_relations() {
340 $source_table_columns = $this->wpda_list_columns->get_table_columns();
341 $target_table_names = WPDA_Dictionary_Lists::get_tables( true );
342 $i = 0;
343 ?>
344 <script language="JavaScript">
345 var row_num = 0;
346 var col_num = [];
347
348 function wpdp_get_columns(item, index, target_id, selected_value = '') {
349 table_name = jQuery(item).val();
350 var url = location.pathname + '?action=get_columns';
351 var data = {table_name: table_name};
352 jQuery.post(
353 url,
354 data,
355 function (data) {
356 jQuery(target_id).find('option').remove();
357 var jsonData = JSON.parse(data);
358 for (i = 0; i < jsonData.length; i++) {
359 if (jsonData[i]['column_name'] === selected_value) {
360 jQuery(target_id).append(
361 jQuery("<option></option>")
362 .attr("value", jsonData[i]['column_name'])
363 .text(jsonData[i]['column_name'])
364 .attr("selected", true)
365 );
366 } else {
367 jQuery(target_id).append(
368 jQuery("<option></option>")
369 .attr("value", jsonData[i]['column_name'])
370 .text(jsonData[i]['column_name'])
371 );
372 }
373 }
374 }
375 );
376 }
377
378 function add_row(relation_type, source_column_name, target_table_name, target_column_name, relation_table_name) {
379 if (relation_type === undefined || relation_type === '') {
380 relation_type = '1n';
381 }
382 if (source_column_name === undefined || source_column_name === '') {
383 source_column_name = [''];
384 }
385 if (relation_table_name === undefined || relation_table_name === '') {
386 relation_table_name = '';
387 }
388 var new_row = `
389 <tr id="relation_${row_num}" style="vertical-align:top;">
390 <td class="wpda-table-structure-first-column-left">
391 <a href="javascript:void(0)" class="dashicons dashicons-arrow-down"></a>
392 <a href="javascript:void(0)" class="dashicons dashicons-arrow-up"></a>
393 <a href="javascript:void(0)" class="dashicons dashicons-trash" onclick="rem_row(event)"></a>
394 <input type="hidden" name="row_num[]" value="${row_num}" />
395 </td>
396 <td>
397 <select name="relation_type[]" onclick="change_relationship(event, ${row_num})">
398 <option value="1n"${ relation_type === '1n' ? ' selected' : '' }>1:n</option>
399 <option value="nm"${ relation_type === 'nm' ? ' selected' : '' }>n:m</option>
400 <option value="lookup"${ relation_type === 'lookup' ? ' selected' : '' }>lookup</option>
401 </select>
402 </td>
403 <td>
404 <select name="source_column_name[]" id="source_column_name_${row_num}">
405 <?php
406 foreach ( $source_table_columns as $column ) {
407 ?>
408 <option value="<?php echo esc_attr( $column['column_name'] ); ?>"${ source_column_name[0] === "<?php echo esc_attr( $column['column_name'] ); ?>" ? " selected" : "" }>
409 <?php echo $column['column_name']; ?>
410 </option>
411 <?php
412 }
413 ?>
414 </select>
415 <input type="hidden" name="num_source_column_name[]" id="num_source_column_name_${row_num}" value="0" />
416 </td>
417 <td>
418 <a href="javascript:void(0)"
419 style="vertical-align:-webkit-baseline-middle;"
420 class="dashicons dashicons-plus"
421 title="Add column"
422 onclick="add_column(${row_num})"
423 id="remove_column_names_${row_num}"
424 ></a>
425 </td>
426 <td>
427 <select id="target_table_name_${row_num}" name="target_table_name[]" onclick="wpdp_get_columns(this, ${row_num}, '#target_column_name_${row_num}')">
428 <option value=""></option>
429 <?php
430 foreach ( $target_table_names as $target_table_name ) {
431 ?>
432 <option value="<?php echo esc_attr( $target_table_name['table_name'] ); ?>"${ target_table_name === "<?php echo esc_attr( $target_table_name['table_name'] ); ?>" ? " selected" : "" }>
433 <?php echo esc_attr( $target_table_name['table_name'] ); ?>
434 </option>
435 <?php
436 }
437 ?>
438 </select>
439 </td>
440 <td>
441 <select name="target_column_name[]" id="target_column_name_${row_num}"></select>
442 </td>
443 <td class="wpda-table-structure-last-column">
444 <select name="relation_table_name_${row_num}" id="relation_table_name_${row_num}"></select>
445 </td>
446 </tr>
447 `;
448 if (jQuery("#wpda_table_structure tr").length === 0) {
449 jQuery("#wpda_table_structure").append(new_row);
450 } else {
451 jQuery("#wpda_table_structure tr:last").after(new_row);
452 }
453 col_num[row_num] = 0;
454 if (jQuery('#target_table_name_' + row_num + ' option:selected').val() !== '') {
455 wpdp_get_columns(jQuery('#target_table_name_' + row_num), row_num, '#target_column_name_' + row_num, target_column_name[0]);
456 for (i = 1; i < target_column_name.length; i++) {
457 add_column(row_num, target_column_name[i]);
458 jQuery('#source_column_name_' + row_num + '_' + i).val(source_column_name[i]);
459 }
460 }
461 jQuery('#relation_table_name_' + row_num).append("<option value=''></option>");
462 <?php
463 foreach ( $target_table_names as $target_table_name ) {
464 $option =
465 "<option value='" . esc_attr( $target_table_name['table_name'] ) . "'>" .
466 esc_attr( $target_table_name['table_name'] ) .
467 "</option>";
468 ?>
469 jQuery('#relation_table_name_' + row_num).append("<?php echo $option; ?>");
470 jQuery('#relation_table_name_' + row_num).val(relation_table_name);
471 <?php
472 }
473 ?>
474 if (relation_type === '1n' || relation_type === 'lookup') {
475 jQuery('#relation_table_name_' + row_num).hide();
476 }
477 row_num++;
478 }
479
480 function change_relationship(e, index) {
481 if (jQuery(e.target).val() === 'nm') {
482 jQuery('#relation_table_name_' + index).show();
483 } else {
484 jQuery('#relation_table_name_' + index).hide();
485 }
486 }
487
488 function rem_row(e) {
489 var curr_id = e.target.parentNode.parentNode.id;
490 if (confirm("Delete relationship?")) {
491 jQuery("#" + curr_id).remove();
492 }
493 }
494
495 function add_column(index, selected_value = '') {
496 var source_column_name_list =
497 `
498 <br id="br_source_column_name_${index}_${col_num[index] + 1}"/>
499 <select name="source_column_name_${index}_${col_num[index] + 1}" id="source_column_name_${index}_${col_num[index] + 1}">
500 <?php
501 foreach ( $source_table_columns as $column ) {
502 ?>
503 <option value="<?php echo esc_attr( $column['column_name'] ); ?>">
504 <?php echo $column['column_name']; ?>
505 </option>
506 <?php
507 }
508 ?>
509 </select>
510 `;
511 //jQuery(source_column_name_list).insertAfter('#source_column_name_' + index);
512 jQuery(source_column_name_list).insertAfter(jQuery('#source_column_name_' + index).parent().children().last());
513
514 var remove_column_names_list =
515 `
516 <div id="remove_column_names_${index}_${col_num[index] + 1}" style="padding-top:4px;">
517 <a href="javascript:void(0)"
518 style="vertical-align:-webkit-baseline-middle;"
519 class="dashicons dashicons-minus"
520 title="Remove column"
521 onclick="rem_column(${index},${col_num[index] + 1})"
522 ></a>
523 </div>
524 `;
525 jQuery(remove_column_names_list).insertAfter(jQuery('#remove_column_names_' + index).parent().children().last());
526
527 var target_column_name_list =
528 `
529 <br id="br_target_column_name_${index}_${col_num[index] + 1}"/>
530 <select name="target_column_name_${index}_${col_num[index] + 1}" id="target_column_name_${index}_${col_num[index] + 1}">
531 </select>
532 `;
533 jQuery(target_column_name_list).insertAfter(jQuery('#target_column_name_' + index).parent().children().last());
534 wpdp_get_columns(jQuery('#target_table_name_' + index), index, '#target_column_name_' + index + '_' + eval(col_num[index] + 1), selected_value);
535
536 col_num[index] += 1;
537 jQuery('#num_source_column_name_' + index).val(col_num[index]);
538 }
539
540 function rem_column(index, seq) {
541 console.log('#source_column_name_' + index + '_' + seq);
542 jQuery('#br_source_column_name_' + index + '_' + seq).remove();
543 jQuery('#source_column_name_' + index + '_' + seq).remove();
544 jQuery('#remove_column_names_' + index + '_' + seq).remove();
545 jQuery('#br_target_column_name_' + index + '_' + seq).remove();
546 jQuery('#target_column_name_' + index + '_' + seq).remove();
547 }
548 </script>
549 <form id="wpdp_form_relations" method="post">
550 <table class="wpda-table-structure">
551 <thead>
552 <tr>
553 <td colspan="5" class="wpda-table-structure-first-column-left" style="text-align:left;">
554 <label style="font-weight: normal;">
555 <?php echo __( 'Manage relationships for table', 'wp-data-access' ); ?>
556 </label>
557 <label>
558 <?php echo esc_attr( $this->wpda_table_name ); ?>
559 </label>
560 </td>
561 </tr>
562 <tr>
563 <th class="wpda-table-structure-first-column-left" style="text-align:right;">
564 <a href="javascript:void(0)"
565 style="vertical-align:-webkit-baseline-middle;"
566 class="dashicons dashicons-plus add-row"
567 onclick="add_row()"
568 ></a>
569 </th>
570 <th>
571 <?php echo __( 'Type', 'wp-data-access' ) ?>
572 </th>
573 <th>
574 <?php echo __( 'Source column name', 'wp-data-access' ) ?>
575 </th>
576 <th style="width:20px;"></th>
577 <th>
578 <?php echo __( 'Target table name', 'wp-data-access' ) ?>
579 </th>
580 <th>
581 <?php echo __( 'Target column name', 'wp-data-access' ) ?>
582 </th>
583 <th>
584 <?php echo __( 'Relation table name (only n:m)', 'wp-data-access' ) ?>
585 <span
586 class="dashicons dashicons-info"
587 title="<?php echo __( 'Table shown on the other end of the n:m relationship (instead of target table shown for 1:n relationships). Not available for 1:n relationships.', 'wp-data-access' ) ?>"
588 style="cursor:pointer;"
589 ></span>
590 </th>
591 </tr>
592 </thead>
593 <tbody id="wpda_table_structure">
594 </tbody>
595 <tfoot>
596 <tr>
597 <td colspan="7">
598 <input type="hidden" name="tab" value="tables"/>
599 <input type="hidden" name="wpda_table_name"
600 value="<?php echo esc_attr( $this->wpda_table_name ); ?>"/>
601 <input type="hidden" name="action" value="edit"/>
602 <input type="hidden" name="action2" value="relation"/>
603 <input type="submit"
604 class="button button-primary"
605 value="<?php echo __( 'Save relationships', 'wp-data-access' ); ?>"/>
606 </td>
607 </tr>
608 </tfoot>
609 </table>
610 </form>
611 <?php
612 if ( isset( $this->table_structure->relationships ) ) {
613 $relationships = $this->table_structure->relationships;
614 if ( 0 < count( $relationships ) ) {
615 foreach ( $relationships as $relationship ) {
616 ?>
617 <script>
618 <?php
619 if ( ! is_array( $relationship->source_column_name ) ) {
620 $source_column_name_array = '[""]';
621 } else {
622 $source_column_name_array = wp_json_encode( $relationship->source_column_name );
623 }
624 echo "var source_column_name_array = " . $source_column_name_array . ";\n";
625
626 if ( ! is_array( $relationship->target_column_name ) ) {
627 $target_column_name_array = '[""]';
628 } else {
629 $target_column_name_array = wp_json_encode( $relationship->target_column_name );
630 }
631 echo "var target_column_name_array = " . $target_column_name_array . ";\n";
632 if ( isset( $relationship->relation_table_name ) ) {
633 $relation_table_name = $relationship->relation_table_name;
634 } else {
635 $relation_table_name = '';
636 }
637 ?>
638 add_row(
639 '<?php echo esc_attr( $relationship->relation_type ); ?>',
640 source_column_name_array,
641 '<?php echo esc_attr( $relationship->target_table_name ); ?>',
642 target_column_name_array,
643 '<?php echo esc_attr( $relation_table_name ); ?>'
644 );
645 </script>
646 <?php
647 }
648 } else {
649 ?>
650 <script>
651 add_row('', '', '', '');
652 </script>
653 <?php
654 }
655 } else {
656 ?>
657 <script>
658 add_row('', '', '', '');
659 </script>
660 <?php
661 }
662 }
663
664 /**
665 * @return mixed
666 */
667 protected function show_list_table() {
668 if ( null === $this->table_structure ) {
669 return __( 'Invalid structure', 'wp-data-access' );
670 }
671 ?>
672 <form id="wpdp_form_labels" method="post">
673 <table class="wpda-table-structure">
674 <thead>
675 <tr>
676 <td colspan="8" class="wpda-table-structure-first-column-left" style="text-align:left;">
677 <label style="font-weight: normal;">
678 <?php echo __( 'Manage columns for list table of table', 'wp-data-access' ); ?>
679 </label>
680 <label>
681 <?php echo esc_attr( $this->wpda_table_name ); ?>
682 </label>
683 </td>
684 </tr>
685 <tr>
686 <th class="wpda-table-structure-first-column-left"></th>
687 <th>
688 <?php echo __( 'Column name', 'wp-data-access' ) ?>
689 </th>
690 <th>
691 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
692 </th>
693 <th>
694 <?php echo __( 'Data type', 'wp-data-access' ) ?>
695 </th>
696 <th>
697 <?php echo __( 'Key?', 'wp-data-access' ) ?>
698 </th>
699 <th>
700 <?php echo __( 'Mandatory?', 'wp-data-access' ) ?>
701 </th>
702 <th></th>
703 <th>
704 <?php echo __( 'List label (uncheck to hide column)', 'wp-data-access' ) ?>
705 </th>
706 <th></th>
707 <th>
708 <?php echo __( 'Lookup', 'wp-data-access' ) ?>
709 </th>
710 </tr>
711 </thead>
712 <tbody>
713 <?php
714 $table_structure = [];
715 if ( isset( $this->table_structure->listtable_column_options ) ) {
716 $structure = $this->table_structure->listtable_column_options;
717 foreach ( $this->table_structure->table as $column ) {
718 $table_structure[ $column->column_name ] = $column;
719 }
720 } else {
721 $structure = $this->table_structure->table;
722 }
723 $i = 0;
724 foreach ( $structure as $column ) {
725 $column_name = $column->column_name;
726 if ( isset( $this->table_structure->listtable_column_options ) && isset ( $table_structure[ $column_name ] ) ) {
727 $data_type = $table_structure[ $column_name ]->data_type;
728 $type_attribute = $table_structure[ $column_name ]->type_attribute;
729 $key = $table_structure[ $column_name ]->key;
730 $mandatory = $table_structure[ $column_name ]->mandatory;
731 $max_length = $table_structure[ $column_name ]->max_length;
732 } else {
733 $msg = new WPDA_Message_Box(
734 [
735 'message_text' => __( "Column $column_name not found for list table", 'wp-data-access' ),
736 'message_type' => 'error',
737 'message_is_dismissible' => false,
738 ]
739 );
740 $msg->box();
741 break;
742 }
743 if ( '' === $max_length ) {
744 $data_type = $data_type . ' ' . $type_attribute;
745 } else {
746 $data_type = $data_type . ' (' . $max_length . ') ' . $type_attribute;
747 }
748 if ( isset( $this->table_structure->listtable_column_options ) ) {
749 $show_in_list = 'on' === $column->show ? 'checked' : '';
750 $label_in_list = $column->label;
751 $lookup_in_list = isset( $column->lookup ) ? $column->lookup : '';
752 } else {
753 $show_in_list = 'checked';
754 $label_in_list = ucfirst( str_replace( '_', ' ', $column_name ) );
755 $lookup_in_list = '';
756 }
757 $i++;
758 ?>
759 <tr id="listtable_<?php echo esc_attr( $i ); ?>">
760 <td class="wpda-table-structure-first-column-left">
761 <a href="javascript:void(0)" class="dashicons dashicons-arrow-down"></a>
762 <a href="javascript:void(0)" class="dashicons dashicons-arrow-up"></a>
763 </td>
764 <td>
765 <?php echo esc_attr( $column_name ); ?>
766 <input type="hidden" name="list_item_name[]"
767 value="<?php echo esc_attr( $column_name ); ?>"/>
768 </td>
769 <td></td>
770 <td>
771 <?php echo esc_attr( $data_type ); ?>
772 </td>
773 <td>
774 <?php echo esc_attr( $key ); ?>
775 </td>
776 <td>
777 <?php echo esc_attr( $mandatory ); ?>
778 </td>
779 <td style="text-align:right;width:16px;">
780 <input type="checkbox"
781 name="<?php echo esc_attr( $column_name ); ?>_show"
782 <?php echo esc_attr( $show_in_list ); ?>
783 style="vertical-align:middle;width:16px;height:16px;"
784 />
785 </td>
786 <td>
787 <input type="text"
788 name="<?php echo esc_attr( $column_name ); ?>"
789 value="<?php echo esc_attr( $label_in_list ); ?>"
790 style="vertical-align:middle;"
791 />
792 </td>
793 <td></td>
794 <td class="wpda-table-structure-last-column">
795 <?php
796 $has_lookup = false;
797 if ( isset( $this->table_structure->relationships ) ) {
798 foreach ( $this->table_structure->relationships as $relationship ) {
799 if ( $column_name === $relationship->source_column_name[0] && 'lookup' === $relationship->relation_type ) {
800 $lookup_column_list = WPDA_Dictionary_Lists::get_table_columns( $relationship->target_table_name );
801 ?>
802 <select name="<?php echo esc_attr( $column_name ); ?>_lookup">
803 <?php
804 foreach ( $lookup_column_list as $lookup_column ) {
805 ?>
806 <option value="<?php echo esc_attr( $lookup_column['column_name'] ); ?>"
807 <?php if ( $lookup_in_list === $lookup_column['column_name'] ) { echo 'selected'; } ?>
808 >
809 <?php echo $lookup_column['column_name']; ?>
810 </option>
811 <?php
812 }
813 ?>
814 </select>
815 <?php
816 $has_lookup = true;
817 }
818 }
819 }
820 if ( ! $has_lookup ) {
821 echo '--';
822 }
823 ?>
824 </td>
825 </tr>
826 <?php
827 }
828 ?>
829 </tbody>
830 <tfoot>
831 <tr>
832 <td colspan="8">
833 <input type="hidden" name="tab" value="tables"/>
834 <input type="hidden" name="wpda_table_name"
835 value="<?php echo esc_attr( $this->wpda_table_name ); ?>"/>
836 <input type="hidden" name="action" value="edit"/>
837 <input type="hidden" name="action2" value="listtable"/>
838 <input type="submit"
839 class="button button-primary"
840 value="<?php echo __( 'Save list table columns', 'wp-data-access' ); ?>"/>
841 </td>
842 </tr>
843 </tfoot>
844 </table>
845 </form>
846 <?php
847 }
848
849 /**
850 * @return mixed
851 */
852 protected function show_table_form() {
853 if ( null === $this->table_structure ) {
854 return __( 'Invalid structure', 'wp-data-access' );
855 }
856 ?>
857 <form id="wpdp_form_labels" method="post">
858 <table class="wpda-table-structure">
859 <thead>
860 <tr>
861 <td colspan="8" class="wpda-table-structure-first-column-left" style="text-align:left;">
862 <label style="font-weight: normal;">
863 <?php echo __( 'Manage columns for data entry form for table', 'wp-data-access' ); ?>
864 </label>
865 <label>
866 <?php echo esc_attr( $this->wpda_table_name ); ?>
867 </label>
868 </td>
869 </tr>
870 <tr>
871 <th class="wpda-table-structure-first-column-left"></th>
872 <th>
873 <?php echo __( 'Column name', 'wp-data-access' ) ?>
874 </th>
875 <th>
876 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
877 </th>
878 <th>
879 <?php echo __( 'Data type', 'wp-data-access' ) ?>
880 </th>
881 <th>
882 <?php echo __( 'Key?', 'wp-data-access' ) ?>
883 </th>
884 <th>
885 <?php echo __( 'Mandatory?', 'wp-data-access' ) ?>
886 </th>
887 <th></th>
888 <th>
889 <?php echo __( 'Form label (uncheck to hide column)', 'wp-data-access' ) ?>
890 </th>
891 <th></th>
892 <th>
893 <?php echo __( 'Lookup', 'wp-data-access' ) ?>
894 </th>
895 </tr>
896 </thead>
897 <tbody>
898 <?php
899 $table_structure = [];
900 if ( isset( $this->table_structure->tableform_column_options ) ) {
901 $structure = $this->table_structure->tableform_column_options;
902 foreach ( $this->table_structure->table as $column ) {
903 $table_structure[ $column->column_name ] = $column;
904 }
905 } else {
906 $structure = $this->table_structure->table;
907 }
908 $i = 0;
909 foreach ( $structure as $column ) {
910 $column_name = $column->column_name;
911 if ( isset( $this->table_structure->tableform_column_options ) && isset ( $table_structure[ $column_name ] ) ) {
912 $data_type = $table_structure[ $column_name ]->data_type;
913 $type_attribute = $table_structure[ $column_name ]->type_attribute;
914 $key = $table_structure[ $column_name ]->key;
915 $mandatory = $table_structure[ $column_name ]->mandatory;
916 $max_length = $table_structure[ $column_name ]->max_length;
917 } else {
918 $msg = new WPDA_Message_Box(
919 [
920 'message_text' => __( "Column $column_name not found for data entry form", 'wp-data-access' ),
921 'message_type' => 'error',
922 'message_is_dismissible' => false,
923 ]
924 );
925 $msg->box();
926 break;
927 }
928 if ( '' === $max_length ) {
929 $data_type = $data_type . ' ' . $type_attribute;
930 } else {
931 $data_type = $data_type . ' (' . $max_length . ') ' . $type_attribute;
932 }
933 if ( isset( $this->table_structure->tableform_column_options ) ) {
934 $show_on_form = 'on' === $column->show ? 'checked' : '';
935 $label_on_form = $column->label;
936 $lookup_in_list = isset( $column->lookup ) ? $column->lookup : '';
937 } else {
938 $show_on_form = 'checked';
939 $label_on_form = ucfirst( str_replace( '_', ' ', $column_name ) );
940 $lookup_in_list = '';
941 }
942 if ( $this->wpda_list_columns->get_auto_increment_column_name() === $column_name ) {
943 // Allow to hide auto_increment column.
944 $key = 'No';
945 $mandatory = 'No';
946 }
947 $i++;
948 ?>
949 <tr id="tableform_<?php echo esc_attr( $i ); ?>">
950 <td class="wpda-table-structure-first-column-left">
951 <a href="javascript:void(0)" class="dashicons dashicons-arrow-down"></a>
952 <a href="javascript:void(0)" class="dashicons dashicons-arrow-up"></a>
953 </td>
954 <td>
955 <?php echo esc_attr( $column_name ); ?>
956 <input type="hidden" name="list_item_name[]"
957 value="<?php echo esc_attr( $column_name ); ?>"/>
958 </td>
959 <td></td>
960 <td>
961 <?php echo esc_attr( $data_type ); ?>
962 </td>
963 <td>
964 <?php echo esc_attr( $key ); ?>
965 </td>
966 <td>
967 <?php echo esc_attr( $mandatory ); ?>
968 </td>
969 <td style="text-align:right;width:16px;">
970 <input type="checkbox"
971 name="<?php echo esc_attr( $column_name ); ?>_show"
972 <?php echo esc_attr( $show_on_form ); ?>
973 <?php if ( 'Yes' === $key || 'Yes' === $mandatory ) {
974 echo ' disabled="disabled"';
975 } ?>
976 style="vertical-align:middle;width:16px;height:16px;"
977 />
978 <?php if ( 'Yes' === $key || 'Yes' === $mandatory ) { ?>
979 <input name="<?php echo esc_attr( $column_name ); ?>_show" type="hidden"
980 value="true"/>
981 <?php } ?>
982 </td>
983 <td>
984 <input type="text"
985 name="<?php echo esc_attr( $column_name ); ?>"
986 value="<?php echo esc_attr( $label_on_form ); ?>"
987 style="vertical-align:middle;"
988 />
989 </td>
990 <td></td>
991 <td class="wpda-table-structure-last-column">
992 <?php
993 $has_lookup = false;
994 if ( isset( $this->table_structure->relationships ) ) {
995 foreach ( $this->table_structure->relationships as $relationship ) {
996 if ( $column_name === $relationship->source_column_name[0] && 'lookup' === $relationship->relation_type ) {
997 $lookup_column_list = WPDA_Dictionary_Lists::get_table_columns( $relationship->target_table_name );
998 ?>
999 <select name="<?php echo esc_attr( $column_name ); ?>_lookup">
1000 <?php
1001 foreach ( $lookup_column_list as $lookup_column ) {
1002 ?>
1003 <option value="<?php echo esc_attr( $lookup_column['column_name'] ); ?>"
1004 <?php if ( $lookup_in_list === $lookup_column['column_name'] ) { echo 'selected'; } ?>
1005 >
1006 <?php echo $lookup_column['column_name']; ?>
1007 </option>
1008 <?php
1009 }
1010 ?>
1011 </select>
1012 <?php
1013 $has_lookup = true;
1014 }
1015 }
1016 }
1017 if ( ! $has_lookup ) {
1018 echo '--';
1019 }
1020 ?>
1021 </td>
1022 </tr>
1023 <?php
1024 }
1025 ?>
1026 </tbody>
1027 <tfoot>
1028 <tr>
1029 <td colspan="8">
1030 <input type="hidden" name="tab" value="tables"/>
1031 <input type="hidden" name="wpda_table_name"
1032 value="<?php echo esc_attr( $this->wpda_table_name ); ?>"/>
1033 <input type="hidden" name="action" value="edit"/>
1034 <input type="hidden" name="action2" value="tableform"/>
1035 <input type="submit"
1036 class="button button-primary"
1037 value="<?php echo __( 'Save data entry form columns', 'wp-data-access' ); ?>"/>
1038 </td>
1039 </tr>
1040 </tfoot>
1041 </table>
1042 </form>
1043 <?php
1044 }
1045
1046 }
1047
1048 }
1049