page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // input var okay. } else { wp_die( __( 'ERROR: Wrong arguments [missing page]', 'wp-data-access' ) ); } if ( isset( $_REQUEST['action'] ) ) { $this->action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); } if ( isset( $_REQUEST['action2'] ) ) { $this->action2 = sanitize_text_field( wp_unslash( $_REQUEST['action2'] ) ); } $this->tabs = array( 'tableinfo' => __( 'Table Settings', 'wp-data-access' ), 'relation' => __( 'Relationships', 'wp-data-access' ), 'listtable' => __( 'List Table', 'wp-data-access' ), 'tableform' => __( 'Data Entry', 'wp-data-access' ), 'reconcile' => __( 'Reconcile', 'wp-data-access' ), ); $this->current_tab = 'tableinfo'; if ( isset( $_REQUEST['tab'] ) ) { $tab = sanitize_text_field( wp_unslash( $_REQUEST['tab'] ) ); // input var okay. if ( isset( $this->tabs[$tab] ) ) { $this->current_tab = $tab; } } $this->wpnonce_requested = ( isset( $_POST['wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['wpnonce'] ) ) : null ); // input var okay. if ( isset( $_REQUEST['wpda_table_name'] ) && !is_array( $_REQUEST['wpda_table_name'] ) ) { $wpda_project_design_table_model = new WPDP_Project_Design_Table_Model(); if ( 'reconcile' === $this->action ) { $wpda_table_name_re = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); // input var okay. $this->is_authorized( $wpda_table_name_re ); $wpda_schema_name_re = ( isset( $_REQUEST['wpda_schema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ) : '' ); // input var okay. $wpda_reverse_engineering = new WPDA_Reverse_Engineering($wpda_table_name_re, $wpda_schema_name_re); $table_structure = $wpda_reverse_engineering->get_designer_format( 'advanced' ); if ( isset( $_REQUEST['keep_options'] ) ) { $param_keep_options = sanitize_text_field( wp_unslash( $_REQUEST['keep_options'] ) ); } else { $param_keep_options = 'off'; } $result_update = $wpda_project_design_table_model->reconcile( $table_structure, $param_keep_options ); if ( false === $result_update ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Update failed', 'wp-data-access' ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } if ( 0 === $result_update ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Nothing to save', 'wp-data-access' ), )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Succesfully saved changes to database', 'wp-data-access' ), )); $msg->box(); } } elseif ( 'reverse_engineering' === $this->action ) { if ( isset( $_REQUEST['wpda_table_name'] ) ) { $wpda_table_name_re = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); // input var okay. $this->is_authorized( WPDP_Project_Design_Table_Model::get_base_table_name() ); $wpda_schema_name_re = ( isset( $_REQUEST['wpda_schema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ) : '' ); // input var okay. $wpda_reverse_engineering = new WPDA_Reverse_Engineering($wpda_table_name_re, $wpda_schema_name_re); $table_structure = $wpda_reverse_engineering->get_designer_format( 'advanced' ); if ( count( $table_structure ) > 0 ) { //phpcs:ignore - 8.1 proof $this->wpda_schema_name = $wpda_schema_name_re; $this->wpda_table_name = $wpda_table_name_re; $this->get_unique_setname(); $this->wpda_table_design = $table_structure; } else { wp_die( __( 'ERROR: Reverse engineering table failed [invalid structure]', 'wp-data-access' ) ); } if ( !WPDP_Project_Design_Table_Model::insert_reverse_engineered( $this->wpda_table_name, $this->wpda_table_setname, $this->wpda_table_design, $this->wpda_schema_name ) ) { $db_error = ( '' === $wpdb->last_error ? '' : ' [' . $wpdb->last_error . ']' ); wp_die( __( 'ERROR: Reverse engineering table failed' . $db_error, 'wp-data-access' ) ); } else { // Convert named array to object (needed to display structure). $this->wpda_table_design = json_decode( json_encode( $table_structure ) ); $wpda_project_design_table_model->prepare_query( $this->wpda_table_setname ); } $this->action2 = 'edit'; $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Table added to respository', 'wp-data-access' ), )); $msg->box(); } else { wp_die( __( 'ERROR: Wrong arguments', 'wp-data-access' ) ); } } elseif ( null !== $this->action2 ) { // Check authorization $table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); // input var okay. $this->is_authorized( $table_name ); $result_update = $wpda_project_design_table_model->update(); if ( false === $result_update ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Update failed', 'wp-data-access' ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { if ( 0 === $result_update ) { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Nothing to save', 'wp-data-access' ), )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => __( 'Successfully saved changes to database', 'wp-data-access' ), )); $msg->box(); } } } $wpda_project_design_table_model->query(); $structure_messages = $wpda_project_design_table_model->validate(); foreach ( $structure_messages as $messages ) { if ( 'ERR' === $messages[0] ) { $msg = new WPDA_Message_Box(array( 'message_text' => $messages[1], 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); } else { $msg = new WPDA_Message_Box(array( 'message_text' => $messages[1], )); $msg->box(); } } $this->table_structure = $wpda_project_design_table_model->get_table_design(); $this->wpda_table_setname = $wpda_project_design_table_model->get_table_setname(); $this->wpda_schema_name = ( isset( $_REQUEST['wpda_schema_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_schema_name'] ) ) : '' ); // input var okay. $this->wpda_schema_name_db = $this->wpda_schema_name; if ( '' === $this->wpda_schema_name ) { $this->wpda_schema_name = $wpdb->dbname; } $this->wpda_table_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_table_name'] ) ); $this->wpda_data_dictionary = new WPDA_Dictionary_Exist($this->wpda_schema_name, $this->wpda_table_name); if ( $this->wpda_data_dictionary->table_exists() ) { $this->wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( $this->wpda_schema_name, $this->wpda_table_name, 'tableform', $this->wpda_table_setname ); if ( 0 === count( $this->wpda_list_columns->get_table_primary_key() ) ) { //phpcs:ignore - 8.1 proof $this->has_primary_key = false; } else { $this->has_primary_key = true; } } else { wp_die( __( 'ERROR: Invalid table name or not authorized', 'wp-data-access' ) ); } } else { wp_die( __( 'ERROR: Argument wpda_table_name not found', 'wp-data-access' ) ); } $this->wpnonce = wp_create_nonce( self::WPNONCE_SEED . $this->wpda_table_name ); } public function prepare_form() { } protected function is_authorized( $table_name ) { if ( !wp_verify_nonce( $this->wpnonce_requested, self::WPNONCE_SEED . $table_name ) ) { wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); } } /** * Create a unique setname for this table */ public function get_unique_setname() { $this->wpda_table_setname = 'default'; // Default for first options set global $wpdb; $query = "select 'x' from `%1s` where wpda_table_name = %s"; $wpdb->get_results( $wpdb->prepare( $query, // phpcs:ignore WordPress.DB.PreparedSQL array(WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ), $this->wpda_table_name) ) ); if ( $wpdb->num_rows > 0 ) { $query = "select 'x' from `%1s` where wpda_schema_name = %s and wpda_table_name = %s and wpda_table_setname = %s"; $i = $wpdb->num_rows + 1; $this->wpda_table_setname = "options_set_{$i}"; $wpdb->get_results( $wpdb->prepare( $query, // phpcs:ignore WordPress.DB.PreparedSQL array( WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ), $this->wpda_schema_name, $this->wpda_table_name, $this->wpda_table_setname ) ) ); while ( $wpdb->num_rows > 0 ) { // Search until a free options set is found $this->wpda_table_setname = "options_set_{$i}"; $wpdb->get_results( $wpdb->prepare( $query, // phpcs:ignore WordPress.DB.PreparedSQL array( WPDA::remove_backticks( WPDP_Project_Design_Table_Model::get_base_table_name() ), $this->wpda_schema_name, $this->wpda_table_name, $this->wpda_table_setname ) ) ); $i++; } } } /** * Adds tabs to page */ protected function add_tabs() { ?>
tabs as $tab => $name ) { $form_id = esc_attr( $tab ) . '_form_id'; $page = esc_attr( WPDP::PAGE_TEMPLATES ); $schema_name = esc_attr( $this->wpda_schema_name_db ); $table_name = esc_attr( $this->wpda_table_name ); $set_name = esc_attr( $this->wpda_table_setname ); $tab_value = esc_attr( $tab ); $tab_form = "\n\t\t\t\t\t\t
\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t
\n\t\t\t\t\t"; echo $tab_form; // phpcs:ignore WordPress.Security.EscapeOutput } ?>
show_title(); $this->add_tabs(); switch ( $this->current_tab ) { case 'tableinfo': $this->show_table_info(); break; case 'relation': if ( true === $this->has_primary_key ) { $this->show_relations(); } else { $this->show_view(); } break; case 'listtable': $this->show_list_table(); break; case 'tableform': if ( true === $this->has_primary_key ) { $this->show_table_form(); } else { $this->show_view(); } break; case 'reconcile': $this->show_reconcile(); break; } ?>
action ) { // Set all forms to read only ?>

Sorry, this feature is only available for tables containing a primary or unique key.
wpda_schema_name || $wpdb->dbname === $this->wpda_schema_name ) { $wpda_table_name = $this->wpda_table_name; } else { $wpda_table_name = $this->wpda_table_name . ' (' . $this->wpda_schema_name . ')'; } $tab_label = ( isset( $this->table_structure->tableinfo->tab_label ) ? $this->table_structure->tableinfo->tab_label : '' ); $default_where = ( isset( $this->table_structure->tableinfo->default_where ) ? $this->table_structure->tableinfo->default_where : '' ); $default_orderby = ( isset( $this->table_structure->tableinfo->default_orderby ) ? $this->table_structure->tableinfo->default_orderby : '' ); $hyperlinks_parent = ( isset( $this->table_structure->tableinfo->hyperlinks_parent ) ? $this->table_structure->tableinfo->hyperlinks_parent : array() ); $hyperlinks_child = ( isset( $this->table_structure->tableinfo->hyperlinks_child ) ? $this->table_structure->tableinfo->hyperlinks_child : array() ); $html = ''; $html_child = ''; $settings_db = WPDA_Table_Settings_Model::query( $this->wpda_table_name, $this->wpda_schema_name ); if ( isset( $settings_db[0]['wpda_table_settings'] ) && '' !== $settings_db[0]['wpda_table_settings'] ) { $settings = json_decode( $settings_db[0]['wpda_table_settings'] ); if ( isset( $settings->hyperlinks ) && is_array( $settings->hyperlinks ) ) { foreach ( $settings->hyperlinks as $hyperlink ) { if ( isset( $hyperlink->hyperlink_label ) ) { $hyperlink_label = $hyperlink->hyperlink_label; if ( isset( $hyperlinks_parent->{$hyperlink_label} ) ) { $checked = ( $hyperlinks_parent->{$hyperlink_label} ? 'checked' : '' ); } else { $checked = ''; } $html .= ""; if ( isset( $hyperlinks_child->{$hyperlink_label} ) ) { $checked = ( $hyperlinks_child->{$hyperlink_label} ? 'checked' : '' ); } else { $checked = ''; } $html_child .= ""; } } } } if ( '' === $html && '' === $html_child ) { $hint = 'Hyperlinks'; } else { $hint = __( 'Hyperlinks - uncheck to disable', 'wp-data-access' ) . ' '; } if ( '' === $html ) { $html = '--'; } if ( '' === $html_child ) { $html_child = '--'; } ?>
wpda_schema_name, $this->wpda_table_name ); ?>
wpda_list_columns->get_table_columns(); $target_table_names = WPDA_Dictionary_Lists::get_tables( true, $this->wpda_schema_name ); $available_databases = WPDA_Dictionary_Lists::get_db_schemas(); $i = 0; global $wpdb; if ( '' === $this->wpda_schema_name || $wpdb->dbname === $this->wpda_schema_name ) { $wpda_table_name = $this->wpda_table_name; } else { $wpda_table_name = $this->wpda_table_name . ' (' . $this->wpda_schema_name . ')'; } ?>
table_structure->relationships ) ) { $relationships = $this->table_structure->relationships; if ( 0 < count( $relationships ) ) { //phpcs:ignore - 8.1 proof foreach ( $relationships as $relationship ) { ?> table_structure ) { return __( 'Invalid structure', 'wp-data-access' ); } global $wpdb; if ( '' === $this->wpda_schema_name || $wpdb->dbname === $this->wpda_schema_name ) { $wpda_table_name = $this->wpda_table_name; } else { $wpda_table_name = $this->wpda_table_name . ' (' . $this->wpda_schema_name . ')'; } ?>
table_structure->listtable_column_options ) ) { $structure = $this->table_structure->listtable_column_options; foreach ( $this->table_structure->table as $column ) { $table_structure[$column->column_name] = $column; } } else { $structure = $this->table_structure->table; } $i = 0; foreach ( $structure as $column ) { $column_name = $column->column_name; if ( isset( $this->table_structure->listtable_column_options ) && isset( $table_structure[$column_name] ) ) { $data_type = $table_structure[$column_name]->data_type; $type_attribute = $table_structure[$column_name]->type_attribute; $key = $table_structure[$column_name]->key; $mandatory = $table_structure[$column_name]->mandatory; $max_length = $table_structure[$column_name]->max_length; } else { $msg = new WPDA_Message_Box(array( 'message_text' => __( "Column {$column_name} not found for list table", 'wp-data-access' ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); break; } if ( '' === $max_length ) { $data_type = $data_type . ' ' . $type_attribute; } else { $data_type = $data_type . ' (' . $max_length . ') ' . $type_attribute; } if ( isset( $this->table_structure->listtable_column_options ) ) { $show_in_list = ( 'on' === $column->show ? 'checked' : '' ); $label_in_list = $column->label; $lookup_in_list = ( isset( $column->lookup ) ? $column->lookup : '' ); } else { $show_in_list = 'checked'; $label_in_list = ucfirst( str_replace( '_', ' ', $column_name ) ); $lookup_in_list = ''; } $i++; ?>
style="vertical-align:middle;width:16px;height:16px;" /> table_structure->relationships ) ) { foreach ( $this->table_structure->relationships as $relationship ) { if ( $column_name === $relationship->source_column_name[0] && ('lookup' === $relationship->relation_type || 'autocomplete' === $relationship->relation_type) ) { if ( isset( $relationship->target_schema_name ) ) { $target_schema_name = $relationship->target_schema_name; } else { $target_schema_name = $this->wpda_schema_name; } $lookup_column_list = WPDA_Dictionary_Lists::get_table_columns( $relationship->target_table_name, $target_schema_name ); ?>
table_structure ) { return __( 'Invalid structure', 'wp-data-access' ); } global $wpdb; if ( '' === $this->wpda_schema_name || $wpdb->dbname === $this->wpda_schema_name ) { $wpda_table_name = $this->wpda_table_name; } else { $wpda_table_name = $this->wpda_table_name . ' (' . $this->wpda_schema_name . ')'; } ?>
table_structure->tableform_column_options ) ) { $structure = $this->table_structure->tableform_column_options; foreach ( $this->table_structure->table as $column ) { $table_structure[$column->column_name] = $column; } } else { $structure = $this->table_structure->table; } $i = 0; foreach ( $structure as $column ) { $column_name = $column->column_name; if ( isset( $this->table_structure->tableform_column_options ) && isset( $table_structure[$column_name] ) ) { $data_type = $table_structure[$column_name]->data_type; $type_attribute = $table_structure[$column_name]->type_attribute; $key = $table_structure[$column_name]->key; $mandatory = $table_structure[$column_name]->mandatory; $max_length = $table_structure[$column_name]->max_length; } else { $msg = new WPDA_Message_Box(array( 'message_text' => __( "Column {$column_name} not found for data entry form", 'wp-data-access' ), 'message_type' => 'error', 'message_is_dismissible' => false, )); $msg->box(); break; } if ( '' === $max_length ) { $data_type = $data_type . ' ' . $type_attribute; } else { $data_type = $data_type . ' (' . $max_length . ') ' . $type_attribute; } if ( isset( $this->table_structure->tableform_column_options ) ) { $show_on_form = ( 'on' === $column->show ? 'checked' : '' ); $label_on_form = $column->label; $lookup_in_list = ( isset( $column->lookup ) ? $column->lookup : '' ); } else { $show_on_form = 'checked'; $label_on_form = ucfirst( str_replace( '_', ' ', $column_name ) ); $lookup_in_list = ''; } if ( isset( $column->readonly ) ) { $readonly_on_form = ( 'on' === $column->readonly ? 'checked' : '' ); } else { $readonly_on_form = ''; } if ( isset( $column->less ) ) { $less_on_form = ( 'on' === $column->less ? 'checked' : '' ); } else { $less_on_form = 'checked'; } if ( isset( $column->default ) ) { $default_on_form = esc_html( $column->default ); } else { $default_on_form = ''; } if ( isset( $column->hide_lookup_key ) ) { $hide_id = ( 'on' === $column->hide_lookup_key ? 'checked' : '' ); } else { $hide_id = 'checked'; } $i++; ?>
wpda_list_columns->get_auto_increment_column_name() === $column_name ? ' (auto increment)' : '' ); ?> wpda_list_columns->get_auto_increment_column_name() === $column_name ) { // Allow to hide auto_increment column $l_key = 'No'; $l_mandatory = 'No'; } else { $l_key = $key; $l_mandatory = $mandatory; } ?> style="vertical-align:middle;width:16px;height:16px;" /> /> /> table_structure->relationships ) ) { foreach ( $this->table_structure->relationships as $relationship ) { if ( $column_name === $relationship->source_column_name[0] && ('lookup' === $relationship->relation_type || 'autocomplete' === $relationship->relation_type) ) { if ( isset( $relationship->target_schema_name ) ) { $target_schema_name = $relationship->target_schema_name; } else { $target_schema_name = $this->wpda_schema_name; } $lookup_column_list = WPDA_Dictionary_Lists::get_table_columns( $relationship->target_table_name, $target_schema_name ); ?> style="vertical-align:middle;width:16px;height:16px;" />
Update table design from database table
  • Add new columns
  • Remove deleted columns
  • Change column settings
  • Update settings