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.84 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 All 160 releases
← All changes | WPDataProjects/Project/WPDP_Project_Table_Form.php +909 -1858 5.5.32.0.13 View file →
@@ -1,793 +1,355 @@
1 1 <?php
2 -
3 2 /**
4 3 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
5 4 *
6 - * @package WPDataProjects\Project
5 + * @package WPDataProjects
7 6 */
8 -namespace WPDataProjects\Project;
9 7
10 -use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist;
11 -use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists;
12 -use WPDataAccess\Plugin_Table_Models\WPDA_Table_Settings_Model;
13 -use WPDataAccess\WPDA;
14 -use WPDataProjects\Data_Dictionary\WPDP_List_Columns_Cache;
15 -use WPDataAccess\Utilities\WPDA_Message_Box;
16 -use WPDataAccess\Plugin_Table_Models\WPDP_Project_Design_Table_Model;
17 -use WPDataAccess\Utilities\WPDA_Reverse_Engineering;
18 -use WPDataProjects\WPDP;
19 -use WPDataAccess\Dashboard\WPDA_Dashboard;
20 -/**
21 - * Class WPDP_Project_Table_Form
22 - *
23 - * @author Peter Schulz
24 - * @since 2.0.0
25 - */
26 -class WPDP_Project_Table_Form {
27 - /**
28 - * Menu slug
29 - *
30 - * @var string
31 - */
32 - protected $page = null;
8 +namespace WPDataProjects\Project {
33 9
34 - /**
35 - * Database schema name
36 - *
37 - * @var string
38 - */
39 - protected $wpda_schema_name = '';
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;
40 14
41 - /**
42 - * Database schema name (as saved in column wpda_schema_name)
43 - *
44 - * Used to allow older versions of the plugin to have an empty schema name stored in column wpda_schema_name.
45 - *
46 - * @var string
47 - */
48 - protected $wpda_schema_name_db = '';
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 {
49 23
50 - /**
51 - * Database table name
52 - *
53 - * @var string
54 - */
55 - protected $wpda_table_name = null;
24 + /**
25 + * @var null
26 + */
27 + protected $page = null;
56 28
57 - /**
58 - * Options set name
59 - *
60 - * @var string
61 - */
62 - protected $wpda_table_setname = null;
29 + /**
30 + * @var null
31 + */
32 + protected $wpda_table_name = null;
33 + /**
34 + * @var array|null
35 + */
36 + protected $table_structure = null;
63 37
64 - /**
65 - * Database table structure
66 - *
67 - * @var array|null
68 - */
69 - protected $table_structure = null;
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;
70 46
71 - /**
72 - * Handle to instance of class WPDA_Dictionary_Exist
73 - *
74 - * @var object|null
75 - */
76 - protected $wpda_data_dictionary = null;
47 + /**
48 + * @var null
49 + */
50 + protected $action = null;
51 + /**
52 + * @var null|string
53 + */
54 + protected $action2 = null;
77 55
78 - /**
79 - * Handle to instance of class WPDP_List_Columns
80 - *
81 - * @var object|null
82 - */
83 - protected $wpda_list_columns = null;
56 + /**
57 + * @var null|boolean
58 + */
59 + protected $has_primary_key = null;
84 60
85 - /**
86 - * Requested action
87 - *
88 - * @var string
89 - */
90 - protected $action = null;
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 + }
91 70
92 - /**
93 - * Requested action2
94 - *
95 - * @var string
96 - */
97 - protected $action2 = null;
71 + if ( isset( $_REQUEST['action'] ) ) {
72 + $this->action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) );
73 + }
98 74
99 - /**
100 - * Indicate whether the table has a primary key
101 - *
102 - * @var boolean|null
103 - */
104 - protected $has_primary_key = null;
75 + if ( isset( $_REQUEST['action2'] ) ) {
76 + $this->action2 = sanitize_text_field( wp_unslash( $_REQUEST['action2'] ) );
77 + }
105 78
106 - /**
107 - * Available tabs
108 - *
109 - * @var array
110 - */
111 - protected $tabs;
112 -
113 - /**
114 - * Current tab
115 - *
116 - * @var
117 - */
118 - protected $current_tab;
119 -
120 - const WPNONCE_SEED = 'wpda-data-templates-';
121 -
122 - protected $wpnonce;
123 -
124 - protected $wpnonce_requested = null;
125 -
126 - /**
127 - * Table design (taken from WPDA_Design_Table_Model)
128 - *
129 - * @var array|null
130 - */
131 - protected $wpda_table_design = null;
132 -
133 - /**
134 - * WPDP_Project_Table_Form constructor
135 - */
136 - public function __construct() {
137 - global $wpdb;
138 - if ( isset( $_REQUEST['page'] ) ) {
139 - $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) );
140 - // input var okay.
141 - } else {
142 - wp_die( __( 'ERROR: Wrong arguments [missing page]', 'wp-data-access' ) );
143 - }
144 - if ( isset( $_REQUEST['action'] ) ) {
145 - $this->action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) );
146 - }
147 - if ( isset( $_REQUEST['action2'] ) ) {
148 - $this->action2 = sanitize_text_field( wp_unslash( $_REQUEST['action2'] ) );
149 - }
150 - $this->tabs = array(
151 - 'tableinfo' => __( 'Table Settings', 'wp-data-access' ),
152 - 'relation' => __( 'Relationships', 'wp-data-access' ),
153 - 'listtable' => __( 'List Table', 'wp-data-access' ),
154 - 'tableform' => __( 'Data Entry', 'wp-data-access' ),
155 - 'reconcile' => __( 'Reconcile', 'wp-data-access' ),
156 - );
157 - $this->current_tab = 'tableinfo';
158 - if ( isset( $_REQUEST['tab'] ) ) {
159 - $tab = sanitize_text_field( wp_unslash( $_REQUEST['tab'] ) );
160 - // input var okay.
161 - if ( isset( $this->tabs[$tab] ) ) {
162 - $this->current_tab = $tab;
163 - }
164 - }
165 - $this->wpnonce_requested = ( isset( $_POST['wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['wpnonce'] ) ) : null );
166 - // input var okay.
167 - if ( isset( $_REQUEST['wpda_table_name'] ) && !is_array( $_REQUEST['wpda_table_name'] ) ) {
168 - $wpda_project_design_table_model = new WPDP_Project_Design_Table_Model();
169 - if ( 'reconcile' === $this->action ) {
170 - $wpda_table_name_re = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) );
171 - // input var okay.
172 - $this->is_authorized( $wpda_table_name_re );
173 - $wpda_schema_name_re = ( isset( $_REQUEST['wpda_schema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ) : '' );
174 - // input var okay.
175 - $wpda_reverse_engineering = new WPDA_Reverse_Engineering($wpda_table_name_re, $wpda_schema_name_re);
176 - $table_structure = $wpda_reverse_engineering->get_designer_format( 'advanced' );
177 - if ( isset( $_REQUEST['keep_options'] ) ) {
178 - $param_keep_options = sanitize_text_field( wp_unslash( $_REQUEST['keep_options'] ) );
179 - } else {
180 - $param_keep_options = 'off';
181 - }
182 - $result_update = $wpda_project_design_table_model->reconcile( $table_structure, $param_keep_options );
183 - if ( false === $result_update ) {
184 - $msg = new WPDA_Message_Box(array(
185 - 'message_text' => __( 'Update failed', 'wp-data-access' ),
186 - 'message_type' => 'error',
187 - 'message_is_dismissible' => false,
188 - ));
189 - $msg->box();
190 - }
191 - if ( 0 === $result_update ) {
192 - $msg = new WPDA_Message_Box(array(
193 - 'message_text' => __( 'Nothing to save', 'wp-data-access' ),
194 - ));
195 - $msg->box();
196 - } else {
197 - $msg = new WPDA_Message_Box(array(
198 - 'message_text' => __( 'Succesfully saved changes to database', 'wp-data-access' ),
199 - ));
200 - $msg->box();
201 - }
202 - } elseif ( 'reverse_engineering' === $this->action ) {
203 - if ( isset( $_REQUEST['wpda_table_name'] ) ) {
204 - $wpda_table_name_re = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) );
205 - // input var okay.
206 - $this->is_authorized( WPDP_Project_Design_Table_Model::get_base_table_name() );
207 - $wpda_schema_name_re = ( isset( $_REQUEST['wpda_schema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ) : '' );
208 - // input var okay.
209 - $wpda_reverse_engineering = new WPDA_Reverse_Engineering($wpda_table_name_re, $wpda_schema_name_re);
210 - $table_structure = $wpda_reverse_engineering->get_designer_format( 'advanced' );
211 - if ( count( $table_structure ) > 0 ) {
212 - //phpcs:ignore - 8.1 proof
213 - $this->wpda_schema_name = $wpda_schema_name_re;
214 - $this->wpda_table_name = $wpda_table_name_re;
215 - $this->get_unique_setname();
216 - $this->wpda_table_design = $table_structure;
217 - } else {
218 - wp_die( __( 'ERROR: Reverse engineering table failed [invalid structure]', 'wp-data-access' ) );
219 - }
220 - if ( !WPDP_Project_Design_Table_Model::insert_reverse_engineered(
221 - $this->wpda_table_name,
222 - $this->wpda_table_setname,
223 - $this->wpda_table_design,
224 - $this->wpda_schema_name
225 - ) ) {
226 - $db_error = ( '' === $wpdb->last_error ? '' : ' [' . $wpdb->last_error . ']' );
227 - wp_die( __( 'ERROR: Reverse engineering table failed' . $db_error, 'wp-data-access' ) );
228 - } else {
229 - // Convert named array to object (needed to display structure).
230 - $this->wpda_table_design = json_decode( json_encode( $table_structure ) );
231 - $wpda_project_design_table_model->prepare_query( $this->wpda_table_setname );
232 - }
233 - $this->action2 = 'edit';
234 - $msg = new WPDA_Message_Box(array(
235 - 'message_text' => __( 'Table added to respository', 'wp-data-access' ),
236 - ));
237 - $msg->box();
238 - } else {
239 - wp_die( __( 'ERROR: Wrong arguments', 'wp-data-access' ) );
240 - }
241 - } elseif ( null !== $this->action2 ) {
242 - // Check authorization
243 - $table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) );
244 - // input var okay.
245 - $this->is_authorized( $table_name );
246 - $result_update = $wpda_project_design_table_model->update();
247 - if ( false === $result_update ) {
248 - $msg = new WPDA_Message_Box(array(
249 - 'message_text' => __( 'Update failed', 'wp-data-access' ),
250 - 'message_type' => 'error',
251 - 'message_is_dismissible' => false,
252 - ));
253 - $msg->box();
254 - } else {
255 - if ( 0 === $result_update ) {
256 - $msg = new WPDA_Message_Box(array(
257 - 'message_text' => __( 'Nothing to save', 'wp-data-access' ),
258 - ));
259 - $msg->box();
260 - } else {
261 - $msg = new WPDA_Message_Box(array(
262 - 'message_text' => __( 'Successfully saved changes to database', 'wp-data-access' ),
263 - ));
264 - $msg->box();
265 - }
266 - }
267 - }
268 - $wpda_project_design_table_model->query();
269 - $structure_messages = $wpda_project_design_table_model->validate();
270 - foreach ( $structure_messages as $messages ) {
271 - if ( 'ERR' === $messages[0] ) {
272 - $msg = new WPDA_Message_Box(array(
273 - 'message_text' => $messages[1],
274 - 'message_type' => 'error',
275 - 'message_is_dismissible' => false,
276 - ));
277 - $msg->box();
278 - } else {
279 - $msg = new WPDA_Message_Box(array(
280 - 'message_text' => $messages[1],
281 - ));
282 - $msg->box();
283 - }
284 - }
285 - $this->table_structure = $wpda_project_design_table_model->get_table_design();
286 - $this->wpda_table_setname = $wpda_project_design_table_model->get_table_setname();
287 - $this->wpda_schema_name = ( isset( $_REQUEST['wpda_schema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ) : '' );
288 - // input var okay.
289 - $this->wpda_schema_name_db = $this->wpda_schema_name;
290 - if ( '' === $this->wpda_schema_name ) {
291 - $this->wpda_schema_name = $wpdb->dbname;
292 - }
293 - $this->wpda_table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) );
294 - $this->wpda_data_dictionary = new WPDA_Dictionary_Exist($this->wpda_schema_name, $this->wpda_table_name);
295 - if ( $this->wpda_data_dictionary->table_exists() ) {
296 - $this->wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns(
297 - $this->wpda_schema_name,
298 - $this->wpda_table_name,
299 - 'tableform',
300 - $this->wpda_table_setname
301 - );
302 - if ( 0 === count( $this->wpda_list_columns->get_table_primary_key() ) ) {
303 - //phpcs:ignore - 8.1 proof
304 - $this->has_primary_key = false;
305 - } else {
306 - $this->has_primary_key = true;
307 - }
308 - } else {
309 - wp_die( __( 'ERROR: Invalid table name or not authorized', 'wp-data-access' ) );
310 - }
311 - } else {
312 - wp_die( __( 'ERROR: Argument wpda_table_name not found', 'wp-data-access' ) );
313 - }
314 - $this->wpnonce = wp_create_nonce( self::WPNONCE_SEED . $this->wpda_table_name );
315 - }
316 -
317 - public function prepare_form() {
318 - }
319 -
320 - protected function is_authorized( $table_name ) {
321 - if ( !wp_verify_nonce( $this->wpnonce_requested, self::WPNONCE_SEED . $table_name ) ) {
322 - wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) );
323 - }
324 - }
325 -
326 - /**
327 - * Create a unique setname for this table
328 - */
329 - public function get_unique_setname() {
330 - $this->wpda_table_setname = 'default';
331 - // Default for first options set
332 - global $wpdb;
333 - $query = "select 'x' from `%1s` where wpda_table_name = %s";
334 - $wpdb->get_results( $wpdb->prepare(
335 - $query,
336 - // phpcs:ignore WordPress.DB.PreparedSQL
337 - array(WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ), $this->wpda_table_name)
338 - ) );
339 - if ( $wpdb->num_rows > 0 ) {
340 - $query = "select 'x' from `%1s` where wpda_schema_name = %s and wpda_table_name = %s and wpda_table_setname = %s";
341 - $i = $wpdb->num_rows + 1;
342 - $this->wpda_table_setname = "options_set_{$i}";
343 - $wpdb->get_results( $wpdb->prepare(
344 - $query,
345 - // phpcs:ignore WordPress.DB.PreparedSQL
346 - array(
347 - WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ),
348 - $this->wpda_schema_name,
349 - $this->wpda_table_name,
350 - $this->wpda_table_setname
351 - )
352 - ) );
353 - while ( $wpdb->num_rows > 0 ) {
354 - // Search until a free options set is found
355 - $this->wpda_table_setname = "options_set_{$i}";
356 - $wpdb->get_results( $wpdb->prepare(
357 - $query,
358 - // phpcs:ignore WordPress.DB.PreparedSQL
359 - array(
360 - WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ),
361 - $this->wpda_schema_name,
362 - $this->wpda_table_name,
363 - $this->wpda_table_setname
364 - )
365 - ) );
366 - $i++;
367 - }
368 - }
369 - }
370 -
371 - /**
372 - * Adds tabs to page
373 - */
374 - protected function add_tabs() {
375 - ?>
376 - <div style="display:none">
377 - <?php
378 - foreach ( $this->tabs as $tab => $name ) {
379 - $form_id = esc_attr( $tab ) . '_form_id';
380 - $page = esc_attr( WPDP::PAGE_TEMPLATES );
381 - $schema_name = esc_attr( $this->wpda_schema_name_db );
382 - $table_name = esc_attr( $this->wpda_table_name );
383 - $set_name = esc_attr( $this->wpda_table_setname );
384 - $tab_value = esc_attr( $tab );
385 - $tab_form = "\n\t\t\t\t\t\t<form id='{$form_id}' method='post' action='?page={$page}&tab={$tab_value}'>\n\t\t\t\t\t\t\t<input type='hidden' name='wpda_schema_name' value='{$schema_name}'/>\n\t\t\t\t\t\t\t<input type='hidden' name='wpda_table_name' value='{$table_name}'/>\n\t\t\t\t\t\t\t<input type='hidden' name='wpda_table_setname' value='{$set_name}'/>\n\t\t\t\t\t\t\t<input type='hidden' name='wpda_table_setname_old' value='{$set_name}'/>\n\t\t\t\t\t\t\t<input type='hidden' name='action' value='edit'/>\n\t\t\t\t\t\t</form>\n\t\t\t\t\t";
386 - echo $tab_form;
387 - // phpcs:ignore WordPress.Security.EscapeOutput
388 - }
389 - ?>
390 - </div>
391 - <h2 class="nav-tab-wrapper">
392 - <?php
393 - foreach ( $this->tabs as $tab => $name ) {
394 - $class = ( $tab === $this->current_tab ? ' nav-tab-active' : '' );
395 - echo '<a class="nav-tab' . esc_attr( $class ) . '"' . ' href="javascript:void(0)"' . ' onclick="submit_form(\'' . esc_attr( $tab ) . '_form_id\')"' . '>' . esc_attr( $name ) . '</a>';
396 - }
397 - ?>
398 - </h2>
399 - <div class="wpdp-spacer"></div>
400 - <?php
401 - }
402 -
403 - /**
404 - * Show page
405 - */
406 - public function show() {
407 - ?>
408 - <div class="wrap">
409 - <?php
410 - $this->show_title();
411 - $this->add_tabs();
412 - switch ( $this->current_tab ) {
413 - case 'tableinfo':
414 - $this->show_table_info();
415 - break;
416 - case 'relation':
417 - if ( true === $this->has_primary_key ) {
418 - $this->show_relations();
419 - } else {
420 - $this->show_view();
421 - }
422 - break;
423 - case 'listtable':
424 - $this->show_list_table();
425 - break;
426 - case 'tableform':
427 - if ( true === $this->has_primary_key ) {
428 - $this->show_table_form();
429 - } else {
430 - $this->show_view();
431 - }
432 - break;
433 - case 'reconcile':
434 - $this->show_reconcile();
435 - break;
436 - }
437 - ?>
438 - </div>
439 - <script type='text/javascript'>
440 - function submit_form(form_id) {
441 - if (wpda_columns_updated===true) {
442 - if (confirm("Your changes will not be saved! Continue?")) {
443 - jQuery('#' + form_id).submit();
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' ) );
444 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();
445 140 } else {
446 - jQuery('#' + form_id).submit();
141 + wp_die( __( 'ERROR: Wrong arguments', 'wp-data-access' ) );
447 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 + }
448 170 }
449 - jQuery(function () {
450 - jQuery('#wpda_table_structure').sortable();
451 - jQuery('#wpda_list_table_options').sortable();
452 - jQuery('#wpda_form_options').sortable();
453 - jQuery('.wpda_tooltip').tooltip();
454 - });
455 - </script>
456 - <?php
457 - if ( 'view' === $this->action ) {
458 - // Set all forms to read only
459 - ?>
460 - <script type='text/javascript'>
461 - jQuery(function () {
462 - jQuery("input").attr("disabled", true);
463 - jQuery("select").attr("disabled", true);
464 - });
465 - </script>
466 - <?php
467 - } else {
468 - // Keep track of changes
469 - ?>
470 - <script type='text/javascript'>
471 - var wpda_columns_updated = false;
472 - jQuery(function () {
473 - jQuery("input").on("change", function() { wpda_columns_updated = true; });
474 - jQuery("select").on("change", function() { wpda_columns_updated = true; });
475 - });
476 - </script>
477 - <?php
478 - }
479 - }
480 171
481 - protected function show_title() {
482 - $title = 'Project Templates';
483 - ?>
484 - <h1 class="wp-heading-inline">
485 - <a
486 - href="?page=<?php
487 - echo esc_attr( $this->page );
488 - ?>"
489 - class="dashicons dashicons-arrow-left-alt2"
490 - title="<?php
491 - echo __( 'Template list', 'wp-data-access' );
492 - ?>"
493 - ></a>
494 - <?php
495 - echo esc_attr( $title );
496 - ?>
497 - </h1>
498 - <?php
499 - }
172 + $wpda_project_design_table_model->query();
173 + $structure_messages = $wpda_project_design_table_model->validate();
500 174
501 - protected function show_view() {
502 - ?>
503 - <div class="wpdp-spacer"></div>
504 - <div>
505 - <fieldset class="wpda_fieldset wpdp_fieldset">
506 - <strong>
507 - Sorry, this feature is only available for tables containing a primary or unique key.
508 - </strong>
509 - </fieldset>
510 - </div>
511 - <?php
512 - }
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 + }
513 194
514 - /**
515 - * Show table info
516 - */
517 - protected function show_table_info() {
518 - global $wpdb;
519 - if ( '' === $this->wpda_schema_name || $wpdb->dbname === $this->wpda_schema_name ) {
520 - $wpda_table_name = $this->wpda_table_name;
521 - } else {
522 - $wpda_table_name = $this->wpda_table_name . ' (' . $this->wpda_schema_name . ')';
523 - }
524 - $tab_label = ( isset( $this->table_structure->tableinfo->tab_label ) ? $this->table_structure->tableinfo->tab_label : '' );
525 - $default_where = ( isset( $this->table_structure->tableinfo->default_where ) ? $this->table_structure->tableinfo->default_where : '' );
526 - $default_orderby = ( isset( $this->table_structure->tableinfo->default_orderby ) ? $this->table_structure->tableinfo->default_orderby : '' );
527 - $hyperlinks_parent = ( isset( $this->table_structure->tableinfo->hyperlinks_parent ) ? $this->table_structure->tableinfo->hyperlinks_parent : array() );
528 - $hyperlinks_child = ( isset( $this->table_structure->tableinfo->hyperlinks_child ) ? $this->table_structure->tableinfo->hyperlinks_child : array() );
529 - $html = '';
530 - $html_child = '';
531 - $settings_db = WPDA_Table_Settings_Model::query( $this->wpda_table_name, $this->wpda_schema_name );
532 - if ( isset( $settings_db[0]['wpda_table_settings'] ) && '' !== $settings_db[0]['wpda_table_settings'] ) {
533 - $settings = json_decode( $settings_db[0]['wpda_table_settings'] );
534 - if ( isset( $settings->hyperlinks ) && is_array( $settings->hyperlinks ) ) {
535 - foreach ( $settings->hyperlinks as $hyperlink ) {
536 - if ( isset( $hyperlink->hyperlink_label ) ) {
537 - $hyperlink_label = $hyperlink->hyperlink_label;
538 - if ( isset( $hyperlinks_parent->{$hyperlink_label} ) ) {
539 - $checked = ( $hyperlinks_parent->{$hyperlink_label} ? 'checked' : '' );
540 - } else {
541 - $checked = '';
542 - }
543 - $html .= "<label style='padding-right: 10px;'>\n\t\t\t\t\t\t\t\t\t<input type='checkbox' name='{$hyperlink_label}_hyperlink' style='width: 16px; height: 16px;' {$checked}/>\n\t\t\t\t\t\t\t\t\t{$hyperlink_label}\n\t\t\t\t\t\t\t\t</label>";
544 - if ( isset( $hyperlinks_child->{$hyperlink_label} ) ) {
545 - $checked = ( $hyperlinks_child->{$hyperlink_label} ? 'checked' : '' );
546 - } else {
547 - $checked = '';
548 - }
549 - $html_child .= "<label style='padding-right: 10px;'>\n\t\t\t\t\t\t\t\t\t<input type='checkbox' name='{$hyperlink_label}_hyperlink_child' style='width: 16px; height: 16px;' {$checked}/>\n\t\t\t\t\t\t\t\t\t{$hyperlink_label}\n\t\t\t\t\t\t\t\t</label>";
550 - }
551 - }
552 - }
553 - }
554 - if ( '' === $html && '' === $html_child ) {
555 - $hint = 'Hyperlinks';
556 - } else {
557 - $hint = __( 'Hyperlinks - uncheck to disable', 'wp-data-access' ) . ' <span class="dashicons dashicons-editor-help wpda_tooltip" title="Use table settings to define where hyperlinks are shown."></span>';
558 - }
559 - if ( '' === $html ) {
560 - $html = '--';
561 - }
562 - if ( '' === $html_child ) {
563 - $html_child = '--';
564 - }
565 - ?>
566 - <form id="wpdp_form_table_info" method="post">
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 );
567 198
568 - <fieldset class="wpda_fieldset wpdp_fieldset">
569 - <legend>
570 - <label style="font-weight: normal;">
571 - <?php
572 - echo __( 'Manage table settings for table ', 'wp-data-access' );
573 - ?>
574 - </label>
575 - <label>
576 - <?php
577 - echo esc_attr( $wpda_table_name );
578 - ?>
579 - </label>
580 - </legend>
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 + }
581 213
582 - <table class="wpda-table-structure wpdp-table-structure">
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">
583 236 <tr>
584 - <td style="text-align: right; padding-right: 5px; width: 140px;" class="wpdp-label-column">
585 - <label>
586 - <?php
587 - echo __( 'Template set name', 'wp-data-access' );
588 - ?>
589 - </label>
590 - </td>
591 237 <td>
592 - <input type="text" name="wpda_table_setname" value="<?php
593 - echo esc_attr( $this->wpda_table_setname );
594 - ?>"/>
595 - <input type="hidden" name="wpda_table_setname_old" value="<?php
596 - echo esc_attr( $this->wpda_table_setname );
597 - ?>"/>
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;
598 245 </td>
599 - <td style="width:5px;"></td>
600 - <td style="width:1%;" class="icon">
601 - <i title="A template set is used to connect a table to a Data Project." class="fas fa-circle-question pointer wpda_tooltip"></i>
246 + <td style="vertical-align: top;">
247 + <label><input type="checkbox" name="keep_options">Keep options?</label>
602 248 </td>
603 249 </tr>
604 - <tr>
605 - <td style="text-align: right; padding-right: 5px;">
606 - <label>
607 - <?php
608 - echo __( 'Tab label', 'wp-data-access' );
609 - ?>
610 - </label>
611 - </td>
612 - <td>
613 - <input type="text" name="tab_label" value="<?php
614 - echo esc_attr( $tab_label );
615 - ?>"/>
616 - </td>
617 - <td style="width:5px;"></td>
618 - <td style="width:1%;" class="icon">
619 - <i title="Label shown on tab when table is shown as child table." class="fas fa-circle-question pointer wpda_tooltip"></i>
620 - </td>
621 - </tr>
622 250 </table>
623 - </fieldset>
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 + }
624 280
625 - <div class="wpdp-spacer"></div>
626 - <div class="wpdp-spacer"></div>
627 -
628 - <fieldset class="wpda_fieldset wpdp_fieldset">
629 - <legend>
630 - <label style="font-weight: normal;">
631 - <?php
632 - echo esc_attr( $hint );
633 - ?>
634 - </label>
635 - </legend>
636 -
637 - <table class="wpda-table-structure wpdp-table-structure">
638 - <tr>
639 - <td style="text-align: right; padding-right: 5px; padding-top: 10px;" class="wpdp-label-column">
640 - <label>
641 - <?php
642 - echo __( 'Hyperlinks parent', 'wp-data-access' );
643 - ?>
644 - </label>
645 - </td>
646 - <td style="padding-top: 10px;">
647 - <?php
648 - echo $html;
649 - // phpcs:ignore WordPress.Security.EscapeOutput
650 - ?>
651 - </td>
652 - </tr>
653 - <tr>
654 - <td style="text-align: right; padding-right: 5px;" class="wpdp-label-column">
655 - <label>
656 - <?php
657 - echo __( 'Hyperlinks child', 'wp-data-access' );
658 - ?>
659 - </label>
660 - </td>
661 - <td>
662 - <?php
663 - echo $html_child;
664 - // phpcs:ignore WordPress.Security.EscapeOutput
665 - ?>
666 - </td>
667 - </tr>
668 - </table>
669 - </fieldset>
670 -
671 - <?php
672 - do_action( 'wpda_data_projects_add_table_option', $this->wpda_schema_name, $this->wpda_table_name );
673 - ?>
674 -
675 - <div class="wpdp-spacer"></div>
676 - <div class="wpdp-spacer"></div>
677 -
678 - <fieldset class="wpda_fieldset wpdp_fieldset">
679 - <legend>
680 - <label style="font-weight: normal;">
681 - <?php
682 - echo __( 'Child table setting only', 'wp-data-access' );
683 - ?>
684 - <span class="dashicons dashicons-editor-help wpda_tooltip" title="These settings only affect child tables! Parent table settings are available on Data Projects page."></span>
685 - </label>
686 - </legend>
687 -
688 - <table class="wpda-table-structure wpdp-table-structure">
689 - <tr>
690 - <td style="text-align: right; padding-right: 5px;" class="wpdp-label-column">
691 - <label>
692 - <?php
693 - echo __( 'Default WHERE', 'wp-data-access' );
694 - ?>
695 - </label>
696 - </td>
697 - <td>
698 - <input type="text" name="default_where" value="<?php
699 - echo esc_attr( $default_where );
700 - ?>"/>
701 - </td>
702 - <td style="width:5px;"></td>
703 - <td style="width:1%;" class="icon">
704 - <i title="Enter a valid sql where clause, for example: name like 'Peter%'" class="fas fa-circle-question pointer wpda_tooltip"></i>
705 - </td>
706 - </tr>
707 - <tr>
708 - <td style="text-align: right; padding-right: 5px;" class="wpdp-label-column">
709 - <label>
710 - <?php
711 - echo __( 'Default ORDER BY', 'wp-data-access' );
712 - ?>
713 - </label>
714 - </td>
715 - <td>
716 - <input type="text" name="default_orderby" value="<?php
717 - echo esc_attr( $default_orderby );
718 - ?>"/>
719 - </td>
720 - <td style="width:5px;"></td>
721 - <td style="width:1%;" class="icon">
722 - <i title="Enter a valid sql order by clause, for example: course_date desc, student_name asc" class="fas fa-circle-question pointer wpda_tooltip"></i>
723 - </td>
724 - </tr>
725 - </table>
726 - </fieldset>
727 -
728 - <div class="wpdp-button-panel">
729 - <input type="hidden" name="wpda_schema_name"
730 - value="<?php
731 - echo esc_attr( $this->wpda_schema_name_db );
732 - ?>"/>
733 - <input type="hidden" name="wpda_table_name"
734 - value="<?php
735 - echo esc_attr( $this->wpda_table_name );
736 - ?>"/>
737 - <input type="hidden" name="action" value="edit"/>
738 - <input type="hidden" name="action2" value="tableinfo"/>
739 - <input type="hidden" name="wpnonce"
740 - value="<?php
741 - echo esc_attr( $this->wpnonce );
742 - ?>"/>
743 - <button type="submit" class="button button-primary">
744 - <i class="fas fa-check wpda_icon_on_button"></i>
745 - <?php
746 - echo __( 'Save table info', 'wp-data-access' );
747 - ?>
748 - </button>
749 - <a href="?page=<?php
750 - echo esc_attr( $this->page );
751 - ?>" class="button button-secondary">
752 - <i class="fas fa-times-circle wpda_icon_on_button"></i>
753 - <?php
754 - echo __( 'Back to list', 'wp-data-access' );
755 - ?>
756 - </a>
757 - </div>
758 -
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>
759 332 </form>
760 - <?php
761 - }
333 + <?php
334 + }
762 335
763 - /**
764 - * Show relationships
765 - */
766 - protected function show_relations() {
767 - $source_table_columns = $this->wpda_list_columns->get_table_columns();
768 - $target_table_names = WPDA_Dictionary_Lists::get_tables( true, $this->wpda_schema_name );
769 - $available_databases = WPDA_Dictionary_Lists::get_db_schemas();
770 - $i = 0;
771 - global $wpdb;
772 - if ( '' === $this->wpda_schema_name || $wpdb->dbname === $this->wpda_schema_name ) {
773 - $wpda_table_name = $this->wpda_table_name;
774 - } else {
775 - $wpda_table_name = $this->wpda_table_name . ' (' . $this->wpda_schema_name . ')';
776 - }
777 - ?>
778 - <script type='text/javascript'>
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">
779 345 var row_num = 0;
780 346 var col_num = [];
781 347
782 - function wpdp_get_tables(schema_name, index, target_id, selected_value, target_column_name = false, source_column_name= false) {
783 - var url = location.pathname + '?action=wpda_get_tables';
784 - var data = {
785 - wpdaschema_name: schema_name,
786 - wpda_wpnonce: '<?php
787 - echo esc_attr( wp_create_nonce( 'wpda-getdata-access-' . WPDA::get_current_user_login() ) );
788 - ?>'
789 - };
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};
790 352 jQuery.post(
791 353 url,
792 354 data,
793 355 function (data) {
@@ -793,59 +355,21 @@
793 355 function (data) {
794 356 jQuery(target_id).find('option').remove();
795 357 var jsonData = JSON.parse(data);
796 358 for (i = 0; i < jsonData.length; i++) {
797 - jQuery(target_id).append(
798 - newitem = jQuery("<option></option>")
799 - .attr("value", jsonData[i]['table_name'])
800 - .text(jsonData[i]['table_name'])
801 - );
802 - if (jsonData[i]['table_name'] === selected_value) {
803 - newitem.attr("selected", true);
804 - }
805 - }
806 - if (!target_column_name) {
807 - jQuery(target_id).trigger("change");
808 - } else {
809 - wpdp_get_columns(selected_value, index, '#target_column_name_' + index, target_column_name[0], schema_name);
810 - for (i=1; i<target_column_name.length; i++) {
811 - add_column(index, target_column_name[i]);
812 - jQuery('#source_column_name_' + index + '_' + i).val(source_column_name[i]);
813 - wpdp_get_columns(selected_value, index, '#target_column_name_' + index + '_' + i, target_column_name[i], schema_name);
814 - }
815 - }
816 - }
817 - );
818 - }
819 -
820 - function wpdp_get_columns(table_name, index, target_id, selected_value, schema_name = '') {
821 - if (schema_name === '') {
822 - schema_name = '<?php
823 - echo esc_attr( $this->wpda_schema_name );
824 - ?>';
825 - }
826 - var url = location.pathname + '?action=wpda_get_columns';
827 - var data = {
828 - wpdaschema_name: schema_name,
829 - table_name: table_name,
830 - wpda_wpnonce: '<?php
831 - echo esc_attr( wp_create_nonce( 'wpda-getdata-access-' . WPDA::get_current_user_login() ) );
832 - ?>'
833 - };
834 - jQuery.post(
835 - url,
836 - data,
837 - function (data) {
838 - jQuery(target_id).find('option').remove();
839 - var jsonData = JSON.parse(data);
840 - for (i = 0; i < jsonData.length; i++) {
841 - jQuery(target_id).append(
842 - newitem = jQuery("<option></option>")
843 - .attr("value", jsonData[i]['column_name'])
844 - .text(jsonData[i]['column_name'])
845 - );
846 359 if (jsonData[i]['column_name'] === selected_value) {
847 - newitem.attr("selected", true);
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 + );
848 372 }
849 373 }
850 374 }
851 375 );
@@ -850,9 +374,9 @@
850 374 }
851 375 );
852 376 }
853 377
854 - function add_row(relation_type, source_column_name, target_table_name, target_column_name, relation_table_name, target_schema_name = '') {
378 + function add_row(relation_type, source_column_name, target_table_name, target_column_name, relation_table_name) {
855 379 if (relation_type === undefined || relation_type === '') {
856 380 relation_type = '1n';
857 381 }
858 382 if (source_column_name === undefined || source_column_name === '') {
@@ -860,104 +384,68 @@
860 384 }
861 385 if (relation_table_name === undefined || relation_table_name === '') {
862 386 relation_table_name = '';
863 387 }
864 - if (target_schema_name === '' || target_schema_name === '<?php
865 - echo esc_attr( $this->wpda_schema_name );
866 - ?>') {
867 - target_schema_name = '<?php
868 - echo esc_attr( $this->wpda_schema_name );
869 - ?>';
870 - populate_lookup = false;
871 - } else {
872 - populate_lookup = true;
873 - }
874 - var new_row = '<tr id="relation_' + row_num + '">' +
875 - '<td class="wpda-table-structure-first-column-move wpda-table-structure-top">' +
876 - ' <span class="dashicons dashicons-move grabbable" style="padding-top:5px;"></span>' +
877 - ' <input type="hidden" name="row_num[]" value="' + row_num + '" />' +
878 - '</td>' +
879 - '<td class="wpda-table-structure-top">' +
880 - ' <select id="relation_type_' + row_num + '" name="relation_type[]" onchange="change_relationship(event, ' + row_num + ')">' +
881 - ' <option value="1n" ' + (relation_type === '1n' ? 'selected' : '') + '>1:n</option>' +
882 - ' <option value="nm" ' + (relation_type === 'nm' ? 'selected' : '') + '>n:m</option>' +
883 - ' <option value="lookup" ' + (relation_type === 'lookup' ? 'selected' : '') + '>lookup listbox</option>' +
884 - ' <option value="autocomplete" ' + (relation_type === 'autocomplete' ? 'selected' : '') + '>lookup autocomplete</option>' +
885 - ' </select>' +
886 - '</td>' +
887 - '<td class="wpda-table-structure-top">' +
888 - ' <select name="source_column_name[]" id="source_column_name_' + row_num + '">' +
889 - <?php
890 - foreach ( $source_table_columns as $column ) {
891 - ?>
892 - '<option value="<?php
893 - echo esc_attr( $column['column_name'] );
894 - ?>" ' + (source_column_name[0] === "<?php
895 - echo esc_attr( $column['column_name'] );
896 - ?>" ? "selected" : "") + '><?php
897 - echo esc_attr( $column['column_name'] );
898 - ?></option>' +
899 - <?php
900 - }
901 - ?>
902 - ' </select>' +
903 - ' <input type="hidden" name="num_source_column_name[]" id="num_source_column_name_' + row_num + '" value="0" />' +
904 - '</td>' +
905 - '<td class="wpda-table-structure-top">' +
906 - ' <a href="javascript:void(0)" style="vertical-align:-webkit-baseline-middle;padding-top:7px;"' +
907 - ' class="dashicons dashicons-plus wpda_tooltip" title="Add column"' +
908 - ' onclick="add_column(' + row_num + ')" id="remove_column_names_' + row_num + '"' +
909 - ' ></a>' +
910 - '</td>' +
911 - '<td class="wpda-table-structure-top">' +
912 - ' <select id="target_schema_name_' + row_num + '" name="target_schema_name[]" onchange="wpdp_get_tables(jQuery(this).val(), ' + row_num + ', \'#target_table_name_' + row_num + '\', \'\')" class="wpda_tooltip" title="Create lookup to other database"' + (relation_type==="lookup"||relation_type==="autocomplete" ? "" : "style=\'display:none;\'") + '>' +
913 - <?php
914 - global $wpdb;
915 - foreach ( $available_databases as $available_database ) {
916 - if ( $wpdb->dbname === $available_database['schema_name'] ) {
917 - $database_printed = "WordPress database ({$available_database['schema_name']})";
918 - } else {
919 - $database_printed = $available_database['schema_name'];
920 - }
921 - ?>
922 - '<option value="<?php
923 - echo esc_attr( $available_database['schema_name'] );
924 - ?>" ' + (target_schema_name === "<?php
925 - echo esc_attr( $available_database['schema_name'] );
926 - ?>" ? "selected" : "") + '><?php
927 - echo esc_attr( $database_printed );
928 - ?></option>' +
929 - <?php
930 - }
931 - ?>
932 - ' </select>' +
933 - ' <select id="target_table_name_' + row_num + '" name="target_table_name[]" onchange="wpdp_get_columns(jQuery(this).val(), ' + row_num + ', \'#target_column_name_' + row_num + '\', \'\', jQuery(\'#target_schema_name_' + row_num + '\').val())">' +
934 - ' <option value=""></option>' +
935 - <?php
936 - foreach ( $target_table_names as $target_table_name ) {
937 - ?>
938 - '<option value="<?php
939 - echo esc_attr( $target_table_name['table_name'] );
940 - ?>" ' + (target_table_name === "<?php
941 - echo esc_attr( $target_table_name['table_name'] );
942 - ?>" ? "selected" : "") + '><?php
943 - echo esc_attr( $target_table_name['table_name'] );
944 - ?></option>' +
945 - <?php
946 - }
947 - ?>
948 - ' </select>' +
949 - '</td>' +
950 - '<td class="wpda-table-structure-top">' +
951 - ' <select name="target_column_name[]" id="target_column_name_' + row_num + '"></select>' +
952 - '</td>' +
953 - '<td class="wpda-table-structure-top">' +
954 - ' <select name="relation_table_name_' + row_num + '" id="relation_table_name_' + row_num + '"></select>' +
955 - '</td>' +
956 - '<td class="wpda-table-structure-last-column wpda-table-structure-top">' +
957 - ' <a href="javascript:void(0)" class="dashicons dashicons-trash" onclick="rem_row(event)" style="float:right;padding-top:5px;"></a>' +
958 - '</td>' +
959 - '</tr>';
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 + `;
960 448 if (jQuery("#wpda_table_structure tr").length === 0) {
961 449 jQuery("#wpda_table_structure").append(new_row);
962 450 } else {
963 451 jQuery("#wpda_table_structure tr:last").after(new_row);
@@ -963,10 +451,9 @@
963 451 jQuery("#wpda_table_structure tr:last").after(new_row);
964 452 }
965 453 col_num[row_num] = 0;
966 454 if (jQuery('#target_table_name_' + row_num + ' option:selected').val() !== '') {
967 - // console.log(target_column_name[0])
968 - wpdp_get_columns(jQuery('#target_table_name_' + row_num).val(), row_num, '#target_column_name_' + row_num, target_column_name[0]);
455 + wpdp_get_columns(jQuery('#target_table_name_' + row_num), row_num, '#target_column_name_' + row_num, target_column_name[0]);
969 456 for (i = 1; i < target_column_name.length; i++) {
970 457 add_column(row_num, target_column_name[i]);
971 458 jQuery('#source_column_name_' + row_num + '_' + i).val(source_column_name[i]);
972 459 }
@@ -971,27 +458,23 @@
971 458 jQuery('#source_column_name_' + row_num + '_' + i).val(source_column_name[i]);
972 459 }
973 460 }
974 461 jQuery('#relation_table_name_' + row_num).append("<option value=''></option>");
975 - <?php
976 - foreach ( $target_table_names as $target_table_name ) {
977 - $option = "<option value='" . esc_attr( $target_table_name['table_name'] ) . "'>" . esc_attr( $target_table_name['table_name'] ) . '</option>';
978 - ?>
979 - jQuery('#relation_table_name_' + row_num).append("<?php
980 - echo $option;
981 - // phpcs:ignore WordPress.Security.EscapeOutput
982 - ?>");
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; ?>");
983 470 jQuery('#relation_table_name_' + row_num).val(relation_table_name);
984 - <?php
985 - }
986 - ?>
987 - if (relation_type === '1n' || relation_type === 'lookup' || relation_type === 'autocomplete') {
471 + <?php
472 + }
473 + ?>
474 + if (relation_type === '1n' || relation_type === 'lookup') {
988 475 jQuery('#relation_table_name_' + row_num).hide();
989 476 }
990 - if (populate_lookup) {
991 - // If lookup table is stored in another schema, we need to populate the listboxes with ajax calls
992 - wpdp_get_tables(jQuery('#target_schema_name_' + row_num).val(), row_num, '#target_table_name_' + row_num, target_table_name, target_column_name, source_column_name);
993 - }
994 477 row_num++;
995 478 }
996 479
997 480 function change_relationship(e, index) {
@@ -999,14 +482,8 @@
999 482 jQuery('#relation_table_name_' + index).show();
1000 483 } else {
1001 484 jQuery('#relation_table_name_' + index).hide();
1002 485 }
1003 -
1004 - if (jQuery(e.target).val() === 'lookup' || jQuery(e.target).val() === 'autocomplete') {
1005 - jQuery('#target_schema_name_' + index).show();
1006 - } else {
1007 - jQuery('#target_schema_name_' + index).hide();
1008 - }
1009 486 }
1010 487
1011 488 function rem_row(e) {
1012 489 var curr_id = e.target.parentNode.parentNode.id;
@@ -1016,58 +493,46 @@
1016 493 }
1017 494
1018 495 function add_column(index, selected_value = '') {
1019 496 var source_column_name_list =
1020 - '<select name="source_column_name_' + index + '_' + (col_num[index] + 1) + '" id="source_column_name_' + index + '_' + (col_num[index] + 1) + '">' +
1021 - <?php
1022 - foreach ( $source_table_columns as $column ) {
1023 - ?>
1024 - '<option value="<?php
1025 - echo esc_attr( $column['column_name'] );
1026 - ?>"><?php
1027 - echo esc_attr( $column['column_name'] );
1028 - ?></option>' +
1029 - <?php
1030 - }
1031 - ?>
1032 - '</select>';
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);
1033 512 jQuery(source_column_name_list).insertAfter(jQuery('#source_column_name_' + index).parent().children().last());
1034 513
1035 514 var remove_column_names_list =
1036 - '<div id="remove_column_names_' + index + '_' + (col_num[index] + 1) + '" style="padding-top:4px;">' +
1037 - ' <a href="javascript:void(0)"' +
1038 - ' style="vertical-align:-webkit-baseline-middle;padding-top:5px;"' +
1039 - ' class="dashicons dashicons-minus wpda_tooltip"' +
1040 - ' title="Remove column"' +
1041 - ' onclick="rem_column(' + index + ',' + (col_num[index] + 1) + ')"' +
1042 - ' ></a>' +
1043 - '</div>';
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 + `;
1044 525 jQuery(remove_column_names_list).insertAfter(jQuery('#remove_column_names_' + index).parent().children().last());
1045 526
1046 527 var target_column_name_list =
1047 - '<select name="target_column_name_' + index + '_' + (col_num[index] + 1) + '" id="target_column_name_' + index + '_' + (col_num[index] + 1) + '">' +
1048 - '</select>';
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 + `;
1049 533 jQuery(target_column_name_list).insertAfter(jQuery('#target_column_name_' + index).parent().children().last());
1050 - if (
1051 - jQuery('#relation_type_' + index).val()==='lookup' ||
1052 - jQuery('#relation_type_' + index).val()==='autocomplete'
1053 - ) {
1054 - // Is lookup accessing a remote database?
1055 - wpdp_get_columns(
1056 - jQuery('#target_table_name_' + index).val(),
1057 - index,
1058 - '#target_column_name_' + index + '_' + (col_num[index] + 1),
1059 - selected_value,
1060 - jQuery('#target_schema_name_' + index).val()
1061 - );
1062 - } else {
1063 - wpdp_get_columns(
1064 - jQuery('#target_table_name_' + index).val(),
1065 - index,
1066 - '#target_column_name_' + index + '_' + (col_num[index] + 1),
1067 - selected_value
1068 - );
1069 - }
534 + wpdp_get_columns(jQuery('#target_table_name_' + index), index, '#target_column_name_' + index + '_' + eval(col_num[index] + 1), selected_value);
1070 535
1071 536 col_num[index] += 1;
1072 537 jQuery('#num_source_column_name_' + index).val(col_num[index]);
1073 538 }
@@ -1072,926 +537,512 @@
1072 537 jQuery('#num_source_column_name_' + index).val(col_num[index]);
1073 538 }
1074 539
1075 540 function rem_column(index, seq) {
541 + console.log('#source_column_name_' + index + '_' + seq);
542 + jQuery('#br_source_column_name_' + index + '_' + seq).remove();
1076 543 jQuery('#source_column_name_' + index + '_' + seq).remove();
1077 544 jQuery('#remove_column_names_' + index + '_' + seq).remove();
545 + jQuery('#br_target_column_name_' + index + '_' + seq).remove();
1078 546 jQuery('#target_column_name_' + index + '_' + seq).remove();
1079 547 }
1080 548 </script>
1081 549 <form id="wpdp_form_relations" method="post">
1082 - <fieldset class="wpda_fieldset wpdp_fieldset">
1083 - <legend>
1084 - <label style="font-weight: normal;">
1085 - <?php
1086 - echo __( 'Manage relationships for table', 'wp-data-access' );
1087 - ?>
1088 - </label>
1089 - <label>
1090 - <?php
1091 - echo esc_attr( $wpda_table_name );
1092 - ?>
1093 - </label>
1094 - </legend>
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";
1095 625
1096 - <table class="wpda-table-structure">
1097 - <thead>
1098 - <tr>
1099 - <th class="wpda-table-structure-first-column-move"></th>
1100 - <th>
1101 - <?php
1102 - echo __( 'Type', 'wp-data-access' );
1103 - ?>
1104 - </th>
1105 - <th>
1106 - <?php
1107 - echo __( 'Source column name', 'wp-data-access' );
1108 - ?>
1109 - </th>
1110 - <th style="width:20px;"></th>
1111 - <th>
1112 - <?php
1113 - echo __( 'Target table name', 'wp-data-access' );
1114 - ?>
1115 - </th>
1116 - <th>
1117 - <?php
1118 - echo __( 'Target column name', 'wp-data-access' );
1119 - ?>
1120 - </th>
1121 - <th>
1122 - <span style="vertical-align:inherit">
1123 - <?php
1124 - echo __( 'Relation table name (only n:m)', 'wp-data-access' );
1125 - ?>
1126 - </span>
1127 - <span
1128 - class="dashicons dashicons-info wpda_tooltip"
1129 - title="<?php
1130 - 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' );
1131 - ?>"
1132 - style="cursor:pointer;"
1133 - ></span>
1134 - </th>
1135 - <th class="wpda-table-structure-last-column">
1136 - <a href="javascript:void(0)"
1137 - style="vertical-align:-webkit-baseline-middle;float:right;"
1138 - class="dashicons dashicons-plus add-row"
1139 - onclick="add_row()"
1140 - ></a>
1141 - </th>
1142 - </tr>
1143 - </thead>
1144 - <tbody id="wpda_table_structure">
1145 - </tbody>
1146 - </table>
1147 - </fieldset>
1148 -
1149 - <div class="wpdp-button-panel">
1150 - <input type="hidden" name="wpda_schema_name"
1151 - value="<?php
1152 - echo esc_attr( $this->wpda_schema_name_db );
1153 - ?>"/>
1154 - <input type="hidden" name="wpda_table_name"
1155 - value="<?php
1156 - echo esc_attr( $this->wpda_table_name );
1157 - ?>"/>
1158 - <input type="hidden" name="wpda_table_setname"
1159 - value="<?php
1160 - echo esc_attr( $this->wpda_table_setname );
1161 - ?>"/>
1162 - <input type="hidden" name="wpda_table_setname_old"
1163 - value="<?php
1164 - echo esc_attr( $this->wpda_table_setname );
1165 - ?>"/>
1166 - <input type="hidden" name="action" value="edit"/>
1167 - <input type="hidden" name="action2" value="relation"/>
1168 - <input type="hidden" name="wpnonce"
1169 - value="<?php
1170 - echo esc_attr( $this->wpnonce );
1171 - ?>"/>
1172 - <button type="submit" class="button button-primary">
1173 - <i class="fas fa-check wpda_icon_on_button"></i>
1174 - <?php
1175 - echo __( 'Save relationships', 'wp-data-access' );
1176 - ?>
1177 - </button>
1178 - <a href="?page=<?php
1179 - echo esc_attr( $this->page );
1180 - ?>" class="button button-secondary">
1181 - <i class="fas fa-times-circle wpda_icon_on_button"></i>
1182 - <?php
1183 - echo __( 'Back to list', 'wp-data-access' );
1184 - ?>
1185 - </a>
1186 - </div>
1187 -
1188 - </form>
1189 - <?php
1190 - if ( isset( $this->table_structure->relationships ) ) {
1191 - $relationships = $this->table_structure->relationships;
1192 - if ( 0 < count( $relationships ) ) {
1193 - //phpcs:ignore - 8.1 proof
1194 - foreach ( $relationships as $relationship ) {
1195 - ?>
1196 - <script type='text/javascript'>
1197 - <?php
1198 - if ( !is_array( $relationship->source_column_name ) ) {
1199 - $source_column_name_array = '[""]';
1200 - } else {
1201 - $source_column_name_array = wp_json_encode( $relationship->source_column_name );
1202 - }
1203 - echo 'var source_column_name_array = ' . $source_column_name_array . ";\n";
1204 - // phpcs:ignore WordPress.Security.EscapeOutput
1205 - if ( !is_array( $relationship->target_column_name ) ) {
1206 - $target_column_name_array = '[""]';
1207 - } else {
1208 - $target_column_name_array = wp_json_encode( $relationship->target_column_name );
1209 - }
1210 - echo 'var target_column_name_array = ' . $target_column_name_array . ";\n";
1211 - // phpcs:ignore WordPress.Security.EscapeOutput
1212 - if ( isset( $relationship->relation_table_name ) ) {
1213 - $relation_table_name = $relationship->relation_table_name;
1214 - } else {
1215 - $relation_table_name = '';
1216 - }
1217 - if ( isset( $relationship->target_schema_name ) ) {
1218 - $target_schema_name = $relationship->target_schema_name;
1219 - } else {
1220 - $target_schema_name = '';
1221 - }
1222 - ?>
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 + ?>
1223 638 add_row(
1224 - '<?php
1225 - echo esc_attr( $relationship->relation_type );
1226 - ?>',
639 + '<?php echo esc_attr( $relationship->relation_type ); ?>',
1227 640 source_column_name_array,
1228 - '<?php
1229 - echo esc_attr( $relationship->target_table_name );
1230 - ?>',
641 + '<?php echo esc_attr( $relationship->target_table_name ); ?>',
1231 642 target_column_name_array,
1232 - '<?php
1233 - echo esc_attr( $relation_table_name );
1234 - ?>',
1235 - '<?php
1236 - echo esc_attr( $target_schema_name );
1237 - ?>'
643 + '<?php echo esc_attr( $relation_table_name ); ?>'
1238 644 );
1239 645 </script>
1240 - <?php
1241 - }
1242 - } else {
1243 - ?>
1244 - <script type='text/javascript'>
646 + <?php
647 + }
648 + } else {
649 + ?>
650 + <script>
1245 651 add_row('', '', '', '');
1246 652 </script>
1247 - <?php
1248 - }
1249 - } else {
1250 - ?>
1251 - <script type='text/javascript'>
653 + <?php
654 + }
655 + } else {
656 + ?>
657 + <script>
1252 658 add_row('', '', '', '');
1253 659 </script>
1254 - <?php
1255 - }
1256 - }
660 + <?php
661 + }
662 + }
1257 663
1258 - /**
1259 - * Show list table settings
1260 - */
1261 - protected function show_list_table() {
1262 - if ( null === $this->table_structure ) {
1263 - return __( 'Invalid structure', 'wp-data-access' );
1264 - }
1265 - global $wpdb;
1266 - if ( '' === $this->wpda_schema_name || $wpdb->dbname === $this->wpda_schema_name ) {
1267 - $wpda_table_name = $this->wpda_table_name;
1268 - } else {
1269 - $wpda_table_name = $this->wpda_table_name . ' (' . $this->wpda_schema_name . ')';
1270 - }
1271 - ?>
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 + ?>
1272 672 <form id="wpdp_form_labels" method="post">
1273 - <fieldset class="wpda_fieldset wpdp_fieldset">
1274 - <legend>
1275 - <label style="font-weight: normal;">
1276 - <?php
1277 - echo __( 'Manage columns for list table of table', 'wp-data-access' );
1278 - ?>
1279 - </label>
1280 - <label>
1281 - <?php
1282 - echo esc_attr( $wpda_table_name );
1283 - ?>
1284 - </label>
1285 - </legend>
1286 -
1287 - <table class="wpda-table-structure">
1288 - <thead>
1289 - <tr>
1290 - <th class="wpda-table-structure-first-column-move"></th>
1291 - <th>
1292 - <?php
1293 - echo __( 'Column name', 'wp-data-access' );
1294 - ?>
1295 - </th>
1296 - <th>
1297 - <?php
1298 - echo __( 'Data type', 'wp-data-access' );
1299 - ?>
1300 - </th>
1301 - <th>
1302 - <?php
1303 - echo __( 'Key', 'wp-data-access' );
1304 - ?>
1305 - </th>
1306 - <th>
1307 - <?php
1308 - echo __( 'Mandatory', 'wp-data-access' );
1309 - ?>
1310 - </th>
1311 - <th>
1312 - <?php
1313 - echo __( 'Visible', 'wp-data-access' );
1314 - ?>
1315 - </th>
1316 - <th>
1317 - <?php
1318 - echo __( 'Label', 'wp-data-access' );
1319 - ?>
1320 - </th>
1321 - <th></th>
1322 - <th>
1323 - <?php
1324 - echo __( 'Lookup', 'wp-data-access' );
1325 - ?>
1326 - </th>
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>
1327 825 </tr>
1328 - </thead>
1329 - <tbody id="wpda_list_table_options">
1330 - <?php
1331 - $table_structure = array();
1332 - if ( isset( $this->table_structure->listtable_column_options ) ) {
1333 - $structure = $this->table_structure->listtable_column_options;
1334 - foreach ( $this->table_structure->table as $column ) {
1335 - $table_structure[$column->column_name] = $column;
1336 - }
1337 - } else {
1338 - $structure = $this->table_structure->table;
1339 - }
1340 - $i = 0;
1341 - foreach ( $structure as $column ) {
1342 - $column_name = $column->column_name;
1343 - if ( isset( $this->table_structure->listtable_column_options ) && isset( $table_structure[$column_name] ) ) {
1344 - $data_type = $table_structure[$column_name]->data_type;
1345 - $type_attribute = $table_structure[$column_name]->type_attribute;
1346 - $key = $table_structure[$column_name]->key;
1347 - $mandatory = $table_structure[$column_name]->mandatory;
1348 - $max_length = $table_structure[$column_name]->max_length;
1349 - } else {
1350 - $msg = new WPDA_Message_Box(array(
1351 - 'message_text' => __( "Column {$column_name} not found for list table", 'wp-data-access' ),
1352 - 'message_type' => 'error',
1353 - 'message_is_dismissible' => false,
1354 - ));
1355 - $msg->box();
1356 - break;
1357 - }
1358 - if ( '' === $max_length ) {
1359 - $data_type = $data_type . ' ' . $type_attribute;
1360 - } else {
1361 - $data_type = $data_type . ' (' . $max_length . ') ' . $type_attribute;
1362 - }
1363 - if ( isset( $this->table_structure->listtable_column_options ) ) {
1364 - $show_in_list = ( 'on' === $column->show ? 'checked' : '' );
1365 - $label_in_list = $column->label;
1366 - $lookup_in_list = ( isset( $column->lookup ) ? $column->lookup : '' );
1367 - } else {
1368 - $show_in_list = 'checked';
1369 - $label_in_list = ucfirst( str_replace( '_', ' ', $column_name ) );
1370 - $lookup_in_list = '';
1371 - }
1372 - $i++;
1373 - ?>
1374 - <tr id="listtable_<?php
1375 - echo esc_attr( $i );
1376 - ?>">
1377 - <td class="wpda-table-structure-first-column-move">
1378 - <span class="dashicons dashicons-move grabbable" style="float:left;"></span>
1379 - </td>
1380 - <td>
1381 - <?php
1382 - echo esc_attr( $column_name );
1383 - ?>
1384 - <input type="hidden" name="list_item_name[]"
1385 - value="<?php
1386 - echo esc_attr( $column_name );
1387 - ?>"/>
1388 - </td>
1389 - <td>
1390 - <?php
1391 - echo esc_attr( $data_type );
1392 - ?>
1393 - </td>
1394 - <td>
1395 - <?php
1396 - echo esc_attr( $key );
1397 - ?>
1398 - </td>
1399 - <td>
1400 - <?php
1401 - echo esc_attr( $mandatory );
1402 - ?>
1403 - </td>
1404 - <td>
1405 - <input type="checkbox"
1406 - name="<?php
1407 - echo esc_attr( $column_name );
1408 - ?>_show"
1409 - <?php
1410 - echo esc_attr( $show_in_list );
1411 - ?>
1412 - style="vertical-align:middle;width:16px;height:16px;"
1413 - />
1414 - </td>
1415 - <td>
1416 - <input type="text"
1417 - name="<?php
1418 - echo esc_attr( $column_name );
1419 - ?>"
1420 - value="<?php
1421 - echo esc_attr( $label_in_list );
1422 - ?>"
1423 - style="vertical-align:middle;"
1424 - />
1425 - </td>
1426 - <td></td>
1427 - <td class="wpda-table-structure-last-column">
1428 - <?php
1429 - $has_lookup = false;
1430 - if ( isset( $this->table_structure->relationships ) ) {
1431 - foreach ( $this->table_structure->relationships as $relationship ) {
1432 - if ( $column_name === $relationship->source_column_name[0] && ('lookup' === $relationship->relation_type || 'autocomplete' === $relationship->relation_type) ) {
1433 - if ( isset( $relationship->target_schema_name ) ) {
1434 - $target_schema_name = $relationship->target_schema_name;
1435 - } else {
1436 - $target_schema_name = $this->wpda_schema_name;
1437 - }
1438 - $lookup_column_list = WPDA_Dictionary_Lists::get_table_columns( $relationship->target_table_name, $target_schema_name );
1439 - ?>
1440 - <select name="<?php
1441 - echo esc_attr( $column_name );
1442 - ?>_lookup">
1443 - <?php
1444 - foreach ( $lookup_column_list as $lookup_column ) {
1445 - ?>
1446 - <option value="<?php
1447 - echo esc_attr( $lookup_column['column_name'] );
1448 - ?>"
1449 - <?php
1450 - if ( $lookup_in_list === $lookup_column['column_name'] ) {
1451 - echo 'selected';
1452 - }
1453 - ?>
1454 - >
1455 - <?php
1456 - echo esc_attr( $lookup_column['column_name'] );
1457 - ?>
1458 - </option>
1459 - <?php
1460 - }
1461 - ?>
1462 - </select>
1463 - <?php
1464 - $has_lookup = true;
1465 - }
1466 - }
1467 - }
1468 - if ( !$has_lookup ) {
1469 - echo '--';
1470 - }
1471 - ?>
1472 - </td>
1473 - </tr>
1474 - <?php
1475 - }
1476 - ?>
1477 - </tbody>
1478 - </table>
1479 - </fieldset>
1480 -
1481 - <div class="wpdp-button-panel">
1482 - <input type="hidden" name="wpda_schema_name"
1483 - value="<?php
1484 - echo esc_attr( $this->wpda_schema_name_db );
1485 - ?>"/>
1486 - <input type="hidden" name="wpda_table_name"
1487 - value="<?php
1488 - echo esc_attr( $this->wpda_table_name );
1489 - ?>"/>
1490 - <input type="hidden" name="wpda_table_setname"
1491 - value="<?php
1492 - echo esc_attr( $this->wpda_table_setname );
1493 - ?>"/>
1494 - <input type="hidden" name="wpda_table_setname_old"
1495 - value="<?php
1496 - echo esc_attr( $this->wpda_table_setname );
1497 - ?>"/>
1498 - <input type="hidden" name="action" value="edit"/>
1499 - <input type="hidden" name="action2" value="listtable"/>
1500 - <input type="hidden" name="wpnonce"
1501 - value="<?php
1502 - echo esc_attr( $this->wpnonce );
1503 - ?>"/>
1504 - <button type="submit" class="button button-primary">
1505 - <i class="fas fa-check wpda_icon_on_button"></i>
1506 - <?php
1507 - echo __( 'Save list table columns', 'wp-data-access' );
1508 - ?>
1509 - </button>
1510 - <a href="?page=<?php
1511 - echo esc_attr( $this->page );
1512 - ?>" class="button button-secondary">
1513 - <i class="fas fa-times-circle wpda_icon_on_button"></i>
1514 - <?php
1515 - echo __( 'Back to list', 'wp-data-access' );
1516 - ?>
1517 - </a>
1518 - </div>
1519 -
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>
1520 845 </form>
1521 - <?php
1522 - }
846 + <?php
847 + }
1523 848
1524 - /**
1525 - * Show data entry form settings
1526 - */
1527 - protected function show_table_form() {
1528 - if ( null === $this->table_structure ) {
1529 - return __( 'Invalid structure', 'wp-data-access' );
1530 - }
1531 - global $wpdb;
1532 - if ( '' === $this->wpda_schema_name || $wpdb->dbname === $this->wpda_schema_name ) {
1533 - $wpda_table_name = $this->wpda_table_name;
1534 - } else {
1535 - $wpda_table_name = $this->wpda_table_name . ' (' . $this->wpda_schema_name . ')';
1536 - }
1537 - ?>
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 + ?>
1538 857 <form id="wpdp_form_labels" method="post">
1539 - <fieldset class="wpda_fieldset wpdp_fieldset">
1540 - <legend>
1541 - <label style="font-weight: normal;">
1542 - <?php
1543 - echo __( 'Manage columns for data entry form for table', 'wp-data-access' );
1544 - ?>
1545 - </label>
1546 - <label>
1547 - <?php
1548 - echo esc_attr( $wpda_table_name );
1549 - ?>
1550 - </label>
1551 - </legend>
1552 -
1553 - <table class="wpda-table-structure">
1554 - <thead>
1555 - <tr>
1556 - <th class="wpda-table-structure-first-column-move"></th>
1557 - <th>
1558 - <?php
1559 - echo __( 'Column name', 'wp-data-access' );
1560 - ?>
1561 - </th>
1562 - <th>
1563 - <?php
1564 - echo __( 'Data type', 'wp-data-access' );
1565 - ?>
1566 - </th>
1567 - <th>
1568 - <?php
1569 - echo __( 'Key', 'wp-data-access' );
1570 - ?>
1571 - </th>
1572 - <th>
1573 - <?php
1574 - echo __( 'Mandatory', 'wp-data-access' );
1575 - ?>
1576 - </th>
1577 - <th>
1578 - <?php
1579 - echo __( 'Visible', 'wp-data-access' );
1580 - ?>
1581 - </th>
1582 - <th>
1583 - <?php
1584 - echo __( 'Read only', 'wp-data-access' );
1585 - ?>
1586 - </th>
1587 - <th>
1588 - <?php
1589 - echo __( 'Less', 'wp-data-access' );
1590 - ?>
1591 - </th>
1592 - <th>
1593 - <?php
1594 - echo __( 'Label', 'wp-data-access' );
1595 - ?>
1596 - </th>
1597 - <th></th>
1598 - <th>
1599 - <?php
1600 - echo __( 'Default value', 'wp-data-access' );
1601 - ?>
1602 - </th>
1603 - <th></th>
1604 - <th>
1605 - <?php
1606 - echo __( 'Lookup', 'wp-data-access' );
1607 - ?>
1608 - </th>
1609 - <th>
1610 - <span style="vertical-align:inherit">
1611 - <?php
1612 - echo __( 'ID?', 'wp-data-access' );
1613 - ?>
1614 - </span>
1615 - <span
1616 - class="dashicons dashicons-info wpda_tooltip"
1617 - title="<?php
1618 - echo __( 'Enable to hide ID in lookup', 'wp-data-access' );
1619 - ?>"
1620 - style="cursor:pointer;"
1621 - ></span>
1622 -
1623 - </th>
1624 - </tr>
1625 - </thead>
1626 - <tbody id="wpda_form_options">
1627 - <?php
1628 - $table_structure = array();
1629 - if ( isset( $this->table_structure->tableform_column_options ) ) {
1630 - $structure = $this->table_structure->tableform_column_options;
1631 - foreach ( $this->table_structure->table as $column ) {
1632 - $table_structure[$column->column_name] = $column;
1633 - }
1634 - } else {
1635 - $structure = $this->table_structure->table;
1636 - }
1637 - $i = 0;
1638 - foreach ( $structure as $column ) {
1639 - $column_name = $column->column_name;
1640 - if ( isset( $this->table_structure->tableform_column_options ) && isset( $table_structure[$column_name] ) ) {
1641 - $data_type = $table_structure[$column_name]->data_type;
1642 - $type_attribute = $table_structure[$column_name]->type_attribute;
1643 - $key = $table_structure[$column_name]->key;
1644 - $mandatory = $table_structure[$column_name]->mandatory;
1645 - $max_length = $table_structure[$column_name]->max_length;
1646 - } else {
1647 - $msg = new WPDA_Message_Box(array(
1648 - 'message_text' => __( "Column {$column_name} not found for data entry form", 'wp-data-access' ),
1649 - 'message_type' => 'error',
1650 - 'message_is_dismissible' => false,
1651 - ));
1652 - $msg->box();
1653 - break;
1654 - }
1655 - if ( '' === $max_length ) {
1656 - $data_type = $data_type . ' ' . $type_attribute;
1657 - } else {
1658 - $data_type = $data_type . ' (' . $max_length . ') ' . $type_attribute;
1659 - }
1660 - if ( isset( $this->table_structure->tableform_column_options ) ) {
1661 - $show_on_form = ( 'on' === $column->show ? 'checked' : '' );
1662 - $label_on_form = $column->label;
1663 - $lookup_in_list = ( isset( $column->lookup ) ? $column->lookup : '' );
1664 - } else {
1665 - $show_on_form = 'checked';
1666 - $label_on_form = ucfirst( str_replace( '_', ' ', $column_name ) );
1667 - $lookup_in_list = '';
1668 - }
1669 - if ( isset( $column->readonly ) ) {
1670 - $readonly_on_form = ( 'on' === $column->readonly ? 'checked' : '' );
1671 - } else {
1672 - $readonly_on_form = '';
1673 - }
1674 - if ( isset( $column->less ) ) {
1675 - $less_on_form = ( 'on' === $column->less ? 'checked' : '' );
1676 - } else {
1677 - $less_on_form = 'checked';
1678 - }
1679 - if ( isset( $column->default ) ) {
1680 - $default_on_form = esc_html( $column->default );
1681 - } else {
1682 - $default_on_form = '';
1683 - }
1684 - if ( isset( $column->hide_lookup_key ) ) {
1685 - $hide_id = ( 'on' === $column->hide_lookup_key ? 'checked' : '' );
1686 - } else {
1687 - $hide_id = 'checked';
1688 - }
1689 - $i++;
1690 - ?>
1691 - <tr id="tableform_<?php
1692 - echo esc_attr( $i );
1693 - ?>">
1694 - <td class="wpda-table-structure-first-column-move">
1695 - <span class="dashicons dashicons-move grabbable" style="float:left;"></span>
1696 - </td>
1697 - <td>
1698 - <?php
1699 - echo esc_attr( $column_name );
1700 - ?>
1701 - <input type="hidden" name="list_item_name[]"
1702 - value="<?php
1703 - echo esc_attr( $column_name );
1704 - ?>"/>
1705 - </td>
1706 - <td>
1707 - <?php
1708 - echo esc_attr( $data_type );
1709 - ?>
1710 - <?php
1711 - echo ( $this->wpda_list_columns->get_auto_increment_column_name() === $column_name ? ' (auto increment)' : '' );
1712 - ?>
1713 - </td>
1714 - <td>
1715 - <?php
1716 - echo ( 'Yes' === $key ? 'Yes' : '' );
1717 - ?>
1718 - </td>
1719 - <td>
1720 - <?php
1721 - echo ( 'Yes' === $mandatory ? 'Yes' : '' );
1722 - ?>
1723 - </td>
1724 - <td>
1725 - <?php
1726 - if ( $this->wpda_list_columns->get_auto_increment_column_name() === $column_name ) {
1727 - // Allow to hide auto_increment column
1728 - $l_key = 'No';
1729 - $l_mandatory = 'No';
1730 - } else {
1731 - $l_key = $key;
1732 - $l_mandatory = $mandatory;
1733 - }
1734 - ?>
1735 - <input type="checkbox"
1736 - name="<?php
1737 - echo esc_attr( $column_name );
1738 - ?>_show"
1739 - <?php
1740 - echo esc_attr( $show_on_form );
1741 - ?>
1742 - <?php
1743 - if ( 'Yes' === $l_key || 'Yes' === $l_mandatory ) {
1744 - echo ' disabled="disabled"';
1745 - }
1746 - ?>
1747 - style="vertical-align:middle;width:16px;height:16px;"
1748 - />
1749 - <?php
1750 - if ( 'Yes' === $l_key || 'Yes' === $l_mandatory ) {
1751 - ?>
1752 - <input name="<?php
1753 - echo esc_attr( $column_name );
1754 - ?>_show"
1755 - type="hidden"
1756 - value="true"/>
1757 - <?php
1758 - }
1759 - ?>
1760 - </td>
1761 - <td>
1762 - <input type="checkbox" name="<?php
1763 - echo esc_attr( $column_name );
1764 - ?>_readonly"
1765 - style="vertical-align:middle;width:16px;height:16px;"
1766 - <?php
1767 - echo esc_attr( $readonly_on_form );
1768 - ?>
1769 - />
1770 - </td>
1771 - <td>
1772 - <input type="checkbox" name="<?php
1773 - echo esc_attr( $column_name );
1774 - ?>_less"
1775 - style="vertical-align:middle;width:16px;height:16px;"
1776 - <?php
1777 - echo esc_attr( $less_on_form );
1778 - ?>
1779 - />
1780 - </td>
1781 - <td>
1782 - <input type="text"
1783 - name="<?php
1784 - echo esc_attr( $column_name );
1785 - ?>"
1786 - value="<?php
1787 - echo esc_attr( $label_on_form );
1788 - ?>"
1789 - style="vertical-align:middle;"
1790 - />
1791 - </td>
1792 - <td></td>
1793 - <td>
1794 - <input type="text"
1795 - name="<?php
1796 - echo esc_attr( $column_name );
1797 - ?>_default"
1798 - value="<?php
1799 - echo esc_attr( $default_on_form );
1800 - ?>"
1801 - style="vertical-align:middle;"
1802 - />
1803 - </td>
1804 - <td></td>
1805 - <td>
1806 - <?php
1807 - $has_lookup = false;
1808 - if ( isset( $this->table_structure->relationships ) ) {
1809 - foreach ( $this->table_structure->relationships as $relationship ) {
1810 - if ( $column_name === $relationship->source_column_name[0] && ('lookup' === $relationship->relation_type || 'autocomplete' === $relationship->relation_type) ) {
1811 - if ( isset( $relationship->target_schema_name ) ) {
1812 - $target_schema_name = $relationship->target_schema_name;
1813 - } else {
1814 - $target_schema_name = $this->wpda_schema_name;
1815 - }
1816 - $lookup_column_list = WPDA_Dictionary_Lists::get_table_columns( $relationship->target_table_name, $target_schema_name );
1817 - ?>
1818 - <select name="<?php
1819 - echo esc_attr( $column_name );
1820 - ?>_lookup">
1821 - <?php
1822 - foreach ( $lookup_column_list as $lookup_column ) {
1823 - ?>
1824 - <option value="<?php
1825 - echo esc_attr( $lookup_column['column_name'] );
1826 - ?>"
1827 - <?php
1828 - if ( $lookup_in_list === $lookup_column['column_name'] ) {
1829 - echo 'selected';
1830 - }
1831 - ?>
1832 - >
1833 - <?php
1834 - echo esc_attr( $lookup_column['column_name'] );
1835 - ?>
1836 - </option>
1837 - <?php
1838 - }
1839 - ?>
1840 - </select>
1841 - <?php
1842 - $has_lookup = true;
1843 - }
1844 - }
1845 - }
1846 - if ( !$has_lookup ) {
1847 - echo '--';
1848 - }
1849 - ?>
1850 - </td>
1851 - <td class="wpda-table-structure-last-column">
1852 - <?php
1853 - if ( $has_lookup ) {
1854 - ?>
1855 - <input type="checkbox"
1856 - name="<?php
1857 - echo esc_attr( $column_name );
1858 - ?>_hide_lookup_key"
1859 - <?php
1860 - echo esc_attr( $hide_id );
1861 - ?>
1862 - style="vertical-align:middle;width:16px;height:16px;"
1863 - />
1864 - <?php
1865 - }
1866 - ?>
1867 - </td>
1868 - </tr>
1869 - <?php
1870 - }
1871 - ?>
1872 - </tbody>
1873 - </table>
1874 - </fieldset>
1875 -
1876 - <div class="wpdp-button-panel">
1877 - <input type="hidden" name="wpda_schema_name"
1878 - value="<?php
1879 - echo esc_attr( $this->wpda_schema_name_db );
1880 - ?>"/>
1881 - <input type="hidden" name="wpda_table_name"
1882 - value="<?php
1883 - echo esc_attr( $this->wpda_table_name );
1884 - ?>"/>
1885 - <input type="hidden" name="wpda_table_setname"
1886 - value="<?php
1887 - echo esc_attr( $this->wpda_table_setname );
1888 - ?>"/>
1889 - <input type="hidden" name="wpda_table_setname_old"
1890 - value="<?php
1891 - echo esc_attr( $this->wpda_table_setname );
1892 - ?>"/>
1893 - <input type="hidden" name="action" value="edit"/>
1894 - <input type="hidden" name="action2" value="tableform"/>
1895 - <input type="hidden" name="wpnonce"
1896 - value="<?php
1897 - echo esc_attr( $this->wpnonce );
1898 - ?>"/>
1899 - <button type="submit" class="button button-primary">
1900 - <i class="fas fa-check wpda_icon_on_button"></i>
1901 - <?php
1902 - echo __( 'Save data entry form columns', 'wp-data-access' );
1903 - ?>
1904 - </button>
1905 - <a href="?page=<?php
1906 - echo esc_attr( $this->page );
1907 - ?>" class="button button-secondary">
1908 - <i class="fas fa-times-circle wpda_icon_on_button"></i>
1909 - <?php
1910 - echo __( 'Back to list', 'wp-data-access' );
1911 - ?>
1912 - </a>
1913 - </div>
1914 -
1915 - </form>
1916 - <?php
1917 - }
1918 -
1919 - protected function show_reconcile() {
1920 - ?>
1921 - <div class="wrap" style="padding-left:8px;">
1922 - <form method="post">
1923 - <fieldset class="wpda_fieldset wpdp_fieldset">
1924 - <legend>
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;">
1925 862 <label style="font-weight: normal;">
1926 - <?php
1927 - echo __( 'Reconcile columns for table', 'wp-data-access' );
1928 - ?>
863 + <?php echo __( 'Manage columns for data entry form for table', 'wp-data-access' ); ?>
1929 864 </label>
1930 865 <label>
1931 - <?php
1932 - echo esc_attr( $this->wpda_table_name );
1933 - ?>
866 + <?php echo esc_attr( $this->wpda_table_name ); ?>
1934 867 </label>
1935 - </legend>
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 + }
1936 1045
1937 - <div>
1938 - <strong>Update table design from database table</strong>
1939 - <ul class="wpdp-reconcile">
1940 - <li>Add new columns</li>
1941 - <li>Remove deleted columns</li>
1942 - <li>Change column settings</li>
1943 - <li>Update settings</li>
1944 - </ul>
1945 - <label style="line-height:30px;padding-bottom:8px">
1946 - <input type="checkbox" name="keep_options" style="vertical-align:middle;">
1947 - <strong>Keep options?</strong> (enable to keep settings of unchanged columns)
1948 - </label>
1949 - </div>
1950 - </fieldset>
1951 -
1952 - <div class="wpdp-spacer"></div>
1953 - <div class="wpdp-spacer"></div>
1954 -
1955 - <div class="wpdp-button-panel">
1956 - <input type="hidden" name="action" value="reconcile"/>
1957 - <input type="hidden" name="wpda_schema_name"
1958 - value="<?php
1959 - echo esc_attr( $this->wpda_schema_name_db );
1960 - ?>">
1961 - <input type="hidden" name="wpda_table_name"
1962 - value="<?php
1963 - echo esc_attr( $this->wpda_table_name );
1964 - ?>">
1965 - <input type="hidden" name="wpda_table_setname"
1966 - value="<?php
1967 - echo esc_attr( $this->wpda_table_setname );
1968 - ?>"/>
1969 - <input type="hidden" name="wpnonce"
1970 - value="<?php
1971 - echo esc_attr( $this->wpnonce );
1972 - ?>"/>
1973 - <button type="submit" class="button button-primary wpda_tooltip"
1974 - onclick="return (confirm('<?php
1975 - echo __( 'Reconcile table? Your current modifications will be lost!' );
1976 - ?>'));"
1977 - >
1978 - <i class="fas fa-check wpda_icon_on_button"></i>
1979 - <?php
1980 - echo __( 'Reconcile Table', 'wp-data-access' );
1981 - ?>
1982 - </button>
1983 - <a href="?page=<?php
1984 - echo esc_attr( $this->page );
1985 - ?>" class="button button-secondary">
1986 - <i class="fas fa-times-circle wpda_icon_on_button"></i>
1987 - <?php
1988 - echo __( 'Back to list', 'wp-data-access' );
1989 - ?>
1990 - </a>
1991 - </div>
1992 - </form>
1993 - </div>
1994 - <?php
1995 - }
1046 + }
1996 1047
1997 1048 }