Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
VirtueMart
Shipping by Rules for VirtueMart
Commits
3fb163d4
Commit
3fb163d4
authored
Dec 08, 2016
by
Reinhold Kainhofer
Browse files
V6.2: Add error/warning/notice/message functions to display messages to the user
Fixes
#30
Fixes
#31
parent
6bd93af3
Changes
9
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
3fb163d4
...
...
@@ -2,7 +2,7 @@ BASE=rules_shipping
BASE_ADV
=
rules_shipping_advanced
PLUGINTYPE
=
vmshipment
ZIPBASE
=
opentools_vm
VERSION
=
6.
1.7
VERSION
=
6.
2
PLUGINFILES
=
$(BASE)
.php
$(BASE)
_base.php
$(BASE)
_framework_joomla.php
$(BASE)
.script.php
$(BASE)
.xml index.html
PLUGINFILES_ADV
=
$(BASE_ADV)
.php
$(BASE)
_base.php
$(BASE)
_framework_joomla.php
$(BASE_ADV)
.script.php
$(BASE_ADV)
.xml index.html
...
...
images/plg_vmshipping_rules_shipping_messages.png
0 → 100644
View file @
3fb163d4
31.1 KB
images/plg_vmshipping_rules_shipping_plugins_icon.png
0 → 100644
View file @
3fb163d4
47.5 KB
library/rules_shipping_framework.php
View file @
3fb163d4
...
...
@@ -171,13 +171,33 @@ class RulesShippingFramework {
return
$this
->
custom_functions
;
}
/** @tag system-specific
* @function printWarning()
* Print a warning in the system-specific way.
* @param $message the warning message to be printed (already properly translated)
/** @tag public-api
* @tag system-specific
* @function message()
* Print a message (to be translated) with given type in the system-specific way.
* @param $message the message to be printed
* @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug")
* @param $args optional arguments to be inserted into the translated message in sprintf-style
*/
protected
function
printWarning
(
$message
)
{
echo
(
$message
);
public
function
message
(
$message
,
$type
)
{
$args
=
func_get_args
();
// Remove the $type from the args passed to __
unset
(
$args
[
1
]);
$msg
=
call_user_func_array
(
array
(
$this
,
"__"
),
$args
);
$this
->
printMessage
(
$msg
,
$type
);
}
/** @tag public-api
* @tag system-specific
* @function error()
* Print an error message (to be translated) in the system-specific way.
* @param $message the error message to be printed
* @param $args optional arguments to be inserted into the translated message in sprintf-style
*/
public
function
error
(
$message
)
{
$args
=
func_get_args
();
array_splice
(
$args
,
1
,
0
,
'error'
);
// insert msg type in second position
call_user_func_array
(
array
(
$this
,
"message"
),
$args
);
}
/** @tag public-api
...
...
@@ -189,16 +209,44 @@ class RulesShippingFramework {
*/
public
function
warning
(
$message
)
{
$args
=
func_get_args
();
$msg
=
call_user_func_array
(
array
(
$this
,
"__"
),
$args
);
$this
->
printWarning
(
$msg
);
array_splice
(
$args
,
1
,
0
,
'warning'
);
// insert msg type in second position
call_user_func_array
(
array
(
$this
,
"message"
),
$args
);
}
/** @tag public-api
* @tag system-specific
* @function notice()
* Print a message (to be translated) in the system-specific way.
* @param $message the message to be printed
* @param $args optional arguments to be inserted into the translated message in sprintf-style
*/
public
function
notice
(
$message
)
{
$args
=
func_get_args
();
array_splice
(
$args
,
1
,
0
,
'notice'
);
// insert msg type in second position
call_user_func_array
(
array
(
$this
,
"message"
),
$args
);
}
/** @tag public-api
* @tag system-specific
* @function debug()
* Print a debug message (untranslated) in the system-specific way.
* @param $message the debug message to be printed
* Print a debug message in the system-specific way.
* @param $message the message to be printed
* @param $args optional arguments to be inserted into the translated message in sprintf-style
*/
public
function
debug
(
$message
)
{
$args
=
func_get_args
();
array_splice
(
$args
,
1
,
0
,
'debug'
);
// insert msg type in second position
call_user_func_array
(
array
(
$this
,
"message"
),
$args
);
}
/** @tag system-specific
* @function printMessage()
* Print a message of given type in the system-specific way.
* @param $message the message to be printed (already properly translated)
* @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug")
*/
protected
function
printMessage
(
$message
,
$type
)
{
echo
(
$message
);
}
/** @tag public-api
...
...
@@ -422,6 +470,12 @@ class RulesShippingFramework {
$this
->
warning
(
'OTSHIPMENT_RULES_UNKNOWN_TYPE'
,
$r
->
getType
(),
$r
->
rulestring
);
break
;
}
// Handle messages (error, warning, message/notice, debug:
foreach
(
$r
->
messages
as
$k
=>
$msgs
)
{
foreach
(
$msgs
as
$msg
)
{
$this
->
message
(
$msg
,
$k
);
}
}
}
if
(
!
is_null
(
$result
[
"rule"
]))
{
$this
->
match
[
$id
]
=
$result
;
...
...
@@ -599,6 +653,7 @@ class ShippingRule {
var
$countries
=
array
();
var
$ruleinfo
=
0
;
var
$includes_tax
=
0
;
var
$messages
=
array
(
'error'
=>
array
(),
'warning'
=>
array
(),
'notice'
=>
array
(),
'debug'
=>
array
());
function
__construct
(
$framework
,
$rule
,
$countries
,
$ruleinfo
)
{
$this
->
framework
=
$framework
;
...
...
@@ -630,6 +685,11 @@ class ShippingRule {
case
'extrashippingcharge'
:
$this
->
shipping
=
$value
;
$this
->
ruletype
=
'modifiers_add'
;
break
;
// modifiers are also stored in the shipping member!
case
'extrashippingmultiplier'
:
$this
->
shipping
=
$value
;
$this
->
ruletype
=
'modifiers_multiply'
;
break
;
// modifiers are also stored in the shipping member!
case
'comment'
:
break
;
// Completely ignore all comments!
case
'error'
:
$this
->
messages
[
'error'
][]
=
$value
;
break
;
case
'warning'
:
$this
->
messages
[
'warning'
][]
=
$value
;
break
;
case
'notice'
:
case
'message'
:
$this
->
messages
[
'notice'
][]
=
$value
;
break
;
case
'debug'
:
$this
->
messages
[
'debug'
][]
=
$value
;
break
;
case
'condition'
:
$this
->
conditions
[]
=
$value
;
break
;
default
:
$this
->
framework
->
warning
(
'OTSHIPMENT_RULES_UNKNOWN_VARIABLE'
,
$var
,
$rulepart
);
}
...
...
@@ -652,9 +712,8 @@ class ShippingRule {
$rulepart
=
trim
(
$rulepart
);
if
(
!
isset
(
$rulepart
)
||
$rulepart
===
''
)
return
;
// Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression!
if
(
preg_match
(
'/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i'
,
$rulepart
,
$matches
))
{
if
(
preg_match
(
'/^\s*(name|variable|definition
|error|warning|message|notice|debug
)\s*=\s*(["\']?)(.*)\2\s*$/i'
,
$rulepart
,
$matches
))
{
$this
->
handleAssignment
(
$matches
[
1
],
$matches
[
3
],
$rulepart
);
return
;
}
...
...
@@ -1033,9 +1092,25 @@ class ShippingRule {
}
}
protected
function
calculateShipping
(
$vals
,
$products
,
$cartvals_callback
)
{
protected
function
calculateShipping
(
$vals
,
$products
,
$cartvals_callback
)
{
return
$this
->
evaluateTerm
(
$this
->
shipping
,
$vals
,
$products
,
$cartvals_callback
);
}
protected
function
stringReplaceVariables
(
$str
,
$vals
)
{
// Evaluate the rule name as a translatable string with variables inserted:
// Replace all {variable} tags in the name by the variables from $vals
$matches
=
array
();
preg_match_all
(
'/{([A-Za-z0-9_]+)}/'
,
$str
,
$matches
);
foreach
(
$matches
[
1
]
as
$m
)
{
$val
=
$this
->
evaluateVariable
(
$m
,
$vals
);
if
(
$val
!==
null
)
{
$str
=
str_replace
(
"
{
".$m."
}
"
,
$val
,
$str
);
}
}
return
$str
;
}
protected
function
evaluateRule
(
&
$vals
,
$products
,
$cartvals_callback
)
{
if
(
$this
->
evaluated
)
...
...
@@ -1059,21 +1134,15 @@ class ShippingRule {
}
// All conditions match
$this
->
match
=
True
;
foreach
(
$this
->
messages
as
$k
=>
$msgs
)
{
foreach
(
$msgs
as
$i
=>
$m
)
{
$this
->
messages
[
$k
][
$i
]
=
$this
->
stringReplaceVariables
(
$m
,
$vals
);
}
}
// Calculate the value (i.e. shipping cost or modifier)
$this
->
value
=
$this
->
calculateShipping
(
$vals
,
$products
,
$cartvals_callback
);
// Evaluate the rule name as a translatable string with variables inserted:
// Replace all {variable} tags in the name by the variables from $vals
$matches
=
array
();
$name
=
$this
->
framework
->
__
(
$this
->
name
);
preg_match_all
(
'/{([A-Za-z0-9_]+)}/'
,
$name
,
$matches
);
foreach
(
$matches
[
1
]
as
$m
)
{
$val
=
$this
->
evaluateVariable
(
$m
,
$vals
);
if
(
$val
!==
null
)
{
$name
=
str_replace
(
"
{
".$m."
}
"
,
$val
,
$name
);
}
}
$this
->
rulename
=
$name
;
$this
->
rulename
=
$this
->
stringReplaceVariables
(
$this
->
framework
->
__
(
$this
->
name
),
$vals
);
}
function
matches
(
&
$vals
,
$products
,
$cartvals_callback
)
{
...
...
@@ -1158,7 +1227,7 @@ class ShippingRule_Advanced extends ShippingRule {
// Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression!
if
(
preg_match
(
'/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i'
,
$rulepart
,
$matches
))
{
if
(
preg_match
(
'/^\s*(name|variable|definition
|error|warning|message|notice|debug
)\s*=\s*(["\']?)(.*)\2\s*$/i'
,
$rulepart
,
$matches
))
{
$this
->
handleAssignment
(
$matches
[
1
],
$matches
[
3
],
$rulepart
);
return
;
}
...
...
releases/plg_opentools_vm_rules_shipping_advanced_v6.2.zip
0 → 100644
View file @
3fb163d4
File added
releases/plg_opentools_vm_rules_shipping_v6.2.zip
0 → 100644
View file @
3fb163d4
File added
rules_shipping.xml
View file @
3fb163d4
...
...
@@ -6,7 +6,7 @@
<authorUrl>
http://www.open-tools.net
</authorUrl>
<copyright>
Copyright (C) 2013-2014, Reinhold Kainhofer
</copyright>
<license>
GPL v3+
</license>
<version>
6.
1.7
</version>
<version>
6.
2
</version>
<description>
OTSHIPMENT_RULES_DESC
</description>
<files>
<filename
plugin=
"rules_shipping"
>
rules_shipping.php
</filename>
...
...
rules_shipping_advanced.xml
View file @
3fb163d4
...
...
@@ -6,7 +6,7 @@
<authorUrl>
http://www.open-tools.net
</authorUrl>
<copyright>
Copyright (C) 2013-2014, Reinhold Kainhofer
</copyright>
<license>
GPL v3+
</license>
<version>
6.
1.7
</version>
<version>
6.
2
</version>
<description>
OTSHIPMENT_RULES_ADV_DESC
</description>
<files>
<filename
plugin=
"rules_shipping_advanced"
>
rules_shipping_advanced.php
</filename>
...
...
rules_shipping_framework_joomla.php
View file @
3fb163d4
...
...
@@ -44,24 +44,20 @@ class RulesShippingFrameworkJoomla extends RulesShippingFramework {
return
$custfuncdefs
;
}
protected
function
printWarning
(
$message
)
{
// Keep track of warning messages, so we don't print them twice:
global
$printed_warnings
;
if
(
!
isset
(
$printed_warnings
))
$printed_warnings
=
array
();
if
(
!
in_array
(
$message
,
$printed_warnings
))
{
JFactory
::
getApplication
()
->
enqueueMessage
(
$message
,
'error'
);
$printed_warnings
[]
=
$message
;
protected
function
printMessage
(
$message
,
$type
)
{
// Keep track of messages, so we don't print them twice:
global
$printed_messages
;
if
(
!
isset
(
$printed_messages
))
$printed_messages
=
array
();
if
(
!
in_array
(
$message
,
$printed_messages
))
{
if
(
$type
==
'debug'
)
{
vmDebug
(
$message
);
}
else
{
JFactory
::
getApplication
()
->
enqueueMessage
(
$message
,
$type
);
}
$printed_messages
[]
=
$message
;
}
}
/** @tag public-api
* @function debug()
* Print a debug message (untranslated) in the system-specific way.
* @param $message the debug message to be printed
*/
public
function
debug
(
$message
)
{
vmDebug
(
$message
);
}
public
function
__
(
$string
)
{
$args
=
func_get_args
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment