, * Bertrand Mansion * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The names of the authors may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category HTML * @package HTML_QuickForm2 * @author Alexey Borzov * @author Bertrand Mansion * @license http://opensource.org/licenses/bsd-license.php New BSD License * @link http://pear.php.net/package/HTML_QuickForm2 */ /** * Exception classes for HTML_QuickForm2 */ require_once 'HTML/QuickForm2/Exception.php'; /** * Javascript aggregator and builder class * * @category HTML * @package HTML_QuickForm2 * @author Alexey Borzov * @author Bertrand Mansion * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version Release: 2.0.2 * @link http://pear.php.net/package/HTML_QuickForm2 */ class HTML_QuickForm2_JavascriptBuilder { /** * Client-side rules * @var array */ protected $rules = array(); /** * Elements' setup code * @var array */ protected $scripts = array(); /** * Whether to generate a validator object for the form if no rules are present * * Needed when the form contains an empty repeat element * * @var array */ protected $forceValidator = array(); /** * Javascript libraries * @var array */ protected $libraries = array( 'base' => array('file' => 'quickform.js') ); /** * Default web path to JS library files * @var string */ protected $defaultWebPath; /** * Default filesystem path to JS library files * @var string */ protected $defaultAbsPath; /** * Current form ID * @var string */ protected $formId = null; /** * Constructor, sets default web path to JS library files and default filesystem path * * @param string $defaultWebPath default web path to JS library files * (to use in " : $path . $library['file']; } } return ($inline && $addScriptTags) ? $this->wrapScript($ret) : $ret; } /** * Sets ID of the form currently being processed * * All subsequent calls to addRule() and addElementJavascript() will store * the scripts for that form * * @param string $formId */ public function setFormId($formId) { $this->formId = $formId; $this->rules[$this->formId] = array(); $this->scripts[$this->formId] = array(); $this->forceValidator[$this->formId] = false; } /** * Adds the Rule javascript to the list of current form Rules * * @param HTML_QuickForm2_Rule $rule Rule instance * @param bool $triggers Whether rule code should contain * "triggers" for live validation */ public function addRule(HTML_QuickForm2_Rule $rule, $triggers = false) { $this->rules[$this->formId][] = $rule->getJavascript($triggers); } /** * Adds element's setup code to form's Javascript * * @param string $script */ public function addElementJavascript($script) { $this->scripts[$this->formId][] = $script; } /** * Enables generating a validator for the current form even if no rules are present */ public function forceValidator() { $this->forceValidator[$this->formId] = true; } /** * Returns per-form javascript (client-side validation and elements' setup) * * @param string $formId form ID, if empty returns code for all forms * @param boolean $addScriptTags whether to enclose code in tags, * empty string if $js is empty */ protected function wrapScript($js) { if ('' != $js) { $js = ""; } return $js; } /** * Encodes a value for use as Javascript literal * * NB: unlike json_encode() we do not enforce UTF-8 charset here * * @param mixed $value * * @return string value as Javascript literal */ public static function encode($value) { if (is_null($value)) { return 'null'; } elseif (is_bool($value)) { return $value? 'true': 'false'; } elseif (is_int($value) || is_float($value)) { return $value; } elseif (is_string($value)) { return '"' . strtr($value, array( "\r" => '\r', "\n" => '\n', "\t" => '\t', "'" => "\\'", '"' => '\"', '\\' => '\\\\' )) . '"'; } elseif (is_array($value)) { // associative array, encoding as JS object if (count($value) && array_keys($value) !== range(0, count($value) - 1)) { return '{' . implode(',', array_map( array('HTML_QuickForm2_JavascriptBuilder', 'encodeNameValue'), array_keys($value), array_values($value) )) . '}'; } return '[' . implode(',', array_map( array('HTML_QuickForm2_JavascriptBuilder', 'encode'), $value )) . ']'; } elseif (is_object($value)) { $vars = get_object_vars($value); return '{' . implode(',', array_map( array('HTML_QuickForm2_JavascriptBuilder', 'encodeNameValue'), array_keys($vars), array_values($vars) )) . '}'; } else { throw new HTML_QuickForm2_InvalidArgumentException( 'Cannot encode ' . gettype($value) . ' as Javascript value' ); } } /** * Callback for array_map used to generate name-value pairs * * @param mixed $name * @param mixed $value * * @return string */ protected static function encodeNameValue($name, $value) { return self::encode((string)$name) . ':' . self::encode($value); } } ?>