Skip to content
Snippets Groups Projects
Commit fa5f77ff authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Implement modifying counters via AJAX

parent c6ebe4b4
No related branches found
No related tags found
No related merge requests found
...@@ -14,8 +14,8 @@ var getCounterData = function (btn) { ...@@ -14,8 +14,8 @@ var getCounterData = function (btn) {
} }
var handleJSONResponse = function (json, counter) { var handleJSONResponse = function (json, counter) {
// updateMessages (json['messages'], "ordernumber"); // updateMessages (json['messages'], "ordernumber");
if (!json.authorized) { if (!json.authorized && !json.success) {
alert (ajax_ordernumber.PLG_ORDERNUMBER_JS_NOT_AUTHORIZED); alert (ajax_ordernumber.ORDERNUMBER_JS_NOT_AUTHORIZED);
} else if (json.error) { } else if (json.error) {
alert (json.error); alert (json.error);
} else { } else {
...@@ -23,114 +23,116 @@ var handleJSONResponse = function (json, counter) { ...@@ -23,114 +23,116 @@ var handleJSONResponse = function (json, counter) {
} }
} }
var ajaxEditCounter = function (btn, nrtype, ctr, value) { var ajaxEditCounter = function (btn, nrtype, ctr, value) {
var counter = getCounterData (btn); var counter = getCounterData (btn);
counter.type = nrtype; counter.type = nrtype;
counter.counter = ctr; counter.counter = ctr;
counter.value = value; counter.value = value;
var value = NaN; var value = NaN;
var msgprefix = ""; var msgprefix = "";
while (isNaN (value) && (value != null)) { while (isNaN (value) && (value != null)) {
value = prompt (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_EDITCOUNTER, msgprefix, counter.counter, counter.value), counter.value); value = prompt (String.Format (ajax_ordernumber.ORDERNUMBER_JS_EDITCOUNTER, msgprefix, counter.counter, counter.value)+ ajax_ordernumber.ajax_url, counter.value);
if (value != null) if (value != null)
value = parseInt (value); value = parseInt (value);
if (isNaN (value)) if (isNaN (value))
msgprefix = ajax_ordernumber.PLG_ORDERNUMBER_JS_INVALID_COUNTERVALUE; msgprefix = ajax_ordernumber.ORDERNUMBER_JS_INVALID_COUNTERVALUE;
} }
if (value != null) { if (value != null) {
var loading = jQuery ("img.wc-ordernumber-loading").first ().clone ().insertAfter (btn).show (); var loading = jQuery ("img.ordernumber-loading").first ().clone ().insertAfter (btn).show ();
jQuery.post ( jQuery.ajax ({
ajax_ordernumber.admin_url, type: "POST",
{ url: ajax_ordernumber.ajax_url,
data: {
action: 'set_counter', action: 'set_counter',
nrtype: counter.type, nrtype: counter.type,
counter: counter.counter, counter: counter.counter,
value: value value: value,
}, },
success: function ( data ) { success: function ( json ) {
try { try {
var json = jQuery.parseJSON (data); handleJSONResponse (json, counter);
handleJSONResponse (json, counter); } catch (e) {
} catch (e) { alert (ajax_ordernumber.ORDERNUMBER_JS_JSONERROR + "\n" + e);
alert (ajax_ordernumber.PLG_ORDERNUMBER_JS_JSONERROR + "\n" + e); return;
return; }
} if (json.success>0) {
if (json.success>0) { jQuery (counter.row).children (".counter_value").text (value);
jQuery (counter.row).children (".counter_value").text (value); } else {
} else { alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_MODIFY_FAILED, counter.counter));
alert (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_MODIFY_FAILED, counter.counter)); }
} },
}, error: function () { alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_MODIFY_FAILED, counter.counter)); },
error: function () { alert (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_MODIFY_FAILED, counter.counter)); }, complete: function () { jQuery (loading).remove (); },
complete: function () { jQuery (loading).remove (); }, });
}); }
}
} }
var ajaxDeleteCounter = function (btn, nrtype, ctr, value) { var ajaxDeleteCounter = function (btn, nrtype, ctr, value) {
var counter = getCounterData (btn); var counter = getCounterData (btn);
counter.type = nrtype; counter.type = nrtype;
counter.counter = ctr; counter.counter = ctr;
counter.value = value; counter.value = value;
var proceed = confirm (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_DELETECOUNTER, counter.counter, counter.value)); var proceed = confirm (String.Format (ajax_ordernumber.ORDERNUMBER_JS_DELETECOUNTER, counter.counter, counter.value));
if (proceed = = true) { if (proceed == true) {
var loading = jQuery ("img.wc-ordernumber-loading").first ().clone ().insertAfter (btn).show (); var loading = jQuery ("img.ordernumber-loading").first ().clone ().insertAfter (btn).show ();
jQuery.post ( jQuery.ajax ({
ajax_ordernumber.admin_url, type: "POST",
{ dataType: "json",
url: ajax_ordernumber.ajax_url,
data: {
action: 'delete_counter', action: 'delete_counter',
nrtype: counter.type, nrtype: counter.type,
counter: counter.counter counter: counter.counter,
}, },
success: function ( data ) { success: function ( json ) {
try { try {
var json = jQuery.parseJSON (data); handleJSONResponse (json, counter);
handleJSONResponse (json, counter); } catch (e) {
} catch (e) { alert (ajax_ordernumber.ORDERNUMBER_JS_JSONERROR + "\n" + e);
alert (ajax_ordernumber.PLG_ORDERNUMBER_JS_JSONERROR + "\n" + e); return;
return; }
} if (json.success>0) {
if (json.success>0) { jQuery (counter.row).fadeOut (1200, function () { jQuery (counter.row).remove (); });
jQuery (counter.row).fadeOut (1500, function () { jQuery (counter.row).remove (); }); } else {
} else { alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_DELETE_FAILED, counter.counter));
alert (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_DELETE_FAILED, counter.counter)); }
} },
}, error: function () { alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_DELETE_FAILED, counter.counter)); },
error: function () { alert (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_DELETE_FAILED, counter.counter)); }, complete: function () { jQuery (loading).remove (); },
complete: function () { jQuery (loading).remove (); }, });
}); }
}
} }
var ajaxAddCounter = function (btn, nrtype) { var ajaxAddCounter = function (btn, nrtype) {
var row = jQuery (btn).parents ("tr.addcounter_row"); var row = jQuery (btn).parents ("tr.addcounter_row");
var countername = prompt (ajax_ordernumber.PLG_ORDERNUMBER_JS_NEWCOUNTER); var countername = prompt (ajax_ordernumber.ORDERNUMBER_JS_NEWCOUNTER);
if (countername != null) { if (countername != null) {
var loading = jQuery ("img.wc-ordernumber-loading").first ().clone ().insertAfter (jQuery (btn).find ("img.ordernumber-counter-addbtn")).show (); var loading = jQuery ("img.ordernumber-loading").first ().clone ().insertAfter (jQuery (btn).find ("img.ordernumber-counter-addbtn")).show ();
jQuery.post ( jQuery.ajax ({
ajax_ordernumber.admin_url, type: "POST",
{ dataType: "json",
url: ajax_ordernumber.ajax_url,
data: {
action: 'add_counter', action: 'add_counter',
nrtype: nrtype, nrtype: nrtype,
counter: countername counter: countername,
value: 0,
}, },
success: function ( data ) {
var json = data ? jQuery.parseJSON (data) : null; success: function ( json ) {
try { try {
var json = jQuery.parseJSON (data); handleJSONResponse (json, null);
handleJSONResponse (json, null); } catch (e) {
} catch (e) { alert (ajax_ordernumber.ORDERNUMBER_JS_JSONERROR + "\n" + e);
alert (ajax_ordernumber.PLG_ORDERNUMBER_JS_JSONERROR + "\n" + e); return;
return; }
} if (json.success>0) {
if (json.success>0) { if (json.newrow) {
if (json.newrow) { jQuery (row).before (jQuery (json.newrow));
jQuery (row).before (jQuery (json.newrow)); }
} } else {
} else { alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_ADD_FAILED, countername));
alert (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_ADD_FAILED, countername)); }
} },
}, error: function () { alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_ADD_FAILED, countername)); },
error: function () { alert (String.Format (vPLG_ORDERNUMBER_JS_ADD_FAILED, countername)); }, complete: function () { jQuery (loading).remove (); },
complete: function () { jQuery (loading).remove (); }, });
}); }
}
} }
...@@ -119,11 +119,14 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g ...@@ -119,11 +119,14 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
array( array(
'name' => __( 'Current Counters', 'woocommerce-advanced-ordernumbers' ), 'name' => __( 'Current Counters', 'woocommerce-advanced-ordernumbers' ),
'desc' => __( 'View and modify the current counter values.', 'woocommerce-advanced-ordernumbers' ), // 'desc' => __( 'View and modify the current counter values.', 'woocommerce-advanced-ordernumbers' ),
'type' => 'title', 'type' => 'title',
'id' => 'ordernumber_counters' 'id' => 'ordernumber_counters'
), ),
array( array(
'name' => __( 'All Ordernumber Counters', 'woocommerce-advanced-ordernumbers' ),
'desc' => __( 'View and modify the current counter values. The counter value is the value used for the previous number. All changes are immediately applied!', 'woocommerce-advanced-ordernumbers' ),
'desc_tip' => true,
'id' => 'ordernumber_counters', 'id' => 'ordernumber_counters',
'type' => 'ordernumber_counters', 'type' => 'ordernumber_counters',
), ),
...@@ -253,9 +256,18 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g ...@@ -253,9 +256,18 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
* Render the Counter Values modification table * Render the Counter Values modification table
*/ */
public function admin_field_counters ($settings) { public function admin_field_counters ($settings) {
// if ( ! isset( $value['id'] ) ) {
// $value['id'] = '';
// }
// if ( ! isset( $value['title'] ) ) {
// $value['title'] = isset( $value['name'] ) ? $value['name'] : '';
// }
// Description handling
$field_description = WC_Admin_Settings::get_field_description( $settings );
extract( $field_description );
// First, get all counter names: // First, get all counter names:
// print "<pre>All options: ".print_r(wp_load_alloptions(),1)."</pre>";
$counters = array(); $counters = array();
$pfxlen = strlen($this->ordernumber_counter_prefix ); $pfxlen = strlen($this->ordernumber_counter_prefix );
foreach (wp_load_alloptions() as $name => $value) { foreach (wp_load_alloptions() as $name => $value) {
...@@ -272,10 +284,14 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g ...@@ -272,10 +284,14 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
} }
} }
?> ?>
<tr valign="top"> <tr valign="top">
<th scope="row" class="titledesc"><?php _e( 'Current Counters:', 'woocommerce' ) ?></th> <th scope="row" class="titledesc">
<td class="forminp"> <label for="<?php echo esc_attr( $settings['id'] ); ?>"><?php echo esc_html( $settings['title'] ); ?></label>
<?php echo $tooltip_html; ?>
</th>
<td class="forminp forminp-<?php echo sanitize_title( $settings['type'] ) ?>">
<img src='<?php echo plugins_url( 'assets/images/loading.gif', __FILE__ ); ?>' class='wc-ordernumber-loading' style="display: none; position: absolute; top: 2px; left: 0px; z-index: 9999;"/> <img src='<?php echo plugins_url( 'assets/images/loading.gif', __FILE__ ); ?>' class='wc-ordernumber-loading' style="display: none; position: absolute; top: 2px; left: 0px; z-index: 9999;"/>
<table class="wc_ordernumber_counters widefat" cellspacing="0"> <table class="wc_ordernumber_counters widefat" cellspacing="0">
<?php <?php
...@@ -305,32 +321,16 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g ...@@ -305,32 +321,16 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
<tbody> <tbody>
<?php <?php
foreach ($counters as $counter) {?> foreach ($counters as $counter) {
<tr class='counter_row counter_type_<?php echo $counter['type'];?>'> echo $this->create_admin_counter_row($counter['type'], $counter['name'], $counter['value']);
<td class='counter_type'><?php _e($counter['type'], 'wooocommerce-advanced-ordernumbers' ); ?></td> }
<td class='counter_format'><?php echo esc_html($counter['name']); ?></td> ?>
<td class='counter_value'><?php echo esc_html($counter['value']); ?></td>
<td class='counter_buttons'>
<div class='ordernumber-ajax-loading'>
<img src='<?php echo plugins_url( 'assets/images/icon-16-edit.png', __FILE__ ); ?>'
class='ordernumber-counter-editbtn ordernumber-btn'
onClick='ajaxEditCounter(this, <?php echo json_encode($counter['type']); ?>, <?php echo json_encode($counter['name']); ?>, <?php echo $counter['value']; ?>)' />
</div>
<div class='ordernumber-ajax-loading'>
<img src='<?php echo plugins_url( 'assets/images/icon-16-delete.png', __FILE__ ); ?>'
class='ordernumber-counter-deletebtn ordernumber-btn'
onClick='ajaxDeleteCounter(this, <?php echo json_encode($counter['type']); ?>, <?php echo json_encode($counter['name']); ?>, <?php echo $counter['value']; ?>)' />
</div>
</td>
</tr>
<?php
} ?>
</tbody> </tbody>
<tfoot> <tfoot>
<tr class='addcounter_row'> <tr class='addcounter_row'>
<td class="counter_type"></td> <td class="counter_type"></td>
<td colspan=3 class='counter_add'> <td colspan=3 class='counter_add'>
<div class='ordernumber-counter-addbtn ordernumber-btn' onClick='ajaxAddCounter(this, "ordernumber")'> <div class='ordernumber-counter-addbtn ordernumber-btn' onClick="ajaxAddCounter(this, 'ordernumber')">
<div class='ordernumber-ajax-loading'><img src='<?php echo plugins_url( 'assets/images/icon-16-new.png', __FILE__ ); ?>' class='ordernumber-counter-addbtn' /></div> <div class='ordernumber-ajax-loading'><img src='<?php echo plugins_url( 'assets/images/icon-16-new.png', __FILE__ ); ?>' class='ordernumber-counter-addbtn' /></div>
<?php _e('Add new counter', 'woocommerce-advanced-ordernumbers'); ?> <?php _e('Add new counter', 'woocommerce-advanced-ordernumbers'); ?>
</div> </div>
...@@ -343,6 +343,27 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g ...@@ -343,6 +343,27 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
<?php <?php
} }
public function create_admin_counter_row ($type, $format, $value=0) {
$html = "
<tr class='counter_row counter_type_" . $type . "'>
<td class='counter_type'>" . __($type, 'wooocommerce-advanced-ordernumbers' ) . "</td>
<td class='counter_format'>" . (empty($format)?("<i>".__("[Global]", 'woocommerce-advanced-ordernumbers')."</i>"):esc_html($format)) . "</td>
<td class='counter_value'>" . esc_html($value) . "</td>
<td class='counter_buttons'>
<div class='ordernumber-ajax-loading'>
<img src='" . plugins_url( 'assets/images/icon-16-edit.png', __FILE__ ) . "'
class='ordernumber-counter-editbtn ordernumber-btn'
onClick='ajaxEditCounter(this, " . json_encode($type) . ", " . json_encode($format) . ", " . json_encode($value) . ")' />
</div>
<div class='ordernumber-ajax-loading'>
<img src='" . plugins_url( 'assets/images/icon-16-delete.png', __FILE__ ) . "'
class='ordernumber-counter-deletebtn ordernumber-btn'
onClick='ajaxDeleteCounter(this, " . json_encode($type) . ", " . json_encode($format) . ", " . json_encode($value) . ")' />
</div>
</td>
</tr>";
return $html;
}
/** /**
...@@ -386,7 +407,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g ...@@ -386,7 +407,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
/** /**
* Counter handling (simple loading/storing counters), storing them as options * Counter handling (simple loading/storing counters), storing them as options
*/ */
function _getCounter($type, $format, $start=1) { function _getCounter($type, $format, $start=0) {
$count = get_option ($this->ordernumber_counter_prefix.$type.'-'.$format, $start); $count = get_option ($this->ordernumber_counter_prefix.$type.'-'.$format, $start);
return $count; return $count;
} }
...@@ -394,7 +415,41 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g ...@@ -394,7 +415,41 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
function _setCounter($type, $format, $value) { function _setCounter($type, $format, $value) {
return update_option($this->ordernumber_counter_prefix.$type.'-'.$format, $value); return update_option($this->ordernumber_counter_prefix.$type.'-'.$format, $value);
} }
function _deleteCounter($type, $format) {
return delete_option($this->ordernumber_counter_prefix.$type.'-'.$format);
}
public function counter_delete_callback() {
$json = array('action' => 'delete_counter', 'success' => 0);
$json['success'] = $this->_deleteCounter($_POST['nrtype'], $_POST['counter']);
// print("Counter was set to $nr, post variables: nrtpe=$_POST['nrtype'], counter=$_POST['counter'], value=$_POST['value']\n\n");
$json["message"] = "Counter was deleted, post variables: nrtpe=".$_POST['nrtype'].", counter=".$_POST['counter']."\n\n";
wp_send_json($json);
}
public function counter_add_callback () {
$type = $_POST['nrtype'];
$format = $_POST['counter'];
// $value = $_POST['value'];
$value = "0";
$json = array('action' => 'add_counter', 'success' => 0);
if ($this->_getCounter($type, $format, -1) != -1) {
// Counter already exists => error message
$json['error'] = sprintf(__('Counter "%s" already exists, cannot create again.'), $format);
} else {
$json['success'] = $this->_setCounter($type, $format, $value);
// $json['error'] = "Created counter $format with value $value";
$json['newrow'] = $this->create_admin_counter_row($type, $format, $value);
}
wp_send_json($json);
}
public function counter_set_callback () {
$json = array('action' => 'set_counter', 'success' => 0);
$json['success'] = $this->_setCounter($_POST['nrtype'], $_POST['counter'], $_POST['value']);
// print("Counter was set to $nr, post variables: nrtpe=".$_POST['nrtype'].", counter=".$_POST['counter'].", value=".$_POST['value']."\n\n");
wp_send_json($json);
}
/** /**
* Variable replacements: * Variable replacements:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment