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