Commit dada32d6 authored by Kenneth Hall's avatar Kenneth Hall

Merge branch 'stage-one-tables' into 'develop'

Stage one tables

See merge request ninja-forms/ninja-forms!3619
parents b95a93e1 512ab6d3
......@@ -31,6 +31,21 @@ _**nf3_forms**_ (Table of individual Forms)
* int(11)
* subs (The Form's number of lifetime Submissions)
* int(11)
* form_title (The displayable title of the Form)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
* default_label_pos (The default label position of fields on the Form)
* varchar(15)
* show_title (Whether or not to show the Form title)
* bit(1)
* clear_complete (Whether or not to clear the Form after submission)
* bit(1)
* hide_complete (Whether or not to hide the Form after submission)
* bit(1)
* logged_in (Whether or not the user must be logged in to view the Form)
* bit(1)
* seq_num (The number of the next submission to the Form)
* int(11)
_**nf3_form_meta**_ (Table of Settings assoicated with each Form)
......@@ -50,6 +65,12 @@ _**nf3_form_meta**_ (Table of Settings assoicated with each Form)
* value (The value of the Setting)
* longtext
* COLLATE DATABASE_DEFAULT
* meta_key (The administrative key of the Setting)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
* meta_value (The value of the Setting)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
### Fields
......@@ -270,6 +291,21 @@ _**nf3_upgrades**_ (Table of Forms as they exist in the current structure and th
* int(11)
* NOT NULL
* DEFAULT 0
### Chunks
_**nf3_chunks**_ (Table of Chunks created on publish to be reconstructed into a Ninja Forms cache)
* id (The unique ID of the Chunk)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary Key
* name (The name of the Chunk)
* varchar(200)
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
* value (The value of the Chunk)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
### Chunks
......@@ -575,3 +611,34 @@ _**nf3_chunks**_ (Table of Chunks created on publish to be reconstructed into a
* value (The value of the Chunk)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
Updated forms tables
### Forms
_**nf3_forms**_ (Table of individual Forms)
ADDED:
* form_title (The displayable title of the Form)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
* default_label_pos (The default label position of fields on the Form)
* varchar(15)
* show_title (Whether or not to show the Form title)
* bit(1)
* clear_complete (Whether or not to clear the Form after submission)
* bit(1)
* hide_complete (Whether or not to hide the Form after submission)
* bit(1)
* logged_in (Whether or not the user must be logged in to view the Form)
* bit(1)
* seq_num (The number of the next submission to the Form)
* int(11)
_**nf3_form_meta**_ (Table of Settings assoicated with each Form)
ADDED:
* meta_key (The administrative key of the Setting)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
* meta_value (The value of the Setting)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
......@@ -32,9 +32,40 @@ abstract class NF_Abstracts_Migration
else {
// We use standard utf8.
return 'DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci';
}
}
}
/**
* Function to get the column collate for ALTER TABLE statements.
*
* @since 3.3.7
*
* @return string
*/
public function collate()
{
global $wpdb;
// If our mysql version is 5.5.3 or higher...
if ( version_compare( $wpdb->db_version(), '5.5.3', '>=' ) ) {
// We can use mb4.
return 'COLLATE utf8mb4_general_ci';
} // Otherwise...
else {
// We use standard utf8.
return 'COLLATE utf8_general_ci';
}
}
/**
* Function to run our stage one db updates.
*/
public function _stage_one()
{
if ( method_exists( $this, 'do_stage_one' ) ) {
$this->do_stage_one();
}
}
public function _run()
{
......
......@@ -298,10 +298,13 @@ class NF_Abstracts_Model
}
}
$meta_select_fields = "SELECT `key`, `value`";//, `meta_key`,
//`meta_value`";
// Query settings from the meta table.
$meta_results = $this->_db->get_results(
$meta_select_fields .
"
SELECT `key`, `value`
FROM `$this->_meta_table_name`
WHERE `parent_id` = $this->_id
"
......@@ -309,7 +312,13 @@ class NF_Abstracts_Model
// Assign settings to the settings property.
foreach ($meta_results as $meta) {
$this->_settings[ $meta->key ] = $meta->value;
// If we don't already have a value from the main table...
// OR If that value was NULL...
if ( ! isset( $this->_settings[ $meta->key ] ) || NULL == $this->_settings[ $meta->key ] ) {
// TODO: Update this logic after removal of original meta columns.
// Set the value from meta.
$this->_settings[ $meta->key ] = $meta->value;
}
}
}
......@@ -578,15 +587,49 @@ class NF_Abstracts_Model
// If the setting is a column, save the settings to the model's table.
if( in_array( $key, $this->_columns ) ){
return $this->_db->update(
$this->_table_name,
array(
$key => $value
),
array(
'id' => $this->_id
)
);
$format = null;
if( in_array( $key, array( 'show_title', 'clear_complete', 'hide_complete', 'logged_in' ) ) ) {
// gotta set the format for the columns that use bit type
$format = '%d';
}
if( 'form' === $this->_type && 'title' == $key ) {
$this->_db->update(
$this->_table_name,
array(
'form_title' => $value
),
array(
'id' => $this->_id
),
$format
);
}
// Don't update the form_title. Duplicating issue for now
if( 'form_title' !== $key ) {
$update_model = $this->_db->update(
$this->_table_name,
array(
$key => $value
),
array(
'id' => $this->_id
),
$format
);
} else {
return 1;
}
/*
* if it's not a form, you can return, but we are still saving some
* settings for forms in the form_meta table
*/
if( 'form' != $this->_type
|| ( 'form' == $this->_type && 'title' == $key ) ) {
return $update_model;
}
}
$meta_row = $this->_db->get_row(
......@@ -600,11 +643,19 @@ class NF_Abstracts_Model
if( $meta_row ){
$update_values = array(
'value' => $value
);
// for forms we need to update the meta_key and meta_value columns
if( 'form' == $this->_type ) {
$update_values[ 'meta_key' ] = $key;
$update_values[ 'meta_value' ] = $value;
}
$result = $this->_db->update(
$this->_meta_table_name,
array(
'value' => $value
),
$update_values,
array(
'key' => $key,
'parent_id' => $this->_id
......@@ -613,13 +664,21 @@ class NF_Abstracts_Model
} else {
$insert_values = array(
'key' => $key,
'value' => $value,
'parent_id' => $this->_id
);
// for forms we need to update the meta_key and meta_value columns
if( 'form' == $this->_type ) {
$insert_values[ 'meta_key' ] = $key;
$insert_values[ 'meta_value' ] = $value;
}
$result = $this->_db->insert(
$this->_meta_table_name,
array(
'key' => $key,
'value' => $value,
'parent_id' => $this->_id
),
$insert_values,
array(
'%s',
'%s',
......@@ -628,7 +687,6 @@ class NF_Abstracts_Model
);
}
return $result;
}
......@@ -643,7 +701,7 @@ class NF_Abstracts_Model
{
if( ! $this->_settings ) return;
foreach( $this->_settings as $key => $value ){
foreach( $this->_settings as $key => $value ) {
$value = maybe_serialize( $value );
$this->_results[] = $this->_save_setting( $key, $value );
}
......@@ -674,7 +732,7 @@ class NF_Abstracts_Model
);
// If a relationship does not exists, then create one.
if( 0 == $this->_db->num_rows ){
if( 0 == $this->_db->num_rows ) {
$this->_db->insert(
$this->_relationships_table,
......
......@@ -17,10 +17,24 @@ class NF_Database_Migrations_FormMeta extends NF_Abstracts_Migration
`parent_id` int NOT NULL,
`key` longtext NOT NULL,
`value` longtext,
`meta_key` longtext,
`meta_value` longtext,
UNIQUE KEY (`id`)
) {$this->charset_collate()};";
dbDelta( $query );
}
/**
* Function to run our stage one upgrades.
*/
public function do_stage_one()
{
$query = "ALTER TABLE {$this->table_name()}
ADD `meta_key` longtext {$this->collate()},
ADD `meta_value` longtext {$this->collate()}";
global $wpdb;
$wpdb->query( $query );
}
}
......@@ -15,15 +15,47 @@ class NF_Database_Migrations_Forms extends NF_Abstracts_Migration
$query = "CREATE TABLE IF NOT EXISTS {$this->table_name()} (
`id` int NOT NULL AUTO_INCREMENT,
`title` longtext,
`key` longtext,
`key` longtext,
`created_at` TIMESTAMP,
`updated_at` DATETIME,
`views` int(11),
`subs` int(11),
`views` int(11),
`subs` int(11),
`form_title` longtext,
`default_label_pos` varchar(15),
`show_title` bit,
`clear_complete` bit,
`hide_complete` bit,
`logged_in` bit,
`seq_num` int,
UNIQUE KEY (`id`)
) {$this->charset_collate()};";
dbDelta( $query );
}
/**
* Function to run our stage one upgrades.
*/
public function do_stage_one()
{
/**
* TODO:
*
DROP `key`,
DROP `views`,
DROP `subs`,
*
*/
$query = "ALTER TABLE {$this->table_name()}
ADD `form_title` longtext {$this->collate()},
ADD `default_label_pos` varchar(15) {$this->collate()},
ADD `show_title` bit,
ADD `clear_complete` bit,
ADD `hide_complete` bit,
ADD `logged_in` bit,
ADD `seq_num` int";
global $wpdb;
$wpdb->query( $query );
}
}
......@@ -13,8 +13,14 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
protected $_columns = array(
'title',
'key',
'created_at'
'created_at',
'form_title',
'default_label_pos',
'show_title',
'clear_complete',
'hide_complete',
'logged_in',
'seq_num'
);
protected $_fields;
......@@ -119,10 +125,21 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
, $form_id ) );
if( $last_seq_num ) {
$wpdb->update( $wpdb->prefix . 'nf3_form_meta', array( 'value' => $last_seq_num + 1 ), array( 'key' => '_seq_num', 'parent_id' => $form_id ) );
$wpdb->update( $wpdb->prefix . 'nf3_form_meta', array( 'value' => $last_seq_num + 1,
'meta_value' => $last_seq_num + 1, 'meta_key' => '_seq_num' )
, array( 'key' => '_seq_num', 'parent_id'
=> $form_id ) );
$wpdb->update( $wpdb->prefix . 'nf3_forms', array( 'seq_num' => $last_seq_num + 1 ), array( 'id' => $form_id ) );
} else {
$last_seq_num = 1;
$wpdb->insert( $wpdb->prefix . 'nf3_form_meta', array( 'key' => '_seq_num', 'value' => $last_seq_num + 1, 'parent_id' => $form_id ) );
$wpdb->insert( $wpdb->prefix . 'nf3_form_meta',
array( 'key' => '_seq_num',
'value' => $last_seq_num + 1,
'parent_id' => $form_id,
'meta_key' => '_seq_num',
'meta_value' => $last_seq_num + 1
) );
$wpdb->update( $wpdb->prefix . 'nf3_forms', array( 'seq_num' => $last_seq_num + 1 ), array( 'id' => $form_id ) );
}
return $last_seq_num;
......@@ -299,6 +316,14 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
));
}
/*
* In order for our new form and form_meta fields to populate on
* duplicate we need to update_settings and save
*/
$new_form = Ninja_Forms()->form( $new_form_id )->get();
$new_form->update_settings( $new_form->get_settings() );
$new_form->save();
return $new_form_id;
}
......
......@@ -296,7 +296,7 @@ final class WPN_Helper
*/
public static function update_nf_cache( $id, $data ) {
// Define our current stage here for use as we run various upgrades.
$CURRENT_STAGE = 0;
$CURRENT_STAGE = 1;
// Serialize our data.
$cache = serialize( $data );
global $wpdb;
......
......@@ -227,7 +227,7 @@ if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf
// If we've not recorded our db version...
if ( ! get_option( 'ninja_forms_db_version' ) ) {
// Set it to the baseline (1.0).
add_option( 'ninja_forms_db_version', '1.0', '', 'no' );
add_option( 'ninja_forms_db_version', '1.1', '', 'no' );
}
/*
......@@ -400,6 +400,13 @@ if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf
// Ensure all of our tables have been defined.
$migrations = new NF_Database_Migrations();
$migrations->migrate();
// If our db version is below 1.1...
if ( version_compare( get_option( 'ninja_forms_db_version' ), '1.1', '<' ) ) {
// Do our stage 1 updates.
$migrations->do_stage_one();
// Update our db version.
update_option( 'ninja_forms_db_version', '1.1' );
}
}
}
......
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