Commit 41d40415 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer

First version of library factorization

parent ca082e16
table.wc_ordernumber_counters {
border: 1px solid #888888;
width: inherit;
}
table.wc_ordernumber_counters td, table.wc_ordernumber_counters th {
padding-bottom: 0px;
padding-top: 0px;
}
table.wc_ordernumber_counters thead th {
text-align: center;
width: auto;
}
td.counter_value {
text-align: center;
}
table.wc_ordernumber_counters thead > tr:nth-child(odd) > th,
table.wc_ordernumber_counters tfoot > tr.addcounter_row > td {
background: #E0E0E0;
}
table.wc_ordernumber_counters tbody > tr:nth-child(even) > td {
background: #F0F0F0;
}
.ordernumber-btn {
cursor: pointer;
}
col.counter_type, th.counter_type, td.counter_type {
display:none;
}
table.wc_ordernumber_counters img {
padding: 0;
margin: 0;
}
div.ordernumber-ajax-loading, div.ordernumber-counter-addbtn {
display: inline;
}
div.ordernumber-ajax-loading, div.ordernumber-ajax-loading img.ordernumber-btn {
position: relative;
top: 0; left: 0;
}
div.ordernumber-ajax-loading img {
z-index:0;
}
String.Format = function () {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++ ) {
var reg = new RegExp ("\\{" + i + "\\}", "gm");
s = s.replace (reg, arguments[i + 1]);
}
return s;
}
var getCounterData = function (btn) {
var row = jQuery (btn).parents ("tr.counter_row");
return { row: row };
}
var handleJSONResponse = function (json, counter) {
// updateMessages (json['messages'], "ordernumber");
if (!json.authorized && !json.success) {
alert (ajax_ordernumber.ORDERNUMBER_JS_NOT_AUTHORIZED);
} else if (json.error) {
alert (json.error);
} else {
// TODO: Which other error checks can we do?
}
}
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.ORDERNUMBER_JS_EDITCOUNTER, msgprefix, counter.counter, counter.value), 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,
},
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.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,
},
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.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,
value: 0,
},
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 (); },
});
}
}
var ordernumberVariablesAddRow = function (template, element) {
jQuery("#" + template + " tr").clone(true)
.appendTo("table#" + element + " tbody")
.find('input,select,button,img')
.removeAttr('disabled');
jQuery("tr#ordernumber-replacements-empty-row")
.addClass("rowhidden")
.find('input')
.attr('disabled', 'disabled');
}
jQuery(document).ready (function () {
jQuery('img.ordernumber-replacement-deletebtn').click(
function () {
jQuery(this).closest('tr').remove();
var count = jQuery(this).closest('table').find('tbody tr').length;
if (count==0) {
jQuery("tr#ordernumber-replacements-empty-row")
.removeClass("rowhidden")
.find('input,select,button,img')
.removeAttr('disabled');
}
}
);
});
table.ordernumber-countertable {
border: 1px solid #888888;
display: inline-table;
width: inherit;
}
table.ordernumber-countertable td, table.ordernumber-countertable th {
padding-bottom: 0px;
padding-top: 0px;
}
table.ordernumber-countertable thead th {
text-align: center;
width: auto;
}
table.ordernumber-countertable thead > tr:nth-child(odd) > th,
table.ordernumber-countertable tfoot > tr.addcounter_row > td {
background: #E0E0E0;
}
table.ordernumber-countertable tbody > tr:nth-child(even) > td {
background: #F0F0F0;
}
table.ordernumber-countertable.table-striped tbody > tr:nth-child(odd) > th {
background: #E0E0E0;
}
.ordernumber-btn {
cursor: pointer;
}
col.counter_type, th.counter_type, td.counter_type {
display:none;
}
td.counter_value {
text-align: center;
}
fieldset table.ordernumber-countertable img {
padding: 0;
margin: 0;
}
div.ordernumber-ajax-loading, div.ordernumber-counter-addbtn {
display: inline;
}
div.ordernumber-ajax-loading, div.ordernumber-ajax-loading img.ordernumber-btn {
position: relative;
top: 0; left: 0;
}
div.ordernumber-ajax-loading img {
z-index:0;
}
img.ordernumber-loading {
display: none;
position: absolute;
top: 2px;
left: 0px;
z-index: 9999;
}
/* Counter custom variable replacements */
table.ordernumber_variables {
border: 1px solid #888888;
width: inherit;
......@@ -19,6 +86,9 @@ table.ordernumber_variables thead th {
td.counter_value {
text-align: center;
}
table.ordernumber_variables input {
background-color: rgba(255,255,255,0.75);
}
table.ordernumber_variables thead > tr:nth-child(odd) > th,
table.ordernumber_variables tfoot > tr.addreplacement_row > td {
......@@ -54,4 +124,8 @@ col.variables_thenvar, col.variables_thenval {
}
.variables_then, .variables_settings {
text-align: center;
width: 20px;
}
/**********************************************************************************
* The global ajax_ordernumber object should have the following entries:
* - Translations:
* ORDERNUMBER_JS_NOT_AUTHORIZED, ORDERNUMBER_JS_INVALID_COUNTERVALUE, ORDERNUMBER_JS_JSONERROR
* ORDERNUMBER_JS_NEWCOUNTER, ORDERNUMBER_JS_EDITCOUNTER, ORDERNUMBER_JS_DELETECOUNTER
* ORDERNUMBER_JS_ADD_FAILED, ORDERNUMBER_JS_MODIFY_FAILED, ORDERNUMBER_JS_DELETE_FAILED
* - ajax_url: The URL for all AJAX calls
* Optional entries (callback functions) are:
* - updateMessages(messages, cssidentifier)
* - parseAjaxResponse(response) => return json
* - modifyAjaxArgs(ajaxargs) => return ajaxargs with modified arguments for jquery.ajax calls
*/
/**********************************************************************************
*
* Javascript for the counter modification table
*
**********************************************************************************/
String.Format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
var getCounterData = function (btn) {
var row = jQuery(btn).closest("tr.counter_row");
return { row: row };
}
var handleJSONResponse = function (json, counter) {
if ('updateMessages' in ajax_ordernumber) {
ajax_ordernumber.updateMessages(json['messages'], "ordernumber");
}
if (!json.authorized && !json.success) {
alert(ajax_ordernumber.ORDERNUMBER_JS_NOT_AUTHORIZED);
} else if (json.error) {
alert(json.error);
} else {
// TODO: Which other error checks can we do?
}
}
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.ORDERNUMBER_JS_EDITCOUNTER, msgprefix, counter.counter, counter.value), 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();
var ajaxargs = {
type: "POST",
url: ajax_ordernumber.ajax_url,
data: {
action: 'setCounter',
nrtype: counter.type,
counter: counter.counter,
value: value
},
success: function ( json ) {
try {
if ('parseAjaxResponse' in ajax_ordernumber) {
json = ajax_ordernumber.parseAjaxResponse(json);
}
handleJSONResponse(json, counter);
} catch (e) {
alert(ajax_ordernumber.ORDERNUMBER_JS_JSONERROR+"\n"+e);
return;
}
if (json.success>0) {
// replace the whole row with the html returned by the AJAX call:
jQuery(counter.row).replaceWith(json.row);
// jQuery(counter.row).find(".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(); },
};
if ('modifyAjaxArgs' in ajax_ordernumber) {
ajaxargs = ajax_ordernumber.modifyAjaxArgs(ajaxargs);
}
jQuery.ajax(ajaxargs);
}
}
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.ORDERNUMBER_JS_DELETECOUNTER, counter.counter, counter.value));
if (proceed == true) {
var loading = jQuery("img.ordernumber-loading").first().clone().insertAfter(btn).show();
var ajaxargs = {
type: "POST",
dataType: "json",
url: ajax_ordernumber.ajax_url,
data: {
action: 'deleteCounter',
nrtype: counter.type,
counter: counter.counter
},
success: function ( json ) {
try {
if ('parseAjaxResponse' in ajax_ordernumber) {
json = ajax_ordernumber.parseAjaxResponse(json);
}
handleJSONResponse(json, counter);
} catch (e) {
alert(ajax_ordernumber.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.ORDERNUMBER_JS_DELETE_FAILED, counter.counter));
}
},
error: function() { alert (String.Format(ajax_ordernumber.ORDERNUMBER_JS_DELETE_FAILED, counter.counter)); },
complete: function() { jQuery(loading).remove(); },
};
if ('modifyAjaxArgs' in ajax_ordernumber) {
ajaxargs = ajax_ordernumber.modifyAjaxArgs(ajaxargs);
}
jQuery.ajax(ajaxargs);
}
}
var ajaxAddCounter = function (btn, nrtype) {
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();
var ajaxargs = {
type: "POST",
dataType: "json",
url: ajax_ordernumber.ajax_url,
data: {
action: "addCounter",
nrtype: nrtype,
counter: countername
},
success: function ( json ) {
try {
if ('parseAjaxResponse' in ajax_ordernumber) {
json = ajax_ordernumber.parseAjaxResponse(json);
}
handleJSONResponse(json, null);
} catch (e) {
alert(ajax_ordernumber.ORDERNUMBER_JS_JSONERROR+"\n"+e);
return;
}
if (json.success>0) {
if (json.row) {
jQuery(row).before(jQuery(json.row));
}
} 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(); },
};
// console.log("Ajaxargs", ajaxargs);
if ('modifyAjaxArgs' in ajax_ordernumber) {
ajaxargs = ajax_ordernumber.modifyAjaxArgs(ajaxargs);
}
jQuery.ajax(ajaxargs);
}
}
/**********************************************************************************
*
* Javascript for the Custom Variables table
*
**********************************************************************************/
var ordernumberVariablesAddRow = function (template, element) {
var cl = jQuery("#" + template + " tr").clone(true);
// Enable all form controls
jQuery(cl).find('input,select,button,img').removeAttr('disabled');
if (jQuery.fn.chosen) {
// select boxes handled by the chosen juery plugin cannot simply be cloned,
// instead we need to re-initialize chosen!
jQuery(cl).find('select').removeClass("chzn-done").removeAttr("id").css("display", "block").next().remove();
jQuery(cl).find('select').chosen({width: "50px"});
}
// Now insert this new row into the table
jQuery(cl).appendTo("table#" + element + " tbody");
jQuery("tr#ordernumber-replacements-empty-row")
.addClass("rowhidden")
.find('input')
.attr('disabled', 'disabled');
}
jQuery(document).ready (function () {
jQuery('img.ordernumber-replacement-deletebtn').click(
function () {
var count = jQuery(this).closest('table').find('tbody tr').length;
if (count<=1) {
jQuery("tr#ordernumber-replacements-empty-row")
.removeClass("rowhidden")
.find('input,select,button,img')
.removeAttr('disabled');
}
jQuery(this).closest('tr').remove();
}
);
jQuery("#ordernumber_variables tbody").sortable();
});
This diff is collapsed.
<?php
/**
* Advanced Ordernumbers generic helper class for WooCommerce
* Reinhold Kainhofer, Open Tools, office@open-tools.net
* @copyright (C) 2012-2015 - Reinhold Kainhofer
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
**/
if ( !defined( 'ABSPATH' ) ) {
die( 'Direct Access to ' . basename( __FILE__ ) . ' is not allowed.' );
}
if (!class_exists( 'OrdernumberHelper' ))
require_once (dirname(__FILE__) . '/library/ordernumber_helper.php');
class OrdernumberHelperWooCommerce extends OrdernumberHelper {
public static $ordernumber_counter_prefix = '_ordernumber_counter_';
function __construct() {
parent::__construct();
load_plugin_textdomain('opentools-ordernumbers', false, basename( dirname( __FILE__ ) ) . '/languages' );
// WC-specific Defaults for the HTML tables
$this->_styles['counter-table-class'] = "widefat";
$this->_styles['variable-table-class'] = "widefat wc_input_table sortable";
}
static function getHelper() {
static $helper = null;
if (!$helper) {
$helper = new OrdernumberHelperWooCommerce();
}
return $helper;
}
/**
* HELPER FUNCTIONS, WooCommerce-specific
*/
public function __($string) {
$string = $this->readableString($string);
return __($string, 'opentools-advanced-ordernumbers');
}
function urlPath($type, $file) {
return plugins_url('library/' . $type . '/' . $file, __FILE__);
}
public function print_admin_styles() {
wp_register_style('ordernumber-styles', $this->urlPath('css', 'ordernumber.css'));
wp_enqueue_style('ordernumber-styles');
}
public function print_admin_scripts() {
wp_register_script( 'ordernumber-script', $this->urlPath('js', 'ordernumber.js', __FILE__), array('jquery') );
wp_enqueue_script( 'ordernumber-script');
// Handle the translations:
$localizations = array( 'ajax_url' => admin_url( 'admin-ajax.php' ) );
$localizations['ORDERNUMBER_JS_JSONERROR'] = $this->__("Error reading response from server:");
$localizations['ORDERNUMBER_JS_NOT_AUTHORIZED'] = $this->__("You are not authorized to modify order number counters.");
$localizations['ORDERNUMBER_JS_NEWCOUNTER'] = $this->__("Please enter the format/name of the new counter:");
$localizations['ORDERNUMBER_JS_ADD_FAILED'] = $this->__("Failed adding counter {0}");
$localizations['ORDERNUMBER_JS_INVALID_COUNTERVALUE'] = $this->__("You entered an invalid value for the counter.\n\n");
$localizations['ORDERNUMBER_JS_EDITCOUNTER'] = $this->__("{0}Please enter the new value for the counter '{1}' (current value: {2}):");
$localizations['ORDERNUMBER_JS_MODIFY_FAILED'] = $this->__("Failed modifying counter {0}");
$localizations['ORDERNUMBER_JS_DELETECOUNTER'] = $this->__("Really delete counter '{0}' with value '{1}'?");
$localizations['ORDERNUMBER_JS_DELETE_FAILED'] = $this->__("Failed deleting counter {0}");
// in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
wp_localize_script( 'ordernumber-script', 'ajax_ordernumber', $localizations );
}
function getAllCounters($type) {
$counters = array();
$pfxlen = strlen(self::$ordernumber_counter_prefix );
foreach (wp_load_alloptions() as $name => $value) {
if (substr($name, 0, $pfxlen) == self::$ordernumber_counter_prefix) {
$parts = explode('-', substr($name, $pfxlen), 2);
if (sizeof($parts)==1) {
array_unshift($parts, 'ordernumber');
}
// print("<h1>$type Counter: $name with value $value encountered, prefix=".self::$ordernumber_counter_prefix.", parts=".print_r($parts,1)."</h1>");
if ($parts[0]==$type) {