Commit 63ec9cf7 authored by Eric Windham's avatar Eric Windham

Merge branch 'issue#3753' into 'feat-stage-two'

Create a cache and update new db columns on form duplicate

See merge request ninja-forms/ninja-forms!3695
parents 5ba959e2 f5d89367
Pipeline #439 passed with stage
in 1 minute and 26 seconds
......@@ -226,10 +226,43 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
Ninja_Forms()->template( 'admin-notice-form-import.html.php', array( 'form_id'=> self::$imported_form_id ) );
}
/**
* This static method is called to duplicate a form using the form ID.
*
* To duplicate a form we:
*
* Check to see if we've ran stage one of the db update process.
* Use SQL to insert a copy of our form and form meta.
* Grab all fields for a specific form.
* Loop over those fields and insert fields and field meta.
* Run ->update_settings() and ->save() on the form model.
* Call our WPN_Helper method to build a form cache.
*
* @since 3.0
* @update UPDATE_VERSION_ON_MERGE
* @param int $form_id ID of the form being duplicated.
* @return $new_form_id ID of our form duplicate.
*/
public static function duplicate( $form_id )
{
global $wpdb;
/**
* Check to see if we've got new field columns.
*
* We do this here instead of the get_sql_queries() method so that we don't hit the db multiple times.
*/
$sql = "SHOW COLUMNS FROM {$wpdb->prefix}nf3_fields LIKE 'field_key'";
$results = $wpdb->get_results( $sql );
// If we don't have the field_key column, we need to remove our new columns.
if ( empty ( $results ) ) {
$db_stage_one_complete = false;
} else {
$db_stage_one_complete = true;
}
// Duplicate the Form Object.
$wpdb->query( $wpdb->prepare(
"
......@@ -261,22 +294,28 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
", $form_id
));
// Get our field and field_meta table column names and values.
$fields_sql = self::get_sql_queries( 'field_table_columns' );
$field_meta_sql = self::get_sql_queries( 'field_meta_table_columns' );
foreach( $old_fields as $old_field ){
// Duplicate the Field Object.
$wpdb->query( $wpdb->prepare(
"
INSERT INTO {$wpdb->prefix}nf3_fields ( `label`, `key`, `type`, `parent_id` )
SELECT `label`, `key`, `type`, %d
INSERT INTO {$wpdb->prefix}nf3_fields ( {$fields_sql[ 'insert' ]} )
SELECT {$fields_sql[ 'select' ]}
FROM {$wpdb->prefix}nf3_fields
WHERE id = %d
", $new_form_id, $old_field->id
));
$new_field_id = $wpdb->insert_id;
// Duplicate the Field Meta.
$wpdb->query( $wpdb->prepare(
"
INSERT INTO {$wpdb->prefix}nf3_field_meta ( `parent_id`, `key`, `value` )
SELECT %d, `key`, `value`
INSERT INTO {$wpdb->prefix}nf3_field_meta ( {$field_meta_sql[ 'insert' ]} )
SELECT {$field_meta_sql[ 'select' ]}
FROM {$wpdb->prefix}nf3_field_meta
WHERE parent_id = %d;
", $new_field_id, $old_field->id
......@@ -294,12 +333,16 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
", $form_id
));
// Get our action and action_meta table columns and values.
$actions_sql = self::get_sql_queries( 'action_table_columns' );
$actions_meta_sql = self::get_sql_queries( 'action_meta_table_columns' );
foreach( $old_actions as $old_action ){
// Duplicate the Action Object.
$wpdb->query( $wpdb->prepare(
"
INSERT INTO {$wpdb->prefix}nf3_actions ( `title`, `key`, `type`, `active`, `parent_id` )
SELECT `title`, `key`, `type`, `active`, %d
INSERT INTO {$wpdb->prefix}nf3_actions ( {$actions_sql[ 'insert' ]} )
SELECT {$actions_sql[ 'select' ]}
FROM {$wpdb->prefix}nf3_actions
WHERE id = %d
", $new_form_id, $old_action->id
......@@ -308,8 +351,8 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
// Duplicate the Action Meta.
$wpdb->query( $wpdb->prepare(
"
INSERT INTO {$wpdb->prefix}nf3_action_meta ( `parent_id`, `key`, `value` )
SELECT %d, `key`, `value`
INSERT INTO {$wpdb->prefix}nf3_action_meta ( {$actions_meta_sql[ 'insert' ]} )
SELECT {$actions_meta_sql[ 'select' ]}
FROM {$wpdb->prefix}nf3_action_meta
WHERE parent_id = %d;
", $new_action_id, $old_action->id
......@@ -324,9 +367,130 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
$new_form->update_settings( $new_form->get_settings() );
$new_form->save();
/*
* Build a cache for this new form.
*/
WPN_Helper::build_nf_cache( $new_form_id );
return $new_form_id;
}
/**
* When duplicating a form, we need to build specific SQL queries.
*
* This is a fairly repetative task, so we've extrapolated the code to its own function.
*
* @since UPDATE_VERSION_ON_MERGE
* @param string $table_name Name of the table we want to update.
* @return array Associative array like: ['insert' => "`column1`, "`column2`", etc", 'select' => "`column1`, etc."]
*/
private function get_sql_queries( $table_name, $db_stage_one_complete = true )
{
/**
* These arrays contain the columns in our database.
*
* Later, if the user hasn't ran stage one of our db update process, we'll remove the items that aren't supported.
*/
$db_columns = array(
'field_table_columns' => array(
'label',
'key',
'type',
'parent_id',
'field_label',
'field_key',
'order',
'required',
'default_value',
'label_pos',
'personally_identifiable',
),
'field_meta_table_columns' => array(
'parent_id',
'key',
'value',
'meta_key',
'meta_value',
),
'action_table_columns' => array(
'title',
'key',
'type',
'active',
'parent_id',
'label',
),
'action_meta_table_columns' => array(
'parent_id',
'key',
'value',
'meta_key',
'meta_value',
),
);
// If we haven't been passed a table name or the table name is invalid, return false.
if ( empty( $table_name ) || ! isset( $db_columns[ $table_name ] ) ) return false;
// If we have not completed stage one of our db update, then we unset new db columns.
if ( ! $db_stage_one_complete ) {
unset(
$field_table_columns[ 'field_label' ],
$field_table_columns[ 'field_key' ],
$field_table_columns[ 'order' ],
$field_table_columns[ 'required' ],
$field_table_columns[ 'default_value' ],
$field_table_columns[ 'label_pos' ],
$field_table_columns[ 'personally_identifiable' ]
);
unset(
$field_meta_table_columns[ 'meta_key' ],
$field_meta_table_columns[ 'meta_value' ]
);
unset(
$action_table_columns[ 'label' ]
);
unset(
$action_meta_table_columns[ 'meta_key' ],
$action_meta_table_columns[ 'meta_value' ]
);
}
/**
* $sql_insert is a var that tracks which table columns we want to insert.
*/
$sql_insert = "";
/**
* $sql_select is a var that tracks the values for each field table column.
*/
$sql_select = "";
/**
* Loop over our table column names and add them to our $sql_insert var.
*/
foreach( $db_columns[ $table_name ] as $column_name ) {
$sql_insert .= "`{$column_name}`,";
if ( 'parent_id' == $column_name ) {
$sql_select .= "%d,";
} else {
$sql_select .= "`{$column_name}`,";
}
}
// Remove any trailing commas.
$sql_insert = rtrim( $sql_insert, ',' );
$sql_select = rtrim( $sql_select, ',' );
return array( 'insert' => $sql_insert, 'select' => $sql_select );
}
public static function export( $form_id, $return = FALSE )
{
//TODO: Set Date Format from Plugin Settings
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment