Hello,
i would like to make following proposal to the conditional objects in
templates:
Its rather flexible and supports normal variables and Session-vars.....
in the template file:
<roundcube:button command="print" imageSel="/images/buttons/
print_sel.png" imageAct="/images/buttons/print_act.png" imagePas="/
images/buttons/print_pas.png" width="32" height="32"
title="printmessage" condition="session:username:test"/>
</div>
in main.inc:
function rcube_xml_command($command, $str_attrib, $add_attrib=array()) { global $IMAP, $CONFIG, $OUTPUT;
$command = strtolower($command); $attrib = parse_attrib_string($str_attrib) + $add_attrib;
//check if tag is conditional
if($attrib['condition']) {
$condition=explode(':',$attrib['condition']);
$logmesg="condition:".$condition[0].'='.print_array($condition);
//console($logmesg);
if(strpos($condition[2],',')) $condition_array=explode(',',
$condition[2]); else $condition_array=array(0=>$condition[2]);
switch ($condition[0]) {
case 'session':
// if sesion var is not set -> switch off command
if(!in_array($_SESSION[$condition[1]],$condition_array))
$command='none';
break;
default: if(!$condition[0]==$condition[1])) $command='none'; // condition is not handled -> switch off command break; } }
Regards!
Edgar Bueltemeyer wrote:
<roundcube:button command="print" imageSel="/images/buttons/print_sel.png" imageAct="/images/buttons/print_act.png" imagePas="/images/buttons/print_pas.png" width="32" height="32" title="printmessage" *condition="session:username:test"*/>
I took your path and added support for expressions. In the above example it would be:
condition="session:username==test"
Robin
--- program/include/main.inc (revision 369) +++ program/include/main.inc (working copy) @@ -1135,6 +1135,23 @@ $command = strtolower($command); $attrib = parse_attrib_string($str_attrib) + $add_attrib;
preg_match('/^(session|config):([a-zA-Z0-9_]+)([=<>!]+)(.*)$/', $attrib['condition'], $condition))
switch ($condition[1])
{
case 'session':
if (!eval("return (\$_SESSION['" . $condition[2] . "']" . $condition[3] . "'" . $condition[4] . "');"))
$command = 'none';
break;
case 'config':
if (!eval("return (\$CONFIG['" . $condition[2] . "']" . $condition[3] . "'" . $condition[4] . "');"))
$command = 'none';
break;
}
--- skins/default/templates/mail.html (revision 369) +++ skins/default/templates/mail.html (working copy) @@ -45,6 +45,7 @@ id="messagelist" cellspacing="0" summary="Message list"
Condition checking 0.2 ;-)
to handle != conditions for the variable thats tested. For handling individual mixed things like: session:username:paul,! mary,fred i have to squeeze my brain a little bit more.........
WARNING! This code is not tested, as of now it can contain errors.....
in the template file:
Session:
<roundcube:button command="print" imageSel="/images/buttons/
print_sel.png" imageAct="/images/buttons/print_act.png" imagePas="/
images/buttons/print_pas.png" width="32" height="32"
title="printmessage" condition="session:username:test"/>
</div>
"is not"
<roundcube:button command="print" imageSel="/images/buttons/
print_sel.png" imageAct="/images/buttons/print_act.png" imagePas="/
images/buttons/print_pas.png" width="32" height="32"
title="printmessage" condition="session:!username:test"/>
</div>
Array:
<roundcube:button command="print" imageSel="/images/buttons/
print_sel.png" imageAct="/images/buttons/print_act.png" imagePas="/
images/buttons/print_pas.png" width="32" height="32"
title="printmessage" condition="session:username:test,peter,paul"/>
</div>
Variabe:
<roundcube:button command="print" imageSel="/images/buttons/
print_sel.png" imageAct="/images/buttons/print_act.png" imagePas="/
images/buttons/print_pas.png" width="32" height="32"
title="printmessage" condition="var:day:monday"/>
</div>
"is not"
<roundcube:button command="print" imageSel="/images/buttons/
print_sel.png" imageAct="/images/buttons/print_act.png" imagePas="/
images/buttons/print_pas.png" width="32" height="32"
title="printmessage" condition="var:!day:monday"/>
</div>
Array:
<roundcube:button command="print" imageSel="/images/buttons/
print_sel.png" imageAct="/images/buttons/print_act.png" imagePas="/
images/buttons/print_pas.png" width="32" height="32"
title="printmessage" condition="var:day:monday,tuesday"/>
</div>
in main.inc:
function rcube_xml_command($command, $str_attrib, $add_attrib=array()) { global $IMAP, $CONFIG, $OUTPUT;
$command = strtolower($command); $attrib = parse_attrib_string($str_attrib) + $add_attrib;
//check if tag is conditional
if($attrib['condition']) {
$condition=explode(':',$attrib['condition']);
$logmesg="condition:".$condition[0].'='.print_array($condition);
//console($logmesg);
if(strpos($condition[1],'!')) {
$condition[1]=substr($condition[1],1);
$cond_neg=1;
}
if(strpos($condition[2],',')) $condition_array=explode(',',
$condition[2]); else $condition_array=array(0=>$condition[2]);
switch ($condition[0]) {
case 'session':
if($cond_neg==1) {
// if sesion var is set -> switch off command
if(in_array($_SESSION[$condition[1]],$condition_array))
$command='none';
}else{
// if sesion var is not set -> switch off command
if(!in_array($_SESSION[$condition[1]],$condition_array))
$command='none';
}
break;
case 'var':
if($cond_neg==1) {
// if var is set -> switch off command
if(in_array($$condition[1],$condition_array))
$command='none';
}else{
// if sesion var is not set -> switch off command
if(!in_array($$condition[1],$condition_array))
$command='none';
}
break;
default:
if($cond_neg==1) {
if($$condition[0]==$condition[1])) $command='none';
// condition is not handled -> switch off command
}else{
if(!$$condition[0]==$condition[1])) $command='none';
}
break;
} }
Regards!
Edgar
Am 16.11.2006 um 13:58 schrieb Robin Elfrink:
Edgar Bueltemeyer wrote:
Its rather flexible and supports normal variables and Session- vars.....
I hadn't thought of that.
But how would you handle expressions like '!=' ?
Robin
OOPS, you have been faster than me.... :-)
cool thing with expressions!
Regards
Am 16.11.2006 um 14:26 schrieb Robin Elfrink:
Edgar Bueltemeyer wrote:
<roundcube:button command="print" imageSel="/images/buttons/print_sel.png" imageAct="/images/buttons/print_act.png" imagePas="/images/buttons/print_pas.png" width="32" height="32" title="printmessage" *condition="session:username:test"*/>
I took your path and added support for expressions. In the above
example it would be:condition="session:username==test"
Robin Index: program/include/main.inc =================================================================== --- program/include/main.inc (revision 369) +++ program/include/main.inc (working copy) @@ -1135,6 +1135,23 @@ $command = strtolower($command); $attrib = parse_attrib_string($str_attrib) + $add_attrib;
- // check if tag is conditional
- if ($attrib['condition'] &&
preg_match('/^(session|config):([a-zA-Z0-9_]+)([=<>!]+)(.*)
$/', $attrib['condition'], $condition))
- {
switch ($condition[1])
{
case 'session':
if (!eval("return (\$_SESSION['" . $condition[2] .
"']" . $condition[3] . "'" . $condition[4] . "');"))
$command = 'none';
break;
case 'config':
if (!eval("return (\$CONFIG['" . $condition[2] . "']" .
$condition[3] . "'" . $condition[4] . "');"))
$command = 'none';
break;
}
- }
- // execute command switch ($command) {
Index: skins/default/templates/mail.html
--- skins/default/templates/mail.html (revision 369) +++ skins/default/templates/mail.html (working copy) @@ -45,6 +45,7 @@ id="messagelist" cellspacing="0" summary="Message list"
- condition="config:drafts_mbox==Drafts" messageIcon="/images/icons/dot.png" unreadIcon="/images/icons/unread.png" deletedIcon="/images/icons/deleted.png"
Hi everybody
I guess I found a good way to handle conditions in a more flexible way. With the changes in rev. 388 conditions in any PHP-styled form can be added to dynamic template objects. The parser simply replaces some parts of the expression and then eval it. This also enables complex conditions with brackets and multiple ANDs and ORs.
For the preview pane implementation there's a simple conditions like <roundcube:if condition="config:preview_pane == true" /> in the mail.html template.
Currently there are three "arrays" which can be used for conditions: session Access PHP session vars config Access any config parameter request Access any request parameter
The only thing that would cause some problems are greater-than signs in such an expression (like "config:pagesize > 20"). This would be considered as the end of the tag because quotes are not regarded in the tag matching regexp.
I've already worked on a new template parsing functions which correctly reads quoted attributes and which should also be capable for nested if-else blocks. It's not ready yet but I guess for now, the current solution will do the job.
Regards, Thomas