Esempio n. 1
0
 /**
  * Evaluate simple expression
  * expression is combination of text, simple expressiones and field variables
  * simple expression - {...}
  * field variable - [field name]
  * expression samples: text1{[field1]*10}text2{function1([field2],'a')}text3
  *
  * @objname:property, @objname:field[fldname].property, @objname:control[ctrlname].property
  * @:prop = @thisobjname:prop
  * [fldname] = @thisobjname:field[fldname].value
  * @demo.BOEvent:Name, @:Name
  * @demo.BOEvent:Field[EventName].Column, @demo.BOEvent:Field[EventName].Value
  * @demo.FMEvent:Control[evt_name].FieldName, @demo.FMEvent:Control[evt_name].Value
  * [EventName] is @demo.BOEvent:Field[EventName].Value in BOEvent.xml
  *
  * @param string $expression - simple expression supported by the openbiz
  * @param object $object
  * @return mixed
  **/
 public static function evaluateExpression($expression, $object)
 {
     // TODO: check if it's "\[", "\]", "\{" or "\}"
     $script = "";
     $start = 0;
     if (!self::isExpression($expression)) {
         return $expression;
     }
     if (self::isCurrentObject($expression)) {
         return $object;
     }
     // evaluate the expression between {}
     while (true) {
         list($tag, $openTagPos, $closeTagPos) = self::getNextContainerPos($expression, $start);
         if ($openTagPos === false) {
             if (substr($expression, $start)) {
                 $script .= substr($expression, $start);
             }
             break;
         }
         if ($openTagPos >= 0 && $closeTagPos > $openTagPos) {
             $script .= substr($expression, $start, $openTagPos - $start);
             $start = $closeTagPos + strlen(self::$expContainers[$tag]);
             $section = substr($expression, $openTagPos + strlen($tag), $closeTagPos - $openTagPos - strlen($tag));
             $_section = $section;
             if ($object) {
                 //Openbiz::$app->getLog()->log(LOG_DEBUG, "EXPRESSION", "###expression 1: ".$section."");
                 $section = Expression::replaceVarExpr($section, $object);
                 // replace variable expr;
                 //Openbiz::$app->getLog()->log(LOG_DEBUG, "EXPRESSION", "###expression 2: ".$section."");
                 if ($_section == $section) {
                     if (is_subclass_of($object, "Openbiz\\Data\\BizDataObj") || get_class($object) == "Openbiz\\Data\\BizDataObj" and strstr($section, '[')) {
                         $section = Expression::replaceFieldsExpr($section, $object);
                     }
                     // replace [field] with its value
                     if (is_subclass_of($object, "Openbiz\\Easy\\EasyForm") || get_class($object) == "Openbiz\\Easy\\EasyForm" and strstr($section, '[')) {
                         $section = Expression::replaceElementsExpr($section, $object);
                     }
                     // replace [field] with its value
                 }
             }
             if ($section === false) {
                 $script = $script == '' ? $section : $script . $section;
             }
             if ($section != null and trim($section) != "" and $section != false) {
                 $ret = null;
                 //$section = str_replace($section, '\', '\\');
                 //echo $section . '<br />';
                 //if (Expression::eval_syntax("\$ret = $section;"))
                 if (($tag == '{fx}' || $tag == '{') && Expression::eval_syntax("\$ret = {$section};")) {
                     eval("\$ret = {$section};");
                 }
                 if ($ret === null) {
                     $ret = $section;
                 }
                 $script = $script == '' ? $ret : $script . $ret;
                 unset($ret);
             }
         } elseif ($openTagPos >= 0 && $closeTagPos <= $openTagPos) {
             break;
         }
     }
     return $script;
 }