Commit 2f1b1574 authored by Kevin Stover's avatar Kevin Stover

Too much to explain; this thing has gotten too large.

parent 5a38ff53
......@@ -19,17 +19,26 @@ jQuery( document ).ready( function( $ ) {
* @param object e Click Event
* @return void
*/
$( document ).on( 'click', '#nf-import-form', function( e ) {
$( document ).on( 'click', '#nf-import-form-submit', function( e ) {
// Make sure that our file field isn't empty.
if ( '' == importFormFile.name ) return false;
// Settings object for our batch processor
var settings = {
// Initial content for the popup modal
content: '<h3>Select a file</h3><p><input id="nf-import-file" type="file" /></p>',
btnPrimaryText: 'Import!',
btnSecondaryText: 'Cancel',
// Batch processor slug. Must match what we have set in our PHP settings array.
batch_type: 'import_form',
extraData: importFormFile
batch_type: 'import_form',
extraData: importFormFile,
loadingText: 'Importing...',
onCompleteCallback: function( response ) {
// If we don't get back a form ID, then bail.
if ( 'undefined' == typeof response.form_id ) return false;
jQuery( '#nf-import-file' ).val('');
var url = jQuery( '#nf-import-url' ).attr( 'href' );
jQuery( '#nf-import-url' ).attr( 'href', url + response.form_id );
jQuery( '#row-nf-import-response' ).show();
}
}
/**
* Instantiate our batch processor.
*
......@@ -53,6 +62,11 @@ jQuery( document ).ready( function( $ ) {
* @return {[type]} [description]
*/
$( document ).on( 'change', '#nf-import-file', function( e ) {
// Hide our success message.
jQuery( '#row-nf-import-response' ).hide();
// Hide our extension type error.
jQuery( '#row-nf-import-type-error' ).hide();
// Grab the file from the input.
var file = e.target.files[0];
// If our file var is empty, bail.
......@@ -63,14 +77,12 @@ jQuery( document ).ready( function( $ ) {
// Use some Regex to get the extension
var extension = file.name.match(/\.[0-9a-z]+$/i);
// If we don't have a .nff extension, bail.
// If we don't have a .nff extension, show our type error and bail.
if ( '.nff' !== extension[0] ) {
jQuery( '#row-nf-import-type-error' ).show();
return false;
}
// Disable the primary button for our batch processing modal.
jQuery( '#nf-button-primary-1' ).hide();
// Instantiate the HTML5 FileReader API.
var reader = new FileReader();
......@@ -83,7 +95,6 @@ jQuery( document ).ready( function( $ ) {
reader.addEventListener( 'load', function () {
importFormFile.name = file.name;
importFormFile.content = reader.result;
jQuery( '#nf-button-primary-1' ).show();
}, false);
// Use the readAsDataURL method of the FileReader API.
......
......@@ -299,7 +299,7 @@ jQuery(document).ready(function($) {
btnPrimaryText: nf_settings.i18n.trashExpiredSubsButtonPrimary,
btnSecondaryText: nf_settings.i18n.trashExpiredSubsButtonSecondary,
batch_type: 'expired_submission_cleanup',
extraData: [ 'test1', 'test2', 'test3' ]
// extraData: [ 'test1', 'test2', 'test3' ]
}
new NinjaBatchProcessor( settings );
});
......
......@@ -3,13 +3,37 @@
*/
function NinjaBatchProcessor( settings ) {
var that = this;
var data = {
var modalData = {
closeOnClick: false,
closeOnEsc: true,
content: settings.content,
btnPrimary: {
text: settings.btnPrimaryText,
callback: function( e ) {
useProgressBar: true
};
/**
* If we haven't been passed any content, make sure we pass an empty content var.
*/
if ( 'undefined' == typeof settings.content ) {
settings.content = '';
}
// Set our modalData content var.
modalData.content = settings.content;
/**
* If we've been passed a loadingText var, pass that along.
*/
if ( 'undefined' != typeof settings.loadingText ) {
modalData.loadingText = settings.loadingText;
}
/**
* If we haven't defined button text for our primary button, we don't want to pass button settings.
*
* Check to see if we've defined primary button text, and if we have, add button settings to the modalData.
*/
if ( 'undefined' != typeof settings.btnPrimaryText ) {
modalData.btnPrimary = {
text: settings.btnPrimaryText,
callback: function( e ) {
// Hide the buttons.
modalInstance.maybeShowActions( false );
// Show the progress bar.
......@@ -17,34 +41,46 @@ function NinjaBatchProcessor( settings ) {
// Begin our cleanup process.
that.postToProcessor( that, -1, modalInstance );
}
},
btnSecondary: {
text: settings.btnSecondaryText,
callback: function( e ) {
modalInstance.toggleModal( false );
}
},
useProgressBar: true,
};
}
};
modalData.btnSecondary = {
text: settings.btnSecondaryText,
callback: function( e ) {
modalInstance.toggleModal( false );
}
};
} else { // If we don't have any buttons defined, then we want to run the batch process on modal open.
modalData.onOpenCallback = function() {
// Hide the buttons.
this.maybeShowActions( false );
// Show the progress bar.
this.maybeShowProgress( true );
// Begin our cleanup process.
that.postToProcessor( that, -1, this );
}
}
this.postToProcessor = function( context, steps, modal, data ) {
if ( 'undefined' == typeof data ) {
var data = {
action: 'nf_batch_process',
batch_type: settings.batch_type,
security: nf_settings.batch_nonce,
security: nfAdmin.batchNonce,
extraData: settings.extraData
};
}
jQuery.post( nf_settings.ajax_url, data, function( response ) {
jQuery.post( ajaxurl, data, function( response ) {
response = JSON.parse( response );
// If we're done...
if ( response.batch_complete ) {
// Push our progress bar to 100%.
modal.setProgress( 100 );
modal.toggleModal( false );
if ( 'undefined' != typeof settings.onCompleteCallback ) {
settings.onCompleteCallback( response );
}
// Exit.
return false;
}
......@@ -77,5 +113,5 @@ function NinjaBatchProcessor( settings ) {
} );
}
var modalInstance = new NinjaModal( data );
var modalInstance = new NinjaModal( modalData );
}
\ No newline at end of file
......@@ -37,6 +37,7 @@ function NinjaModal ( data ) {
this.buttons.secondary = {};
this.buttons.primary.data = ( 'undefined' != typeof data.btnPrimary ? data.btnPrimary : false );
this.buttons.secondary.data = ( 'undefined' != typeof data.btnSecondary ? data.btnSecondary : false );
this.onOpenCallback = ( 'undefined' != typeof data.onOpenCallback ? data.onOpenCallback : false );
// See if we need the progress bar.
this.useProgressBar = ( 'undefined' != typeof data.useProgressBar ? data.useProgressBar : false );
if ( this.useProgressBar ) {
......@@ -170,6 +171,10 @@ NinjaModal.prototype.initModal = function () {
// Attach the callback.
jQuery( this.content ).find( '#nf-button-secondary-' + this.dataId ).click( that.buttons.secondary.callback );
}
if ( that.onOpenCallback ) {
that.onOpenCallback();
}
},
} );
// Setup our data id to keep the DOM ids unique.
......@@ -267,6 +272,12 @@ NinjaModal.prototype.renderContent = function () {
this.buttons.primary.dom.onclick = this.buttons.primary.callback;
this.buttons.secondary.dom.onclick = this.buttons.secondary.callback;
}
} else if ( this.onOpenCallback ) {
// this.popup.onOpen = function() {
// console.log( 'still got it' );
// this.onOpenCallback();
// }
}
// Set our content.
this.popup.setContent( document.createElement( 'div' ).appendChild( contentBox ).parentElement.innerHTML );
......
This diff is collapsed.
This diff is collapsed.
......@@ -21,6 +21,31 @@ define( [], function() {
* @return {void}
*/
maybeOpenModal: function( e ) {
e.preventDefault();
// Settings object for our batch processor
var settings = {
// Initial content for the popup modal
content: '',
// Batch processor slug. Must match what we have set in our PHP settings array.
batch_type: 'import_form_template',
loadingText: 'Importing...',
extraData: { template: this.model.get( 'id' ) },
onCompleteCallback: function( response ) {
// Bail if we don't return a form ID.
if ( 'undefined' == response.form_id ) return false;
window.location.href = nfAdmin.builderURL + response.form_id;
}
}
/**
* Instantiate our batch processor.
*
* This will open the modal and present the user with content and buttons.
*/
new NinjaBatchProcessor( settings );
// If this isn't an ad, then early return
if ( 'ad' != this.model.get( 'type' ) ) {
return true;
......
......@@ -44,6 +44,10 @@ class NF_AJAX_REST_BatchProcess extends NF_AJAX_REST_Controller
$batch = new NF_Admin_Processes_ImportForm(
$request_data );
break;
case 'import_form_template':
$batch = new NF_Admin_Processes_ImportFormTemplate(
$request_data );
break;
default:
$data[ 'error' ] = __( 'Invalid request.', 'ninja-forms' );
break;
......
......@@ -49,4 +49,10 @@ abstract class NF_Abstracts_BatchProcess
*/
}
public function respond()
{
echo wp_json_encode( $this->response );
wp_die();
}
}
\ No newline at end of file
......@@ -155,6 +155,7 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
wp_enqueue_script( 'backbone-marionette-3', Ninja_Forms::$url . 'assets/js/lib/backbone.marionette3.min.js', array( 'jquery', 'backbone' ) );
wp_enqueue_script( 'nf-jbox', Ninja_Forms::$url . 'assets/js/lib/jBox.min.js', array( 'jquery' ) );
wp_enqueue_script( 'nf-ninjamodal', Ninja_Forms::$url . 'assets/js/lib/ninjaModal.js', array( 'jquery' ) );
wp_enqueue_script( 'nf-batch-processor', Ninja_Forms::$url . 'assets/js/lib/batch-processor.js', array( 'nf-ninjamodal' ) );
wp_enqueue_script( 'nf-moment', Ninja_Forms::$url . 'assets/js/lib/moment-with-locales.min.js', array( 'jquery', 'nf-dashboard' ) );
wp_enqueue_script( 'nf-dashboard', Ninja_Forms::$url . 'assets/js/min/dashboard.min.js', array( 'backbone-radio', 'backbone-marionette-3' ) );
......@@ -172,6 +173,7 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
'doingCleanup' => ( ! get_option( 'ninja_forms_data_is_clean' ) &&
isset( $_REQUEST[ 'action' ] ) &&
'cleanup' == $_REQUEST[ 'action' ] ) ? 1 : 0,
'builderURL' => admin_url( 'admin.php?page=ninja-forms&form_id=' ),
) );
wp_enqueue_style( 'nf-builder', Ninja_Forms::$url . 'assets/css/builder.css' );
......@@ -192,6 +194,11 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
add_submenu_page( 'ninja-forms', '', '', 'read', '', '' );
}
/**
* TODO: Remove this function and its hook because we are handling template imports via the batch processor.
* @since 3.0
* @return void
*/
private function import_from_template()
{
$template = sanitize_title( $_GET['form_id'] );
......
......@@ -161,9 +161,9 @@ final class NF_Admin_Menus_ImportExport extends NF_Abstracts_Submenu
wp_enqueue_script( 'ninja_forms_admin_import_export' );
wp_localize_script( 'ninja_forms_admin_import_export', 'nf_settings', array(
wp_localize_script( 'ninja_forms_admin_import_export', 'nfAdmin', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'batch_nonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
'batchNonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
'i18n' => array(
'trashExpiredSubsMessage' => __( 'Are you sure you want to trash all expired submissions?', 'ninja-forms' ),
'trashExpiredSubsButtonPrimary' => __( 'Trash', 'ninja-forms' ),
......
......@@ -154,12 +154,44 @@ final class NF_Admin_Menus_Settings extends NF_Abstracts_Submenu
wp_enqueue_style( 'nf-combobox', Ninja_Forms::$url . 'assets/css/combobox.css' );
wp_enqueue_style( 'jBox', Ninja_Forms::$url . 'assets/css/jBox.css' );
wp_register_script( 'ninja_forms_admin_menu_settings', Ninja_Forms::$url . 'assets/js/admin-settings.js', array( 'jquery' ), FALSE, TRUE );
/**
* This wp_localize_script call should eventually be removed.
*
* TODO: Remove this function call when we've replaced references to nf_settings in our JS with nfAdmin.
*/
wp_localize_script( 'ninja_forms_admin_menu_settings', 'nf_settings', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'forms' => $form_options,
'nf_nuke_title' => __( 'Remove ALL Ninja Forms data and uninstall?', 'ninja-forms' ),
'nonce' => wp_create_nonce( "ninja_forms_settings_nonce" ),
'batch_nonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
'batchNonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
'i18n' => array(
'downgradeMessage' => __( 'Are you sure you want to downgrade?', 'ninja-forms' ),
'downgradeWarningMessage' => __( 'You WILL lose any forms or submissions created on this version of Ninja Forms.', 'ninja-forms' ),
'downgradeConfirmMessage' => __( 'Type ', 'ninja-forms' ) . '<span style="color: red";>' . 'DOWNGRADE' . "</span>" . __( ' to confirm.', 'ninja-forms' ),
'downgradeButtonPrimary' => __( 'Downgrade', 'ninja-forms'),
'downgradeButtonSecondary' => __( 'Cancel', 'ninja-forms' ),
'trashExpiredSubsMessage' => __( 'Are you sure you want to trash all expired submissions?', 'ninja-forms' ),
'trashExpiredSubsButtonPrimary' => __( 'Trash', 'ninja-forms' ),
'trashExpiredSubsButtonSecondary' => __( 'Cancel', 'ninja-forms' ),
),
'allow_telemetry' => $allow_tel,
));
/**
* Duplicating the localization above with an nfAdmin variable for consistency.
*
* Eventually, we should remove all references to nf_settings, which isn't very descriptive or specific with nfAdmin instead.
*
* TODO: Replace references to nf_settings object in admin JS files with nfAdmin.
*/
wp_localize_script( 'ninja_forms_admin_menu_settings', 'nfAdmin', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'forms' => $form_options,
'nf_nuke_title' => __( 'Remove ALL Ninja Forms data and uninstall?', 'ninja-forms' ),
'nonce' => wp_create_nonce( "ninja_forms_settings_nonce" ),
'batchNonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
'i18n' => array(
'downgradeMessage' => __( 'Are you sure you want to downgrade?', 'ninja-forms' ),
'downgradeWarningMessage' => __( 'You WILL lose any forms or submissions created on this version of Ninja Forms.', 'ninja-forms' ),
......
......@@ -5,15 +5,15 @@
*/
class NF_Admin_Processes_ImportForm extends NF_Abstracts_BatchProcess
{
private $response = array(
protected $response = array(
'batch_complete' => false
);
private $_slug = 'import_form';
protected $_slug = 'import_form';
private $fields_per_step = 20;
private $form;
protected $form;
/**
* Store an array of columns that we want to store in our table rather than meta.
......@@ -36,7 +36,7 @@ class NF_Admin_Processes_ImportForm extends NF_Abstracts_BatchProcess
'seq_num' => 'seq_num',
);
private $fields_db_columns = array(
protected $fields_db_columns = array(
'parent_id' => 'parent_id',
'id' => 'id',
'key' => 'key',
......@@ -51,7 +51,7 @@ class NF_Admin_Processes_ImportForm extends NF_Abstracts_BatchProcess
'personally_identifiable' => 'personally_identifiable',
);
private $actions_db_columns = array(
protected $actions_db_columns = array(
'title' => 'title',
'key' =>'key',
'type' =>'type',
......@@ -136,6 +136,7 @@ class NF_Admin_Processes_ImportForm extends NF_Abstracts_BatchProcess
* Add them to our "Values" string for insertion later.
*/
$insert_values = '';
foreach( $this->form[ 'settings' ] as $meta_key => $meta_value ) {
$meta_value = maybe_serialize( $meta_value );
$wpdb->escape_by_ref( $meta_value );
......@@ -334,8 +335,7 @@ class NF_Admin_Processes_ImportForm extends NF_Abstracts_BatchProcess
update_option( 'nf_import_form', $this->form, false );
}
echo wp_json_encode( $this->response );
wp_die();
$this->respond();
}
/**
......@@ -345,9 +345,16 @@ class NF_Admin_Processes_ImportForm extends NF_Abstracts_BatchProcess
{
global $wpdb;
// If we aren't passed any form content, bail.
if ( empty ( $_POST[ 'extraData' ][ 'content' ] ) ) {
$this->cleanup();
$this->respond();
}
$data = explode( ';base64,', $_POST[ 'extraData' ][ 'content' ] );
$data = base64_decode( $data[ 1 ] );
$data = json_decode( $data, true );
// $data = WPN_Helper::utf8_decode( json_decode( WPN_Helper::json_cleanup( html_entity_decode( $data ) ), true ) );
$data = json_decode( WPN_Helper::json_cleanup( html_entity_decode( $data ) ), true );
// $data is now a form array.
$this->form = $data;
......@@ -392,6 +399,8 @@ class NF_Admin_Processes_ImportForm extends NF_Abstracts_BatchProcess
// Tell our JS that we're done.
$this->response[ 'batch_complete' ] = true;
// Return our new Form ID
$this->response[ 'form_id' ] = $this->form[ 'ID' ];
}
/*
......
<?php if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Class NF_Abstracts_Batch_Process
*/
class NF_Admin_Processes_ImportFormTemplate extends NF_Admin_Processes_ImportForm
{
protected $_slug = 'import_form_template';
/**
* Function to run any setup steps necessary to begin processing.
*/
public function startup()
{
global $wpdb;
// If we haven't been passed a template as extraData, then bail.
if ( ! isset ( $_POST[ 'extraData' ][ 'template' ] ) || empty ( $_POST[ 'extraData' ][ 'template' ] ) ) {
$this->cleanup();
}
$template_file_name = $_POST[ 'extraData' ][ 'template' ];
// Grab the data from the appropriate file location.
$registered_templates = Ninja_Forms::config( 'NewFormTemplates' );
if( isset( $registered_templates[ $template_file_name ] ) && ! empty( $registered_templates[ $template_file_name ][ 'form' ] ) ) {
$form_data = $registered_templates[ $template_file_name ][ 'form' ];
} else {
$form_data = Ninja_Forms::template( $template_file_name . '.nff', array(), TRUE );
}
/**
* If we don't have any form data, run cleanup.
*
* TODO: We probably need to show an error to the user here.
*/
if( ! $form_data ) {
$this->cleanup();
}
$this->form = json_decode( html_entity_decode( $form_data ), true );
// Determine how many steps this will take.
$this->response[ 'step_total' ] = $this->get_steps();
/**
* 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.
*
* Also, set our db stage 1 tracker to false.
*/
if ( empty ( $results ) ) {
unset( $this->actions_db_columns[ 'label' ] );
$db_stage_one_complete = false;
} else {
// Add a form value that stores whether or not we have our new DB columns.
$db_stage_one_complete = true;
}
$this->form[ 'stage_one_complete' ] = $db_stage_one_complete;
add_option( 'nf_doing_' . $this->_slug, 'true', false );
}
}
\ No newline at end of file
<div class="wrap">
<button id="nf-import-form" class="button-primary">Import</button>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="nf_import_security" id="nf_import_security"
value="<?php echo wp_create_nonce( 'ninja_forms_import_form_nonce' );?>" />
<table class="form-table">
<tbody>
<table class="form-table">
<tbody>
<tr id="row-nf-import-response" style="display:none;background-color:#ffc;">
<th></th>
<td><?php printf( __( 'Form Imported Successfully. %sView Form%s', 'ninja-forms' ), '<a id="nf-import-url" href="' . admin_url( 'admin.php?page=ninja-forms&form_id=' ) . '">', '</a>' ); ?></td>
</tr>
<tr id="row_nf_import_form">
<th scope="row">
<label for="nf_import_form"><?php echo __( 'Select a file', 'ninja-forms' ); ?></label>
<label for="nf-import-file"><?php echo __( 'Select a file', 'ninja-forms' ); ?></label>
</th>
<td>
<input type="file" name="nf_import_form" id="nf_import_form" class="widefat">
<input type="file" id="nf-import-file" class="widefat">
</td>
</tr>
<tr id="row_nf_import_form_encoding">
<th scope="row">
<label for="nf_import_form_turn_off_encoding"><?php _e( 'Disable UTF-8 Encoding', 'ninja-forms' ); ?></label>
</th>
<td>
<input type="checkbox" name="nf_import_form_turn_off_encoding"
id="nf_import_form_turn_off_encoding">
<label style="color:red;font-style: italic;"
for="nf_import_form_turn_off_encoding">
If you are having trouble importing forms, please
click here to disable UTF-8 encoding
and try again.
</label>
</td>
<tr id="row-nf-import-type-error" style="display:none;color:red;">
<th></th>
<td><?php printf( __( 'Please select a Ninja Forms export. %sMust be in .nff format%s.', 'ninja-forms' ), '<strong>', '</strong>' ); ?></td>
</tr>
<tr id="row_nf_import_form_submit">
<th scope="row">
<label for="nf_import_form_submit"><?php _e( 'Import Form', 'ninja-forms' ); ?></label>
<label for="nf-import-form-submit"><?php _e( 'Import Form', 'ninja-forms' ); ?></label>
</th>
<td>
<input type="submit" id="nf_import_form_submit" class="button-secondary" value="<?php echo __( 'Import Form', 'ninja-forms' ) ;?>">
<input type="button" id="nf-import-form-submit" class="button-secondary" value="<?php echo __( 'Import Form', 'ninja-forms' ) ;?>">
</td>
</tr>
</tbody>
</table>
</form>
</tbody>
</table>
</div>
\ No newline at end of file
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