Exemplo n.º 1
0
 /**
  * Converts  a PHP array into a JS array
  * supports of multu-dimensional array.
  * Keeps keys as they are (associative arrays).
  *
  * @access public
  * @param  string  $arr     the array to convert
  * @param  string  $varname the variable name to declare
  * @param  boolean $global  if true, the JS var will be global
  * @param  int     $level   Not public, used for recursive calls
  * @return mixed   a PEAR_Error if no script was started
  *                 or the converted array
  */
 function convertArray($arr, $varname, $global = false, $level = 0)
 {
     $var = '';
     if ($global) {
         $var = 'var ';
     }
     if (is_array($arr)) {
         $length = sizeof($arr);
         $var .= $varname . ' = Array(' . $length . ')' . HTML_JAVASCRIPT_NL;
         foreach ($arr as $key => $cell) {
             $jskey = '"' . $key . '"';
             if (is_array($cell)) {
                 $level++;
                 $var .= HTML_Javascript_Convert::convertArray($cell, 'tmp' . $level, $global, $level);
                 $var .= $varname . "[{$jskey}] = tmp{$level}" . HTML_JAVASCRIPT_NL;
                 $var .= "tmp{$level} = null" . HTML_JAVASCRIPT_NL;
             } else {
                 $value = is_string($cell) ? '"' . HTML_Javascript_Convert::escapeString($cell) . '"' : $cell;
                 $var .= $varname . "[{$jskey}] = {$value}" . HTML_JAVASCRIPT_NL;
             }
         }
         return $var;
     } else {
         return HTML_Javascript::raiseError(HTML_JAVASCRIPT_ERROR_INVVAR);
     }
 }
Exemplo n.º 2
0
 /**
  * Checks the output mode and acts according to it
  *
  * @param  string   $str the string returned from the calling function
  * @return mixed    depends on the output mode,
  *                  $str if it's HTML_JAVASCRIPT_OUTPUT_RETURN, true otherwise
  * @access private
  */
 function _out($str)
 {
     static $fp;
     if (isset($this)) {
         $mode = $this->_mode;
         $file = $this->_file;
     } else {
         return $str;
     }
     switch ($mode) {
         case HTML_JAVASCRIPT_OUTPUT_RETURN:
             return $str;
             break;
         case HTML_JAVASCRIPT_OUTPUT_ECHO:
             echo $str;
             return true;
             break;
         case HTML_JAVASCRIPT_OUTPUT_FILE:
             if ($fp = @fopen($file, 'ab')) {
                 fwrite($fp, $str);
             } else {
                 HTML_Javascript::raiseError(HTML_JAVASCRIPT_ERROR_WRITEFILE);
             }
             return true;
             break;
         default:
             HTML_Javascript::raiseError('Invalid output mode');
             break;
     }
 }