Commit fa5f77ff authored by Reinhold Kainhofer's avatar Reinhold Kainhofer

Implement modifying counters via AJAX

parent c6ebe4b4
......@@ -14,8 +14,8 @@ var getCounterData = function (btn) {
}
var handleJSONResponse = function (json, counter) {
// updateMessages (json['messages'], "ordernumber");
if (!json.authorized) {
alert (ajax_ordernumber.PLG_ORDERNUMBER_JS_NOT_AUTHORIZED);
if (!json.authorized && !json.success) {
alert (ajax_ordernumber.ORDERNUMBER_JS_NOT_AUTHORIZED);
} else if (json.error) {
alert (json.error);
} else {
......@@ -23,114 +23,116 @@ var handleJSONResponse = function (json, counter) {
}
}
var ajaxEditCounter = function (btn, nrtype, ctr, value) {
var counter = getCounterData (btn);
counter.type = nrtype;
counter.counter = ctr;
counter.value = value;
var value = NaN;
var msgprefix = "";
while (isNaN (value) && (value != null)) {
value = prompt (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_EDITCOUNTER, msgprefix, counter.counter, counter.value), counter.value);
if (value != null)
value = parseInt (value);
if (isNaN (value))
msgprefix = ajax_ordernumber.PLG_ORDERNUMBER_JS_INVALID_COUNTERVALUE;
}
if (value != null) {
var loading = jQuery ("img.wc-ordernumber-loading").first ().clone ().insertAfter (btn).show ();
jQuery.post (
ajax_ordernumber.admin_url,
{
var counter = getCounterData (btn);
counter.type = nrtype;
counter.counter = ctr;
counter.value = value;
var value = NaN;
var msgprefix = "";
while (isNaN (value) && (value != null)) {
value = prompt (String.Format (ajax_ordernumber.ORDERNUMBER_JS_EDITCOUNTER, msgprefix, counter.counter, counter.value)+ ajax_ordernumber.ajax_url, counter.value);
if (value != null)
value = parseInt (value);
if (isNaN (value))
msgprefix = ajax_ordernumber.ORDERNUMBER_JS_INVALID_COUNTERVALUE;
}
if (value != null) {
var loading = jQuery ("img.ordernumber-loading").first ().clone ().insertAfter (btn).show ();
jQuery.ajax ({
type: "POST",
url: ajax_ordernumber.ajax_url,
data: {
action: 'set_counter',
nrtype: counter.type,
counter: counter.counter,
value: value
value: value,
},
success: function ( data ) {
try {
var json = jQuery.parseJSON (data);
handleJSONResponse (json, counter);
} catch (e) {
alert (ajax_ordernumber.PLG_ORDERNUMBER_JS_JSONERROR + "\n" + e);
return;
}
if (json.success>0) {
jQuery (counter.row).children (".counter_value").text (value);
} else {
alert (String.Format (ajax_ordernumber.PLG_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 (); },
});
}
success: function ( json ) {
try {
handleJSONResponse (json, counter);
} catch (e) {
alert (ajax_ordernumber.ORDERNUMBER_JS_JSONERROR + "\n" + e);
return;
}
if (json.success>0) {
jQuery (counter.row).children (".counter_value").text (value);
} else {
alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_MODIFY_FAILED, counter.counter));
}
},
error: function () { alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_MODIFY_FAILED, counter.counter)); },
complete: function () { jQuery (loading).remove (); },
});
}
}
var ajaxDeleteCounter = function (btn, nrtype, ctr, value) {
var counter = getCounterData (btn);
counter.type = nrtype;
counter.counter = ctr;
counter.value = value;
var proceed = confirm (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_DELETECOUNTER, counter.counter, counter.value));
if (proceed = = true) {
var loading = jQuery ("img.wc-ordernumber-loading").first ().clone ().insertAfter (btn).show ();
jQuery.post (
ajax_ordernumber.admin_url,
{
var counter = getCounterData (btn);
counter.type = nrtype;
counter.counter = ctr;
counter.value = value;
var proceed = confirm (String.Format (ajax_ordernumber.ORDERNUMBER_JS_DELETECOUNTER, counter.counter, counter.value));
if (proceed == true) {
var loading = jQuery ("img.ordernumber-loading").first ().clone ().insertAfter (btn).show ();
jQuery.ajax ({
type: "POST",
dataType: "json",
url: ajax_ordernumber.ajax_url,
data: {
action: 'delete_counter',
nrtype: counter.type,
counter: counter.counter
counter: counter.counter,
},
success: function ( data ) {
try {
var json = jQuery.parseJSON (data);
handleJSONResponse (json, counter);
} catch (e) {
alert (ajax_ordernumber.PLG_ORDERNUMBER_JS_JSONERROR + "\n" + e);
return;
}
if (json.success>0) {
jQuery (counter.row).fadeOut (1500, function () { jQuery (counter.row).remove (); });
} else {
alert (String.Format (ajax_ordernumber.PLG_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 (); },
});
}
success: function ( json ) {
try {
handleJSONResponse (json, counter);
} catch (e) {
alert (ajax_ordernumber.ORDERNUMBER_JS_JSONERROR + "\n" + e);
return;
}
if (json.success>0) {
jQuery (counter.row).fadeOut (1200, function () { jQuery (counter.row).remove (); });
} else {
alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_DELETE_FAILED, counter.counter));
}
},
error: function () { alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_DELETE_FAILED, counter.counter)); },
complete: function () { jQuery (loading).remove (); },
});
}
}
var ajaxAddCounter = function (btn, nrtype) {
var row = jQuery (btn).parents ("tr.addcounter_row");
var countername = prompt (ajax_ordernumber.PLG_ORDERNUMBER_JS_NEWCOUNTER);
if (countername != null) {
var loading = jQuery ("img.wc-ordernumber-loading").first ().clone ().insertAfter (jQuery (btn).find ("img.ordernumber-counter-addbtn")).show ();
jQuery.post (
ajax_ordernumber.admin_url,
{
var row = jQuery (btn).parents ("tr.addcounter_row");
var countername = prompt (ajax_ordernumber.ORDERNUMBER_JS_NEWCOUNTER);
if (countername != null) {
var loading = jQuery ("img.ordernumber-loading").first ().clone ().insertAfter (jQuery (btn).find ("img.ordernumber-counter-addbtn")).show ();
jQuery.ajax ({
type: "POST",
dataType: "json",
url: ajax_ordernumber.ajax_url,
data: {
action: 'add_counter',
nrtype: nrtype,
counter: countername
counter: countername,
value: 0,
},
success: function ( data ) {
var json = data ? jQuery.parseJSON (data) : null;
try {
var json = jQuery.parseJSON (data);
handleJSONResponse (json, null);
} catch (e) {
alert (ajax_ordernumber.PLG_ORDERNUMBER_JS_JSONERROR + "\n" + e);
return;
}
if (json.success>0) {
if (json.newrow) {
jQuery (row).before (jQuery (json.newrow));
}
} else {
alert (String.Format (ajax_ordernumber.PLG_ORDERNUMBER_JS_ADD_FAILED, countername));
}
},
error: function () { alert (String.Format (vPLG_ORDERNUMBER_JS_ADD_FAILED, countername)); },
complete: function () { jQuery (loading).remove (); },
});
}
success: function ( json ) {
try {
handleJSONResponse (json, null);
} catch (e) {
alert (ajax_ordernumber.ORDERNUMBER_JS_JSONERROR + "\n" + e);
return;
}
if (json.success>0) {
if (json.newrow) {
jQuery (row).before (jQuery (json.newrow));
}
} else {
alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_ADD_FAILED, countername));
}
},
error: function () { alert (String.Format (ajax_ordernumber.ORDERNUMBER_JS_ADD_FAILED, countername)); },
complete: function () { jQuery (loading).remove (); },
});
}
}
......@@ -119,11 +119,14 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
array(
'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',
'id' => 'ordernumber_counters'
),
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',
'type' => 'ordernumber_counters',
),
......@@ -253,9 +256,18 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
* Render the Counter Values modification table
*/
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:
// print "<pre>All options: ".print_r(wp_load_alloptions(),1)."</pre>";
$counters = array();
$pfxlen = strlen($this->ordernumber_counter_prefix );
foreach (wp_load_alloptions() as $name => $value) {
......@@ -272,10 +284,14 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
}
}
?>
<tr valign="top">
<th scope="row" class="titledesc"><?php _e( 'Current Counters:', 'woocommerce' ) ?></th>
<td class="forminp">
<th scope="row" class="titledesc">
<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;"/>
<table class="wc_ordernumber_counters widefat" cellspacing="0">
<?php
......@@ -305,32 +321,16 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
<tbody>
<?php
foreach ($counters as $counter) {?>
<tr class='counter_row counter_type_<?php echo $counter['type'];?>'>
<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
} ?>
foreach ($counters as $counter) {
echo $this->create_admin_counter_row($counter['type'], $counter['name'], $counter['value']);
}
?>
</tbody>
<tfoot>
<tr class='addcounter_row'>
<td class="counter_type"></td>
<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>
<?php _e('Add new counter', 'woocommerce-advanced-ordernumbers'); ?>
</div>
......@@ -343,6 +343,27 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
<?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
/**
* 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);
return $count;
}
......@@ -394,7 +415,41 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
function _setCounter($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:
......
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