Antwort
 
LinkBack (1) Themen-Optionen Thema durchsuchen Thema bewerten Ansicht
  #1  
Alt 08.11.06, 13:06
Benutzerbild von Junior
TYPO3 Forum Team
Administrator
 
Registriert seit: 18.08.04
Ort: Zürich
Alter: 33
Beiträge: 2.776
Junior eine Nachricht über Skype™ schicken
What about implementing the combination of conditions?

What about implementing the combination of conditions? For instance in the context of TypoScript Templates you can place more "conditions" in the same (real) condition:

PHP-Code:
[browser netscape][browser operasomeTypoScript 123 [GLOBAL] 

They are evaluated by OR-ing the result of each sub-condition (done in the class t3lib_matchCondition). We could implement something alike and maybe even better. For instance we could implement a syntax like this:

PHP-Code:
CON 1 ] && [ CON 2 ] || [ CON 3 
This will be read like "Returns true if condition 1 and condition 2 are true OR if condition 3 is true". In other words we implement the ability to AND and OR conditions together.

The implementation goes as follows:

PHP-Code:
    class myConditions {
     
     
/**
      * Splits the input condition line into AND and OR parts 
      * which are separately evaluated and logically combined to the final output.
      */
     
function match($conditionLine)    {
             
// Getting the value from inside of the wrapping 
             // square brackets of the condition line:
         
$insideSqrBrackets trim(ereg_replace('\]$','',substr($conditionLine,1)));
         
             
// The "weak" operator, OR, takes precedence:
         
$ORparts split('\][[:space:]]*\|\|[[:space:]]*\[',$insideSqrBrackets);
         foreach(
$ORparts as $andString)    {
             
$resBool FALSE;
                 
                 
// Splits by the "&&" and operator:
             
$ANDparts split('\][[:space:]]*\&\&[[:space:]]*\[',$andString);
             foreach(
$ANDparts as $condStr)    {
                 
$resBool $this->evalConditionStr($condStr) ? TRUE FALSE;
                 if (!
$resBool)    break;
             }
             
             if (
$resBool)    break;
         }
         return 
$resBool;
     }
     
     
/**
      * Evaluates the inner part of the conditions.
      */
     
function evalConditionStr($condStr)    {
             
// Splitting value into a key and value based on the "=" sign
         
list($key,$value) = explode('=',$condStr,2);
         
         switch(
trim($key))    {
             case 
'UserIpRange':
                 return 
t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), trim($value)) ? TRUE FALSE;
             break;
             case 
'Browser':
                 return 
$GLOBALS['CLIENT']['BROWSER']==trim($value);
             break;
         }
     }
 } 

With this implementation I can make a condition line like this:

PHP-Code:
UserIpRange 192.168.*.*] && [Browser msie]
headerImage fileadmin/img1.jpg 
So if I'm in the right IP range AND has the right browser the value of "headerImage" will be "fileadmin/img1.jpg"

If we modify the TypoScript as follows the same condition applies but if the browser is Netscape then the condition will evaluated to true regardless of the IP range:

PHP-Code:
[UserIpRange 192.168.*.*] && [Browser msie] || [Browser netscapeheaderImage fileadmin/img1.jpg 
This is because the conditions are read like the parenthesis levels show:

PHP-Code:
"UserIpRange = 192.168.*.*" AND "Browser = msie" ) OR "Browser = netscape" 
The order of the "||" and "&&" operators may be a problem now. For instance:

PHP-Code:
[UserIpRange 192.168.*.*] || [UserIpRange 212.237.*.*] && [Browser msie]
headerImage fileadmin/img1.jpg 
Imagine this condition; I would like it to read as "If User IP Range is either #1 or #2 provided that the browser is MSIE in any case!". But right now it will be true if the User IP range is 192.168.... OR if either the range is 212.... and the browser is MSIE.

Formally, this is what I want:

PHP-Code:
"UserIpRange = 192.168.*.*" OR "UserIpRange = 212.237.*.*" ) AND "Browser = msie" 
My solution will be to implement a second way of OR'ing conditions together - by simply implying an OR between two "condition sections" if no operator is there. Thus the line above could be implemented as follows:

PHP-Code:
[UserIpRange 192.168.*.*][UserIpRange 212.237.*.*] && [Browser msie]
headerImage fileadmin/img1.jpg 
Line 10 will be understood in this way:

