Ejemplo n.º 1
0
 private function _HandleFunctions($pstrTok, &$pStack, $pdtFormat, &$parrVars)
 {
     if (!$pstrTok->isFunction) {
         throw new Exception("Unsupported function token [" . $pstrTok->val . "]");
     }
     $varTmp = $pstrTok->val;
     $arrArgs = array();
     $varTerm = MyZebra_Tokenizer::$ARG_TERMINAL;
     while (!$pStack->IsEmpty()) {
         $varTerm = $pStack->Pop();
         if (!$varTerm->isArgTerminal) {
             $arrArgs[] = $varTerm;
         } else {
             break;
         }
     }
     switch ($varTmp) {
         case "ARRAY":
             if (count($arrArgs) < 1) {
                 throw new Exception($varTmp . " requires atleast one operand!");
             }
             $arrArray = array();
             $objTmp = 0;
             $intCntr = count($arrArgs);
             while (--$intCntr >= 0) {
                 $varTerm = $arrArgs[$intCntr];
                 if ($varTerm->isVariable) {
                     if (!isset($parrVars[$varTerm->val])) {
                         throw new Exception("Variable [" . $varTerm->val . "] not defined");
                     } else {
                         $varTerm = $parrVars[$varTerm->val];
                     }
                 }
                 $arrArray = array_merge($arrArray, MyZebra_Tokenizer::toArray($varTerm->val));
             }
             $pStack->Push(MyZebra_Tokenizer::makeToken($arrArray, MyZebra_Tokenizer::$TOKEN_TYPE['ARRAY']));
             break;
         case "TODAY":
             $pStack->Push(MyZebra_Tokenizer::makeToken(MyZebra_DateParser::currentDate(), MyZebra_Tokenizer::$TOKEN_TYPE['DATE']));
             break;
         case "ACOS":
         case "ASIN":
         case "ATAN":
             throw new Exception("Function [" . $varTmp . "] is not implemented!");
             break;
         case "ABS":
         case "CHR":
         case "COS":
         case "FIX":
         case "HEX":
         case "LOG":
         case "RAND":
         case "ROUND":
         case "SIN":
         case "SQRT":
         case "TAN":
             if ($varTmp != "RAND") {
                 if (count($arrArgs) < 1) {
                     throw new Exception($varTmp . " requires atleast one argument!");
                 } else {
                     if (count($arrArgs) > 1) {
                         throw new Exception($varTmp . " requires only one argument!");
                     }
                 }
             } else {
                 if (count($arrArgs) < 1) {
                     throw new Exception($varTmp . " requires atleast one argument!");
                 } else {
                     if (count($arrArgs) > 2) {
                         throw new Exception($varTmp . " requires at most two arguments!");
                     }
                 }
             }
             $varTerm = $arrArgs[0];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm->val];
                 }
             }
             $objTmp = $varTerm->val;
             $rand_min = $rand_max = 0;
             if (!$varTerm->isNumber) {
                 throw new Exception($varTmp . " operates on numeric operands only!");
             } else {
                 $objTmp = MyZebra_Tokenizer::toNumber($varTerm->val);
                 if ($varTmp == "RAND") {
                     $rand_max = floor($objTmp);
                     if (count($arrArgs) == 2) {
                         $varTerm = $arrArgs[1];
                         if ($varTerm->isVariable) {
                             if (!isset($parrVars[$varTerm->val])) {
                                 throw new Exception("Variable [" . $varTerm->val . "] not defined");
                             } else {
                                 $varTerm = $parrVars[$varTerm->val];
                             }
                         }
                         if (!$varTerm->isNumber) {
                             throw new Exception($varTmp . " operates on numeric operands only!");
                         }
                         $objTmp = MyZebra_Tokenizer::toNumber($varTerm->val);
                         $rand_min = floor($objTmp);
                     }
                 }
             }
             if ($varTmp == "ABS") {
                 $pStack->Push(MyZebra_Tokenizer::makeToken(abs($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
             } else {
                 if ($varTmp == "CHR") {
                     $pStack->Push(MyZebra_Tokenizer::makeToken(chr($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
                 } else {
                     if ($varTmp == "COS") {
                         $pStack->Push(MyZebra_Tokenizer::makeToken(cos($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                     } else {
                         if ($varTmp == "FIX") {
                             $pStack->Push(MyZebra_Tokenizer::makeToken(floor($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                         } else {
                             if ($varTmp == "HEX") {
                                 $pStack->Push(MyZebra_Tokenizer::makeToken(dechex($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
                             } else {
                                 if ($varTmp == "LOG") {
                                     $pStack->Push(MyZebra_Tokenizer::makeToken(log($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                                 } else {
                                     if ($varTmp == "RAND") {
                                         $pStack->Push(MyZebra_Tokenizer::makeToken(mt_rand($rand_min, $rand_max), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                                     } else {
                                         if ($varTmp == "ROUND") {
                                             $pStack->Push(MyZebra_Tokenizer::makeToken(round($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                                         } else {
                                             if ($varTmp == "SIN") {
                                                 $pStack->Push(MyZebra_Tokenizer::makeToken(sin($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                                             } else {
                                                 if ($varTmp == "SQRT") {
                                                     $pStack->Push(MyZebra_Tokenizer::makeToken(sqrt($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                                                 } else {
                                                     if ($varTmp == "TAN") {
                                                         $pStack->Push(MyZebra_Tokenizer::makeToken(tan($objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             break;
         case "STR":
             if (count($arrArgs) < 1) {
                 throw new Exception($varTmp . " requires atleast one argument!");
             } else {
                 if (count($arrArgs) > 2) {
                     throw new Exception($varTmp . " requires at most two arguments!");
                 }
             }
             $varTerm = $arrArgs[count($arrArgs) - 1];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm->val];
                 }
             }
             // if date, output formated date string
             if ($varTerm->isDate) {
                 $format = '';
                 if (count($arrArgs) == 2) {
                     $varFormat = $arrArgs[0];
                     if ($varFormat->isVariable) {
                         if (!isset($parrVars[$varFormat->val])) {
                             throw new Exception("Variable [" . $varFormat->val . "] not defined");
                         } else {
                             $varFormat = $parrVars[$varFormat->val];
                         }
                     }
                     if (!$varFormat->isStringLiteral) {
                         throw new Exception("format argument for " . $varTmp . " must be a string!");
                     }
                     $format = $varFormat->val;
                 }
                 $pStack->Push(MyZebra_Tokenizer::makeToken(MyZebra_DateParser::formatDate($varTerm->val, $format), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
             } else {
                 // just convert to string
                 $pStack->Push(MyZebra_Tokenizer::makeToken((string) $varTerm->val . '', MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
             }
             break;
         case "ASC":
             if (count($arrArgs) > 1) {
                 throw new Exception($varTmp . " requires only one argument!");
             } else {
                 if (count($arrArgs) < 1) {
                     throw new Exception($varTmp . " requires atleast one argument!");
                 }
             }
             $varTerm = $arrArgs[0];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm]->val;
                 }
             }
             if (!$varTerm->isStringLiteral) {
                 throw new Exception($varTmp . " requires a string type operand!");
             } else {
                 $pStack->Push(MyZebra_Tokenizer::makeToken(ord($varTerm->val[0]), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
             }
             break;
         case "REGEX":
             if (count($arrArgs) < 1) {
                 throw new Exception($varTmp . " requires atleast one argument!");
             } else {
                 if (count($arrArgs) > 2) {
                     throw new Exception($varTmp . " requires at most two arguments!");
                 }
             }
             $varTerm = $arrArgs[count($arrArgs) - 1];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm->val];
                 }
             }
             if (!$varTerm->isStringLiteral) {
                 throw new Exception($varTmp . " operates on string type operands!");
             }
             $opts = MyZebra_Tokenizer::$EMPTY_STRING;
             if (count($arrArgs) == 2) {
                 $opts = $arrArgs[0];
                 if ($opts->isVariable) {
                     if (!isset($parrVars[$opts->val])) {
                         throw new Exception("Variable [" . $opts->val . "] not defined");
                     } else {
                         $opts = $parrVars[$opts->val];
                     }
                 }
                 if (!$opts->isStringLiteral) {
                     throw new Exception($varTmp . " operates on string type operands!");
                 }
             }
             $pStack->Push(MyZebra_Tokenizer::makeToken(MyZebra_Functions::Regex($varTerm->val, $opts->val), MyZebra_Tokenizer::$TOKEN_TYPE['REGEX']));
             break;
         case "LCASE":
         case "UCASE":
         case "NUM":
             if (count($arrArgs) < 1) {
                 throw new Exception($varTmp . " requires atleast one argument!");
             } else {
                 if (count($arrArgs) > 1) {
                     throw new Exception($varTmp . " requires only one argument!");
                 }
             }
             $varTerm = $arrArgs[0];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm->val];
                 }
             }
             if (!$varTerm->isStringLiteral && $varTmp != "NUM") {
                 throw new Exception($varTmp . " requires a string type operand!");
             } else {
                 if ($varTmp == "LCASE") {
                     $pStack->Push(MyZebra_Tokenizer::makeToken(strtolower($varTerm->val), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
                 } else {
                     if ($varTmp == "UCASE") {
                         $pStack->Push(MyZebra_Tokenizer::makeToken(strtoupper($varTerm->val), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
                     } else {
                         if ($varTmp == "NUM") {
                             $objTmp = MyZebra_Tokenizer::toNumber($varTerm->val) + 0.0;
                             if (is_nan($objTmp)) {
                                 throw new Exception($varTmp . " cannot convert [" . $varTerm->val . "] to number!");
                             }
                             $pStack->Push(MyZebra_Tokenizer::makeToken($objTmp, MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                         }
                     }
                 }
             }
             break;
         case "LEN":
             if (count($arrArgs) < 1) {
                 throw new Exception($varTmp . " requires atleast one argument!");
             } else {
                 if (count($arrArgs) > 1) {
                     throw new Exception($varTmp . " requires only one argument!");
                 }
             }
             $varTerm = $arrArgs[0];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm->val];
                 }
             }
             if (!$varTerm->isArray && !$varTerm->isStringLiteral) {
                 throw new Exception($varTmp . " requires a string or array type operand!");
             } else {
                 if ($varTerm->isStringLiteral) {
                     $pStack->Push(MyZebra_Tokenizer::makeToken(strlen($varTerm->val), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                 } else {
                     $pStack->Push(MyZebra_Tokenizer::makeToken(count($varTerm->val), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                 }
             }
             break;
         case "USER":
             if (count($arrArgs) < 1) {
                 throw new Exception($varTmp . " requires atleast one argument!");
             } else {
                 if (count($arrArgs) > 1) {
                     throw new Exception($varTmp . " requires only one argument!");
                 }
             }
             $varTerm = $arrArgs[0];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm->val];
                 }
             }
             if (!$varTerm->isStringLiteral) {
                 throw new Exception($varTmp . " requires a string type operand!");
             } else {
                 $pStack->Push(MyZebra_Tokenizer::makeToken(MyZebra_Functions::User($varTerm->val), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
             }
             break;
         case "COOKIE":
             if (count($arrArgs) < 1) {
                 throw new Exception($varTmp . " requires atleast one argument!");
             } else {
                 if (count($arrArgs) > 1) {
                     throw new Exception($varTmp . " requires only one argument!");
                 }
             }
             $varTerm = $arrArgs[0];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm->val];
                 }
             }
             if (!$varTerm->isStringLiteral) {
                 throw new Exception($varTmp . " requires a string type operand!");
             } else {
                 $pStack->Push(MyZebra_Tokenizer::makeToken(MyZebra_Functions::Cookie($varTerm->val), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
             }
             break;
         case "CONTAINS":
             if (count($arrArgs) < 2) {
                 throw new Exception($varTmp . " requires atleast two arguments!");
             } else {
                 if (count($arrArgs) > 2) {
                     throw new Exception($varTmp . " requires only two arguments!");
                 }
             }
             $varTerm = $arrArgs[1];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm->val];
                 }
             }
             $varTerm2 = $arrArgs[0];
             if ($varTerm2->isVariable) {
                 if (!isset($parrVars[$varTerm2->val])) {
                     throw new Exception("Variable [" . $varTerm2->val . "] not defined");
                 } else {
                     $varTerm2 = $parrVars[$varTerm2->val];
                 }
             }
             if (!$varTerm->isArray) {
                 throw new Exception($varTmp . " requires an array as first argument!");
             } else {
                 $found = MyZebra_Functions::Contains($varTerm->val, $varTerm2->val);
                 //in_array($varTerm2->val,$varTerm->val);
                 $pStack->Push(MyZebra_Tokenizer::makeToken($found, MyZebra_Tokenizer::$TOKEN_TYPE['BOOLEAN']));
             }
             break;
         case "DATE":
             if (count($arrArgs) < 2) {
                 throw new Exception($varTmp . " requires atleast two arguments!");
             } else {
                 if (count($arrArgs) > 2) {
                     throw new Exception($varTmp . " requires only two arguments!");
                 }
             }
             $varTerm = $arrArgs[1];
             if ($varTerm->isVariable) {
                 if (!isset($parrVars[$varTerm->val])) {
                     throw new Exception("Variable [" . $varTerm->val . "] not defined");
                 } else {
                     $varTerm = $parrVars[$varTerm->val];
                 }
             }
             $varFormat = $arrArgs[0];
             if ($varFormat->isVariable) {
                 if (!isset($parrVars[$varFormat->val])) {
                     throw new Exception("Variable [" . $varFormat->val . "] not defined");
                 } else {
                     $varFormat = $parrVars[$varFormat->val];
                 }
             }
             $dateobj = array();
             if (!$varTerm->isStringLiteral || !$varFormat->isStringLiteral) {
                 throw new Exception($varTmp . " requires string type operands!");
             } else {
                 if (!MyZebra_Tokenizer::isDate($varTerm->val, $varFormat->val, $dateobj)) {
                     throw new Exception($varTmp . " can not convert [" . $varTerm->val . "] to a valid date with format [" . $varFormat->val . "]!");
                 } else {
                     if (isset($dateobj['date'])) {
                         $pStack->Push(MyZebra_Tokenizer::makeToken($dateobj['date'], MyZebra_Tokenizer::$TOKEN_TYPE['DATE']));
                     } else {
                         throw new Exception($varTmp . " unknown error");
                     }
                 }
             }
             break;
         case "LEFT":
         case "RIGHT":
             if (count($arrArgs) < 2) {
                 throw new Exception($varTmp . " requires atleast two arguments!");
             } else {
                 if (count($arrArgs) > 2) {
                     throw new Exception($varTmp . " requires only two arguments!");
                 }
             }
             for ($intCntr = 0; $intCntr < count($arrArgs); $intCntr++) {
                 $varTerm = $arrArgs[$intCntr];
                 if ($varTerm->isVariable) {
                     if (!isset($parrVars[$varTerm->val])) {
                         throw new Exception("Variable [" . $varTerm->val . "] not defined");
                     } else {
                         $varTerm = $parrVars[$varTerm->val];
                     }
                 }
                 if ($intCntr == 0 && !$varTerm->isNumber) {
                     throw new Exception($varTmp . " operator requires numeric length!");
                 } else {
                     if ($intCntr == 1 && !$varTerm->isStringLiteral) {
                         throw new Exception($varTmp . " operator requires a string operand!");
                     }
                 }
                 $arrArgs[$intCntr] = $varTerm;
             }
             $varTerm = $arrArgs[1]->val;
             $objTmp = MyZebra_Tokenizer::toNumber($arrArgs[0]->val);
             if ($varTmp == "LEFT") {
                 $pStack->Push(MyZebra_Tokenizer::makeToken(substr($varTerm, 0, $objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
             } else {
                 $pStack->Push(MyZebra_Tokenizer::makeToken(substr($varTerm, strlen($varTerm) - $objTmp, $objTmp), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
             }
             break;
         case "MID":
         case "IIF":
             if (count($arrArgs) < 3) {
                 throw new Exception($varTmp . " requires atleast three arguments!");
             } else {
                 if (count($arrArgs) > 3) {
                     throw new Exception($varTmp . " requires only three arguments!");
                 }
             }
             for ($intCntr = 0; $intCntr < count($arrArgs); $intCntr++) {
                 $varTerm = $arrArgs[$intCntr];
                 if ($varTerm->isVariable) {
                     if (!isset($parrVars[$varTerm->val])) {
                         throw new Exception("Variable [" . $varTerm->val . "] not defined");
                     } else {
                         $varTerm = $parrVars[$varTerm->val];
                     }
                 }
                 if ($varTmp == "MID" && $intCntr <= 1 && !$varTerm->isNumber) {
                     throw new Exception($varTmp . " operator requires numeric lengths!");
                 } else {
                     if ($varTmp == "MID" && $intCntr == 2 && !$varTerm->isStringLiteral) {
                         throw new Exception($varTmp . " operator requires a string input!");
                     } else {
                         if ($varTmp == "IIF" && $intCntr == 2 && !$varTerm->isBoolean && !$varTerm->isNumber) {
                             throw new Exception($varTmp . " operator requires boolean condition!");
                         }
                     }
                 }
                 $arrArgs[$intCntr] = $varTerm;
             }
             if ($varTmp == "MID") {
                 $varTerm = $arrArgs[2]->val;
                 $objOp1 = MyZebra_Tokenizer::toNumber($arrArgs[1]->val);
                 $objOp2 = MyZebra_Tokenizer::toNumber($arrArgs[0]->val);
                 $pStack->Push(MyZebra_Tokenizer::makeToken(substr($varTerm, $objOp1, $objOp2 - $objOp1), MyZebra_Tokenizer::$TOKEN_TYPE['STRING_LITERAL']));
             } else {
                 $varTerm = MyZebra_Tokenizer::toBoolean($arrArgs[2]->val);
                 if ($varTerm) {
                     $objOp1 = $arrArgs[1];
                 } else {
                     $objOp1 = $arrArgs[0];
                 }
                 $pStack->Push($objOp1);
             }
             break;
         case "AVG":
         case "MAX":
         case "MIN":
             if (count($arrArgs) < 1) {
                 throw new Exception($varTmp . " requires atleast one operand!");
             }
             $_arr = array();
             $intCntr = count($arrArgs);
             while (--$intCntr >= 0) {
                 $varTerm = $arrArgs[$intCntr];
                 if ($varTerm->isVariable) {
                     if (!isset($parrVars[$varTerm->val])) {
                         throw new Exception("Variable [" . $varTerm->val . "] not defined");
                     } else {
                         $varTerm = $parrVars[$varTerm->val];
                     }
                 }
                 if (!$varTerm->isNumber && !$varTerm->isArray) {
                     throw new Exception($varTmp . " requires numeric or array operands only!");
                 }
                 if (!$varTerm->isArray) {
                     $_arr = array_merge($_arr, MyZebra_Tokenizer::toArray(MyZebra_Tokenizer::toNumber($varTerm->val)));
                 } else {
                     $_arr = array_merge($_arr, $varTerm->val);
                 }
             }
             $intCntr = -1;
             $objTmp = 0;
             while (++$intCntr < count($_arr)) {
                 $varTerm = $_arr[$intCntr];
                 if ($varTmp == "AVG") {
                     $objTmp += $varTerm;
                 } else {
                     if ($varTmp == "MAX") {
                         if ($intCntr == 0) {
                             $objTmp = $varTerm;
                         } else {
                             if ($objTmp < $varTerm) {
                                 $objTmp = $varTerm;
                             }
                         }
                     } else {
                         if ($varTmp == "MIN") {
                             if ($intCntr == 0) {
                                 $objTmp = $varTerm;
                             } else {
                                 if ($objTmp > $varTerm) {
                                     $objTmp = $varTerm;
                                 }
                             }
                         }
                     }
                 }
             }
             if ($varTmp == "AVG" && !empty($_arr)) {
                 $pStack->Push(MyZebra_Tokenizer::makeToken($objTmp / count($_arr), MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
             } else {
                 if ($varTmp == "AVG") {
                     $pStack->Push(MyZebra_Tokenizer::makeToken(0, MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                 } else {
                     $pStack->Push(MyZebra_Tokenizer::makeToken($objTmp, MyZebra_Tokenizer::$TOKEN_TYPE['NUMBER']));
                 }
             }
             unset($_arr);
             break;
     }
 }
Ejemplo n.º 2
0
 private function translate_field($name, &$field, $additional_options = array())
 {
     // extend additional_options with defaults
     extract(array_merge(array('preset_value' => null, 'placeholder' => null, 'value_escape' => false, 'make_readonly' => false, 'is_tax' => false, 'max_width' => null, 'max_height' => null, 'single_select' => false), $additional_options));
     // add the "name" element
     // the "&" symbol is there so that $obj will be a reference to the object in PHP 4
     // for PHP 5+ there is no need for it
     $type = 'text';
     $attributes = array();
     $value = '';
     $name_orig = $name;
     if (!$is_tax) {
         if ($placeholder && $placeholder !== null && !empty($placeholder) && is_string($placeholder)) {
             // use translated value by WPML if exists
             $placeholder = cred_translate('Value: ' . $placeholder, $placeholder, 'cred-form-' . $this->_form->form->post_title . '-' . $this->_form->form->ID);
         }
         if ($preset_value && $preset_value !== null && is_string($preset_value) && !empty($preset_value)) {
             // use translated value by WPML if exists
             $data_value = cred_translate('Value: ' . $preset_value, $preset_value, 'cred-form-' . $this->_form->form->post_title . '-' . $this->_form->form->ID);
         } elseif ($this->_post_data && isset($this->_post_data['fields'][$name_orig])) {
             $data_value = $this->_post_data['fields'][$name_orig][0];
         } else {
             $data_value = null;
         }
         $value = '';
         // save a map between options / actual values for these types to be used later
         if ($field['type'] == 'checkboxes' || $field['type'] == 'radio' || $field['type'] == 'select' || $field['type'] == 'multiselect') {
             $tmp = array();
             foreach ($field['data']['options'] as $optionKey => $optionData) {
                 if ($optionKey != 'default' && is_array($optionData)) {
                     $tmp[$optionKey] = $field['type'] == 'checkboxes' ? $optionData['set_value'] : $optionData['value'];
                 }
             }
             $this->_field_values_map[$field['slug']] = $tmp;
             unset($tmp);
             unset($optionKey);
             unset($optionData);
         }
         switch ($field['type']) {
             case 'form_messages':
                 $type = 'messages';
                 break;
             case 'recaptcha':
                 $type = 'recaptcha';
                 $value = '';
                 $attributes = array('error_message' => $this->getLocalisedMessage('enter_valid_captcha'), 'show_link' => $this->getLocalisedMessage('show_captcha'), 'no_keys' => __('Enter your ReCaptcha keys at the CRED Settings page in order for ReCaptcha API to work', 'wp-cred'));
                 if (self::$recaptcha_settings !== false) {
                     $attributes['public_key'] = self::$recaptcha_settings['public_key'];
                     $attributes['private_key'] = self::$recaptcha_settings['private_key'];
                 }
                 if ($this->_form_count == 1) {
                     $attributes['open'] = true;
                 }
                 // used to load additional js script
                 $this->hasRecaptcha = true;
                 break;
             case 'file':
                 $type = 'file';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 break;
             case 'image':
                 $type = 'file';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 // show previous post featured image thumbnail
                 if ('_featured_image' == $name) {
                     $value = '';
                     if (isset($this->_post_data['extra']['featured_img_html'])) {
                         $attributes['display_featured_html'] = $this->_post_data['extra']['featured_img_html'];
                     }
                 }
                 break;
             case 'date':
                 $type = 'date';
                 $format = '';
                 if (isset($field['data']) && isset($field['data']['validate'])) {
                     $format = $field['data']['validate']['date']['format'];
                 }
                 if (!in_array($format, $this->supported_date_formats)) {
                     $format = 'F j, Y';
                 }
                 $attributes['format'] = $format;
                 $attributes['readonly_element'] = false;
                 //cred_log('Date time '.$data_value);
                 if ($data_value !== null && !empty($data_value) && (is_numeric($data_value) || is_int($data_value) || is_long($data_value))) {
                     MyZebra_DateParser::setDateLocaleStrings(self::$localized_strings['days'], self::$localized_strings['months']);
                     //cred_log('Days '.print_r(self::$localized_strings['days'],true));
                     //$value = date($format,$data_value);
                     // format localized date form timestamp
                     $value = MyZebra_DateParser::formatDate($data_value, $format, true);
                     //cred_log('Date value '.$value);
                 }
                 break;
             case 'select':
                 $type = 'select';
                 $value = array();
                 $attributes = array();
                 $attributes['options'] = array();
                 $default = array();
                 foreach ($field['data']['options'] as $key => $option) {
                     $index = $key;
                     //$option['value'];
                     if ($key == 'default') {
                         $default[] = $option;
                     } else {
                         $attributes['options'][$index] = $option['title'];
                         if ($data_value !== null && $data_value == $option['value']) {
                             $value[] = $key;
                         }
                         if (isset($option['dummy']) && $option['dummy']) {
                             $attributes['dummy'] = $key;
                         }
                     }
                 }
                 if (empty($value) && !empty($default)) {
                     $value = $default;
                 }
                 if (isset($this->_field_values_map[$field['slug']])) {
                     $attributes['actual_options'] = $this->_field_values_map[$field['slug']];
                 }
                 break;
             case 'multiselect':
                 $type = 'select';
                 $value = array();
                 $attributes = array();
                 $attributes['options'] = array();
                 $attributes['multiple'] = 'multiple';
                 $default = array();
                 foreach ($field['data']['options'] as $key => $option) {
                     $index = $key;
                     //$option['value'];
                     if ($key == 'default') {
                         $default = (array) $option;
                     } else {
                         $attributes['options'][$index] = $option['title'];
                         if ($data_value !== null && $data_value == $option['value']) {
                             $value[] = $key;
                         }
                         if (isset($option['dummy']) && $option['dummy']) {
                             $attributes['dummy'] = $key;
                         }
                     }
                 }
                 if (empty($value) && !empty($default)) {
                     $value = $default;
                 }
                 if (isset($this->_field_values_map[$field['slug']])) {
                     $attributes['actual_options'] = $this->_field_values_map[$field['slug']];
                 }
                 break;
             case 'radio':
                 $type = 'radios';
                 $value = array();
                 $attributes = '';
                 $default = '';
                 foreach ($field['data']['options'] as $key => $option) {
                     $index = $key;
                     //$option['display_value'];
                     if ($key == 'default') {
                         $default = $option;
                     } else {
                         $value[$index] = $option['title'];
                         if ($data_value !== null && $data_value == $option['value']) {
                             $attributes = $key;
                         }
                     }
                 }
                 if ($data_value === null && !empty($default)) {
                     $attributes = $default;
                 }
                 $def = $attributes;
                 $attributes = array('default' => $def);
                 if (isset($this->_field_values_map[$field['slug']])) {
                     $attributes['actual_values'] = $this->_field_values_map[$field['slug']];
                 }
                 break;
             case 'checkboxes':
                 $type = 'checkboxes';
                 $name .= '[]';
                 $value = array();
                 $attributes = array();
                 if (is_array($data_value)) {
                     $data_value = array_keys($data_value);
                 } elseif ($data_value !== null) {
                     $data_value = array($data_value);
                 }
                 foreach ($field['data']['options'] as $key => $option) {
                     $index = $key;
                     $value[$index] = $option['title'];
                     if (isset($option['checked']) && $option['checked'] && $data_value === null) {
                         $attributes[] = $index;
                     } elseif ($data_value !== null && in_array($index, $data_value)) {
                         $attributes[] = $index;
                     }
                 }
                 $def = $attributes;
                 $attributes = array('default' => $def);
                 if (isset($this->_field_values_map[$field['slug']])) {
                     $attributes['actual_values'] = $this->_field_values_map[$field['slug']];
                 }
                 //print_r($attributes);
                 //print_r($value);
                 break;
             case 'checkbox':
                 $type = 'checkbox';
                 $value = $field['data']['set_value'];
                 if ($data_value !== null && $data_value == $value) {
                     $attributes = array('checked' => 'checked');
                 }
                 break;
             case 'textarea':
                 $type = 'textarea';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 break;
             case 'wysiwyg':
                 $type = 'wysiwyg';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 $attributes = array('disable_xss_filters' => true);
                 if ($name == 'post_content' && isset($this->_form->fields['form_settings']->has_media_button) && $this->_form->fields['form_settings']->has_media_button) {
                     $attributes['has_media_button'] = true;
                 }
                 break;
             case 'form_submit':
                 $type = 'submit';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 break;
             case 'numeric':
                 $type = 'text';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 break;
             case 'phone':
                 $type = 'text';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 break;
             case 'url':
                 $type = 'text';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 break;
             case 'email':
                 $type = 'text';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 break;
             case 'textfield':
                 $type = 'text';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 if ($placeholder && null !== $placeholder && !empty($placeholder)) {
                     $attributes['placeholder'] = $placeholder;
                 }
                 break;
             case 'password':
                 $type = 'password';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 if ($placeholder && null !== $placeholder && !empty($placeholder)) {
                     $attributes['placeholder'] = $placeholder;
                 }
                 break;
             case 'hidden':
                 $type = 'hidden';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 break;
             case 'skype':
                 $type = 'skype';
                 if ($data_value !== null && is_string($data_value)) {
                     $data_value = array('skypename' => $data_value, 'style' => '');
                 }
                 if ($data_value !== null) {
                     $value = $data_value;
                 } else {
                     $value = array('skypename' => '', 'style' => '');
                 }
                 $attributes = array('ajax_url' => admin_url('admin-ajax.php'), 'edit_skype_text' => $this->getLocalisedMessage('edit_skype_button'), 'value' => $data_value, '_nonce' => wp_create_nonce('insert_skype_button'));
                 break;
             default:
                 $type = 'text';
                 if ($data_value !== null) {
                     $value = $data_value;
                 }
                 break;
         }
         if ($make_readonly) {
             if (!is_array($attributes)) {
                 $attributes = array();
             }
             $attributes['readonly'] = 'readonly';
         }
         // no extra escaping, just default framework XSS filter
         /*if ($value_escape)
           {
               if (!is_array($attributes))
                   $attributes=array();
               $attributes['escape']=true;
           }*/
         // repetitive field (special care)
         if (isset($field['data']['repetitive']) && $field['data']['repetitive'] == 1) {
             $name .= '[]';
             $objs =& $this->_myzebra_form->add_repeatable($type, $name, $value, $attributes);
             if (isset($this->_post_data['fields'][$name_orig]) && count($this->_post_data['fields'][$name_orig]) > 1) {
                 for ($ii = 1; $ii < count($this->_post_data['fields'][$name_orig]) && count($this->_post_data['fields'][$name_orig]); $ii++) {
                     $data_value = $this->_post_data['fields'][$name_orig][$ii];
                     $atts = array();
                     switch ($type) {
                         case 'skype':
                             $atts = array('value' => $data_value);
                             break;
                         case 'date':
                             $format = '';
                             if (isset($field['data']) && isset($field['data']['validate'])) {
                                 $format = $field['data']['validate']['date']['format'];
                             }
                             if (!in_array($format, $this->supported_date_formats)) {
                                 $format = 'F j, Y';
                             }
                             $atts['format'] = $format;
                             $atts['readonly_element'] = false;
                             if (!empty($data_value)) {
                                 MyZebra_DateParser::setDateLocaleStrings(self::$localized_strings['days'], self::$localized_strings['months']);
                                 //$atts['value'] = date($format,$data_value);
                                 // format localized date form timestamp
                                 $atts['value'] = MyZebra_DateParser::formatDate($data_value, $format, true);
                                 //cred_log('Date value '.$atts['value']);
                             }
                             break;
                         case 'file':
                             $atts['value'] = $data_value;
                             break;
                         case 'text':
                             $atts['value'] = $data_value;
                             break;
                         case 'wysiwyg':
                         case 'textarea':
                             $atts['value'] = $data_value;
                             break;
                         case 'checkbox':
                             $value = $field['data']['set_value'];
                             if ($data_value == $value) {
                                 $atts = array('checked' => 'checked');
                             }
                             break;
                         case 'select':
                             $value = array();
                             foreach ($field['data']['options'] as $key => $option) {
                                 $index = $option['value'];
                                 //$option['set_value'];
                                 if ($key == 'default' && $data_value == '') {
                                     $value[] = $field['data']['options'][$option]['value'];
                                 } elseif ($data_value != '') {
                                     $value[] = $data_value;
                                 }
                             }
                             $atts['value'] = $value;
                             break;
                         default:
                             $atts['value'] = $data_value;
                             break;
                     }
                     $objs->addControl($atts);
                 }
             }
         } else {
             $objs =& $this->_myzebra_form->add($type, $name, $value, $attributes);
         }
         if (!is_array($objs)) {
             $oob = array($objs);
         } else {
             $oob = $objs;
         }
         $ids = array();
         // add validation rules if needed
         foreach ($oob as &$obj) {
             $obj->setPrimeName($name_orig);
             if ('hidden' == $type) {
                 $obj->attributes['user_defined'] = true;
             }
             // field belongs to a container?
             if ($this->_current_group !== null) {
                 $this->_current_group->addControl($obj);
                 //$obj->setParent($this->_current_group);
             }
             $atts = $obj->get_attributes(array('id', 'type'));
             $ids[] = $atts['id'];
             if ($atts['type'] == 'label') {
                 continue;
             }
             switch ($type) {
                 case 'file':
                     $upload = wp_upload_dir();
                     // set rules
                     $obj->set_rule(array('upload' => array($upload['path'], $upload['url'], true, 'error', $this->getLocalisedMessage('upload_failed'))));
                     $obj->set_attributes(array('external_upload' => true));
                     // we will handle actual upload
                     if ($field['type'] == 'image') {
                         // set rules
                         $obj->set_rule(array('image' => array('error', $this->getLocalisedMessage('not_valid_image'))));
                     } else {
                         // if general file upload, restrict to Wordpress allowed file types
                         $obj->set_rule(array('filetype' => array($this->wp_mimes, 'error', $this->getLocalisedMessage('file_type_not_allowed'))));
                     }
                     if (null !== $max_width && is_numeric($max_width)) {
                         $max_width = intval($max_width);
                         $obj->set_rule(array('image_max_width' => array($max_width, sprintf($this->getLocalisedMessage('image_width_larger'), $max_width))));
                     }
                     if (null !== $max_height && is_numeric($max_height)) {
                         $max_height = intval($max_height);
                         $obj->set_rule(array('image_max_height' => array($max_height, sprintf($this->getLocalisedMessage('image_height_larger'), $max_height))));
                     }
                     break;
             }
             if (isset($field['data']) && isset($field['data']['validate'])) {
                 foreach ($field['data']['validate'] as $method => $validation) {
                     if ($validation['active'] == 1) {
                         switch ($method) {
                             case 'required':
                                 // set rules
                                 $obj->set_rule(array('required' => array('error', $this->getLocalisedMessage('field_required'))));
                                 break;
                             case 'hidden':
                                 // set rules
                                 $obj->set_rule(array('hidden' => array('error', $this->getLocalisedMessage('values_do_not_match'))));
                                 // default attribute to check against submitted value
                                 $obj->set_attributes(array('default' => $obj->attributes['value']));
                                 break;
                             case 'date':
                                 // set rules
                                 $obj->set_rule(array('date' => array('error', $this->getLocalisedMessage('enter_valid_date'))));
                                 break;
                             case 'email':
                                 // set rules
                                 $obj->set_rule(array('email' => array('error', $this->getLocalisedMessage('enter_valid_email'))));
                                 break;
                             case 'number':
                                 // set rules
                                 $obj->set_rule(array('number' => array('', 'error', $this->getLocalisedMessage('enter_valid_number'))));
                                 break;
                             case 'image':
                             case 'file':
                                 break;
                             case 'url':
                                 // set rules
                                 $obj->set_rule(array('url' => array('error', $this->getLocalisedMessage('enter_valid_url'))));
                                 break;
                         }
                     }
                 }
             }
         }
     } else {
         if (!array_key_exists('master_taxonomy', $field)) {
             if ($field['hierarchical']) {
                 if (in_array($preset_value, array('checkbox', 'select'))) {
                     $tax_display = $preset_value;
                 } else {
                     $tax_display = 'checkbox';
                 }
             }
             if ($this->_post_data && isset($this->_post_data['taxonomies'][$name_orig])) {
                 if (!$field['hierarchical']) {
                     $data_value = array('terms' => $this->_post_data['taxonomies'][$name_orig]['terms'], 'add_text' => $this->getLocalisedMessage('add_taxonomy'), 'remove_text' => $this->getLocalisedMessage('remove_taxonomy'), 'ajax_url' => admin_url('admin-ajax.php'), 'auto_suggest' => true);
                 } else {
                     $data_value = array('terms' => $this->_post_data['taxonomies'][$name_orig]['terms'], 'all' => $field['all'], 'type' => $tax_display, 'single_select' => $single_select);
                 }
             } else {
                 if (!$field['hierarchical']) {
                     $data_value = array('add_text' => $this->getLocalisedMessage('add_taxonomy'), 'remove_text' => $this->getLocalisedMessage('remove_taxonomy'), 'ajax_url' => admin_url('admin-ajax.php'), 'auto_suggest' => true);
                 } else {
                     $data_value = array('all' => $field['all'], 'type' => $tax_display, 'single_select' => $single_select);
                 }
             }
             // if not hierarchical taxonomy
             if (!$field['hierarchical']) {
                 $objs =& $this->_myzebra_form->add('taxonomy', $name, $value, $data_value);
             } else {
                 $objs =& $this->_myzebra_form->add('taxonomyhierarchical', $name, $value, $data_value);
             }
             // register this taxonomy field for later use by auxilliary taxonomy fields
             $this->_taxonomy_aux['taxonomy'][$name_orig] =& $objs;
             // if a taxonomy auxiliary field exists attached to this taxonomy, add this taxonomy id to it
             if (isset($this->_taxonomy_aux['aux'][$name_orig])) {
                 $this->_taxonomy_aux['aux'][$name_orig]->set_attributes(array('master_taxonomy_id' => $objs->attributes['id']));
             }
             if (!is_array($objs)) {
                 $oob = array($objs);
             } else {
                 $oob = $objs;
             }
             $ids = array();
             foreach ($oob as &$obj) {
                 $obj->setPrimeName($name_orig);
                 // field belongs to a container?
                 if ($this->_current_group !== null) {
                     $this->_current_group->addControl($obj);
                     //$obj->setParent($this->_current_group);
                 }
                 $atts = $obj->get_attributes(array('id', 'type'));
                 $ids[] = $atts['id'];
             }
         } else {
             if ($preset_value && $preset_value !== null) {
                 // use translated value by WPML if exists
                 $data_value = cred_translate('Value: ' . $preset_value, $preset_value, 'cred-form-' . $this->_form->form->post_title . '-' . $this->_form->form->ID);
             } else {
                 $data_value = null;
             }
             $ids = array();
             if (in_array($field['type'], array('show_popular', 'add_new'))) {
                 if ($field['type'] == 'show_popular') {
                     $objs =& $this->_myzebra_form->add('taxonomypopular', $name, $value, array('popular' => $field['taxonomy']['most_popular'], 'show_popular_text' => $this->getLocalisedMessage('show_popular'), 'hide_popular_text' => $this->getLocalisedMessage('hide_popular')));
                 } elseif ($field['type'] == 'add_new') {
                     $objs =& $this->_myzebra_form->add('taxonomyhierarchicaladdnew', $name, $value, array('add_new_text' => $this->getLocalisedMessage('add_new_taxonomy'), 'add_text' => $this->getLocalisedMessage('add_taxonomy'), 'parent_text' => __('-- Parent --', 'wp-cred')));
                 }
                 // register this taxonomy auxilliary field for later use by taxonomy fields
                 $this->_taxonomy_aux['aux'][$field['master_taxonomy']] =& $objs;
                 // if a taxonomy field exists that this field is attached, link to its id here
                 if (isset($this->_taxonomy_aux['taxonomy'][$field['master_taxonomy']])) {
                     $objs->set_attributes(array('master_taxonomy_id' => $this->_taxonomy_aux['taxonomy'][$field['master_taxonomy']]->attributes['id']));
                 }
                 if (!is_array($objs)) {
                     $oob = array($objs);
                 } else {
                     $oob = $objs;
                 }
                 foreach ($oob as &$obj) {
                     $atts = $obj->get_attributes(array('id', 'type'));
                     $ids[] = $atts['id'];
                 }
             }
         }
     }
     return $ids;
     // return the ids of the created fields
 }