Ejemplo n.º 1
0
 /**
  * Replace elements expression with value from $formObj
  *
  * @param string $expression
  * @param EasyForm $formObj
  * @return mixed
  */
 protected static function replaceElementsExpr($expression, $formObj)
 {
     $script = "";
     $start = 0;
     // replace [field] with field value
     while (true) {
         $pos0 = strpos($expression, "[", $start);
         $pos1 = strpos($expression, "]", $start);
         if ($pos0 === false) {
             $script .= substr($expression, $start);
             break;
         }
         if ($pos0 >= 0 && $pos1 > $pos0) {
             $script .= substr($expression, $start, $pos0 - $start);
             $start = $pos1 + 1;
             $elementName = substr($expression, $pos0 + 1, $pos1 - $pos0 - 1);
             // get field value
             $element = $formObj->getElement($elementName);
             if ($element) {
                 $fldval = $element->getValue();
             } else {
                 $fldval = null;
             }
             if ($fldval !== null) {
                 $script .= $fldval;
             } else {
                 //$script .= substr($expression, $pos0, $pos1 - $pos0);
                 //return "fail to evaluate $expression";
                 return $expression;
                 // return the original expression once it can't find element
             }
         } elseif ($pos0 >= 0 && $pos1 <= $pos0) {
             break;
         }
     }
     return $script;
 }