PHP-Code:
[UserIpRange 192.168.*.*](implied OR here!)[UserIpRange 212.237.*.*] && [Browser msie
The function match() of the condition class will have to be modified as follows:

PHP-Code:
     /**
      * Splits the input condition line into AND and OR parts 
      * which are separately evaluated and logically combined to the final output.
      */
     
function match($conditionLine)    {
             
// Getting the value from inside of the wrapping 
             // square brackets of the condition line:
         
$insideSqrBrackets trim(ereg_replace('\]$','',substr($conditionLine,1)));
         
             
// The "weak" operator, OR, takes precedence:
         
$ORparts split('\][[:space:]]*\|\|[[:space:]]*\[',$insideSqrBrackets);
         foreach(
$ORparts as $andString)    {
             
$resBool FALSE;
                 
                 
// Splits by the "&&" and operator:
             
$ANDparts split('\][[:space:]]*\&\&[[:space:]]*\[',$andString);
             foreach(
$ANDparts as $subOrStr)    {
             
                     
// Split by no operator between ] and [ (sub-OR)
                 
$subORparts split('\][[:space:]]*\[',$subOrStr);
                 
$resBool FALSE;
                 foreach(
$subORparts as $condStr)    {
                     if (
$this->evalConditionStr($condStr))    {
                         
$resBool TRUE;
                         break;
                     }
                 }            
 
                 if (!
$resBool)    break;
             }
             
             if (
$resBool)    break;
         }
         return 
$resBool;
     } 
That's it.

In fact this function might be implemented for TypoScript Templates some day. Currently they only offer the OR'ing of condition-subparts, but this parsing above could probably be applied directly. Who knows, maybe it has already been done when you are reading this document...
Addendum to the reference for our application

Remember in the previous sections? We defined three tables with properties that could be used in TypoScript in the context of our case-story application. To that reference we should now add a section with conditions which defines the following:

#1: Line syntax:

A condition is splitted into smaller parts which are logically AND'ed or OR'ed together. Each sub-part of the condition line is separated by "] (Operator) [" where operator can be "&&" (AND) , "||" (OR) or nothing at all (also meaning OR "below" AND in order).

The format of the condition line therefore is:

PHP-Code:
COND1 ] || [ COND2 ] && [ COND3 ] [ COND4 
....etc

where the operators has precedence as indicated by these illustrative parenthesis:

PHP-Code:
COND1 ] || ( [ COND2 ] && ( [ COND3 ] [ COND4 ] ) ) 
(Notice: Between COND3 and COND4 the blank space is implicitly an OR)

#2: Subpart syntax:

For each subpart (for example "[ COND 1 ]") the content is evaluated as follows:

PHP-Code:
KEY VALUE 
where KEY denotes a type of condition:

PHP-Code:
[title]KEY[/title
UserIpRange

PHP-Code:
[title]Description[/title
Returns true if the clients remote IP address matches the pattern given as value.

The value is matched against REMOTE_ADDR by the function
PHP-Code:
t3lib_div::cmpIP() 
which you can consult for details on the syntax.

PHP-Code:
[title]Example[/title]
[
UserIpRange 192.168.*.* 
]

PHP-Code:
[title]KEY[/title
Browser

PHP-Code:
[title]Description[/title
Returns true if the client browser matches one of the keywords below:

Value you can use:

konqu = Konqueror
opera = Opera
msie = Microsoft Internet Explorer
net = Netscape (or any other)

Values are evaluated against the output of the function t3lib_div::clientInfo() which can be consulted for details on the values for browsers.

PHP-Code:
[title]Example[/title]
[
Browser msie
© www.TYPO3.org
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Wong this Post!Spurl this Post!Reddit! Diesen Post bei linksilo.de bookmarken!
Mit Zitat antworten
Antwort


Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht Thema bewerten
Thema bewerten:

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge anzufügen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

vB Code ist An.
Smileys sind An.
[IMG] Code ist Aus.
HTML-Code ist An.
Trackbacks are An
Pingbacks are An
Refbacks are An

LinkBacks (?)
LinkBack to this Thread: http://www.typo3forum.net/forum/faq-backend/17782-what-about-implementing-combination-conditions.html
Erstellt von For Type Datum
Refinance Interest Rate - jersey interest refinance, interest rate scientific This thread Refback 15.11.07 19:04

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
Conditions Problem tommboogie TYPO3 3.x Fragen und Probleme 6 09.05.07 19:32
conditions Sareen TYPO3 4.x Fragen und Probleme 4 30.04.07 14:57
CSS Conditions für RTE Mik3e TYPO3 4.x Fragen und Probleme 4 20.02.07 12:25
If / If / Else mit Conditions möglich...? w@ TYPO3 3.x Fragen und Probleme 6 21.07.06 07:14
Conditions kurtzman TYPO3 3.x Fragen und Probleme 5 27.02.06 12:28


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:25 Uhr.


Powered by vBulletin® Version 3.6.8 Patch Level 2 (Deutsch)
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Template-Modifikationen durch TMS