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

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

492 lines 15.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
5 *
6 * @package WPDataProjects\Project
7 */
8
9 namespace WPDataProjects\Project {
10
11 use WPDataAccess\Dashboard\WPDA_Dashboard;
12 use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Exist;
13 use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists;
14 use WPDataAccess\List_Table\WPDA_List_Table;
15 use WPDataAccess\Plugin_Table_Models\WPDP_Project_Design_Table_Model;
16 use WPDataAccess\Utilities\WPDA_Message_Box;
17 use WPDataAccess\WPDA;
18
19 /**
20 * Class WPDP_Project_Table_List extends WPDA_List_Table
21 *
22 * @see WPDA_List_Table
23 *
24 * @author Peter Schulz
25 * @since 2.0.0
26 */
27 class WPDP_Project_Table_List extends WPDA_List_Table {
28
29 /**
30 * WPDP_Project_Table_List constructor
31 *
32 * Get and sort list of all database tables
33 *
34 * @param array $args
35 */
36 public function __construct( array $args = array() ) {
37 $args['column_headers'] = self::column_headers_labels();
38 $args['title'] = '';
39 $args['allow_import'] = 'off';
40 $args['allow_insert'] = 'off';
41
42 parent::__construct( $args );
43 }
44
45 /**
46 * Overwrite method add_header_button to add reverse engineering
47 */
48 protected function add_header_button() {
49 global $wpdb;
50
51 // Check table access to prepare table listbox content
52 $user_default_schema = WPDA::get_user_default_scheme();
53 if ( $wpdb->dbname === $user_default_schema ) {
54 $table_access = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS );
55 } else {
56 $table_access = get_option( WPDA::BACKEND_OPTIONNAME_DATABASE_ACCESS . $user_default_schema );
57 }
58 if ( false === $table_access ) {
59 $table_access = 'show';
60 }
61
62 // Get available databases
63 $schema_names = WPDA_Dictionary_Lists::get_db_schemas();
64 switch ( $table_access ) {
65 case 'show':
66 $tables = $this->get_all_db_tables( $user_default_schema );
67 break;
68 case 'hide':
69 $tables = $this->get_all_db_tables( $user_default_schema );
70 // Remove WordPress tables from listbox content
71 $tables_named = array();
72 foreach ( $tables as $table ) {
73 $tables_named[ $table ] = true;
74 }
75 foreach ( $wpdb->tables( 'all', true ) as $wp_table ) {
76 unset( $tables_named[ $wp_table ] );
77 }
78 $tables = array();
79 foreach ( $tables_named as $key => $value ) {
80 array_push( $tables, $key );//phpcs:ignore - 8.1 proof
81 }
82 break;
83 default:
84 // Show only selected tables and views
85 if ( $wpdb->dbname === $user_default_schema ) {
86 $tables = WPDA::get_option( WPDA::OPTION_BE_TABLE_ACCESS_SELECTED );
87 } else {
88 $tables = get_option( WPDA::BACKEND_OPTIONNAME_DATABASE_SELECTED . $user_default_schema );
89 }
90 if ( false === $tables ) {
91 $tables = '';
92 }
93 }
94
95 $wpnonce = wp_create_nonce( WPDP_Project_Table_Form::WPNONCE_SEED . WPDP_Project_Design_Table_Model::get_base_table_name() );
96 ?>
97 <form
98 method="post"
99 action="?page=<?php echo esc_attr( $this->page ); ?>&tab=tables"
100 style="display: block"
101 >
102 <input type="hidden" name="action" value="reverse_engineering">
103 <div id="add_table_to_repository" style="display:none">
104 <select name="wpda_schema_name" id="wpda_schema_name">
105 <?php
106 foreach ( $schema_names as $schema_name ) {
107 $selected = $user_default_schema === $schema_name['schema_name'] ? ' selected' : '';
108 $database = $user_default_schema === $schema_name['schema_name'] ? "Wordpress database ({$schema_name['schema_name']})" : $schema_name['schema_name'];
109 echo "<option value='{$schema_name['schema_name']}'{$selected}>{$database}</option>"; // phpcs:ignore WordPress.Security.EscapeOutput
110 }
111 ?>
112 </select>
113 <select name="wpda_table_name" id="wpda_table_name">
114 <?php
115 if ( is_array( $tables ) ) {
116 foreach ( $tables as $table ) {
117 ?>
118 <option value="<?php echo esc_attr( $table ); ?>"><?php echo esc_attr( $table ); ?></option>
119 <?php
120 }
121 }
122 ?>
123 </select>
124 <input type="hidden" name="wpnonce"
125 value="<?php echo esc_attr( $wpnonce ); ?>"/>
126 <input type="submit"
127 value="<?php echo __( 'Add Template For Selected Table To Repository', 'wp-data-access' ); ?>"
128 class="button button-secondary">
129 <input type="button"
130 value="<?php echo __( 'Cancel', 'wp-data-access' ); ?>"
131 class="button button-secondary"
132 onclick="jQuery('#no_repository_buttons').show(); jQuery('#add_table_to_repository').hide(); return false;">
133 </div>
134 </form>
135 <script type='text/javascript'>
136 function update_table_list(schema_name) {
137 var url = location.pathname + '?action=wpda_get_tables';
138 var data = {
139 wpdaschema_name: schema_name,
140 wpda_wpnonce: '<?php echo esc_attr( wp_create_nonce( 'wpda-getdata-access-' . WPDA::get_current_user_login() ) ); ?>'
141 };
142 jQuery.post(
143 url,
144 data,
145 function (data) {
146 jQuery('#wpda_table_name').empty();
147
148 var tables = JSON.parse(data);
149 for (var i = 0; i < tables.length; i++) {
150 jQuery('<option/>', {
151 value: tables[i].table_name,
152 html: tables[i].table_name
153 }).appendTo("#wpda_table_name");
154 }
155 }
156 );
157 }
158 jQuery(function () {
159 jQuery('#wpda_schema_name').on('change', function () {
160 update_table_list(jQuery('#wpda_schema_name').val());
161 });
162 });
163 </script>
164 <?php
165 }
166
167 /**
168 * Get all db tables and views
169 *
170 * @param string $database Database schema name
171 *
172 * @return array
173 */
174 protected function get_all_db_tables( $database ) {
175 $tables = array();
176 $db_tables = WPDA_Dictionary_Lists::get_tables( true, $database ); // select all db tables and views
177 foreach ( $db_tables as $db_table ) {
178 //phpcs:ignore - 8.1 proof
179 array_push( $tables, $db_table['table_name'] ); // add table or view to array
180 }
181
182 return $tables;
183 }
184
185 /**
186 * Overwrites method get_columns to add checkbox
187 *
188 * @return array
189 */
190 public function get_columns() {
191 $columns = array();
192
193 if ( $this->bulk_actions_enabled ) {
194 $columns = array( 'cb' => '<input type="checkbox" />' );
195 }
196
197 return array_merge( $columns, $this->column_headers );//phpcs:ignore - 8.1 proof
198 }
199
200 /**
201 * Overwrites method column_default
202 *
203 * @param array $item
204 * @param string $column_name
205 *
206 * @return mixed|string
207 */
208 public function column_default( $item, $column_name ) {
209 switch ( $column_name ) {
210 case 'table_found':
211 if ( 'rdb:' === substr( $item['wpda_schema_name'], 0, 4 ) ) {
212 return __( 'N/A', 'wp-data-access' );
213 }
214 $wpda_dictionary_exist = new WPDA_Dictionary_Exist( $item['wpda_schema_name'], $item['wpda_table_name'] );
215 if ( $wpda_dictionary_exist->table_exists() ) {
216 return __( 'Yes', 'wp-data-access' );
217 } else {
218 return __( 'No', 'wp-data-access' );
219 }
220 case 'table_has_relations':
221 $table_design = json_decode( $item['wpda_table_design'] );
222 if ( isset( $table_design->relationships ) ) {
223 return 'Yes';
224 } else {
225 return 'No';
226 }
227 case 'table_rows':
228 if ( 'rdb:' === substr( $item['wpda_schema_name'], 0, 4 ) ) {
229 return __( 'N/A', 'wp-data-access' );
230 }
231 global $wpdb;
232 $suppress = $wpdb->suppress_errors( true );
233 if ( '' === $item['wpda_schema_name'] || null === $item['wpda_schema_name'] ) {
234 $query = $wpdb->prepare(
235 'select count(*) from `%1s`', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
236 array(
237 WPDA::remove_backticks( $item['wpda_table_name'] ),
238 )
239 );
240 } else {
241 $query = $wpdb->prepare(
242 'select count(*) from `%1s`.`%1s`', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
243 array(
244 WPDA::remove_backticks( $item['wpda_schema_name'] ),
245 WPDA::remove_backticks( $item['wpda_table_name'] ),
246 )
247 );
248 }
249 $rows = $wpdb->get_var( $query ); // phpcs:ignore WordPress.DB.PreparedSQL
250 $wpdb->suppress_errors( $suppress );
251 return null === $rows ? '-' : $rows;
252 case 'wpda_table_name':
253 // Validate schema, table and column names
254 $warning = WPDA::validate_names( $item['wpda_schema_name'], $item['wpda_table_name'] );
255 if ( '' !== $warning ) {
256 $content = $item['wpda_table_name'] . $warning .
257 substr( parent::column_default( $item, $column_name ), strlen( $item['wpda_table_name'] ) );
258
259 return $content;
260 }
261 case 'wpda_schema_name':
262 global $wpdb;
263 if ( $wpdb->dbname === $item[ $column_name ] ) {
264 return "WordPress database ({$item[ $column_name ]})";
265 }
266 default:
267 return parent::column_default( $item, $column_name );
268 }
269 }
270
271 /**
272 * Overwrite method column_default_add_action to add tab argument
273 *
274 * @param array $item
275 * @param string $column_name
276 * @param array $actions
277 */
278 protected function column_default_add_action( $item, $column_name, &$actions ) {
279 parent::column_default_add_action( $item, $column_name, $actions );
280
281 // Add copy table options action.
282 $wp_nonce_action = "wpda-copy-{$this->table_name}";
283 $wp_nonce = esc_attr( wp_create_nonce( $wp_nonce_action ) );
284 $form_id = '_' . ( self::$list_number - 1 );
285 $copy_form =
286 '<form' .
287 " id='copy_form$form_id'" .
288 " action='?page=" . esc_attr( $this->page ) . "'" .
289 " method='post'>" .
290 $this->get_key_input_fields( $item ) .
291 $this->add_parent_args_as_string( $item ) .
292 "<input type='hidden' name='wpdaschema_name' value='<?php echo esc_attr( $this->schema_name ); ?>' />" .
293 "<input type='hidden' name='table_name' value='<?php echo esc_attr( $this->table_name ); ?>' />" .
294 "<input type='hidden' name='action' value='copy' />" .
295 "<input type='hidden' name='_wpnonce' value='$wp_nonce'>" .
296 $this->page_number_item .
297 '</form>'
298 ?>
299
300 <script type='text/javascript'>
301 jQuery("#wpda_invisible_container").append("<?php echo $copy_form; // phpcs:ignore WordPress.Security.EscapeOutput ?>");
302 </script>
303
304 <?php
305
306 // Add link to submit form.
307 $copy_warning = __( "Copy project template set?\\n\\'Cancel\\' to stop, \\'OK\\' to copy.", 'wp-data-access' );
308 $actions['copy'] = sprintf(
309 '<a href="javascript:void(0)"
310 class="edit wpda_tooltip"
311 title="Copy template set"
312 onclick="if (confirm(\'%s\')) jQuery(\'#%s\').submit()">
313 <span style="white-space:nowrap">
314 <i class="fas fa-copy wpda_icon_on_button"></i>
315 %s
316 </span>
317 </a>
318 ',
319 $copy_warning,
320 "copy_form$form_id",
321 __( 'Copy', 'wp-data-access' )
322 );
323 }
324
325 /**
326 * Overwrite method to implement copy action
327 *
328 * @return array|void
329 */
330 public function process_bulk_action() {
331 if ( 'copy' === $this->current_action() ) {
332 $wp_nonce_action = "wpda-copy-{$this->table_name}";
333 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; // input var okay.
334 if ( ! wp_verify_nonce( $wp_nonce, $wp_nonce_action ) ) {
335 die( __( 'ERROR: Not authorized', 'wp-data-access' ) );
336 }
337
338 if ( isset( $_REQUEST['wpda_schema_name'] ) ) {
339 $wpda_schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ); // input var okay.
340 }
341 if ( isset( $_REQUEST['wpda_table_name'] ) ) {
342 $wpda_table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); // input var okay.
343 }
344 if ( isset( $_REQUEST['wpda_table_setname'] ) ) {
345 $wpda_table_setname = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_setname'] ) ); // input var okay.
346 }
347
348 $unique_wpda_table_setname = $this->get_unique_setname( $wpda_schema_name, $wpda_table_name, $wpda_table_setname );
349
350 global $wpdb;
351 $wpda_table_design_raw = $wpdb->get_results(
352 $wpdb->prepare(
353 'SELECT * FROM `%1s` WHERE wpda_schema_name = %s AND wpda_table_name = %s AND wpda_table_setname = %s', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
354 array(
355 WPDA::remove_backticks( $this->table_name ),
356 $wpda_schema_name,
357 $wpda_table_name,
358 $wpda_table_setname,
359 )
360 ),
361 'ARRAY_A'
362 );
363 if ( $wpdb->num_rows > 0 ) {
364 $wpda_table_design_raw[0]['wpda_table_setname'] = $unique_wpda_table_setname;
365 $rows_inserted = $wpdb->insert(
366 $this->table_name,
367 $wpda_table_design_raw[0]
368 );
369 switch ( $rows_inserted ) {
370 case 0:
371 $msg = new WPDA_Message_Box(
372 array(
373 'message_text' => __( 'Could not copy table options [source not found]', 'wp-data-access' ),
374 'message_type' => 'error',
375 'message_is_dismissible' => false,
376 )
377 );
378 $msg->box();
379 break;
380 case 1:
381 $msg = new WPDA_Message_Box(
382 array(
383 'message_text' => __( 'Table options copied', 'wp-data-access' ),
384 )
385 );
386 $msg->box();
387 break;
388 default:
389 $msg = new WPDA_Message_Box(
390 array(
391 'message_text' => __( 'Could not copy table options [too many rows]', 'wp-data-access' ),
392 'message_type' => 'error',
393 'message_is_dismissible' => false,
394 )
395 );
396 $msg->box();
397 }
398 }
399 } else {
400 parent::process_bulk_action();
401 }
402 }
403
404 /**
405 * Create a unique setname for this table
406 *
407 * @param $wpda_schema_name
408 * @param $wpda_table_name
409 * @param $wpda_table_setname
410 *
411 * @return string
412 */
413 protected function get_unique_setname( $wpda_schema_name, $wpda_table_name, $wpda_table_setname ) {
414 global $wpdb;
415
416 $i = 2;
417 $query = "select 'x' from `%1s` where wpda_schema_name = %s and wpda_table_name = %s and wpda_table_setname = %s";
418
419 $unique_wpda_table_setname = "{$wpda_table_setname}_$i";
420 $wpdb->get_results(
421 $wpdb->prepare(
422 $query, // phpcs:ignore WordPress.DB.PreparedSQL
423 array(
424 WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ),
425 $wpda_schema_name,
426 $wpda_table_name,
427 $unique_wpda_table_setname,
428 )
429 )
430 );
431 while ( $wpdb->num_rows > 0 ) {
432 // Search until a free options set is found
433 $i ++;
434 $unique_wpda_table_setname = "{$wpda_table_setname}_$i";
435 $wpdb->get_results(
436 $wpdb->prepare(
437 $query, // phpcs:ignore WordPress.DB.PreparedSQL
438 array(
439 WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ),
440 $wpda_schema_name,
441 $wpda_table_name,
442 $unique_wpda_table_setname,
443 )
444 )
445 );
446 }
447
448 return $unique_wpda_table_setname;
449 }
450
451 /**
452 * Overwrite method show to add tab argument
453 */
454 public function show() {
455 parent::show();
456 ?>
457 <script type='text/javascript'>
458 var wpda_main_form_action = jQuery('#wpda_main_form').attr('action') + '&tab=tables';
459 jQuery('#wpda_main_form').attr('action', wpda_main_form_action);
460 </script>
461 <?php
462 }
463
464 public static function column_headers_labels() {
465 return array(
466 'wpda_table_name' => __( 'Table/View Name', 'wp-data-access' ),
467 'wpda_schema_name' => __( 'Database', 'wp-data-access' ),
468 'wpda_table_setname' => __( 'Template Set Name', 'wp-data-access' ),
469 'table_found' => __( 'Exists in DB?', 'wp-data-access' ),
470 'table_has_relations' => __( 'Has Relationships?', 'wp-data-access' ),
471 'table_rows' => __( '#Rows', 'wp-data-access' ),
472 );
473 }
474
475 public function get_bulk_actions() {
476 $actions = parent::get_bulk_actions();
477
478 unset(
479 $actions['bulk-export'],
480 $actions['bulk-export-xml'],
481 $actions['bulk-export-json'],
482 $actions['bulk-export-excel'],
483 $actions['bulk-export-csv']
484 );
485
486 return $actions;
487 }
488
489 }
490
491 }
492