Exemple #1
0
    /**
     * Display a textarea with validation for the entered aliases and expressions
     *
     * @param array          $arFieldInfo Information about the current input field
     * @param t3lib_tceforms $tceforms    Form rendering library object
     * @return string HTML code
     */
    public function textCombinations($arFieldInfo, t3lib_tceforms $tceforms)
    {
        $text = $tceforms->getSingleField_typeText($arFieldInfo['table'], $arFieldInfo['field'], $arFieldInfo['row'], $arFieldInfo);
        $evaluator = new Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator();
        $arTokens = $evaluator->tokenize($arFieldInfo['itemFormElValue']);
        $arNotFound = array();
        $arUnknownTokens = array();
        foreach ($arTokens as $token) {
            if (is_array($token) && $token[0] === Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator::T_VAR) {
                $contexts = Tx_Contexts_Context_Container::get()->initAll();
                $bFound = false;
                foreach ($contexts as $context) {
                    if ($context->getAlias() == $token[1]) {
                        $bFound = true;
                    }
                }
                if (!$bFound) {
                    $arNotFound[] = $token[1];
                }
            } elseif (is_array($token) && $token[0] === Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator::T_UNKNOWN) {
                $arUnknownTokens[] = $token[1];
            }
        }
        if (!$arNotFound && !$arUnknownTokens) {
            return $text;
        }
        $html = <<<HTM
{$text}<br />
<div class="typo3-message message-error">
    <div class="message-body">
HTM;
        if ($arNotFound) {
            $strNotFound = implode(', ', $arNotFound);
            $html .= <<<HTM
<div>
    {$GLOBALS['LANG']->sL('LLL:EXT:contexts/Resources/Private/Language' . '/flexform.xml:aliasesNotFound')}: {$strNotFound}
</div>
HTM;
        }
        if ($arUnknownTokens) {
            $strUnknownTokens = implode(', ', $arUnknownTokens);
            $html .= <<<HTM
<div>
    {$GLOBALS['LANG']->sL('LLL:EXT:contexts/Resources/Private/Language' . '/flexform.xml:unknownTokensFound')}: {$strUnknownTokens}
</div>
HTM;
        }
        $html .= <<<HTM
    </div>
</div>
HTM;
        return $html;
    }
 /**
  * Handle a token: Creates scopes for each expression in parenthesis and
  * does some syntax checks
  *
  * @param int|array $token
  * @throws Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator_Exception
  * @return void
  */
 protected function handleToken($token)
 {
     switch ($token) {
         case self::T_NEGATE:
             $this->nextTokenNegated = !$this->nextTokenNegated;
             break;
         case self::T_PL:
             $this->pushScope();
             break;
         case self::T_PR:
             $scope = $this->getScope();
             $this->popScope();
             if (!$scope->parentScope->parentScope) {
                 throw new Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator_Exception('Found not opened closing parentheses');
             }
             if (is_int(end($this->tokens))) {
                 throw new Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator_Exception('Unexpected )');
             }
             $this->parentScope->pushToken($scope);
             break;
         case self::T_AND:
         case self::T_OR:
         case self::T_XOR:
             if (!$this->tokens || is_int(end($this->tokens))) {
                 throw new Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator_Exception('Unexpected Operator');
             }
             $this->pushToken($token);
             break;
         case self::T_END:
             if (is_int(end($this->tokens))) {
                 throw new Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator_Exception('Unexpected end');
             }
             if ($this->getScope()->parentScope->parentScope) {
                 throw new Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator_Exception('Missing closing parentheses');
             }
             break;
         default:
             if ($token[0] == self::T_VAR) {
                 if ($this->tokens && !is_int(end($this->tokens))) {
                     throw new Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator_Exception('Unexpected variable');
                 }
                 $this->pushToken($token);
             } else {
                 throw new Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator_Exception('Unexpected "' . $token[1] . '"');
             }
     }
 }
 public function testAndNot()
 {
     $strExpression = 'a && !b';
     $arValues = array('a' => true, 'b' => true);
     $this->assertFalse(Tx_Contexts_Context_Type_Combination_LogicalExpressionEvaluator::run($strExpression, $arValues));
 }