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

Version 1.6.2: Remove callback class and instead directly loop through all replacements

Restructure code to get away from the helper class, instead create explicit list of replacements
parent e9a0a8ca
No related branches found
No related tags found
No related merge requests found
BASE=ordernumber
PLUGINTYPE=vmshopper
VERSION=1.6.1
VERSION=1.6.2
PLUGINFILES=$(BASE).php $(BASE).script.php $(BASE).xml index.html
# TRANSDIR=../../../administrator/language/
......
......@@ -10,75 +10,6 @@
defined('_JEXEC') or die( 'Direct Access to ' . basename( __FILE__ ) . ' is not allowed.' ) ;
if (!class_exists('vmShopperPlugin')) require(JPATH_VM_PLUGINS . DS . 'vmshopperplugin.php');
/* For php 5.2 we cannot use lambda functions or any other way to pass local variables to the callback.
The only way to pass a local variable to a callback for preg_replace_callback is to
create a class instance... */
class ReplacementCallback {
private $details;
private $nrtype;
function __construct($nrtype, $details) {
$this->details = $details;
$this->nrtype = $nrtype;
}
/* Return a random "string" of the given length taken from the given alphabet */
static function randomString($alphabet, $len) {
$alen = strlen($alphabet);
$r = "";
for ($n=0; $n<$len; $n++) {
$r .= $alphabet[mt_rand(0, $alen-1)];
}
return $r;
}
function replace ($match) {
$varname = strtolower($match[1]);
switch ($varname) {
case "year": return date ("Y");
case "year2": return date ("y");
case "month": return date("m");
case "day": return date("d");
case "hour": return date("H");
case "hour12": return date("h");
case "ampm": return date("a");
case "minute": return date("i");
case "second": return date("s");
case "random":
/* the regexp matches (random)(Type)(Len) as match 1 to 3, Len is optional */
$len = ($match[3]?$match[3]:1);
// Fallback: If no Type is given, use Digit
$alphabet = "0123456789";
// Select the correct alphabet depending on Type
switch (strtolower($match[2])) {
case "digit": $alphabet = "0123456789"; break;
case "hex": $alphabet = "0123456789abcdef"; break;
case "letter": $alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; break;
case "uletter": $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; break;
case "lletter": $alphabet = "abcdefghijklmnopqrstuvwxyz"; break;
case "alphanum": $alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; break;
}
return self::randomString ($alphabet, $len);
case "vendorid": return ($this->details->virtuemart_vendor_id);
case "userid": return ($this->details->virtuemart_user_id);
case "ipaddress": return ($this->details->ip_address);
// Only for Invoice:
case "ordernumber": return ($this->details->order_number);
case "orderid": return ($this->details->virtuemart_order_id);
case "lastname": return ($this->details->last_name);
case "firstname": return ($this->details->first_name);
case "company": return ($this->details->company);
case "city": return ($this->details->city);
case "zip": return ($this->details->zip);
case "orderstatus": return ($this->details->order_status);
}
// No variable type matched, so don't replace, return the original string
return $match[0];
}
}
class plgVmShopperOrdernumber extends vmShopperPlugin {
function __construct(& $subject, $config) {
......@@ -104,29 +35,85 @@ class plgVmShopperOrdernumber extends vmShopperPlugin {
// We don't need this function, but the parent class declares it abstract, so we need to overload
function plgVmOnUpdateOrderBEShopper($_orderID) {}
/* Return a random "string" of the given length taken from the given alphabet */
static function randomString($alphabet, $len) {
$alen = strlen($alphabet);
$r = "";
for ($n=0; $n<$len; $n++) {
$r .= $alphabet[mt_rand(0, $alen-1)];
}
return $r;
}
function replaceRandom ($match) {
/* the regexp matches (random)(Type)(Len) as match, Type and Len is optional */
$len = ($match[3]?$match[3]:1);
// Fallback: If no Type is given, use Digit
$alphabet = "0123456789";
// Select the correct alphabet depending on Type
switch (strtolower($match[2])) {
case "digit": $alphabet = "0123456789"; break;
case "hex": $alphabet = "0123456789abcdef"; break;
case "letter": $alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; break;
case "uletter": $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; break;
case "lletter": $alphabet = "abcdefghijklmnopqrstuvwxyz"; break;
case "alphanum": $alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; break;
}
return self::randomString ($alphabet, $len);
}
/* Replace the format variables, match[1] is the variable name, match[2] and match[3] are only used for random fields */
/* Type 0 means order number, type 1 means invoice number, type 2 means customer number, 3 means order password */
function replace_fields ($fmt, $nrtype, $details) {
// Match variables for the form random[type][count] and everything else.
// This makes it possible to handle "random" just like any other type!
$patterns = array (
'/\[(random)(.*?)([0-9]*?)\]/', // For randomTypeN, spit the three parts
'/\[([^\]]+)\]/' // Everything else matches whole variable name
// First, replace all randomXXX[n] fields. This needs to be done with a regexp and a callback:
$fmt = preg_replace_callback ('/\[(random)(.*?)([0-9]*?)\]/', array($this, 'replaceRandom'), $fmt);
$reps = array (
"[year]" => date ("Y"),
"[year2]" => date ("y"),
"[month]" => date("m"),
"[day]" => date("d"),
"[hour]" => date("H"),
"[hour12]" => date("h"),
"[ampm]" => date("a"),
"[minute]" => date("i"),
"[second]" => date("s"),
"[userid]" => $details->virtuemart_user_id,
"[vendorid]" => $details->virtuemart_vendor_id
);
// // TODO: in php 5.3 and 5.4 we can easier pass $orderDetails to the callback
// function my_callback ($match) {
// return self::replacementCallback ($match, $details);
// };
// return preg_replace_callback ($patterns,
// function ($match) use ($details) {
// return self::replacementCallback ($match, $details);
// }, $fmt);
/* php 5.2 does not allow lambda functions and there is no other way to
pass a local variable to the callback than a class instance! */
$callback = new ReplacementCallback ($nrtype, $details);
return preg_replace_callback ($patterns, array($callback, 'replace'), $fmt);
if ($nrtype==0 or $nrtype == 1) {
$reps["[ipaddress]"] = $details->ip_address;
}
if ($nrtype==1) {
// Only for Invoice:
$reps["[ordernumber]"] = $details->order_number;
$reps["[orderid]"] = $details->virtuemart_order_id;
$reps["[lastname]"] = $details->last_name;
$reps["[firstname]"] = $details->first_name;
$reps["[company]"] = $details->company;
$reps["[city]"] = $details->city;
$reps["[zip]"] = $details->zip;
$reps["[orderstatus]"] = $details->order_status;
}
if ($nrtype==2) {
// Customer number:
print_r($details);
$reps["[username]"] = $details->username;
$reps["[name]"] = $details->name;
$reps["[email]"] = $details->email;
$reps["[company]"] = $details->company;
$reps["[title]"] = $details->title;
$reps["[firstname]"] = $details->first_name;
$reps["[middlename]"] = $details->middle_name;
$reps["[lastname]"] = $details->last_name;
$reps["[zip]"] = $details->zip;
$reps["[city]"] = $details->city;
$reps["[countryid]"] = $details->virtuemart_country_id;
$reps["[stateid]"] = $details->virtuemart_state_id;
$reps["[user_is_vendor]"] = $details->user_is_vendor;
}
return str_ireplace (array_keys($reps), array_values($reps), $fmt);
}
/* Type 0 means order number, type 1 means invoice number, type 2 means customer number */
......@@ -154,7 +141,6 @@ class plgVmShopperOrdernumber extends vmShopperPlugin {
$db->query();
// return the format with the counter inserted
return str_replace ("#", sprintf('%0' . $padding . 's', $count), $nr);
}
......@@ -162,7 +148,7 @@ class plgVmShopperOrdernumber extends vmShopperPlugin {
function plgVmOnUserOrder(&$orderDetails/*,&$data*/) {
// Is order number customization enabled?
if ($this->params->get('customize_order_number')) {
$nrtype=0; /*order-nr*/
$nrtype = 0; /*order-nr*/
$fmt = $this->params->get ('order_number_format', "#");
$global = $this->params->get ('order_number_global', 1);
$padding = $this->params->get ('order_number_padding', 1);
......@@ -171,8 +157,9 @@ class plgVmShopperOrdernumber extends vmShopperPlugin {
}
// Is order password customization enabled?
if ($this->params->get('customize_order_password')) {
$nrtype = 3; /* order password */
$fmt = $this->params->get ('order_password_format', "[randomHex8]");
$passwd = $this->replace_fields ($fmt, 3, $orderDetails);
$passwd = $this->replace_fields ($fmt, $nrtype, $orderDetails);
$orderDetails->order_pass = $passwd;
}
}
......@@ -186,7 +173,7 @@ class plgVmShopperOrdernumber extends vmShopperPlugin {
$pdfInvoice = (int)VmConfig::get('pdf_invoice', 0); // backwards compatible
$force_create_invoice=JRequest::getInt('create_invoice', 0);
if ( in_array($orderDetails['order_status'],$orderstatusForInvoice) or $pdfInvoice==1 or $force_create_invoice==1 ){
$nrtype=1; /*invoice-nr*/
$nrtype = 1; /*invoice-nr*/
$fmt = $this->params->get ('invoice_number_format', "#");
$global = $this->params->get ('invoice_number_global', 1);
$padding = $this->params->get ('invoice_number_padding', 1);
......@@ -202,7 +189,7 @@ class plgVmShopperOrdernumber extends vmShopperPlugin {
function plgVmOnUserStore(&$data) {
// Is order number customization enabled?
if ($this->params->get('customize_customer_number') && isset($data['customer_number_bycore']) && $data['customer_number_bycore']==1) {
$nrtype=2; /*customer-nr*/
$nrtype = 2; /*customer-nr*/
$fmt = $this->params->get ('customer_number_format', "#");
$global = $this->params->get ('customer_number_global', 1);
$padding = $this->params->get ('customer_number_padding', 1);
......
......@@ -57,8 +57,10 @@ class plgVmShopperOrdernumberInstallerScript
public function update(JAdapterInstance $adapter)
{
jimport( 'joomla.filesystem.file' );
JFile::delete( JPATH_ROOT . DS . "administrator" . DS . "language" . DS . "en-GB" . DS . "en-GB.plg_vmshopper_ordernumber.sys.ini");
JFile::delete( JPATH_ROOT . DS . "administrator" . DS . "language" . DS . "de-DE" . DS . "de-DE.plg_vmshopper_ordernumber.sys.ini");
$file = JPATH_ROOT . DS . "administrator" . DS . "language" . DS . "en-GB" . DS . "en-GB.plg_vmshopper_ordernumber.sys.ini";
if (JFile::exists($file)) JFile::delete($file);
$file = JPATH_ROOT . DS . "administrator" . DS . "language" . DS . "de-DE" . DS . "de-DE.plg_vmshopper_ordernumber.sys.ini";
if (JFile::exists($file)) JFile::delete($file);
return true;
}
......
......@@ -7,8 +7,8 @@
<authorUrl>http://www.kainhofer.com/</authorUrl>
<copyright>Copyright (C) 2012-2013 Reinhold Kainhofer. All rights reserved.</copyright>
<license>http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3</license>
<version>1.6.1</version>
<releaseDate>2013-01-09</releaseDate>
<version>1.6.2</version>
<releaseDate>2013-01-11</releaseDate>
<releaseType>Minor update</releaseType>
<downloadUrl>http://www.kainhofer.com/virtuemart-2-extensions/vm2-ordernumber-plugin.html</downloadUrl>
......
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment