PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.2
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.2
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 / WPDataAccess / Simple_Form / WPDA_Simple_Form.php

WPDA_Simple_Form.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.2, at WPDataAccess/Simple_Form/WPDA_Simple_Form.php

1,665 lines 55.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 WPDataAccess\Simple_Form
7 */
8 namespace WPDataAccess\Simple_Form;
9
10 use WPDataAccess\Data_Dictionary\WPDA_List_Columns ;
11 use WPDataAccess\Plugin_Table_Models\WPDA_Media_Model ;
12 use WPDataAccess\Plugin_Table_Models\WPDA_Table_Settings_Model ;
13 use WPDataAccess\Utilities\WPDA_Message_Box ;
14 use WPDataAccess\WPDA ;
15 use WPDataAccess\Dashboard\WPDA_Dashboard ;
16 /**
17 * Class WPDA_Simple_Form
18 *
19 * Generates a simple dynamic data entry form on request. Data entry form consists of the following components:
20 * + WPDA_Simple_Form (layout management)
21 * + WPDA_Simple_Form_Data (data management)
22 * + WPDA_Simple_Form_Item (data item management)
23 * + WPDA_Simple_Form_Type_Icon (data type icon)
24 *
25 * Simple forms can be generated for tables only. It is not possible to generate simple forms for views.
26 *
27 * Simple forms can be generated for tables with a primary key only. For tables that do not have a primary key it
28 * is not possibble to generate a simple form as records in the table cannot be recognized uniquely.
29 *
30 * Primary key fields are disable in update mode. This is to prevent inconsistency. Use method
31 * {@see WPDA_Simple_Form::set_update_keys()} to allow updating primary key items. Make sure you understand to
32 * consequences!!!
33 *
34 * @author Peter Schulz
35 * @since 1.0.0
36 */
37 class WPDA_Simple_Form
38 {
39 /**
40 * Static id to create unique form names on the page
41 *
42 * @var int
43 */
44 protected static $form_id = 0 ;
45 /**
46 * Warning icon
47 */
48 const WARNING_ICON = '<span class="dashicons dashicons-warning"></span> ' ;
49 /**
50 * Form id
51 *
52 * @var int
53 */
54 protected $current_form_id ;
55 /**
56 * Page title
57 *
58 * @var string
59 */
60 protected $title = null ;
61 /**
62 * Add action to page title?
63 *
64 * @var string TRUE = add action
65 */
66 protected $add_action_to_title ;
67 /**
68 * Page subtitle
69 *
70 * @var string
71 */
72 protected $subtitle = '' ;
73 /**
74 * Page fieldset title
75 *
76 * @var string
77 */
78 protected $fieldset_title = '' ;
79 /**
80 * Hides warning update text
81 *
82 * @var string TRUE = hide warning (overwrites plugin settings)
83 */
84 protected $no_warning_update_text = 'FALSE' ;
85 /**
86 * Database schema name
87 *
88 * @var string
89 */
90 protected $schema_name ;
91 /**
92 * Database table name
93 *
94 * @var string
95 */
96 protected $table_name ;
97 /**
98 * Handle to data management component
99 *
100 * @var WPDA_Simple_Form_Data
101 */
102 protected $row_data ;
103 /**
104 * Handle to database record
105 *
106 * @var object
107 */
108 protected $row ;
109 /**
110 * Handle to WPDA_List_Columns
111 *
112 * @var WPDA_List_Columns
113 */
114 protected $wpda_list_columns ;
115 /**
116 * Access column names
117 *
118 * @var array
119 */
120 protected $table_columns ;
121 /**
122 * Access column headers
123 *
124 * @var array
125 */
126 protected $table_column_headers ;
127 /**
128 * Display column headers
129 *
130 * @var array
131 */
132 protected $table_column_header ;
133 /**
134 * Column headers (labels, arguments)
135 *
136 * @var array
137 */
138 protected $column_headers ;
139 /**
140 * Form items
141 *
142 * @var array
143 */
144 protected $form_items = array() ;
145 /**
146 * Form items named array
147 *
148 * Use this array to quickly find an item in the form
149 *
150 * @var array
151 */
152 protected $form_items_named = array() ;
153 /**
154 * Form items old values (if applicable)
155 *
156 * @var array
157 */
158 protected $form_items_old_values = array() ;
159 /**
160 * Form items new values (if applicable)
161 *
162 * @var array
163 */
164 protected $form_items_new_values = array() ;
165 /**
166 * Menu slug current page
167 *
168 * @var string
169 */
170 protected $page ;
171 /**
172 * Primary page action
173 *
174 * @var string
175 */
176 protected $action ;
177 /**
178 * Secondary page action
179 *
180 * @var string
181 */
182 protected $action2 = '' ;
183 /**
184 * Auto increment value
185 *
186 * @var int
187 */
188 protected $auto_increment_value = -1 ;
189 /**
190 * Defines it keys are updatable
191 *
192 * @var bool
193 */
194 protected $update_keys_allowed = false ;
195 /**
196 * Error message number
197 *
198 * @var int
199 */
200 protected $wpda_err = 0 ;
201 /**
202 * Error message text
203 *
204 * @var string
205 */
206 protected $wpda_msg = '' ;
207 /**
208 * Indicates whether the title should be displayed or not
209 *
210 * @var bool
211 */
212 protected $show_title = true ;
213 /**
214 * Indicates whether the back button should be displayed or not
215 *
216 * @var bool
217 */
218 protected $show_back_button = true ;
219 /**
220 * Indicates whether the back icon in the title should be displayed or not
221 *
222 * @var bool
223 */
224 protected $show_back_icon = true ;
225 /**
226 * Indicates whether table type should be checked to display subtitle.
227 *
228 * @var bool
229 */
230 protected $check_table_type = true ;
231 /**
232 * Page number item name (default 'page_number')
233 *
234 * The name can be changed for pages on multiple levels. This is needed to get back to the right page in
235 * parent-child page.
236 *
237 * @var string
238 */
239 protected $page_number_item_name = 'page_number' ;
240 /**
241 * Real page number
242 *
243 * @var null
244 */
245 protected $page_number_link = '' ;
246 /**
247 * Page number text item
248 *
249 * @var null
250 */
251 protected $page_number_item = '' ;
252 /**
253 * Used to changed button text
254 *
255 * @var string
256 */
257 protected $back_to_list_text = '' ;
258 /**
259 * Table settings (taken from plugin table settings table)
260 *
261 * @var mixed|null
262 */
263 protected $wpda_table_settings = null ;
264 /**
265 * Show help icon if hrlp url is available
266 *
267 * @var null|string
268 */
269 protected $help_url = null ;
270 /**
271 * Hide Add New button
272 *
273 * @var null|string
274 */
275 protected $hide_add_new = false ;
276 /**
277 * Hide schema and table name
278 *
279 * @var bool
280 */
281 protected $hide_db_info = false ;
282 /**
283 * Group items into fieldsets
284 *
285 * @var array
286 */
287 protected $fieldsets = array() ;
288 /**
289 * WPDA_Simple_Form constructor
290 *
291 * Performs the following steps:
292 * + Check if requested action is allowed
293 * + Check if table is provided
294 * + Save OLD and NEW request values
295 * + Create WPDA_Simple_Form_Data object
296 *
297 * @param string $schema_name Database schema name.
298 * @param string $table_name Database table name.
299 * @param WPDA_List_Columns $wpda_list_columns Reference to column array.
300 * @param array $args Messages (named array).
301 *
302 * @since 1.0.0
303 *
304 * @see WPDA_Simple_Form_Data
305 */
306 public function __construct(
307 $schema_name,
308 $table_name,
309 &$wpda_list_columns,
310 $args = array()
311 )
312 {
313 // Get page and handling arguments.
314
315 if ( isset( $_REQUEST['page'] ) ) {
316 $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) );
317 // input var okay.
318 } else {
319 wp_die( __( 'ERROR: Wrong arguments [missing page argument]', 'wp-data-access' ) );
320 }
321
322
323 if ( isset( $_REQUEST['action'] ) ) {
324 // Possible values: "new", "edit" and "view".
325 $this->action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) );
326 // input var okay.
327 } else {
328
329 if ( isset( $args['action'] ) ) {
330 $this->action = $args['action'];
331 } else {
332 wp_die( __( 'ERROR: Wrong arguments [missing action argument]', 'wp-data-access' ) );
333 }
334
335 }
336
337
338 if ( isset( $_REQUEST['action2'] ) ) {
339 $this->action2 = sanitize_text_field( wp_unslash( $_REQUEST['action2'] ) );
340 // input var okay.
341 }
342
343 $this->schema_name = $schema_name;
344 $this->table_name = $table_name;
345 if ( '' === $this->table_name ) {
346 // Without a table name it makes no sense to continue.
347 wp_die( __( 'ERROR: Wrong arguments [missing table_name argument]' ) );
348 }
349 $this->hide_db_info = isset( $args['hide_db_info'] ) && true === $args['hide_db_info'];
350
351 if ( !WPDA::is_wpda_table( $this->table_name ) ) {
352 // Check access rights for tables that do not belong to the plugin.
353 if ( 'on' !== WPDA::get_option( WPDA::OPTION_BE_ALLOW_INSERT ) && 'new' === $this->action ) {
354 // Insert not allowed.
355 wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) );
356 }
357 if ( 'on' !== WPDA::get_option( WPDA::OPTION_BE_VIEW_LINK ) && 'view' === $this->action ) {
358 // Viewing not allowed.
359 wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) );
360 }
361 if ( 'on' !== WPDA::get_option( WPDA::OPTION_BE_ALLOW_UPDATE ) && 'edit' === $this->action ) {
362 // Update not allowed.
363 wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) );
364 }
365 }
366
367 // Get columns information.
368 $this->wpda_list_columns = $wpda_list_columns;
369 $this->table_columns = $this->wpda_list_columns->get_table_columns();
370 $this->table_column_headers = $this->wpda_list_columns->get_table_column_headers();
371 // Set page title.
372
373 if ( isset( $args['title'] ) ) {
374 $this->title = $args['title'];
375 $this->add_action_to_title = true;
376 if ( isset( $args['add_action_to_title'] ) && 'FALSE' === $args['add_action_to_title'] ) {
377 $this->add_action_to_title = false;
378 }
379 }
380
381 // Set page subtitle.
382
383 if ( isset( $args['subtitle'] ) ) {
384 $this->subtitle = $args['subtitle'];
385 } else {
386
387 if ( $this->check_table_type ) {
388 global $wpdb ;
389 $wp_tables = $wpdb->tables( 'all', true );
390
391 if ( isset( $wp_tables[substr( $this->table_name, strlen( $wpdb->prefix ) )] ) ) {
392 $this->subtitle = self::WARNING_ICON . WPDA::get_table_type_text( WPDA::TABLE_TYPE_WP );
393 } elseif ( WPDA::is_wpda_table( $this->table_name ) ) {
394 $this->subtitle = self::WARNING_ICON . WPDA::get_table_type_text( WPDA::TABLE_TYPE_WPDA );
395 }
396
397 }
398
399 }
400
401 if ( isset( $args['no_warning_update_text'] ) ) {
402 $this->no_warning_update_text = $args['no_warning_update_text'];
403 }
404 // Add customizable success and failure messages to support i18n.
405 // This allows to overwrite message in classes extending WPDA_Simple_Form.
406 $args = wp_parse_args( $args, array(
407 'wpda_success_msg' => __( 'Succesfully saved changes to database', 'wp-data-access' ),
408 'wpda_failure_msg' => __( 'Saving changes to database failed', 'wp-data-access' ),
409 'show_title' => true,
410 'show_back_button' => true,
411 'show_back_icon' => true,
412 ) );
413 // Get arguments from URL (old and new values).
414 $this->get_url_arguments();
415 // Add data object for DML.
416 $this->row_data = new WPDA_Simple_Form_Data(
417 $this->schema_name,
418 $this->table_name,
419 $this->wpda_list_columns,
420 $this,
421 $args['wpda_success_msg'],
422 $args['wpda_failure_msg']
423 );
424 $this->current_form_id = 'wpda_simple_form_' . self::$form_id++;
425 if ( isset( $args['show_title'] ) ) {
426 $this->show_title = $args['show_title'];
427 }
428 if ( isset( $args['show_back_button'] ) ) {
429 $this->show_back_button = $args['show_back_button'];
430 }
431 if ( isset( $args['show_back_icon'] ) ) {
432 $this->show_back_icon = $args['show_back_icon'];
433 }
434 // Overwrite column header text if column headers were provided.
435 $this->column_headers = ( isset( $args['column_headers'] ) ? $args['column_headers'] : '' );
436 // Get current page number of list table
437 if ( 'page_number' !== $this->page_number_item_name ) {
438
439 if ( isset( $_REQUEST['page_number'] ) ) {
440 $requested_page_number = sanitize_text_field( wp_unslash( $_REQUEST['page_number'] ) );
441 // input var okay.
442 $this->page_number_link = '&page_number=' . $requested_page_number;
443 $this->page_number_item = "<input type='hidden' name='page_number' value='{$requested_page_number}'/>";
444 }
445
446 }
447
448 if ( isset( $_REQUEST[$this->page_number_item_name] ) ) {
449 $requested_page_number = sanitize_text_field( wp_unslash( $_REQUEST[$this->page_number_item_name] ) );
450 // input var okay.
451 $this->page_number_link .= '&paged=' . $requested_page_number;
452 $this->page_number_item .= "<input type='hidden' name='" . $this->page_number_item_name . "' value='{$requested_page_number}'/>";
453 }
454
455 // Add search arguments to link to return to same page
456 foreach ( $_REQUEST as $key => $value ) {
457
458 if ( substr( $key, 0, 19 ) === 'wpda_search_column_' ) {
459 $this->page_number_link .= "&{$key}={$value}";
460 $this->page_number_item .= "<input type='hidden' name='{$key}' value='{$value}' />";
461 }
462
463 }
464 // Check if button text "back to list" should be changed
465
466 if ( isset( $args['back_to_list_text'] ) && '' !== $args['back_to_list_text'] ) {
467 $this->back_to_list_text = $args['back_to_list_text'];
468 } else {
469 $this->back_to_list_text = __( 'List', 'wp-data-access' );
470 }
471
472 // Get table settings
473 $wpda_table_settings = WPDA_Table_Settings_Model::query( $this->table_name, $this->schema_name );
474 if ( isset( $wpda_table_settings[0]['wpda_table_settings'] ) ) {
475 $this->wpda_table_settings = json_decode( $wpda_table_settings[0]['wpda_table_settings'] );
476 }
477 if ( isset( $args['help_url'] ) ) {
478 $this->help_url = $args['help_url'];
479 }
480 if ( isset( $args['hide_add_new'] ) ) {
481 $this->hide_add_new = $args['hide_add_new'];
482 }
483 global $wpda_project_mode ;
484 if ( isset( $wpda_project_mode['allow_insert'] ) && 'only' === $wpda_project_mode['allow_insert'] ) {
485 $this->hide_add_new = true;
486 }
487 }
488
489 /**
490 * Get URL arguments.
491 *
492 * @since 1.5.0
493 */
494 protected function get_url_arguments()
495 {
496 // Get OLD and NEW values for all items.
497 foreach ( $this->wpda_list_columns->get_table_columns() as $column ) {
498
499 if ( isset( $_REQUEST[$column['column_name'] . '_old'] ) ) {
500 $this->form_items_old_values[$column['column_name']] = wp_unslash( $_REQUEST[$column['column_name'] . '_old'] );
501 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
502 }
503
504 if ( isset( $_REQUEST[$column['column_name']] ) ) {
505
506 if ( is_array( $_REQUEST[$column['column_name']] ) ) {
507 $column_array = '';
508 foreach ( $_REQUEST[$column['column_name']] as $column_value ) {
509 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
510 $column_array .= wp_unslash( $column_value ) . ',';
511 }
512 if ( '' !== $column_array ) {
513 $this->form_items_new_values[$column['column_name']] = substr( $column_array, 0, strlen( $column_array ) - 1 );
514 }
515 } else {
516 $this->form_items_new_values[$column['column_name']] = wp_unslash( $_REQUEST[$column['column_name']] );
517 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
518 }
519
520 }
521 }
522 }
523
524 /**
525 * Prepare form items and handle transactions
526 *
527 * Performs the following steps:
528 * + Checks for posted data
529 * + Saves changes if applicable (displays success or failure message)
530 * + Prepares simple form
531 *
532 * @param bool $allow_save
533 */
534 public function prepare_form( $allow_save = true )
535 {
536 $set_back_form_values = false;
537
538 if ( $allow_save && ('new' === $this->action || 'edit' === $this->action) && 'save' === $this->action2 ) {
539 // Security check (is action allowed?).
540 $wp_nonce = ( isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '' );
541 // input var okay.
542 if ( !wp_verify_nonce( $wp_nonce, $this->get_nonce_action() ) ) {
543 wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) );
544 }
545
546 if ( 'new' === $this->action ) {
547 // Prepare row and items for validation
548 $this->row = $this->row_data->new_row();
549 $this->prepare_items( true );
550 // Save new record.
551
552 if ( !$this->validate( true ) ) {
553 $set_back_form_values = true;
554 } else {
555 // Insert record.
556 $add_row_result = $this->row_data->add_row();
557
558 if ( $add_row_result ) {
559 // Insert succeeded:
560 // (1) save auto_increment value (if applicable)
561 // (2) change action to edit
562 if ( is_numeric( $add_row_result ) ) {
563 // save auto_increment value (1).
564 $this->auto_increment_value = $add_row_result;
565 }
566 // change action to edit (2).
567 $this->action = 'edit';
568 $this->prepare_row();
569 } else {
570 // Insert failed:
571 // (1) set back form items
572 $set_back_form_values = true;
573 }
574
575 }
576
577 } else {
578 // Prepare row and items for validation
579 $this->row = $this->row_data->get_row( $this->auto_increment_value, $this->wpda_err );
580 $this->prepare_items( true );
581 // Update existing record.
582 if ( $this->validate() ) {
583 // Update record.
584 $this->row_data->set_row();
585 }
586 $this->prepare_row();
587 $set_back_form_values = true;
588 }
589
590 } else {
591 $this->prepare_row();
592 }
593
594 $this->prepare_items( $set_back_form_values );
595
596 if ( '1' === $this->wpda_err && $this->update_keys_allowed && 'edit' === $this->action && 'save' === $this->action2 ) {
597 $keys_have_changed = false;
598 // There was an error! If key updates are allowed, we might have needed to set back the keys to their
599 // original values. We'll inform the user with a message box.
600 foreach ( $this->wpda_list_columns->get_table_primary_key() as $pk_column ) {
601
602 if ( $this->get_old_value( $pk_column ) !== $this->get_new_value( $pk_column ) ) {
603 $keys_have_changed = true;
604 break;
605 }
606
607 }
608
609 if ( $keys_have_changed ) {
610 $msg = new WPDA_Message_Box( array(
611 'message_text' => __( 'Key columns have been reversed to their original values', 'wp-data-access' ),
612 ) );
613 $msg->box();
614 }
615
616 }
617
618
619 if ( null === $this->title ) {
620
621 if ( 'new' === $this->action ) {
622 $action_in_title = __( 'Add new row to', 'wp-data-access' );
623 } else {
624 $action_in_title = ucfirst( $this->action );
625 }
626
627 $this->title = $action_in_title . ' ' . __( 'table', 'wp-data-access' ) . ' ' . strtoupper( $this->table_name );
628 } else {
629
630 if ( 'new' === $this->action ) {
631 $title_action = 'Add New ';
632 } else {
633 $title_action = ucfirst( $this->action );
634 }
635
636 if ( $this->add_action_to_title ) {
637 $this->title = $title_action . ' ' . $this->title;
638 }
639 }
640
641
642 if ( is_admin() && ($this->page === \WP_Data_Access_Admin::PAGE_MAIN || substr( $this->page, 0, 13 ) === \WP_Data_Access_Admin::PAGE_EXPLORER) ) {
643 $this->fieldset_title = $this->title;
644 $this->title = 'Data Explorer';
645 }
646
647 }
648
649 /**
650 * Overwrite to group items into fieldsets
651 */
652 protected function add_fieldsets()
653 {
654 $fields = array();
655 foreach ( $this->form_items as $item ) {
656 $fields[] = $item->get_item_name();
657 }
658 // Add just one default fieldset which contains all fields
659 $this->fieldsets[$this->fieldset_title] = array(
660 'id' => '',
661 'fields' => $fields,
662 );
663 }
664
665 /**
666 * Show simple form
667 *
668 * Performs the following steps:
669 * + Shows simple form for requested table (HTML ande Javascript)
670 *
671 * @param boolean $allow_save Allow to save data
672 * @param string $add_param Parameter to be added to form action.
673 *
674 * @since 1.0.0
675 */
676 public function show( $allow_save = true, $add_param = '' )
677 {
678 $this->add_fieldsets();
679 // Prepare url
680
681 if ( is_admin() ) {
682 $url = "?page={$this->page}";
683 } else {
684 $url = '';
685 }
686
687
688 if ( !$this->hide_db_info ) {
689
690 if ( '' !== $this->schema_name ) {
691 $url .= ( $url === '' ? '?' : '&' );
692 $url .= "wpdaschema_name={$this->schema_name}";
693 }
694
695 $url .= ( $url === '' ? '?' : '&' );
696 $url .= "table_name={$this->table_name}";
697 }
698
699 $url_back = $url;
700 $url .= $add_param;
701 ?>
702 <div class="wrap">
703 <?php
704
705 if ( $this->show_title ) {
706 ?>
707 <h1>
708 <?php
709
710 if ( is_admin() && $this->show_back_icon ) {
711 ?>
712 <a
713 href="javascript:void(0)"
714 onclick="jQuery('#<?php
715 echo esc_attr( $this->current_form_id ) ;
716 ?>_backbutton').submit()"
717 style="display: inline-block; vertical-align: unset;"
718 class="dashicons dashicons-arrow-left-alt wpda_tooltip"
719 title="<?php
720 echo esc_attr( $this->back_to_list_text ) ;
721 ?>"
722 ></a>
723 <?php
724 }
725
726 ?>
727 <span style="vertical-align:top;"><?php
728 echo esc_attr( $this->title ) ;
729 ?></span>
730 <?php
731 if ( WPDA_Dashboard::add_actions_to_page_title() || substr( $this->page, 0, 13 ) === \WP_Data_Access_Admin::PAGE_EXPLORER || !is_admin() ) {
732
733 if ( null !== $this->help_url ) {
734 ?>
735 <a href="<?php
736 echo esc_attr( $this->help_url ) ;
737 ?>" target="_blank" class="wpda_tooltip" title="Plugin Help - open a new tab or window">
738 <span class="dashicons dashicons-editor-help"
739 style="text-decoration:none;vertical-align:top;font-size:30px;">
740 </span></a>
741 <?php
742 } else {
743 if ( 'view' !== $this->action && !$this->hide_add_new ) {
744
745 if ( WPDA::is_wpda_table( $this->table_name ) || ('on' === WPDA::get_option( WPDA::OPTION_BE_ALLOW_INSERT ) && count( $this->wpda_list_columns->get_table_primary_key() )) > 0 ) {
746 $title = __( 'Add new row to table', 'wp-data-access' );
747 ?>
748 <form
749 method="post"
750 action="<?php
751 echo esc_attr( $url ) ;
752 ?>"
753 style="display: inline-block; vertical-align: bottom;"
754 >
755 <div>
756 <input type="hidden" name="action" value="new">
757 <button type="submit" class="page-title-action wpda_tooltip"
758 title="<?php
759 echo esc_attr( $title ) ;
760 ?>"
761 >
762 <span class="material-icons wpda_icon_on_button">add_circle</span>
763 <?php
764 echo __( 'Add New', 'wp-data-access' ) ;
765 ?>
766 </button>
767 </div>
768 </form>
769 <?php
770 }
771
772 }
773 }
774
775 }
776 ?>
777 </h1>
778 <div class="wpda_subtitle"><strong><?php
779 echo wp_kses( $this->subtitle, array(
780 'span' => array(
781 'class' => array(),
782 ),
783 ) ) ;
784 ?></strong>
785 </div>
786 <?php
787 // Add custom code before the data entry form
788 do_action_ref_array( 'wpda_before_simple_form', array( $this ) );
789 ?>
790 <p></p>
791 <?php
792 }
793
794 ?>
795 <form id="<?php
796 echo esc_attr( $this->current_form_id ) ;
797 ?>"
798 method="post" enctype="multipart/form-data"
799 action="<?php
800 echo esc_attr( $url ) ;
801 ?>">
802 <div>
803 <?php
804 $js_code = '';
805 // All column JS code will be stored here and added to the end of the form.
806 foreach ( $this->fieldsets as $fieldset_title => $fieldset ) {
807
808 if ( isset( $fieldset['id'] ) && '' !== trim( $fieldset['id'] ) ) {
809 $id = trim( $fieldset['id'] );
810 } else {
811 $id = '';
812 }
813
814 $expandable = ( isset( $fieldset['expandable'] ) && true === $fieldset['expandable'] ? true : false );
815 $expandable_state = ( isset( $_REQUEST["wpda_fieldset_expand_{$id}"] ) ? sanitize_text_field( $_REQUEST["wpda_fieldset_expand_{$id}"] ) : 'off' );
816 $expandable_icons = ( 'on' === $expandable_state ? 'remove_circle' : 'add_circle' );
817 ?>
818 <fieldset class="wpda_fieldset">
819 <?php
820
821 if ( '' !== $fieldset_title ) {
822 ?>
823 <legend <?php
824 echo ( '' === $id ? '' : 'id="' . esc_attr( $id ) . '"' ) ;
825 ?>>
826 <?php
827
828 if ( $expandable && '' !== $id ) {
829 echo '<div class="wpda-fieldset-expand" onclick="expandFieldset(\'' . esc_attr( $id ) . '\')">' . '<i id="wpda_field_expand_' . esc_attr( $id ) . '" class="wpda-fieldset-expand material-icons">' . esc_attr( $expandable_icons ) . '</i>' . '<span class="wpda-fieldset-expand">' . esc_html( $fieldset_title ) . '</span>' . '<input type="hidden" id="wpda_fieldset_expand_' . esc_attr( $id ) . '" name="wpda_fieldset_expand_' . esc_attr( $id ) . '" value="' . esc_attr( $expandable_state ) . '" />' . '</div>' ;
830 } else {
831 echo esc_html( $fieldset_title ) ;
832 }
833
834 ?>
835 </legend>
836 <?php
837 }
838
839 ?>
840 <table id="wpda_table_expand_<?php
841 echo esc_attr( $id ) ;
842 ?>"
843 class="wpda_simple_table <?php
844 echo esc_attr( $this->table_name ) ;
845 ?>"
846 <?php
847 echo ( $expandable && 'off' === $expandable_state ? 'style="display: none"' : '' ) ;
848 ?>
849 cellspacing="0"
850 cellpadding="0"
851 >
852 <?php
853 $fieldset_columns = array_flip( $fieldset['fields'] );
854 foreach ( $this->form_items as $item ) {
855
856 if ( isset( $fieldset_columns[$item->get_item_name()] ) ) {
857 $item->show( $this->action, $this->update_keys_allowed );
858 $js_code .= $item->get_item_js();
859 // Add column specific JS code.
860 }
861
862 }
863 ?>
864 </table>
865 </fieldset>
866 <?php
867 }
868 ?>
869 </div>
870 <div><?php
871 $this->add_form_logic();
872 ?></div>
873 <?php
874 // Add custom code after the data entry form
875 do_action_ref_array( 'wpda_after_simple_form', array( $this ) );
876 ?>
877 <p></p>
878 <input type="hidden" name="action" value="<?php
879 echo esc_attr( $this->action ) ;
880 ?>"/>
881 <input type="hidden" name="action2" value="save"/>
882 <input type="hidden" name="wpda_message" value=""/>
883 <?php
884 if ( 'view' !== $this->action ) {
885 ?>
886 <input type="hidden" name="postaction" id="postaction" value=""/>
887 <?php
888 }
889 ?>
890 <?php
891 $this->add_parent_args();
892 echo $this->page_number_item ;
893 // phpcs:ignore WordPress.Security.EscapeOutput
894 wp_nonce_field( $this->get_nonce_action( false ), '_wpnonce', false );
895 ?>
896 <?php
897
898 if ( 'view' !== $this->action ) {
899 ?>
900 <button type="submit"
901 class="button button-primary"
902 name="submit_button"
903 onclick="return submit_form(event)">
904 <span class="material-icons wpda_icon_on_button">check</span>
905 <?php
906 echo __( 'Submit', 'wp-data-access' ) ;
907 ?>
908 </button>
909 <?php
910
911 if ( $this->show_back_button && $this->show_back_icon ) {
912 ?>
913 <button type="submit"
914 class="button button-secondary"
915 name="submit_button"
916 onclick="return submit_form(event)">
917 <span class="material-icons wpda_icon_on_button">check</span>
918 <?php
919 echo __( 'Submit', 'wp-data-access' ) ;
920 ?>
921 <span class="material-icons wpda_icon_on_button">arrow_forward</span>
922 <?php
923 echo esc_attr( $this->back_to_list_text ) ;
924 ?>
925 </button>
926 <?php
927 }
928
929
930 if ( $this->show_back_button ) {
931 ?>
932 <button type="button"
933 onclick="jQuery('#<?php
934 echo esc_attr( $this->current_form_id ) ;
935 ?>_backbutton').submit();"
936 class="button button-secondary">
937 <span class="material-icons wpda_icon_on_button">arrow_back</span>
938 <?php
939 echo esc_attr( $this->back_to_list_text ) ;
940 ?>
941 </button>
942 <?php
943 }
944
945 ?>
946 <span style="float:right;"><?php
947 $this->add_buttons();
948 ?></span>
949 <?php
950 }
951
952 ?>
953 <?php
954
955 if ( 'view' === $this->action ) {
956 ?>
957 <button type="button"
958 onclick="jQuery('#<?php
959 echo esc_attr( $this->current_form_id ) ;
960 ?>_backbutton').submit()"
961 class="button button-secondary">
962 <span class="material-icons wpda_icon_on_button">arrow_back</span>
963 <?php
964 echo esc_attr( $this->back_to_list_text ) ;
965 ?>
966 </button>
967 <?php
968 }
969
970 ?>
971 </form>
972 <form id="<?php
973 echo esc_attr( $this->current_form_id ) ;
974 ?>_backbutton"
975 method="post" style="display:none"
976 action="<?php
977 echo esc_attr( $url_back ) ;
978 ?>">
979 <?php
980 $this->add_parent_args();
981 echo $this->page_number_item ;
982 // phpcs:ignore WordPress.Security.EscapeOutput
983 ?>
984 <input type="hidden" name="action" value="list">
985 </form>
986 </div>
987
988 <script type='text/javascript'>
989 <?php
990
991 if ( 'view' !== $this->action ) {
992 ?>
993 function submit_form(e) {
994 if (typeof pre_submit_form === "function") {
995 // Perform pre submit
996 pre_submit = pre_submit_form();
997 if (!pre_submit) {
998 return false;
999 }
1000 }
1001 if (
1002 jQuery(e.target).hasClass("button-secondary") ||
1003 jQuery(e.target).parent().hasClass("button-secondary")
1004 ) {
1005 jQuery("#postaction").val("list");
1006 } else {
1007 jQuery("#postaction").val("");
1008 }
1009 var failed = false;
1010 jQuery('.wpda_not_null').each(function(i, obj) {
1011 if (jQuery(obj).val() === '') {
1012 objData = jQuery(obj).data();
1013 if (objData.dbIsNull===undefined || objData.dbIsNull!==false) {
1014 alert(<?php
1015 echo __( '\'Item\'', 'wp-data-access' ) ;
1016 ?> +' ' + jQuery(obj).attr('name') + ' ' + <?php
1017 echo __( '\'must be entered\'', 'wp-data-access' ) ;
1018 ?>);
1019 failed = true;
1020 }
1021 }
1022 });
1023 jQuery('.wpda_input_error').each(function(i, obj) {
1024 alert(<?php
1025 echo __( '\'Column\'', 'wp-data-access' ) ;
1026 ?> +' ' + jQuery(obj).attr('name') + <?php
1027 echo __( '\': max size exceeded\'', 'wp-data-access' ) ;
1028 ?>);
1029 failed = true;
1030 });
1031 jQuery('.wpda_hyperlink').each(function(i, obj) {
1032 hyperlink_name = jQuery(obj).attr('name');
1033 hyperlink_label = jQuery('#' + hyperlink_name + '_label').val();
1034 hyperlink_url = jQuery('#' + hyperlink_name + '_url').val();
1035 hyperlink_target = jQuery('#' + hyperlink_name + '_target').is(':checked') ? '_blank' : '';
1036 hyperlink_update = {
1037 "label" : hyperlink_label,
1038 "url" : hyperlink_url,
1039 "target" : hyperlink_target
1040 };
1041 jQuery(obj).val(JSON.stringify(hyperlink_update));
1042 });
1043 return !failed;
1044 }
1045 <?php
1046 }
1047
1048 ?>
1049 jQuery(function () {
1050 <?php
1051
1052 if ( 'view' === $this->action ) {
1053 ?>
1054 jQuery("#<?php
1055 echo esc_attr( $this->current_form_id ) ;
1056 ?> input").not(':button').prop("readonly", true);
1057 jQuery("#<?php
1058 echo esc_attr( $this->current_form_id ) ;
1059 ?> textarea").prop("readonly", true);
1060 jQuery("#<?php
1061 echo esc_attr( $this->current_form_id ) ;
1062 ?> select").prop("disabled", true);
1063 <?php
1064 }
1065
1066 ?>
1067 <?php
1068
1069 if ( !$this->update_keys_allowed && 'new' !== $this->action ) {
1070 ?>
1071 jQuery("#<?php
1072 echo esc_attr( $this->current_form_id ) ;
1073 ?> input.wpda_primary_key").prop("readonly", true);
1074 jQuery("#<?php
1075 echo esc_attr( $this->current_form_id ) ;
1076 ?> select.wpda_primary_key").prop("disabled", true);
1077 <?php
1078 }
1079
1080 ?>
1081 <?php
1082
1083 if ( 'new' === $this->action ) {
1084 ?>
1085 jQuery("#<?php
1086 echo esc_attr( $this->current_form_id ) ;
1087 ?> input.auto_increment").prop("readonly", true);
1088 <?php
1089 }
1090
1091 ?>
1092 jQuery("#<?php
1093 echo esc_attr( $this->current_form_id ) ;
1094 ?> input.wpda_readonly").prop("readonly", true);
1095 jQuery('.wpda_data_type_number').on('keyup paste', function () {
1096 numberFormat = jQuery(this).data('numberFormat').split(",");
1097 this.value = this.value.replace(/[^\d\-]/g, ''); // Allow only 0-9
1098 if (isNaN(this.value) || this.value.length>numberFormat[0]) {
1099 jQuery(this).addClass('wpda_input_error');
1100 if (this.value.length>numberFormat[0]) {
1101 jQuery.notify('<?php
1102 echo __( 'Max size exceeded' ) ;
1103 ?>','error');
1104 }
1105 } else {
1106 jQuery(this).removeClass('wpda_input_error');
1107 }
1108 });
1109 jQuery('.wpda_data_type_float').on('keyup paste', function () {
1110 numberFormat = jQuery(this).data('numberFormat').split(",");
1111 maxNumber = Math.pow(10, numberFormat[0] - numberFormat[1]);
1112 this.value = this.value.replace(/[^\d\-\.\,]/g, ''); // Allow only 0-9 . ,
1113 if (isNaN(this.value) || this.value>=maxNumber) {
1114 jQuery(this).addClass('wpda_input_error');
1115 if (this.value>=maxNumber) {
1116 jQuery.notify('<?php
1117 echo __( 'Max size exceeded' ) ;
1118 ?>','error');
1119 }
1120 } else {
1121 jQuery(this).removeClass('wpda_input_error');
1122 }
1123 currentNumber = this.value.split(".");
1124 if (currentNumber.length===1) {
1125 currentNumber = this.value.split(",");
1126 }
1127 if (currentNumber.length===2 && currentNumber[1].length>numberFormat[1]) {
1128 jQuery(this).addClass('wpda_input_error');
1129 jQuery.notify('<?php
1130 echo __( 'Max size exceeded' ) ;
1131 ?>','error');
1132 }
1133 });
1134 jQuery( '.wpda_tooltip' ).tooltip();
1135 // Add toolbar icons
1136 jQuery("#wpda_toolbar_icon_add_row").on("click", function() {
1137 jQuery("#wpda_new_row_table_name").val("<?php
1138 echo esc_attr( $this->table_name ) ;
1139 ?>");
1140 jQuery("#wpda_new_row").submit();
1141 });
1142 });
1143 <?php
1144 echo $js_code ;
1145 // phpcs:ignore WordPress.Security.EscapeOutput
1146 ?>
1147 </script>
1148 <?php
1149 }
1150
1151 /**
1152 * Overwrite to add logic to form
1153 *
1154 * @since 2.0.15
1155 */
1156 public function add_form_logic()
1157 {
1158 }
1159
1160 /**
1161 * Overwrite to add buttons to right bottom
1162 *
1163 * @since 2.0.15
1164 */
1165 public function add_buttons()
1166 {
1167 }
1168
1169 /**
1170 * Creates a wpnonce action
1171 *
1172 * Set wp_nonce action for security check:
1173 * prefix + table name + primary key values
1174 *
1175 * @param boolean $use_old_value TRUE = use old key values, FALSE = use new key values.
1176 *
1177 * @return string wp_nonce action holding: prefix + table name + primary key values.
1178 * @since 1.0.0
1179 */
1180 public function get_nonce_action( $use_old_value = true )
1181 {
1182 // Add prefix + table name to wp_nonce action.
1183 $wp_nonce_action = "wpda-simple-form-{$this->table_name}";
1184
1185 if ( $this->wpda_list_columns->get_table_alternative_keys() ) {
1186 $wp_nonce_action .= '-*';
1187 } else {
1188 foreach ( $this->wpda_list_columns->get_table_primary_key() as $pk_column ) {
1189 // Add primary key value to wp_nonce action.
1190
1191 if ( 'new' === $this->action ) {
1192 // New records have no key values on form startup.
1193 $wp_nonce_action .= '-?';
1194 } else {
1195 // Add primary key value to wp_nonce action.
1196
1197 if ( 'save' === $this->action2 && $use_old_value ) {
1198 // Use old value (in cases where primary key update is allowed).
1199 $wp_nonce_action .= ( isset( $_REQUEST[$pk_column . '_old'] ) ? '-' . sanitize_text_field( wp_unslash( $_REQUEST[$pk_column . '_old'] ) ) : '-?' );
1200 // input var okay.
1201 } else {
1202
1203 if ( isset( $_REQUEST[$pk_column] ) && '' !== sanitize_text_field( wp_unslash( $_REQUEST[$pk_column] ) ) ) {
1204 // input var okay.
1205 // Use new value.
1206 $wp_nonce_action .= '-' . sanitize_text_field( wp_unslash( $_REQUEST[$pk_column] ) );
1207 // input var okay.
1208 } elseif ( -1 !== $this->auto_increment_value ) {
1209 // Use auto increment value (updated wp_nonce action after insert).
1210 $wp_nonce_action .= '-' . $this->auto_increment_value;
1211 } else {
1212 // No value found, let security check handle wrong wp_nonce action.
1213 $wp_nonce_action .= '-?';
1214 }
1215
1216 }
1217
1218 }
1219
1220 }
1221 }
1222
1223 return str_replace( ' ', '_', $wp_nonce_action );
1224 }
1225
1226 /**
1227 * Perform validation check
1228 *
1229 * Called to perform default validation before insert and update.
1230 *
1231 * Extend class WPDA_Simple_Form and override this method if you need validation on insert and/or update or
1232 * if you prefer to change error messages. If you want to handle inserts and updates differently, the
1233 * following information might be helpful:
1234 * + on insert: $this->action = 'new'
1235 * + on update: $this->action = 'edit'
1236 *
1237 * Use set_message to show messages (info as well as error).
1238 *
1239 * @param boolean $pre_insert Do not check auto_increment during pre-insert
1240 *
1241 * @return boolean TRUE = validation succeeded, FALSE = validation failed.
1242 * @since 1.0.0
1243 */
1244 protected function validate( $pre_insert = false )
1245 {
1246 foreach ( $this->form_items as $item ) {
1247 if ( !$item->is_valid( $pre_insert ) ) {
1248 return false;
1249 }
1250 }
1251 return true;
1252 }
1253
1254 /**
1255 * Get current from database table
1256 *
1257 * Handle insert, update and save diffently.
1258 *
1259 * @since 1.0.0
1260 */
1261 protected function prepare_row()
1262 {
1263
1264 if ( 'edit' === $this->action || 'view' === $this->action || 'save' === $this->action2 ) {
1265 // Get record by primary key, use auto_increment for new records.
1266 $this->row = $this->row_data->get_row( $this->auto_increment_value, $this->wpda_err );
1267 } else {
1268 // There's no record yet.
1269 $this->row = $this->row_data->new_row();
1270 }
1271
1272 }
1273
1274 /**
1275 * Set item attributes
1276 *
1277 * If you want to change the layout of your simple form(s), consider to extend class WPDA_Simple_Form and
1278 * override this method.
1279 *
1280 * @param boolean $set_back_form_values TRUE = set back user entered value, FALSE = set to database value.
1281 *
1282 * @since 1.0.0
1283 */
1284 protected function prepare_items( $set_back_form_values = false )
1285 {
1286 $count_cols = count( $this->table_columns );
1287 for ( $i = 0 ; $i < $count_cols ; $i++ ) {
1288 $column_name = $this->table_columns[$i]['column_name'];
1289 $item_enum = '';
1290 if ( 'enum' === $this->table_columns[$i]['data_type'] || 'set' === $this->table_columns[$i]['data_type'] ) {
1291 $item_enum = $this->table_columns[$i]['column_type'];
1292 }
1293
1294 if ( $set_back_form_values ) {
1295 // Set value to what user entered.
1296 $item_value = $this->get_new_value( $column_name );
1297 } else {
1298 // Get value from database.
1299 $item_value = ( isset( $this->row ) ? $this->row[0][$column_name] : null );
1300 }
1301
1302 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item';
1303 $check_data_type = WPDA::get_type( $this->table_columns[$i]['data_type'] );
1304
1305 if ( 'enum' === $this->table_columns[$i]['data_type'] ) {
1306 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_Enum';
1307 } elseif ( 'set' === $this->table_columns[$i]['data_type'] ) {
1308 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_Set';
1309 } elseif ( 'date' === $check_data_type || 'time' === $check_data_type ) {
1310 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_DateTime';
1311 } elseif ( 'tinytext' === $this->table_columns[$i]['data_type'] || 'text' === $this->table_columns[$i]['data_type'] || 'mediumtext' === $this->table_columns[$i]['data_type'] || 'longtext' === $this->table_columns[$i]['data_type'] ) {
1312 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_Textarea';
1313 } elseif ( 'tinyint(1)' === $this->table_columns[$i]['column_type'] ) {
1314 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_Boolean';
1315 }
1316
1317
1318 if ( is_admin() ) {
1319 $media_type = WPDA_Media_Model::get_column_media( $this->table_name, $column_name, $this->schema_name );
1320
1321 if ( 'Image' === $media_type ) {
1322 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_Image';
1323 } elseif ( 'Attachment' === $media_type ) {
1324 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_Media';
1325 } elseif ( 'Hyperlink' === $media_type ) {
1326 if ( !(isset( $this->wpda_table_settings->table_settings->hyperlink_definition ) && 'text' === $this->wpda_table_settings->table_settings->hyperlink_definition) ) {
1327 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_Hyperlink';
1328 }
1329 } elseif ( 'Audio' === $media_type ) {
1330 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_Audio';
1331 } elseif ( 'Video' === $media_type ) {
1332 $wpda_simple_form_item_class = 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form_Item_Video';
1333 }
1334
1335 }
1336
1337 $item_label = '';
1338
1339 if ( isset( $this->column_headers[$column_name] ) ) {
1340 $item_label = $this->column_headers[$column_name];
1341 } elseif ( isset( $this->table_column_headers[$column_name] ) ) {
1342 $item_label = $this->table_column_headers[$column_name];
1343 }
1344
1345 $item = new $wpda_simple_form_item_class( array(
1346 'item_name' => $column_name,
1347 'data_type' => $this->table_columns[$i]['data_type'],
1348 'item_label' => $item_label,
1349 'item_value' => $item_value,
1350 'item_default_value' => $this->table_columns[$i]['column_default'],
1351 'item_extra' => $this->table_columns[$i]['extra'],
1352 'item_enum' => $item_enum,
1353 'column_type' => $this->table_columns[$i]['column_type'],
1354 'is_nullable' => $this->table_columns[$i]['is_nullable'],
1355 'user_update' => $set_back_form_values,
1356 ) );
1357 $item->set_is_key_column( $this->is_key_column( $column_name ) );
1358 $this->add_form_item( $i, $item );
1359 }
1360 // Add dynamic hyperlinks (if applicable)
1361 if ( isset( $this->wpda_table_settings->hyperlinks ) ) {
1362 foreach ( $this->wpda_table_settings->hyperlinks as $hyperlink ) {
1363 $hyperlink_label = ( isset( $hyperlink->hyperlink_label ) ? $hyperlink->hyperlink_label : '' );
1364 $hyperlink_form = ( isset( $hyperlink->hyperlink_form ) ? $hyperlink->hyperlink_form : false );
1365 $hyperlink_html = ( isset( $hyperlink->hyperlink_html ) ? $hyperlink->hyperlink_html : '' );
1366
1367 if ( true === $hyperlink_form && $hyperlink_label !== '' && $hyperlink_html !== '' ) {
1368 $hyperlink_target = ( isset( $hyperlink->hyperlink_form ) ? $hyperlink->hyperlink_target : false );
1369 foreach ( $this->form_items as $item ) {
1370 $item_name = $item->get_item_name();
1371 $item_value = $item->get_item_value();
1372 $hyperlink_html = str_replace( "\$\${$item_name}\$\$", $item_value, $hyperlink_html );
1373 }
1374 $item = new WPDA_Simple_Form_Item_Dynamic_Hyperlink( array(
1375 'item_name' => $hyperlink_label,
1376 'data_type' => 'varchar2',
1377 'hyperlink_label' => $hyperlink_label,
1378 'hyperlink_target' => $hyperlink_target,
1379 'hyperlink_html' => $hyperlink_html,
1380 ) );
1381 $this->add_form_item( $i++, $item );
1382 }
1383
1384 }
1385 }
1386 }
1387
1388 /**
1389 * Get new value for item
1390 *
1391 * Or empty if no new value available.
1392 *
1393 * @param string $column_name Column name.
1394 *
1395 * @return mixed|string New value for column or empty.
1396 * @since 1.0.0
1397 */
1398 public function get_new_value( $column_name )
1399 {
1400 return ( isset( $this->form_items_new_values[$column_name] ) ? $this->form_items_new_values[$column_name] : null );
1401 }
1402
1403 /**
1404 * Add item to form
1405 *
1406 * @param int $index Item sequence number.
1407 * @param WPDA_Simple_Form_Item $item Reference to simple form item.
1408 *
1409 * @since 1.0.0
1410 *
1411 * @see WPDA_Simple_Form_Item
1412 */
1413 protected function add_form_item( $index, $item )
1414 {
1415 // Add item to form
1416 $this->form_items[$index] = $item;
1417 // Add position to named array for quick access of single items
1418 $item_name = $item->get_item_name();
1419 $this->form_items_named[$item_name] = $index;
1420 }
1421
1422 /**
1423 * Get item position
1424 *
1425 * @param string $item_name Form item name
1426 *
1427 * @return bool|int Position item or false if not found
1428 *
1429 * @since 2.0.15
1430 */
1431 protected function get_item_index( $item_name )
1432 {
1433
1434 if ( isset( $this->form_items_named[$item_name] ) ) {
1435 return $this->form_items_named[$item_name];
1436 } else {
1437 return false;
1438 }
1439
1440 }
1441
1442 /**
1443 * Get old value form item
1444 *
1445 * Or empty if no old value available.
1446 *
1447 * @param string $column_name Column name.
1448 *
1449 * @return mixed|string Old value for column or empty.
1450 * @since 1.0.0
1451 */
1452 public function get_old_value( $column_name )
1453 {
1454 return ( isset( $this->form_items_old_values[$column_name] ) ? $this->form_items_old_values[$column_name] : null );
1455 }
1456
1457 /**
1458 * Is column part of primary key?
1459 *
1460 * @param string $column_name Column name.
1461 *
1462 * @return bool TRUE = column is part of primary key, FALSE = column is not part of primary key.
1463 * @since 1.0.0
1464 */
1465 protected function is_key_column( $column_name )
1466 {
1467 foreach ( $this->wpda_list_columns->get_table_primary_key() as $pk_column ) {
1468 if ( $column_name === $pk_column ) {
1469 return true;
1470 }
1471 }
1472 return false;
1473 }
1474
1475 /**
1476 * Get primary form action
1477 *
1478 * @return string Primary page action
1479 * @since 1.0.0
1480 */
1481 public function get_form_action()
1482 {
1483 return $this->action;
1484 }
1485
1486 /**
1487 * Get secondary form action
1488 *
1489 * @return string Secondary page action
1490 * @since 1.0.0
1491 */
1492 public function get_form_action2()
1493 {
1494 return $this->action2;
1495 }
1496
1497 /**
1498 * Defines whether primary key item can be updated
1499 *
1500 * @param boolean $update_keys_allowed Allow keys to be updated.
1501 *
1502 * @since 1.0.0
1503 */
1504 protected function set_update_keys( $update_keys_allowed )
1505 {
1506 $this->update_keys_allowed = $update_keys_allowed;
1507 }
1508
1509 /**
1510 * Reorder columns
1511 *
1512 * Reorders the column array in the order as defined in argument $columns_ordered.
1513 *
1514 * @param array $columns_ordered Ordered column names.
1515 *
1516 * @since 1.0.0
1517 */
1518 protected function order_and_filter_columns( $columns_ordered )
1519 {
1520 $column_array_ordered = array();
1521 $i = 0;
1522 foreach ( $columns_ordered as $key => $value ) {
1523 $column_array_ordered[$i++] = $this->table_columns[$this->get_column_position( $this->table_columns, $value )];
1524 }
1525 $this->table_columns = $column_array_ordered;
1526 }
1527
1528 /**
1529 * Get column position in column array
1530 *
1531 * @param array $column_array Column array.
1532 * @param string $column_name Column name.
1533 *
1534 * @return int Position of $column_name in $column_array
1535 * @since 1.0.0
1536 */
1537 protected function get_column_position( $column_array = array(), $column_name )
1538 {
1539 $count_cols = count( $column_array );
1540 for ( $i = 0 ; $i < $count_cols ; $i++ ) {
1541 if ( $column_array[$i]['column_name'] === $column_name ) {
1542 return $i;
1543 }
1544 }
1545 return -1;
1546 }
1547
1548 /**
1549 * Add dummy column to form
1550 *
1551 * @param string $column_name Column name.
1552 *
1553 * @since 1.0.0
1554 */
1555 protected function add_dummy_column( $column_name )
1556 {
1557 $this->table_columns[] = array(
1558 'column_name' => $column_name,
1559 'data_type' => 'varchar',
1560 'extra' => '',
1561 'column_type' => '',
1562 'is_nullable' => 'YES',
1563 'column_default' => '',
1564 );
1565 }
1566
1567 /**
1568 * Set message number and text
1569 *
1570 * Assigns values to $this->wpda_err and $this->wpda_msg.
1571 *
1572 * @param string $wpda_err '0' = INFO, '1' = ERROR.
1573 * @param string $wpda_msg Message to be displayed.
1574 *
1575 * @since 1.0.0
1576 */
1577 protected function set_message( $wpda_err, $wpda_msg )
1578 {
1579 // Wrong values will be handled by message box class.
1580 $this->wpda_err = $wpda_err;
1581 $this->wpda_msg = $wpda_msg;
1582 }
1583
1584 /**
1585 * Use this method to build parent child relationships.
1586 *
1587 * Overwrite this function if you want to use the form as a child form related to some parent
1588 * form. You can add parent arguments to calls to make sure you get back to the right parent.
1589 *
1590 * @since 1.5.0
1591 */
1592 protected function add_parent_args()
1593 {
1594 }
1595
1596 /**
1597 * Use this method to build parent child relationships.
1598 *
1599 * Overwrite this function if you want to use the form as a child form related to some parent
1600 * form. You can add parent arguments to calls to make sure you get back to the right parent.
1601 *
1602 * @since 1.6.9
1603 */
1604 protected function add_parent_args_to_back_button()
1605 {
1606 }
1607
1608 /**
1609 * Set item labels
1610 *
1611 * @param array $item_labels Named array containing item name/label pairs
1612 *
1613 * @since 2.0.15
1614 */
1615 public function set_labels( $item_labels )
1616 {
1617 if ( is_array( $item_labels ) ) {
1618 foreach ( $item_labels as $key => $label ) {
1619 foreach ( $this->form_items as $form_item ) {
1620 if ( $form_item->get_item_name() === $key ) {
1621 $form_item->set_label( $label );
1622 }
1623 }
1624 }
1625 }
1626 }
1627
1628 /**
1629 * Hide items
1630 *
1631 * @param array $items_to_hide Array contains item names of items to be defined as hidden
1632 *
1633 * @since 2.0.15
1634 */
1635 public function hide_items( $items_to_hide )
1636 {
1637 if ( is_array( $items_to_hide ) ) {
1638 foreach ( $items_to_hide as $column_name ) {
1639 foreach ( $this->form_items as $form_item ) {
1640 if ( $form_item->get_item_name() === $column_name ) {
1641 $form_item->set_hide_item_init( true );
1642 }
1643 }
1644 }
1645 }
1646 }
1647
1648 /**
1649 * Number of items
1650 *
1651 * @return int
1652 *
1653 * @since 2.0.15
1654 */
1655 public function count()
1656 {
1657 return count( $this->form_items );
1658 }
1659
1660 public function get_row()
1661 {
1662 return $this->row;
1663 }
1664
1665 }