Commit 8ecb25c8 authored by Eric Windham's avatar Eric Windham

changes for form delete popup to allow user to export form and/or submissions...

changes for form delete popup to allow user to export form and/or submissions and delete submissions for that form
parent 01acc288
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
File mode changed from 100755 to 100644
File mode changed from 100644 to 100755
......@@ -19,7 +19,7 @@ define( ['models/formModel'], function( FormModel ) {
this.listenTo( nfRadio.channel( 'dashboard' ), 'forms:delete', this.modalConfirm );
this.listenTo( nfRadio.channel( 'dashboard' ), 'forms:duplicate', this.duplicate );
this.modal = new jBox( 'Modal', {
width: 300,
width: 400,
addClass: 'dashboard-modal',
overlay: true,
closeOnClick: 'body'
......@@ -32,28 +32,79 @@ define( ['models/formModel'], function( FormModel ) {
modalConfirm: function( view ){
var message, container, messageBox, title, buttons, confirm, cancel, lineBreak;
var formID = view.model.get( 'id' );
var formTitle = view.model.get( 'title' );
container = document.createElement( 'div' );
container.style.paddingRight = '20px';
container.style.paddingLeft = '20px';
container.style.paddingBottom = '20px';
messageBox = document.createElement( 'p' );
title = document.createElement( 'em' );
buttons = document.createElement( 'div' );
confirm = document.createElement( 'div' );
cancel = document.createElement( 'div' );
lineBreak = document.createElement( 'br' );
container.classList.add( 'message' );
title.innerHTML = view.model.get( 'title' );
messageBox.innerHTML += 'Once deleted, a Form cannot be recovered.';
messageBox.appendChild( lineBreak );
messageBox.innerHTML += 'Are you sure you want to delete ';
messageBox.appendChild( title );
messageBox.innerHTML += '?';
title.innerHTML = formTitle;
messageBox.innerHTML += 'Once deleted, this form (<strong>'
+ formTitle + '</strong>), it\'s fields, and submissions' +
' cannot be recovered. Proceed with caution.';
' recovered. This includes submission data for that form.';
messageBox.appendChild( document.createElement( 'br') );
messageBox.appendChild( document.createElement( 'br') );
var exportFormLink = document.createElement( 'a' );
// link to export page with this form selected
exportFormLink.href = '/wp-admin/admin.php?page=nf-import-export&exportFormId='
+ formID;
exportFormLink.innerHTML = '<i class="fa fa-download"' +
' style="padding:5px;"></i>Export' +
' Form';
exportFormLink.target = '_blank'; // open in new tab
messageBox.appendChild( exportFormLink );
messageBox.appendChild( document.createElement( 'br') );
var exportSubmissionLink = document.createElement( 'a' );
// link to export submissions page
exportSubmissionLink.href = '/wp-admin/admin.php?page=nf-processing&action=download_all_subs&form_id='
+ formID + '&redirect=' + encodeURIComponent('/wp-admin/edit.php?post_status=all&post_type=nf_sub&form_id='
+ formID );
exportSubmissionLink.target = '_blank';
exportSubmissionLink.innerHTML = '<i class="fa fa-download" ' +
'style="padding:5px;"></i>Export Submissions';
messageBox.appendChild( exportSubmissionLink );
messageBox.appendChild( document.createElement( 'br') );
container.appendChild( messageBox );
var inputLabel = document.createElement( 'label' );
inputLabel.for = 'confirmDeleteFormInput';
inputLabel.innerHTML = 'Type <span style="color:red;">DELETE</span>' +
' to confirm';
var deleteInput = document.createElement( 'input' );
deleteInput.type = 'text';
deleteInput.id = 'confirmDeleteFormInput';
deleteInput.style.marginTop = '10px';
deleteInput.style.width = '100%';
deleteInput.style.height = '2.5em';
deleteInput.style.fontSize = '1em';
container.appendChild( inputLabel );
container.appendChild( document.createElement( 'br' ) );
container.appendChild( deleteInput );
container.appendChild( document.createElement( 'br' ) );
container.appendChild( document.createElement( 'br' ) );
confirm.innerHTML = 'Delete';
confirm.classList.add( 'confirm', 'nf-button', 'primary' );
confirm.classList.add( 'confirm', 'nf-button', 'primary', 'pull-right' );
cancel.innerHTML = 'Cancel';
cancel.classList.add( 'cancel', 'nf-button', 'secondary' );
cancel.style.float = 'right';
buttons.appendChild( confirm );
buttons.appendChild( cancel );
buttons.appendChild( confirm );
buttons.classList.add( 'buttons' );
container.appendChild( buttons );
message = document.createElement( 'div' );
......@@ -73,7 +124,13 @@ define( ['models/formModel'], function( FormModel ) {
var btnConfirm = this.modal.container[0].getElementsByClassName('confirm')[0];
btnConfirm.addEventListener('click', function() {
that.confirmDelete( view );
var deleteInputVal = document.getElementById( 'confirmDeleteFormInput' ).value;
if( 'DELETE' === deleteInputVal ) {
that.confirmDelete(view);
} else {
that.modalClose();
}
} );
},
......@@ -89,6 +146,7 @@ define( ['models/formModel'], function( FormModel ) {
'line-height': 0,
'display': 'none'
}, 500 );
console.log(view);
view.model.destroy();
this.modalClose();
},
......
......@@ -16,7 +16,7 @@ class NF_AJAX_Controllers_DeleteAllData extends NF_Abstracts_Controller
$post_result = 0;
$max_cnt = 250;
$form_id = $_POST[ 'form' ];
// sequel for getting 250 subs at a time
// SQL for getting 250 subs at a time
$sub_sql = "SELECT id FROM `" . $wpdb->prefix . "posts` AS p
LEFT JOIN `" . $wpdb->prefix . "postmeta` AS m ON p.id = m.post_id
WHERE p.post_type = 'nf_sub' AND m.meta_key = '_form_id'
......
......@@ -186,7 +186,26 @@ final class NF_Admin_Menus_ImportExport extends NF_Abstracts_Submenu
public function template_export_forms()
{
$forms = Ninja_Forms()->form()->get_forms();
/**
* we're gonna create a new array so that we can select a form in the
* export drop down based on a url parameter
**/
$formObjs = Ninja_Forms()->form()->get_forms();
$forms = array();
foreach( $formObjs as $form ) {
$selected = '';
if( isset( $_REQUEST[ 'exportFormId' ] )
&& $form->get_id() == $_REQUEST[ 'exportFormId' ] ) {
$selected = 'selected';
}
$forms[] = array(
'id' => $form->get_id(),
'title' => $form->get_setting( 'title' ),
'selected' => $selected,
);
}
Ninja_Forms::template( 'admin-metabox-import-export-forms-export.html.php', compact( 'forms' ) );
}
......
......@@ -58,9 +58,56 @@ final class NF_Database_Models_Form extends NF_Abstracts_Model
delete_option( $chunked_option_flag );
}
$this->delete_submissions();
delete_option( 'nf_form_' . $this->_id );
}
private function delete_submissions( ) {
global $wpdb;
$total_subs_deleted = 0;
$post_result = 0;
$max_cnt = 250;
// SQL for getting 250 subs at a time
$sub_sql = "SELECT id FROM `" . $wpdb->prefix . "posts` AS p
LEFT JOIN `" . $wpdb->prefix . "postmeta` AS m ON p.id = m.post_id
WHERE p.post_type = 'nf_sub' AND m.meta_key = '_form_id'
AND m.meta_value = %s LIMIT " . $max_cnt;
while ($post_result <= $max_cnt ) {
$subs = $wpdb->get_col( $wpdb->prepare( $sub_sql, $this->_id ),0 );
// if we are out of subs, then stop
if( 0 === count( $subs ) ) break;
// otherwise, let's delete the postmeta as well
$delete_meta_query = "DELETE FROM `" . $wpdb->prefix . "postmeta` WHERE post_id IN ( [IN] )";
$delete_meta_query = $this->prepare_in( $delete_meta_query, $subs );
$meta_result = $wpdb->query( $delete_meta_query );
if ( $meta_result > 0 ) {
// now we actually delete the posts(nf_sub)
$delete_post_query = "DELETE FROM `" . $wpdb->prefix . "posts` WHERE id IN ( [IN] )";
$delete_post_query = $this->prepare_in( $delete_post_query, $subs );
$post_result = $wpdb->query( $delete_post_query );
$total_subs_deleted = $total_subs_deleted + $post_result;
}
}
}
private function prepare_in( $sql, $vals ) {
global $wpdb;
$not_in_count = substr_count( $sql, '[IN]' );
if ( $not_in_count > 0 ) {
$args = array( str_replace( '[IN]', implode( ', ', array_fill( 0, count( $vals ), '%d' ) ), str_replace( '%', '%%', $sql ) ) );
// This will populate ALL the [IN]'s with the $vals, assuming you have more than one [IN] in the sql
for ( $i=0; $i < substr_count( $sql, '[IN]' ); $i++ ) {
$args = array_merge( $args, $vals );
}
$sql = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( $args ) );
}
return $sql;
}
public static function get_next_sub_seq( $form_id )
{
global $wpdb;
......
......@@ -11,11 +11,9 @@
<td style="width:30%;">
<select name="nf_export_form" id="nf_export_form" class="">
<?php foreach( $forms as $form ): ?>
<option value="<?php echo intval(
$form->get_id() ); ?>"><?php echo
$form->get_setting( 'title' ) . " ( ID: " . $form->get_id() . " )";
?>
</option>
<option value="<?php echo
$form['id']; ?>"<?php echo $form[ 'selected' ];?>><?php echo $form['title'] . " ( ID: " .
$form['id'] . " )"; ?></option>
<?php endforeach; ?>
</select>
</td>
......@@ -29,11 +27,12 @@
<ul class="widefat" id="nf_form_export_options">
<?php foreach( $forms as $form ): ?>
<li class="nf-form-option"
data-val="<?php echo intval( $form->get_id() ); ?>">
data-val="<?php echo intval( $form[ 'id' ] );
?>">
<span class="nf-form-option-item"><?php echo
$form->get_setting( 'title' )
$form[ 'title' ]
. " ( ID: "
. $form->get_id() . " )"; ?>
. $form[ 'id' ] . " )"; ?>
</span>
</li>
<?php endforeach; ?>
......
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