| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataAccess\Plugin_Table_Models |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataAccess\Plugin_Table_Models { |
| 10 |
|
| 11 |
use WPDataAccess\Data_Dictionary\WPDA_List_Columns_Cache; |
| 12 |
use WPDataAccess\Utilities\WPDA_Message_Box; |
| 13 |
use WPDataAccess\WPDA; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class WPDP_Project_Design_Table_Model extends WPDA_Plugin_Table_Base_Model |
| 17 |
* |
| 18 |
* @see WPDA_Plugin_Table_Base_Model |
| 19 |
* |
| 20 |
* @author Peter Schulz |
| 21 |
* @since 2.0.0 |
| 22 |
*/ |
| 23 |
class WPDP_Project_Design_Table_Model extends WPDA_Plugin_Table_Base_Model { |
| 24 |
|
| 25 |
const BASE_TABLE_NAME = 'wpda_project_table'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Cached table options |
| 29 |
* |
| 30 |
* @var null|array |
| 31 |
*/ |
| 32 |
protected static $cache_table_options = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Requested action |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $action2; |
| 40 |
|
| 41 |
/** |
| 42 |
* Name of table where design is stored |
| 43 |
* |
| 44 |
* @var string|null |
| 45 |
*/ |
| 46 |
protected $table_name = null; |
| 47 |
|
| 48 |
/** |
| 49 |
* Design schema name |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
protected $wpda_schema_name = ''; |
| 54 |
|
| 55 |
/** |
| 56 |
* Design table name |
| 57 |
* |
| 58 |
* @var string|null |
| 59 |
*/ |
| 60 |
protected $wpda_table_name = null; |
| 61 |
|
| 62 |
/** |
| 63 |
* Options set name (to allow multiple table definitions) |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
protected $wpda_table_setname = null; |
| 68 |
|
| 69 |
/** |
| 70 |
* Check if options set name was changed |
| 71 |
* |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
protected $wpda_table_setname_old = null; |
| 75 |
|
| 76 |
/** |
| 77 |
* The actual table design |
| 78 |
* |
| 79 |
* @var string|null |
| 80 |
*/ |
| 81 |
protected $wpda_table_design = null; |
| 82 |
|
| 83 |
/** |
| 84 |
* WPDP_Project_Design_Table_Model constructor |
| 85 |
* |
| 86 |
* Get action2 arguments |
| 87 |
*/ |
| 88 |
public function __construct() { |
| 89 |
$this->table_name = self::get_base_table_name(); |
| 90 |
|
| 91 |
if ( isset( $_REQUEST['wpda_schema_name'] ) ) { |
| 92 |
$this->wpda_schema_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ); |
| 93 |
} |
| 94 |
|
| 95 |
// Watch out for arrays! (array = starting export) |
| 96 |
if ( isset( $_REQUEST['wpda_table_name'] ) && ! is_array( $_REQUEST['wpda_table_name'] ) ) { |
| 97 |
$this->wpda_table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( isset( $_REQUEST['wpda_table_setname'] ) ) { |
| 101 |
$this->wpda_table_setname = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_setname'] ) ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( isset( $_REQUEST['wpda_table_setname_old'] ) ) { |
| 105 |
$this->wpda_table_setname_old = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_setname_old'] ) ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( isset( $_REQUEST['action2'] ) ) { |
| 109 |
$this->action2 = sanitize_text_field( wp_unslash( $_REQUEST['action2'] ) ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Prepare query to use setname (after reverse engineering) |
| 115 |
* |
| 116 |
* @param $wpda_table_setname |
| 117 |
*/ |
| 118 |
public function prepare_query( $wpda_table_setname ) { |
| 119 |
$this->wpda_table_setname = $wpda_table_setname; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Get design for a specific table name |
| 124 |
* |
| 125 |
* @return int Number of rows. |
| 126 |
*/ |
| 127 |
public function query() { |
| 128 |
if ( null === $this->wpda_table_name ) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
global $wpdb; |
| 133 |
$wpda_table_design_raw = $wpdb->get_results( |
| 134 |
$wpdb->prepare( |
| 135 |
'SELECT wpda_table_design FROM `%1s` WHERE wpda_schema_name = %s AND wpda_table_name = %s AND wpda_table_setname = %s', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 136 |
array( |
| 137 |
WPDA::remove_backticks( $this->table_name ), |
| 138 |
$this->wpda_schema_name, |
| 139 |
$this->wpda_table_name, |
| 140 |
null === $this->wpda_table_setname_old ? $this->wpda_table_setname : $this->wpda_table_setname_old, |
| 141 |
) |
| 142 |
), |
| 143 |
'ARRAY_A' |
| 144 |
); |
| 145 |
if ( $wpdb->num_rows > 0 ) { |
| 146 |
$this->wpda_table_design = json_decode( $wpda_table_design_raw[0]['wpda_table_design'] ); |
| 147 |
} |
| 148 |
|
| 149 |
return $wpdb->num_rows; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Return table design |
| 154 |
* |
| 155 |
* @return null|array |
| 156 |
*/ |
| 157 |
public function get_table_design() { |
| 158 |
return $this->wpda_table_design; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Return table design |
| 163 |
* |
| 164 |
* @return null|array |
| 165 |
*/ |
| 166 |
public function get_table_setname() { |
| 167 |
return $this->wpda_table_setname; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Overwrite method validate to add table options validation |
| 172 |
* |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public function validate() { |
| 176 |
$structure_messages = array(); |
| 177 |
|
| 178 |
if ( ! isset( $this->wpda_table_design->design_mode ) ) { |
| 179 |
$structure_messages[] = array( 'ERR', 'Invalid structure [missing element design_mode]' ); |
| 180 |
} |
| 181 |
|
| 182 |
if ( ! isset( $this->wpda_table_design->engine ) |
| 183 |
&& ( |
| 184 |
! isset( $this->wpda_table_design->table_type ) |
| 185 |
|| |
| 186 |
( isset( $this->wpda_table_design->table_type ) && 'TABLE' === $this->wpda_table_design->table_type ) |
| 187 |
) |
| 188 |
) { |
| 189 |
$structure_messages[] = array( 'ERR', 'Invalid structure [missing element engine]' ); |
| 190 |
} |
| 191 |
|
| 192 |
if ( ! isset( $this->wpda_table_design->collation ) |
| 193 |
&& ( |
| 194 |
! isset( $this->wpda_table_design->table_type ) |
| 195 |
|| |
| 196 |
( isset( $this->wpda_table_design->table_type ) && 'TABLE' === $this->wpda_table_design->table_type ) |
| 197 |
) |
| 198 |
) { |
| 199 |
$structure_messages[] = array( 'ERR', 'Invalid structure [missing element collation]' ); |
| 200 |
} |
| 201 |
|
| 202 |
if ( ! isset( $this->wpda_table_design->table ) ) { |
| 203 |
$structure_messages[] = array( 'ERR', 'Invalid structure [missing element table]' ); |
| 204 |
} else { |
| 205 |
$unique_column_names = array(); |
| 206 |
foreach ( $this->wpda_table_design->table as $column ) { |
| 207 |
$unique_column_names[ $column->column_name ] = true; |
| 208 |
} |
| 209 |
if ( count( $unique_column_names ) !== count( $this->wpda_table_design->table ) ) {//phpcs:ignore - 8.1 proof |
| 210 |
$structure_messages[] = array( 'ERR', 'Column name must be unique within a table' ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if ( ! isset( $this->wpda_table_design->indexes ) ) { |
| 215 |
$structure_messages[] = array( 'ERR', 'Invalid structure [missing element indexes]' ); |
| 216 |
} else { |
| 217 |
$unique_index_names = array(); |
| 218 |
foreach ( $this->wpda_table_design->indexes as $index ) { |
| 219 |
$unique_index_names[ $index->index_name ] = true; |
| 220 |
} |
| 221 |
if ( count( $unique_index_names ) !== count( $this->wpda_table_design->indexes ) ) {//phpcs:ignore - 8.1 proof |
| 222 |
$structure_messages[] = array( 'ERR', 'Index name must be unique within a table' ); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if ( isset( $this->wpda_table_design->tableform_column_options ) ) { |
| 227 |
//phpcs:ignore - 8.1 proof |
| 228 |
if ( count( $this->wpda_table_design->table ) !== count( $this->wpda_table_design->tableform_column_options ) ) { |
| 229 |
$structure_messages[] = array( 'ERR', 'Invalid structure [run reconcile]' ); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return $structure_messages; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Overwrite method prepare_update to prepare table options |
| 238 |
*/ |
| 239 |
public function prepare_update() { |
| 240 |
$this->query(); |
| 241 |
|
| 242 |
if ( isset( $_REQUEST['design_mode'] ) ) { |
| 243 |
$this->wpda_table_design->design_mode = sanitize_text_field( wp_unslash( $_REQUEST['design_mode'] ) ); |
| 244 |
} |
| 245 |
|
| 246 |
if ( isset( $_REQUEST['engine'] ) ) { |
| 247 |
$this->wpda_table_design->engine = sanitize_text_field( wp_unslash( $_REQUEST['engine'] ) ); |
| 248 |
} |
| 249 |
|
| 250 |
if ( isset( $_REQUEST['collation'] ) ) { |
| 251 |
$this->wpda_table_design->collation = sanitize_text_field( wp_unslash( $_REQUEST['collation'] ) ); |
| 252 |
} |
| 253 |
|
| 254 |
if ( isset( $_REQUEST['column_name'] ) ) { |
| 255 |
$this->wpda_table_design->table = $this->get_table_structure(); |
| 256 |
} else { |
| 257 |
if ( isset( $_REQUEST['submitted_changes'] ) && 'table' === $_REQUEST['submitted_changes'] ) { |
| 258 |
$this->wpda_table_design->table = array(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if ( isset( $_REQUEST['column_names'] ) ) { |
| 263 |
$this->wpda_table_design->indexes = $this->get_indexes(); |
| 264 |
} else { |
| 265 |
if ( isset( $_REQUEST['submitted_changes'] ) && 'indexes' === $_REQUEST['submitted_changes'] ) { |
| 266 |
$this->wpda_table_design->indexes = array(); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
switch ( $this->action2 ) { |
| 271 |
case 'relation': |
| 272 |
$this->prepare_update_relation(); |
| 273 |
break; |
| 274 |
case 'listtable': |
| 275 |
$this->prepare_update_listtable(); |
| 276 |
break; |
| 277 |
case 'tableform': |
| 278 |
$this->prepare_update_tableform(); |
| 279 |
break; |
| 280 |
case 'tableinfo': |
| 281 |
$this->prepare_update_tableinfo(); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Set relationships to prepare update |
| 287 |
*/ |
| 288 |
protected function prepare_update_relation() { |
| 289 |
unset( $this->wpda_table_design->relationships ); |
| 290 |
/** |
| 291 |
* CWG cleared these as being future proof on the assumption that the fields in $_REQUEST are all arrays |
| 292 |
* If that is not a safe assumption, this needs to be heavily revised |
| 293 |
*/ |
| 294 |
//phpcs:ignore - 8.1 proof |
| 295 |
if ( isset( $_REQUEST['row_num'] ) ) { |
| 296 |
$no_columns = count( $_REQUEST['row_num'] ); |
| 297 |
if ( |
| 298 |
isset( $_REQUEST['relation_type'] ) && |
| 299 |
$no_columns === count( $_REQUEST['relation_type'] ) && |
| 300 |
isset( $_REQUEST['source_column_name'] ) && |
| 301 |
$no_columns === count( $_REQUEST['source_column_name'] ) && |
| 302 |
isset( $_REQUEST['target_table_name'] ) && |
| 303 |
$no_columns === count( $_REQUEST['target_table_name'] ) && |
| 304 |
isset( $_REQUEST['target_column_name'] ) && |
| 305 |
$no_columns === count( $_REQUEST['target_column_name'] ) |
| 306 |
) { |
| 307 |
for ( $i = 0; $i < $no_columns; $i ++ ) { |
| 308 |
$relation_type = sanitize_text_field( wp_unslash( $_REQUEST['relation_type'][ $i ] ) ); |
| 309 |
$source_column_name = sanitize_text_field( wp_unslash( $_REQUEST['source_column_name'][ $i ] ) ); |
| 310 |
$target_table_name = sanitize_text_field( wp_unslash( $_REQUEST['target_table_name'][ $i ] ) ); |
| 311 |
$target_column_name = sanitize_text_field( wp_unslash( $_REQUEST['target_column_name'][ $i ] ) ); |
| 312 |
if ( isset( $_REQUEST['target_schema_name'][ $i ] ) ) { |
| 313 |
$target_schema_name = sanitize_text_field( wp_unslash( $_REQUEST['target_schema_name'][ $i ] ) ); |
| 314 |
} else { |
| 315 |
$target_schema_name = ''; |
| 316 |
} |
| 317 |
|
| 318 |
if ( 'nm' === $relation_type ) { |
| 319 |
if ( isset( $_REQUEST[ 'relation_table_name_' . $i ] ) ) { |
| 320 |
$relation_table_name = sanitize_text_field( wp_unslash( $_REQUEST[ 'relation_table_name_' . $i ] ) ); |
| 321 |
} else { |
| 322 |
$relation_table_name = ''; |
| 323 |
} |
| 324 |
if ( trim( $relation_table_name ) === '' ) { |
| 325 |
$msg = new WPDA_Message_Box( |
| 326 |
array( |
| 327 |
'message_text' => __( 'Invalid array [missing required fields]', 'wp-data-access' ), |
| 328 |
'message_type' => 'error', |
| 329 |
'message_is_dismissible' => false, |
| 330 |
) |
| 331 |
); |
| 332 |
$msg->box(); |
| 333 |
|
| 334 |
return; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
if ( |
| 339 |
trim( $relation_type ) !== '' && |
| 340 |
trim( $source_column_name ) !== '' && |
| 341 |
trim( $target_table_name ) !== '' && |
| 342 |
trim( $target_column_name ) !== '' |
| 343 |
) { |
| 344 |
$source_column_name_array = array(); |
| 345 |
$target_column_name_array = array(); |
| 346 |
|
| 347 |
array_push( $source_column_name_array, $source_column_name );//phpcs:ignore - 8.1 proof |
| 348 |
array_push( $target_column_name_array, $target_column_name );//phpcs:ignore - 8.1 proof |
| 349 |
|
| 350 |
if ( isset( $_REQUEST['num_source_column_name'][ $i ] ) ) { |
| 351 |
$num_source_column_name = sanitize_text_field( wp_unslash( $_REQUEST['num_source_column_name'][ $i ] ) ); |
| 352 |
if ( is_numeric( $num_source_column_name ) ) { |
| 353 |
for ( $j = 1; $j <= $num_source_column_name; $j ++ ) { |
| 354 |
if ( |
| 355 |
isset( $_REQUEST[ 'source_column_name_' . $i . '_' . $j ] ) && |
| 356 |
isset( $_REQUEST[ 'target_column_name_' . $i . '_' . $j ] ) |
| 357 |
) { |
| 358 |
array_push( $source_column_name_array, sanitize_text_field( wp_unslash( $_REQUEST[ 'source_column_name_' . $i . '_' . $j ] ) ) );//phpcs:ignore - 8.1 proof |
| 359 |
array_push( $target_column_name_array, sanitize_text_field( wp_unslash( $_REQUEST[ 'target_column_name_' . $i . '_' . $j ] ) ) );//phpcs:ignore - 8.1 proof |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
$this->wpda_table_design->relationships[ $i ]['relation_type'] = $relation_type; |
| 366 |
$this->wpda_table_design->relationships[ $i ]['source_column_name'] = $source_column_name_array; |
| 367 |
$this->wpda_table_design->relationships[ $i ]['target_table_name'] = $target_table_name; |
| 368 |
$this->wpda_table_design->relationships[ $i ]['target_column_name'] = $target_column_name_array; |
| 369 |
if ( 'nm' === $relation_type ) { |
| 370 |
$this->wpda_table_design->relationships[ $i ]['relation_table_name'] = $relation_table_name; |
| 371 |
} elseif ( 'lookup' === $relation_type || 'autocomplete' === $relation_type ) { |
| 372 |
$this->wpda_table_design->relationships[ $i ]['target_schema_name'] = $target_schema_name; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
} else { |
| 377 |
$msg = new WPDA_Message_Box( |
| 378 |
array( |
| 379 |
'message_text' => __( 'Invalid array [missing required fields]', 'wp-data-access' ), |
| 380 |
'message_type' => 'error', |
| 381 |
'message_is_dismissible' => false, |
| 382 |
) |
| 383 |
); |
| 384 |
$msg->box(); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Set table options for list table to prepare update |
| 391 |
*/ |
| 392 |
protected function prepare_update_listtable() { |
| 393 |
$column_options = $this->get_column_options_from_request(); |
| 394 |
if ( null !== $column_options ) { |
| 395 |
$this->wpda_table_design->listtable_column_options = $column_options; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Set table options for data entry form to prepare update |
| 401 |
*/ |
| 402 |
protected function prepare_update_tableform() { |
| 403 |
$column_options = $this->get_column_options_from_request(); |
| 404 |
if ( null !== $column_options ) { |
| 405 |
$this->wpda_table_design->tableform_column_options = $column_options; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Set table level options to prepare update |
| 411 |
*/ |
| 412 |
protected function prepare_update_tableinfo() { |
| 413 |
if ( isset( $_REQUEST['table_setname'] ) ) { |
| 414 |
$this->wpda_table_setname = sanitize_text_field( wp_unslash( $_REQUEST['table_setname'] ) ); |
| 415 |
} |
| 416 |
|
| 417 |
if ( isset( $_REQUEST['tab_label'] ) ) { |
| 418 |
$this->wpda_table_design->tableinfo->tab_label = |
| 419 |
sanitize_text_field( wp_unslash( $_REQUEST['tab_label'] ) ); |
| 420 |
} |
| 421 |
|
| 422 |
if ( isset( $_REQUEST['default_where'] ) ) { |
| 423 |
$this->wpda_table_design->tableinfo->default_where = |
| 424 |
sanitize_text_field( wp_unslash( $_REQUEST['default_where'] ) ); |
| 425 |
} |
| 426 |
|
| 427 |
if ( isset( $_REQUEST['default_orderby'] ) ) { |
| 428 |
$this->wpda_table_design->tableinfo->default_orderby = |
| 429 |
sanitize_text_field( wp_unslash( $_REQUEST['default_orderby'] ) ); |
| 430 |
} |
| 431 |
|
| 432 |
$settings_db = WPDA_Table_Settings_Model::query( $this->wpda_table_name, $this->wpda_schema_name ); |
| 433 |
if ( isset( $settings_db[0]['wpda_table_settings'] ) && '' !== $settings_db[0]['wpda_table_settings'] ) { |
| 434 |
$settings = json_decode( $settings_db[0]['wpda_table_settings'] ); |
| 435 |
if ( isset( $settings->hyperlinks ) && is_array( $settings->hyperlinks ) ) { |
| 436 |
$hyperlinks = array(); |
| 437 |
$hyperlinks_child = array(); |
| 438 |
foreach ( $settings->hyperlinks as $hyperlink ) { |
| 439 |
if ( isset( $hyperlink->hyperlink_label ) ) { |
| 440 |
$hyperlink_label_replace_spaces = ucfirst( str_replace( ' ', '_', $hyperlink->hyperlink_label ) ); |
| 441 |
|
| 442 |
if ( isset( $_REQUEST[ "{$hyperlink_label_replace_spaces}_hyperlink" ] ) ) { |
| 443 |
$hyperlinks[ $hyperlink->hyperlink_label ] = true; |
| 444 |
} else { |
| 445 |
$hyperlinks[ $hyperlink->hyperlink_label ] = false; |
| 446 |
} |
| 447 |
|
| 448 |
if ( isset( $_REQUEST[ "{$hyperlink_label_replace_spaces}_hyperlink_child" ] ) ) { |
| 449 |
$hyperlinks_child[ $hyperlink->hyperlink_label ] = true; |
| 450 |
} else { |
| 451 |
$hyperlinks_child[ $hyperlink->hyperlink_label ] = false; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
$this->wpda_table_design->tableinfo->hyperlinks_parent = $hyperlinks; |
| 456 |
$this->wpda_table_design->tableinfo->hyperlinks_child = $hyperlinks_child; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
if ( has_filter( 'wpda_data_projects_save_table_option' ) ) { |
| 461 |
$this->wpda_table_design->tableinfo->custom_table_settings = apply_filters( |
| 462 |
'wpda_data_projects_save_table_option', |
| 463 |
'', |
| 464 |
$this->wpda_schema_name, |
| 465 |
$this->wpda_table_name |
| 466 |
); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Get table column options |
| 472 |
* |
| 473 |
* @return array|null |
| 474 |
*/ |
| 475 |
protected function get_column_options_from_request() { |
| 476 |
if ( isset( $_REQUEST['list_item_name'] ) ) { |
| 477 |
$tableform_column_options = array(); |
| 478 |
$i = 0; |
| 479 |
foreach ( $_REQUEST['list_item_name'] as $column_name ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 480 |
$tableform_column_options[] = array( |
| 481 |
'column_name' => $column_name, |
| 482 |
'label' => isset( $_REQUEST[ $column_name ] ) ? |
| 483 |
sanitize_text_field( wp_unslash( $_REQUEST[ $column_name ] ) ) : |
| 484 |
ucfirst( str_replace( '_', ' ', $column_name ) ), |
| 485 |
'readonly' => isset( $_REQUEST[ "{$column_name}_readonly" ] ) ? 'on' : 'off', |
| 486 |
'less' => isset( $_REQUEST[ "{$column_name}_less" ] ) ? 'on' : 'off', |
| 487 |
'show' => isset( $_REQUEST[ "{$column_name}_show" ] ) ? 'on' : 'off', |
| 488 |
'lookup' => isset( $_REQUEST[ "{$column_name}_lookup" ] ) ? |
| 489 |
sanitize_text_field( wp_unslash( $_REQUEST[ "{$column_name}_lookup" ] ) ) : |
| 490 |
false, |
| 491 |
'hide_lookup_key' => isset( $_REQUEST[ "{$column_name}_hide_lookup_key" ] ) ? 'on' : 'off', |
| 492 |
); |
| 493 |
if ( isset( $_REQUEST[ "{$column_name}_default" ] ) && '' !== $_REQUEST[ "{$column_name}_default" ] ) { |
| 494 |
$tableform_column_options[ $i ]['default'] = |
| 495 |
sanitize_text_field( wp_unslash( $_REQUEST[ "{$column_name}_default" ] ) ); |
| 496 |
} |
| 497 |
$i ++; |
| 498 |
} |
| 499 |
|
| 500 |
return $tableform_column_options; |
| 501 |
} else { |
| 502 |
return null; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* Reconcile table |
| 508 |
* |
| 509 |
* @param object $table_structure Table structure |
| 510 |
* @param string $param_keep_options Possible values: 'on' and 'off' |
| 511 |
* |
| 512 |
* @return mixed Result of SQL update |
| 513 |
*/ |
| 514 |
public function reconcile( |
| 515 |
$table_structure, |
| 516 |
$param_keep_options |
| 517 |
) { |
| 518 |
if ( 1 === $this->query() ) { |
| 519 |
// Table definition. |
| 520 |
$this->wpda_table_design->design_mode = $table_structure['design_mode']; |
| 521 |
$this->wpda_table_design->engine = $table_structure['engine']; |
| 522 |
$this->wpda_table_design->collation = $table_structure['collation']; |
| 523 |
// Table columns. |
| 524 |
$this->wpda_table_design->table = $table_structure['table']; |
| 525 |
// Table indexes. |
| 526 |
$this->wpda_table_design->indexes = $table_structure['indexes']; |
| 527 |
|
| 528 |
if ( 'on' !== $param_keep_options ) { |
| 529 |
// Clear arrays. |
| 530 |
$this->wpda_table_design->listtable_column_options = array(); |
| 531 |
$this->wpda_table_design->tableform_column_options = array(); |
| 532 |
$this->wpda_table_design->tableinfo = array( |
| 533 |
'tab_label' => '', |
| 534 |
'default_where' => '', |
| 535 |
'default_orderby' => '', |
| 536 |
'custom_table_settings' => '', |
| 537 |
); |
| 538 |
} else { |
| 539 |
// Remove non existing columns from arrays. |
| 540 |
$new_listtable_column_options = array(); |
| 541 |
foreach ( $this->wpda_table_design->listtable_column_options as $listtable_column_option ) { |
| 542 |
$column_found = false; |
| 543 |
foreach ( $this->wpda_table_design->table as $table_column ) { |
| 544 |
if ( $table_column->column_name === $listtable_column_option->column_name ) { |
| 545 |
$column_found = true; |
| 546 |
} |
| 547 |
} |
| 548 |
if ( $column_found ) { |
| 549 |
// Add only column to array that were found in the table definition. |
| 550 |
array_push( $new_listtable_column_options, $listtable_column_option );//phpcs:ignore - 8.1 proof |
| 551 |
} |
| 552 |
} |
| 553 |
$this->wpda_table_design->listtable_column_options = $new_listtable_column_options; |
| 554 |
$new_tableform_column_options = array(); |
| 555 |
foreach ( $this->wpda_table_design->tableform_column_options as $tableform_column_option ) { |
| 556 |
$column_found = false; |
| 557 |
foreach ( $this->wpda_table_design->table as $table_column ) { |
| 558 |
if ( $table_column->column_name === $tableform_column_option->column_name ) { |
| 559 |
$column_found = true; |
| 560 |
} |
| 561 |
} |
| 562 |
if ( $column_found ) { |
| 563 |
// Add only column to array that were found in the table definition. |
| 564 |
array_push( $new_tableform_column_options, $tableform_column_option );//phpcs:ignore - 8.1 proof |
| 565 |
} |
| 566 |
} |
| 567 |
$this->wpda_table_design->tableform_column_options = $new_tableform_column_options; |
| 568 |
} |
| 569 |
|
| 570 |
$wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( $this->wpda_schema_name, $this->wpda_table_name ); |
| 571 |
$table_column_headers = $wpda_list_columns->get_table_column_headers(); |
| 572 |
|
| 573 |
foreach ( $this->wpda_table_design->table as $column ) { |
| 574 |
if ( 'on' !== $param_keep_options ) { |
| 575 |
// Every column must be added to array. |
| 576 |
$this->reconcile_add_column( |
| 577 |
$column, |
| 578 |
$wpda_list_columns->get_column_label( $column->column_name ), |
| 579 |
isset( $table_column_headers[ $column->column_name ] ) ? |
| 580 |
$table_column_headers[ $column->column_name ] : |
| 581 |
$wpda_list_columns::get_default_column_label( $column->column_name ) |
| 582 |
); |
| 583 |
} else { |
| 584 |
// Add only new columns to array. |
| 585 |
$column_found = false; |
| 586 |
foreach ( $this->wpda_table_design->listtable_column_options as $listtable_column_option ) { |
| 587 |
if ( isset( $column->column_name ) && isset( $listtable_column_option->column_name ) ) { |
| 588 |
if ( $column->column_name === $listtable_column_option->column_name ) { |
| 589 |
$column_found = true; |
| 590 |
break; |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
if ( ! $column_found ) { |
| 595 |
// Add column to both arrays. |
| 596 |
$this->reconcile_add_column( |
| 597 |
$column, |
| 598 |
$wpda_list_columns->get_column_label( $column->column_name ), |
| 599 |
isset( $table_column_headers[ $column->column_name ] ) ? |
| 600 |
$table_column_headers[ $column->column_name ] : |
| 601 |
$wpda_list_columns::get_default_column_label( $column->column_name ) |
| 602 |
); |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
global $wpdb; |
| 608 |
|
| 609 |
return $wpdb->update( |
| 610 |
$this->table_name, |
| 611 |
array( |
| 612 |
'wpda_table_design' => json_encode( $this->wpda_table_design ), |
| 613 |
), |
| 614 |
array( |
| 615 |
'wpda_schema_name' => $this->wpda_schema_name, |
| 616 |
'wpda_table_name' => $this->wpda_table_name, |
| 617 |
'wpda_table_setname' => $this->wpda_table_setname, |
| 618 |
) |
| 619 |
); |
| 620 |
} |
| 621 |
|
| 622 |
/** |
| 623 |
* Reconcile a columns |
| 624 |
* |
| 625 |
* @param object $column Column info |
| 626 |
*/ |
| 627 |
private function reconcile_add_column( $column, $label_list, $label_form ) { |
| 628 |
$this->wpda_table_design->listtable_column_options[] = |
| 629 |
array( |
| 630 |
'column_name' => $column->column_name, |
| 631 |
'label' => $label_list, |
| 632 |
'show' => 'on', |
| 633 |
); |
| 634 |
$this->wpda_table_design->tableform_column_options[] = |
| 635 |
array( |
| 636 |
'column_name' => $column->column_name, |
| 637 |
'label' => $label_form, |
| 638 |
'show' => 'on', |
| 639 |
); |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* Get column options for specific table name and label type |
| 644 |
* |
| 645 |
* @param string $table_name Database table name |
| 646 |
* @param string $label_type Label type |
| 647 |
* @param string $setname Options set name |
| 648 |
* |
| 649 |
* @return array|null |
| 650 |
*/ |
| 651 |
public static function get_column_options( $table_name, $label_type, $setname = 'default', $schema_name = '' ) { |
| 652 |
if ( ! isset( self::$cache_table_options[ "$table_name.$setname" ] ) ) { |
| 653 |
global $wpdb; |
| 654 |
$table_json = $wpdb->get_results( |
| 655 |
$wpdb->prepare( |
| 656 |
"SELECT wpda_table_design FROM `%1s` WHERE wpda_schema_name = %s AND wpda_table_name = %s AND ( wpda_table_setname = %s OR wpda_table_setname = 'default') ORDER BY IF( wpda_table_setname='default', 1 , 0 )", // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 657 |
array( |
| 658 |
WPDA::remove_backticks( self::get_base_table_name() ), |
| 659 |
$schema_name, |
| 660 |
$table_name, |
| 661 |
$setname, |
| 662 |
) |
| 663 |
), |
| 664 |
'ARRAY_A' |
| 665 |
); // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 666 |
|
| 667 |
if ( $wpdb->num_rows > 0 ) { |
| 668 |
if ( isset( $table_json[0]['wpda_table_design'] ) ) { |
| 669 |
$table_obj = json_decode( $table_json[0]['wpda_table_design'] ); |
| 670 |
if ( isset( $table_obj->table ) ) { |
| 671 |
self::$cache_table_options[ "$table_name.$setname" ]['table'] = $table_obj->table; |
| 672 |
} |
| 673 |
self::$cache_table_options[ "$table_name.$setname" ]['tableinfo'] = isset( $table_obj->tableinfo ) ? $table_obj->tableinfo : null; |
| 674 |
if ( isset( $table_obj->tableform_column_options ) ) { |
| 675 |
self::$cache_table_options[ "$table_name.$setname" ]['tableform'] = $table_obj->tableform_column_options; |
| 676 |
} |
| 677 |
if ( isset( $table_obj->listtable_column_options ) ) { |
| 678 |
self::$cache_table_options[ "$table_name.$setname" ]['listtable'] = $table_obj->listtable_column_options; |
| 679 |
} |
| 680 |
if ( isset( $table_obj->relationships ) ) { |
| 681 |
self::$cache_table_options[ "$table_name.$setname" ]['relationships'] = $table_obj->relationships; |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
} |
| 686 |
if ( 'tableform' === $label_type && isset( self::$cache_table_options[ "$table_name.$setname" ]['tableform'] ) ) { |
| 687 |
return self::$cache_table_options[ "$table_name.$setname" ]['tableform']; |
| 688 |
} |
| 689 |
if ( 'listtable' === $label_type && isset( self::$cache_table_options[ "$table_name.$setname" ]['listtable'] ) ) { |
| 690 |
return self::$cache_table_options[ "$table_name.$setname" ]['listtable']; |
| 691 |
} |
| 692 |
if ( 'relationships' === $label_type && isset( self::$cache_table_options[ "$table_name.$setname" ]['relationships'] ) ) { |
| 693 |
return array( |
| 694 |
'table' => isset( self::$cache_table_options[ "$table_name.$setname" ]['table'] ) ? self::$cache_table_options[ "$table_name.$setname" ]['table'] : null, |
| 695 |
'tableinfo' => isset( self::$cache_table_options[ "$table_name.$setname" ]['tableinfo'] ) ? self::$cache_table_options[ "$table_name.$setname" ]['tableinfo'] : null, |
| 696 |
'relationships' => self::$cache_table_options[ "$table_name.$setname" ]['relationships'], |
| 697 |
); |
| 698 |
} |
| 699 |
if ( 'tableinfo' === $label_type ) { |
| 700 |
return isset( self::$cache_table_options[ "$table_name.$setname" ]['tableinfo'] ) ? self::$cache_table_options[ "$table_name.$setname" ]['tableinfo'] : null; |
| 701 |
} |
| 702 |
|
| 703 |
return null; |
| 704 |
} |
| 705 |
|
| 706 |
/** |
| 707 |
* Overwrites method insert_reverse_engineered to add table options |
| 708 |
* |
| 709 |
* @param $wpda_table_name |
| 710 |
* @param $wpda_table_setname |
| 711 |
* @param $wpda_table_design |
| 712 |
* |
| 713 |
* @return bool |
| 714 |
*/ |
| 715 |
public static function insert_reverse_engineered( $wpda_table_name, $wpda_table_setname, $wpda_table_design, $wpda_schema_name = '' ) { |
| 716 |
global $wpdb; |
| 717 |
|
| 718 |
$table_name = self::get_base_table_name(); |
| 719 |
|
| 720 |
$wpda_table_design['table_type'] = WPDA_Design_Table_Model::get_table_type( $wpda_table_name, $wpda_schema_name ); |
| 721 |
|
| 722 |
$wpda_table_design['listtable_column_options'] = array(); |
| 723 |
$wpda_table_design['tableform_column_options'] = array(); |
| 724 |
$wpda_table_design['tableinfo'] = array( |
| 725 |
'tab_label' => '', |
| 726 |
'default_where' => '', |
| 727 |
'default_orderby' => '', |
| 728 |
'custom_table_settings' => '', |
| 729 |
); |
| 730 |
|
| 731 |
$wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( $wpda_schema_name, $wpda_table_name ); |
| 732 |
$table_column_headers = $wpda_list_columns->get_table_column_headers(); |
| 733 |
|
| 734 |
foreach ( $wpda_table_design['table'] as $column ) { |
| 735 |
$label = $wpda_list_columns->get_column_label( $column->column_name ); |
| 736 |
$wpda_table_design['listtable_column_options'][] = |
| 737 |
array( |
| 738 |
'column_name' => $column->column_name, |
| 739 |
'label' => $label, |
| 740 |
'show' => 'on', |
| 741 |
); |
| 742 |
|
| 743 |
$label = isset( $table_column_headers[ $column->column_name ] ) ? |
| 744 |
$table_column_headers[ $column->column_name ] : $wpda_list_columns::get_default_column_label( $column->column_name ); |
| 745 |
$wpda_table_design['tableform_column_options'][] = |
| 746 |
array( |
| 747 |
'column_name' => $column->column_name, |
| 748 |
'label' => $label, |
| 749 |
'show' => 'on', |
| 750 |
); |
| 751 |
} |
| 752 |
|
| 753 |
return ( |
| 754 |
1 === $wpdb->insert( |
| 755 |
$table_name, |
| 756 |
array( |
| 757 |
'wpda_schema_name' => $wpda_schema_name, |
| 758 |
'wpda_table_name' => $wpda_table_name, |
| 759 |
'wpda_table_setname' => $wpda_table_setname, |
| 760 |
'wpda_table_design' => json_encode( $wpda_table_design ), |
| 761 |
) |
| 762 |
) |
| 763 |
); |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
* Update table design |
| 768 |
* |
| 769 |
* @return bool TRUE = update successful, FALSE ; update failed. |
| 770 |
*/ |
| 771 |
public function update() { |
| 772 |
if ( null === $this->wpda_table_name ) { |
| 773 |
return false; |
| 774 |
} |
| 775 |
|
| 776 |
$this->prepare_update(); |
| 777 |
|
| 778 |
$setname_old = |
| 779 |
$this->wpda_table_setname !== $this->wpda_table_setname_old ? |
| 780 |
$this->wpda_table_setname_old : |
| 781 |
$this->wpda_table_setname; |
| 782 |
|
| 783 |
global $wpdb; |
| 784 |
$result_update = $wpdb->update( |
| 785 |
$this->table_name, |
| 786 |
array( |
| 787 |
'wpda_table_setname' => $this->wpda_table_setname, |
| 788 |
'wpda_table_design' => json_encode( $this->wpda_table_design ), |
| 789 |
), |
| 790 |
array( |
| 791 |
'wpda_schema_name' => $this->wpda_schema_name, |
| 792 |
'wpda_table_name' => $this->wpda_table_name, |
| 793 |
'wpda_table_setname' => $setname_old, |
| 794 |
) |
| 795 |
); |
| 796 |
|
| 797 |
if ( '' !== $wpdb->last_error ) { |
| 798 |
$msg = new WPDA_Message_Box( |
| 799 |
array( |
| 800 |
'message_text' => $wpdb->last_error, |
| 801 |
'message_type' => 'error', |
| 802 |
'message_is_dismissible' => false, |
| 803 |
) |
| 804 |
); |
| 805 |
$msg->box(); |
| 806 |
} |
| 807 |
|
| 808 |
if ( ! $result_update && $this->wpda_table_setname !== $this->wpda_table_setname_old ) { |
| 809 |
$this->wpda_table_setname = $this->wpda_table_setname_old; |
| 810 |
} |
| 811 |
|
| 812 |
return $result_update; |
| 813 |
} |
| 814 |
|
| 815 |
/** |
| 816 |
* Get table design from function arguments instead of HTTP arguments |
| 817 |
* |
| 818 |
* @param $wpda_schema_name |
| 819 |
* @param $wpda_table_name |
| 820 |
* @param $wpda_set_name |
| 821 |
* |
| 822 |
* @return mixed|null |
| 823 |
*/ |
| 824 |
public static function static_query( $wpda_schema_name, $wpda_table_name, $wpda_set_name ) { |
| 825 |
$wpda_table_design_raw = self::do_static_query( $wpda_schema_name, $wpda_table_name, $wpda_set_name ); |
| 826 |
|
| 827 |
global $wpdb; |
| 828 |
if ( $wpdb->num_rows === 0 && $wpda_set_name !== 'default' ) { |
| 829 |
$wpda_table_design_raw = self::do_static_query( $wpda_schema_name, $wpda_table_name, 'default' ); |
| 830 |
} |
| 831 |
|
| 832 |
if ( $wpdb->num_rows > 0 ) { |
| 833 |
return json_decode( $wpda_table_design_raw[0]['wpda_table_design'] ); |
| 834 |
} else { |
| 835 |
return null; |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
protected static function do_static_query( $wpda_schema_name, $wpda_table_name, $wpda_set_name ) { |
| 840 |
global $wpdb; |
| 841 |
|
| 842 |
return $wpdb->get_results( |
| 843 |
$wpdb->prepare( |
| 844 |
'SELECT wpda_table_design FROM `%1s` WHERE wpda_schema_name = %s AND wpda_table_name = %s AND wpda_table_setname = %s', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders |
| 845 |
array( |
| 846 |
WPDA::remove_backticks( self::get_base_table_name() ), |
| 847 |
$wpda_schema_name, |
| 848 |
$wpda_table_name, |
| 849 |
$wpda_set_name, |
| 850 |
) |
| 851 |
), |
| 852 |
'ARRAY_A' |
| 853 |
); |
| 854 |
} |
| 855 |
|
| 856 |
} |
| 857 |
|
| 858 |
} |
| 859 |
|