cvs: pear /HTML_Javascript Javascript.php package.xml /HTML_Javascript/Javascript Convert.php
| From: | Tal Peer | Date: | Mon, 22 Jul 2002 14:32:12 +0000 |
| Subject: | cvs: pear /HTML_Javascript Javascript.php package.xml /HTML_Javascript/Javascript Convert.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-5281@lists.php.net to get a copy of this message | ||
tal Mon Jul 22 10:32:12 2002 EDT
Added files:
/pear/HTML_Javascript/Javascript Convert.php
Modified files:
/pear/HTML_Javascript Javascript.php package.xml
Log:
Banana Split :-)
Index: pear/HTML_Javascript/Javascript.php diff -u pear/HTML_Javascript/Javascript.php:1.6 pear/HTML_Javascript/Javascript.php:1.7 --- pear/HTML_Javascript/Javascript.php:1.6 Mon Jul 22 08:55:40 2002 +++ pear/HTML_Javascript/Javascript.php Mon Jul 22 10:32:12 2002 @@ -33,40 +33,31 @@ */ define('HTML_JAVASCRIPT_ERROR_NOEND', 501, true); -/** -* Invalid variable error -* -* @const HTML_JAVASCRIPT_ERROR_INVVAR -*/ -define('HTML_JAVASCRIPT_ERROR_INVVAR', 502, true); + require_once('PEAR.php'); /** -* A class for converting PHP variables into Javascript variables +* A class for performing basic JavaScript operations * * Usage example: * * echo "<html><head><title>lala</title></head><body>"; * $js = new HTML_Javascript(); * echo $js->startScript(); -* echo $js->convertString("Hello,\n My name is tal peer", 'foo', true); -* echo $js->writeLine('foo',true); -* $a = array('foo','bar','foobarism',1,98); -* echo $js->convertArray($a, 'bar', true); +* echo $js->writeLine('foo',false); * echo $js->writeLine('bar[0]', true); * echo $js->writeLine('bar[3]', true); * echo $js->endScript(); * echo "</body></html>"; * * TODO: -* - Add support for multidimensional arrays in convertArray() * - Add more JS operations (prompt, functions, interaction with forms) -* - Cleanup the API +* - API cleanups... * * @author Tal Peer <tal@php.net> -* @package HTML -* @version 0.9-dev +* @package HTML_Javascript +* @version 0.9 * @access public */ class HTML_Javascript extends PEAR @@ -89,7 +80,6 @@ $this->PEAR(); } - /** * Used to end the script (</script>) * @@ -105,89 +95,7 @@ return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } } - - /** - * Used to terminate escape characters in strings, as javascript doesn't allow them - * - * @param string $str the string to be processed - * @return mixed the processed string - * @access public - */ - function escapeString($str) - { - $js_escape = array( - "\r" => '\r', - "\n" => '\n', - "\t" => '\t', - "'" => "\\'", - '\\' => '\\\\' - ); - - return strtr($str,$js_escape); - } - - /** - * Converts a PHP string into a JS string - * - * @access public - * @param string $str the string to convert - * @param string $varname the variable name to declare - * @param boolean $global if true, the JS var will be global - * @return mixed a PEAR_Error if no script was started or the converted string - */ - function convertString($str, $varname, $global = false) - { - if (!$this->_started) { - return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); - } - - $var = ''; - $str = $this->escapeString($str); - if($global) { - $var = 'var '; - } - - $var .= $varname.' = "'.$str.'"'; - return $var."\n"; - } - - /** - * Converts a PHP variable into a JS variable - * Note: you can safely provide strings, arrays or booleans as arguments for this function - * - * @access public - * @param mixed $var the variable to convert - * @param string $varname the variable name to declare - * @param boolean $global if true, the JS var will be global - * @return mixed a PEAR_Error if no script was started or the converted variable - */ - function convertVar($var, $varname, $global = false) - { - if (!$this->_started) { - return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); - } - - if (!(is_string($var) OR is_array($var) OR is_bool($var))) { - $ret = ''; - if ($global) { - $ret = 'var '; - } - - $ret .= $varname.' = '.$var; - return $ret."\n"; - } else { - if (is_array($var)) { - return $this->convertArray($var, $varname, $global); - } elseif (is_string($var)) { - return $this->convertString($var, $varname, $global); - } elseif (is_bool($var)) { - return $this->convertBoolean($var, $varname, $global); - } else { - $this->raiseError(HTML_JAVASCRIPT_ERROR_INVVAR); - } - } - } - + /** * A custom error handler * @@ -198,7 +106,7 @@ function raiseError($code) { $ret = null; - + switch ($code) { case HTML_JAVASCRIPT_ERROR_NOSTART: $ret = PEAR::raiseError('No script started', HTML_JAVASCRIPT_ERROR_NOSTART); @@ -206,47 +114,15 @@ case HTML_JAVASCRIPT_ERROR_NOEND: $ret = PEAR::raiseError('Last script was not ended', HTML_JAVASCRIPT_ERROR_NOEND); break; - case HTML_JAVASCRIPT_ERROR_INVVAR: - $ret = PEAR::raiseError('Invalid variable', HTML_JAVASCRIPT_ERROR_INVVAR); - break; default: $ret = false; break; } - + return $ret; } /** - * Converts a PHP boolean variable into a JS boolean variable - * - * @access public - * @param boolean $bool the boolean variable - * @param string $varname the variable name to declare - * @param boolean $global set to true to make the JS variable global - * @return mixed a PEAR_Error on error or a string with the declaration - */ - function convertBoolean($bool, $varname, $global = false) - { - if (!$this->_started) { - return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); - } - - $var = ''; - if($global) { - $var = 'var '; - } - - $var .= $varname.' = '; - if ($bool) { - $var .= 'true'; - } else { - $var .= 'false'; - } - return $var."\n"; - } - - /** * Starts a new script * * @access public @@ -259,48 +135,9 @@ } else { $this->_started = true; return "<script language=\"javascript\">\n"; - } - } - - /** - * Converts a PHP array into a JS array - * Note: support for multi-dimensional arrays is not yet implemented, DO NOT use them - * - * @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 - * @return mixed a PEAR_Error if no script was started or the converted array - */ - function convertArray($arr, $varname, $global = false) - { - if (!$this->_started) { - return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); - } - - $var = ''; - if ($global) { - $var = 'var '; - } - - $var .= $varname.' = Array('; - foreach ($arr as $key => $cell) { - if ($key != 0) { - $var .= ','; - } - if (is_string($cell)) { - $cell = $this->escapeString($cell); - - $var .= '"'.$cell.'"'; - } else { - $var .= $cell; - } - } - $var .= ')'; - return $var."\n"; } - + /** * A wrapper for document.writeln * @@ -321,7 +158,7 @@ return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } } - + /** * A wrapper for document.writeln with an addtional <br /> tag * @@ -342,7 +179,7 @@ return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } } - + /** * A wrapper for alert * @@ -358,6 +195,27 @@ return 'alert('.$str.')'."\n"; } else { return 'alert("'.$str.'")'."\n"; + } + } else { + return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); + } + } + + /** + * Opens a propmt (input box) + * + * @param string $str the string that will appear in the prompt + * @param string $assign the JS var that the input will be assigned to + * @param string $var wether $str is a JS var or not + * @return mixed PEAR_Error or the processed string + */ + function prompt($str, $assign, $var = false) + { + if ($this->_started) { + if ($var) { + return $assign.'prompt('.$str.')'."\n"; + } else { + return $assign.'("'.$str.'")'."\n"; } } else { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); Index: pear/HTML_Javascript/package.xml diff -u pear/HTML_Javascript/package.xml:1.2 pear/HTML_Javascript/package.xml:1.3 --- pear/HTML_Javascript/package.xml:1.2 Tue Jul 16 14:52:39 2002 +++ pear/HTML_Javascript/package.xml Mon Jul 22 10:32:12 2002 @@ -21,8 +21,8 @@ </maintainers> <release> - <version>0.9</version> - <date>2002-07-16</date> + <version>0.9.1</version> + <date>2002-07-22</date> <state>beta</state> <license>PHP 2.02</license> <notes> @@ -31,6 +31,9 @@ <filelist> <dir name="/" baseinstalldir="HTML"> <file role="php">Javascript.php</file> + </dir> + <dir name="Javascript" baseinstalldir="HTML"> + <file role="php">Convert.php</file> </dir> </filelist> </release> Index: pear/HTML_Javascript/Javascript/Convert.php +++ pear/HTML_Javascript/Javascript/Convert.php <?php /** * Invalid variable error * * @const HTML_JAVASCRIPT_ERROR_INVVAR */ define('HTML_JAVASCRIPT_ERROR_INVVAR', 502, true); require_once('PEAR.php'); /** * A class for converting PHP variables into JavaScript variables * * Usage example: * * $js = new HTML_Javascript_Convert() * $a = array('foo','bar','buz',1,2,3); * $b = $js->convertArray($a, 'arr', true); * * TODO: * -Add support for multidimensional arrays in convertArray() * * @author Tal Peer <tal@php.net> * @package HTML_Javascript * @version 0.9 * @access public */ class HTML_Javascript_Convert extends PEAR { /** * Constructor - creates a new HTML_Javascript_Convert object * */ function HTML_Javscript_Convert() { $this->PEAR(); } /** * Used to terminate escape characters in strings, as javascript doesn't allow them * * @param string $str the string to be processed * @return mixed the processed string * @access public */ function escapeString($str) { $js_escape = array( "\r" => '\r', "\n" => '\n', "\t" => '\t', "'" => "\\'", '\\' => '\\\\' ); return strtr($str,$js_escape); } /** * Converts a PHP string into a JS string * * @access public * @param string $str the string to convert * @param string $varname the variable name to declare * @param boolean $global if true, the JS var will be global * @return mixed a PEAR_Error if no script was started or the converted string */ function convertString($str, $varname, $global = false) { if (!$this->_started) { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } $var = ''; $str = $this->escapeString($str); if($global) { $var = 'var '; } $var .= $varname.' = "'.$str.'"'; return $var."\n"; } /** * Converts a PHP variable into a JS variable * Note: you can safely provide strings, arrays or booleans as arguments for this function * * @access public * @param mixed $var the variable to convert * @param string $varname the variable name to declare * @param boolean $global if true, the JS var will be global * @return mixed a PEAR_Error if no script was started or the converted variable */ function convertVar($var, $varname, $global = false) { if (!$this->_started) { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } if (!(is_string($var) OR is_array($var) OR is_bool($var))) { $ret = ''; if ($global) { $ret = 'var '; } $ret .= $varname.' = '.$var; return $ret."\n"; } else { if (is_array($var)) { return $this->convertArray($var, $varname, $global); } elseif (is_string($var)) { return $this->convertString($var, $varname, $global); } elseif (is_bool($var)) { return $this->convertBoolean($var, $varname, $global); } else { $this->raiseError(HTML_JAVASCRIPT_ERROR_INVVAR); } } } /** * A custom error handler * * @access public * @param integer $code the error code * @return mixed false if the error code is invalid, or a PEAR_Error otherwise */ function raiseError($code) { $ret = null; switch ($code) { case HTML_JAVASCRIPT_ERROR_INVVAR: $ret = PEAR::raiseError('Invalid variable', HTML_JAVASCRIPT_ERROR_INVVAR); break; default: $ret = false; break; } return $ret; } /** * Converts a PHP boolean variable into a JS boolean variable * * @access public * @param boolean $bool the boolean variable * @param string $varname the variable name to declare * @param boolean $global set to true to make the JS variable global * @return mixed a PEAR_Error on error or a string with the declaration */ function convertBoolean($bool, $varname, $global = false) { if (!$this->_started) { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } $var = ''; if($global) { $var = 'var '; } $var .= $varname.' = '; if ($bool) { $var .= 'true'; } else { $var .= 'false'; } return $var."\n"; } /** * Converts a PHP array into a JS array * Note: support for multi-dimensional arrays is not yet implemented, DO NOT use them * * @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 * @return mixed a PEAR_Error if no script was started or the converted array */ function convertArray($arr, $varname, $global = false) { if (!$this->_started) { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } $var = ''; if ($global) { $var = 'var '; } $var .= $varname.' = Array('; foreach ($arr as $key => $cell) { if ($key != 0) { $var .= ','; } if (is_string($cell)) { $cell = $this->escapeString($cell); $var .= '"'.$cell.'"'; } else { $var .= $cell; } } $var .= ')'; return $var."\n"; } }
Index: pear/HTML_Javascript/Javascript.php diff -u pear/HTML_Javascript/Javascript.php:1.6 pear/HTML_Javascript/Javascript.php:1.7 --- pear/HTML_Javascript/Javascript.php:1.6 Mon Jul 22 08:55:40 2002 +++ pear/HTML_Javascript/Javascript.php Mon Jul 22 10:32:12 2002 @@ -33,40 +33,31 @@ */ define('HTML_JAVASCRIPT_ERROR_NOEND', 501, true); -/** -* Invalid variable error -* -* @const HTML_JAVASCRIPT_ERROR_INVVAR -*/ -define('HTML_JAVASCRIPT_ERROR_INVVAR', 502, true); + require_once('PEAR.php'); /** -* A class for converting PHP variables into Javascript variables +* A class for performing basic JavaScript operations * * Usage example: * * echo "<html><head><title>lala</title></head><body>"; * $js = new HTML_Javascript(); * echo $js->startScript(); -* echo $js->convertString("Hello,\n My name is tal peer", 'foo', true); -* echo $js->writeLine('foo',true); -* $a = array('foo','bar','foobarism',1,98); -* echo $js->convertArray($a, 'bar', true); +* echo $js->writeLine('foo',false); * echo $js->writeLine('bar[0]', true); * echo $js->writeLine('bar[3]', true); * echo $js->endScript(); * echo "</body></html>"; * * TODO: -* - Add support for multidimensional arrays in convertArray() * - Add more JS operations (prompt, functions, interaction with forms) -* - Cleanup the API +* - API cleanups... * * @author Tal Peer <tal@php.net> -* @package HTML -* @version 0.9-dev +* @package HTML_Javascript +* @version 0.9 * @access public */ class HTML_Javascript extends PEAR @@ -89,7 +80,6 @@ $this->PEAR(); } - /** * Used to end the script (</script>) * @@ -105,89 +95,7 @@ return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } } - - /** - * Used to terminate escape characters in strings, as javascript doesn't allow them - * - * @param string $str the string to be processed - * @return mixed the processed string - * @access public - */ - function escapeString($str) - { - $js_escape = array( - "\r" => '\r', - "\n" => '\n', - "\t" => '\t', - "'" => "\\'", - '\\' => '\\\\' - ); - - return strtr($str,$js_escape); - } - - /** - * Converts a PHP string into a JS string - * - * @access public - * @param string $str the string to convert - * @param string $varname the variable name to declare - * @param boolean $global if true, the JS var will be global - * @return mixed a PEAR_Error if no script was started or the converted string - */ - function convertString($str, $varname, $global = false) - { - if (!$this->_started) { - return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); - } - - $var = ''; - $str = $this->escapeString($str); - if($global) { - $var = 'var '; - } - - $var .= $varname.' = "'.$str.'"'; - return $var."\n"; - } - - /** - * Converts a PHP variable into a JS variable - * Note: you can safely provide strings, arrays or booleans as arguments for this function - * - * @access public - * @param mixed $var the variable to convert - * @param string $varname the variable name to declare - * @param boolean $global if true, the JS var will be global - * @return mixed a PEAR_Error if no script was started or the converted variable - */ - function convertVar($var, $varname, $global = false) - { - if (!$this->_started) { - return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); - } - - if (!(is_string($var) OR is_array($var) OR is_bool($var))) { - $ret = ''; - if ($global) { - $ret = 'var '; - } - - $ret .= $varname.' = '.$var; - return $ret."\n"; - } else { - if (is_array($var)) { - return $this->convertArray($var, $varname, $global); - } elseif (is_string($var)) { - return $this->convertString($var, $varname, $global); - } elseif (is_bool($var)) { - return $this->convertBoolean($var, $varname, $global); - } else { - $this->raiseError(HTML_JAVASCRIPT_ERROR_INVVAR); - } - } - } - + /** * A custom error handler * @@ -198,7 +106,7 @@ function raiseError($code) { $ret = null; - + switch ($code) { case HTML_JAVASCRIPT_ERROR_NOSTART: $ret = PEAR::raiseError('No script started', HTML_JAVASCRIPT_ERROR_NOSTART); @@ -206,47 +114,15 @@ case HTML_JAVASCRIPT_ERROR_NOEND: $ret = PEAR::raiseError('Last script was not ended', HTML_JAVASCRIPT_ERROR_NOEND); break; - case HTML_JAVASCRIPT_ERROR_INVVAR: - $ret = PEAR::raiseError('Invalid variable', HTML_JAVASCRIPT_ERROR_INVVAR); - break; default: $ret = false; break; } - + return $ret; } /** - * Converts a PHP boolean variable into a JS boolean variable - * - * @access public - * @param boolean $bool the boolean variable - * @param string $varname the variable name to declare - * @param boolean $global set to true to make the JS variable global - * @return mixed a PEAR_Error on error or a string with the declaration - */ - function convertBoolean($bool, $varname, $global = false) - { - if (!$this->_started) { - return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); - } - - $var = ''; - if($global) { - $var = 'var '; - } - - $var .= $varname.' = '; - if ($bool) { - $var .= 'true'; - } else { - $var .= 'false'; - } - return $var."\n"; - } - - /** * Starts a new script * * @access public @@ -259,48 +135,9 @@ } else { $this->_started = true; return "<script language=\"javascript\">\n"; - } - } - - /** - * Converts a PHP array into a JS array - * Note: support for multi-dimensional arrays is not yet implemented, DO NOT use them - * - * @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 - * @return mixed a PEAR_Error if no script was started or the converted array - */ - function convertArray($arr, $varname, $global = false) - { - if (!$this->_started) { - return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); - } - - $var = ''; - if ($global) { - $var = 'var '; - } - - $var .= $varname.' = Array('; - foreach ($arr as $key => $cell) { - if ($key != 0) { - $var .= ','; - } - if (is_string($cell)) { - $cell = $this->escapeString($cell); - - $var .= '"'.$cell.'"'; - } else { - $var .= $cell; - } - } - $var .= ')'; - return $var."\n"; } - + /** * A wrapper for document.writeln * @@ -321,7 +158,7 @@ return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } } - + /** * A wrapper for document.writeln with an addtional <br /> tag * @@ -342,7 +179,7 @@ return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } } - + /** * A wrapper for alert * @@ -358,6 +195,27 @@ return 'alert('.$str.')'."\n"; } else { return 'alert("'.$str.'")'."\n"; + } + } else { + return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); + } + } + + /** + * Opens a propmt (input box) + * + * @param string $str the string that will appear in the prompt + * @param string $assign the JS var that the input will be assigned to + * @param string $var wether $str is a JS var or not + * @return mixed PEAR_Error or the processed string + */ + function prompt($str, $assign, $var = false) + { + if ($this->_started) { + if ($var) { + return $assign.'prompt('.$str.')'."\n"; + } else { + return $assign.'("'.$str.'")'."\n"; } } else { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); Index: pear/HTML_Javascript/package.xml diff -u pear/HTML_Javascript/package.xml:1.2 pear/HTML_Javascript/package.xml:1.3 --- pear/HTML_Javascript/package.xml:1.2 Tue Jul 16 14:52:39 2002 +++ pear/HTML_Javascript/package.xml Mon Jul 22 10:32:12 2002 @@ -21,8 +21,8 @@ </maintainers> <release> - <version>0.9</version> - <date>2002-07-16</date> + <version>0.9.1</version> + <date>2002-07-22</date> <state>beta</state> <license>PHP 2.02</license> <notes> @@ -31,6 +31,9 @@ <filelist> <dir name="/" baseinstalldir="HTML"> <file role="php">Javascript.php</file> + </dir> + <dir name="Javascript" baseinstalldir="HTML"> + <file role="php">Convert.php</file> </dir> </filelist> </release> Index: pear/HTML_Javascript/Javascript/Convert.php +++ pear/HTML_Javascript/Javascript/Convert.php <?php /** * Invalid variable error * * @const HTML_JAVASCRIPT_ERROR_INVVAR */ define('HTML_JAVASCRIPT_ERROR_INVVAR', 502, true); require_once('PEAR.php'); /** * A class for converting PHP variables into JavaScript variables * * Usage example: * * $js = new HTML_Javascript_Convert() * $a = array('foo','bar','buz',1,2,3); * $b = $js->convertArray($a, 'arr', true); * * TODO: * -Add support for multidimensional arrays in convertArray() * * @author Tal Peer <tal@php.net> * @package HTML_Javascript * @version 0.9 * @access public */ class HTML_Javascript_Convert extends PEAR { /** * Constructor - creates a new HTML_Javascript_Convert object * */ function HTML_Javscript_Convert() { $this->PEAR(); } /** * Used to terminate escape characters in strings, as javascript doesn't allow them * * @param string $str the string to be processed * @return mixed the processed string * @access public */ function escapeString($str) { $js_escape = array( "\r" => '\r', "\n" => '\n', "\t" => '\t', "'" => "\\'", '\\' => '\\\\' ); return strtr($str,$js_escape); } /** * Converts a PHP string into a JS string * * @access public * @param string $str the string to convert * @param string $varname the variable name to declare * @param boolean $global if true, the JS var will be global * @return mixed a PEAR_Error if no script was started or the converted string */ function convertString($str, $varname, $global = false) { if (!$this->_started) { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } $var = ''; $str = $this->escapeString($str); if($global) { $var = 'var '; } $var .= $varname.' = "'.$str.'"'; return $var."\n"; } /** * Converts a PHP variable into a JS variable * Note: you can safely provide strings, arrays or booleans as arguments for this function * * @access public * @param mixed $var the variable to convert * @param string $varname the variable name to declare * @param boolean $global if true, the JS var will be global * @return mixed a PEAR_Error if no script was started or the converted variable */ function convertVar($var, $varname, $global = false) { if (!$this->_started) { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } if (!(is_string($var) OR is_array($var) OR is_bool($var))) { $ret = ''; if ($global) { $ret = 'var '; } $ret .= $varname.' = '.$var; return $ret."\n"; } else { if (is_array($var)) { return $this->convertArray($var, $varname, $global); } elseif (is_string($var)) { return $this->convertString($var, $varname, $global); } elseif (is_bool($var)) { return $this->convertBoolean($var, $varname, $global); } else { $this->raiseError(HTML_JAVASCRIPT_ERROR_INVVAR); } } } /** * A custom error handler * * @access public * @param integer $code the error code * @return mixed false if the error code is invalid, or a PEAR_Error otherwise */ function raiseError($code) { $ret = null; switch ($code) { case HTML_JAVASCRIPT_ERROR_INVVAR: $ret = PEAR::raiseError('Invalid variable', HTML_JAVASCRIPT_ERROR_INVVAR); break; default: $ret = false; break; } return $ret; } /** * Converts a PHP boolean variable into a JS boolean variable * * @access public * @param boolean $bool the boolean variable * @param string $varname the variable name to declare * @param boolean $global set to true to make the JS variable global * @return mixed a PEAR_Error on error or a string with the declaration */ function convertBoolean($bool, $varname, $global = false) { if (!$this->_started) { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } $var = ''; if($global) { $var = 'var '; } $var .= $varname.' = '; if ($bool) { $var .= 'true'; } else { $var .= 'false'; } return $var."\n"; } /** * Converts a PHP array into a JS array * Note: support for multi-dimensional arrays is not yet implemented, DO NOT use them * * @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 * @return mixed a PEAR_Error if no script was started or the converted array */ function convertArray($arr, $varname, $global = false) { if (!$this->_started) { return $this->raiseError(HTML_JAVASCRIPT_ERROR_NOSTART); } $var = ''; if ($global) { $var = 'var '; } $var .= $varname.' = Array('; foreach ($arr as $key => $cell) { if ($key != 0) { $var .= ','; } if (is_string($cell)) { $cell = $this->escapeString($cell); $var .= '"'.$cell.'"'; } else { $var .= $cell; } } $var .= ')'; return $var."\n"; } }