- <?php
- /* vim: set number autoindent tabstop=4 shiftwidth=4 softtabstop=4: */
-
- /**
- * This package add live-text type to HTML_QuickForm
- *
- * A live-text is an HTML input text that intercept key-typing
- * to perform an ajax request and shows results.
- *
- * PHP versions 4 and 5
- *
- * LICENSE: This source file is subject to version 3.0 of the PHP license
- * that is available through the world-wide-web at the following URI:
- * http://www.php.net/license/3_0.txt. If you did not receive a copy of
- * the PHP License and are unable to obtain it through the web, please
- * send a note to license@php.net so we can mail you a copy immediately.
- *
- * @category HTML
- * @package HTML_QuickForm_LiveText
- * @author Fabio Ambrosanio <fabio@ambrosanio.com>
- * @license http://www.php.net/license/3_01.txt PHP
- * @version @package_version@
- *
- * $Id: livetext.php,v 1.7 2007/04/11 16:17:07 fabamb Exp $
- */
- require_once 'HTML/QuickForm/text.php';
-
- /**
- * This class represents a live-text element of HTML_QuickForm framework.
- *
- * @author Fabio Ambrosanio <fabio@ambrosanio.com>
- * @category HTML
- * @package HTML_QuickForm_LiveText
- * @version @package_version@
- * @license http://www.php.net/license/3_01.txt PHP
- */
- class HTML_QuickForm_LiveText extends HTML_QuickForm_text
- {
- // {{{ properties
-
-
-
- /**
- * Filename of css stylesheet
- *
- * @access private
- * @var string
- */
- var $_css = 'livetext.css';
-
- /**
- * Path of css stylesheet
- *
- * @access private
- * @var string
- */
- var $_css_path = false;
-
- /**
- * Filenames javascript libraries
- *
- * @access private
- * @var string
- */
- var $_js = array('livetext.js');
-
- /**
- * Options for the LiveText element
- * You can modify behaviors of element
- *
- * @var array
- * @access private
- */
- var $_options = array(
- 'server' => 'auto_server.php', // name of the AJAX server
-
- 'stub' => 'LiveText', // AJAX stub/class provided by the server
-
- 'method' => 'search', // method of the stub that perform the search
-
- 'minSearch' => 3, // minimum chars in input text befor start the search
-
- 'resultClass' => 'LiveTextResult', // CSS class of result container
-
- 'optionClass' => 'LiveTextOption', // CSS class of result single option
-
- 'showAsTable' => false, // if true results are showed in a table
-
- 'showHeaders' => true, // if true result keys are shows atop the table
- // (if showAsTable is true)
-
- 'keys' => false, // shows only specified keys
-
-
-
- );
-
- // }}}
-
- // {{{ constructor
-
-
-
- /**
- * Class constructor
- *
- * @param string $elementName (required)Input field name attribute.
- * @param string $elementLabel (required)Input field label in form.
- * @param array $options (optional)An associative array which elements specify different
- * aspects and behaviors of the searchnox.
- * @param mixed $attributes (optional)Either a typical HTML attribute string
- * or an associative array. Date format is passed along the attributes.
- * @access public
- * @return HTML_QuickForm_LiveText
- */
- function HTML_QuickForm_LiveText($elementName = null, $elementLabel = null, $options = null, $attributes = null)
- {
- $this->HTML_QuickForm_text($elementName, $elementLabel, $attributes);
- $this->_persistantFreeze = true;
- $id = $this->getAttribute('id');
- if ("$id" == '') {
- $this->updateAttributes(array(
- 'id' => $elementName
- ));
- }
-
- // autocomplete is off by default
- if (!isset($attributes['autocomplete'])) {
- $this->updateAttributes(array(
- 'autocomplete' => 'off'
- ));
- }
-
- // update element options
- if ($options && is_array($options)) {
- $this->_options = array_merge($this->_options, $options);
- }
- } //end constructor
-
- // }}}
-
- // {{{ toHtml()
-
-
-
- /**
- * Returns Html for the LiveText input element
- *
- * @access public
- * @return string the HTML string representing the searchbox
- */
- function toHtml()
- {
- if ($this->_flagFrozen) {
- // input froze: return parent html
- $html = parent::toHtml();
- } else {
- // write out style classes and javascript functions
- $html = $this->_getCSS();
- $html .= $this->_getJS();
-
- $id = $this->getAttribute('id');
- $minSearch = $this->_options['minSearch'];
-
- // build input text field
- $this->updateAttributes(array(
- 'onKeyUp' => "LTKeyPress('$id', event, $minSearch)",
- 'onblur' => "LTSearchHide('$id');"
- ));
- $input = parent::toHtml();
-
- // build result container
- $resultClass = $this->_options['resultClass'];
- $html .= "<div id='$id.result' class='$resultClass'></div>" . $input;
- }
-
- return $html;
- }// end func toHtml
-
- // }}}
-
- // {{{ setCSS($filename, $path)
-
-
-
- /**
- * Sets new CSS file
- *
- * @param string $filename filename of CSS file
- * @param string $path path of CSS file
- * @param string $options associative array which elements specify CSS classes for different aspects cf the searchbox
- */
- function setCSS($filename, $path = false, $options = false)
- {
- $this->_css = $filename;
- $this->_css_path = $path;
- if ($options && is_array($options)) {
- $this->_options = array_merge($this->_options, $options);
- }
- }// end func setCSS
-
- // }}}
-
-
-
- /**
- * Returns CSS styles
- *
- * @return string
- * @access private
- */
- function _getCSS()
- {
- if (defined('HTML_QUICKFORM_LIVETEXT_CSS_' . $this->_css_path . $this->_css)) return '';
- define('HTML_QUICKFORM_LIVETEXT_CSS_' . $this->_css_path . $this->_css, true);
-
- $location = $this->_getFileLocation($this->_css, $this->_css_path);
- $css = "<style type=\"text/css\">";
- $css .= "/* ";
- if ($this->_css_path) $css .= $this->_css_path . DIRECTORY_SEPARATOR;
- $css .= $this->_css;
- $css .= " */";
- $css .= file_get_contents($location);
- $css .= "</style>";
- return $css;
- }
-
- /**
- * Returns javascript functions
- *
- * @return string
- * @access private
- */
- function _getJS()
- {
- $id = $this->getAttribute('id');
- $server = $this->_options['server'];
- $stub = $this->_options['stub'];
- $method = $this->_options['method'];
-
- // create common javascript
- if (!defined('HTML_QUICKFORM_LIVETEXT_JS')) {
- define('HTML_QUICKFORM_LIVETEXT_JS', true);
-
- $js .= "<script src='$server?client=all'></script>";
-
- foreach($this->_js as $filename) {
- $location = $this->_getFileLocation($filename, false);
- $js .= "<script type='text/javascript'>" . file_get_contents($location) . "</script>";
- }
- }
-
- // create javascript for import stubs
- $stub_js_sentinel = "HTML_QUICKFORM_LIVETEXT_" . $server ."_" . $stub . "_JS";
- if (!defined($stub_js_sentinel)) {
- define($stub_js_sentinel, true);
- $js .= "<script src='$server?stub=all'></script>";
- }
-
- // create javascript to handle this element
- $optionClass = $this->_options['optionClass'];
- $js .= <<<JS
- <script>
- LTCallbacks['$id'] = {
- $method: function(result) {
- //alert(HTML_AJAX_Util.quickPrint(result));
- LTBuildResult('$id', result);
- }
- };
-
- LTHelpers['$id'] = new $stub(LTCallbacks['$id']);
- LTHelpers['$id'].dispatcher.queue = 'ordered';
- LTSearchers['$id'] = function(what) {
- LTHelpers['$id'].$method(what);
- };
-
- LTOptionClasses['$id'] = '$optionClass';
- JS;
-
- // if this element need a map between ids and result keys
- // add it to global structure
- if (isset($this->_options['map'])) {
- $js .= "LTMaps['$id'] = [];\n";
- foreach($this->_options['map'] as $k => $v) {
- $js .= "LTMaps['$id']['$k'] = '$v';\n";
- }
- }
-
- if (isset($this->_options['showAsTable']) && $this->_options['showAsTable']) {
- $js .= "LTShowAsTable['$id'] = true;\n";
- if (isset($this->_options['showHeaders']) && $this->_options['showHeaders']) {
- $js .= "LTShowHeaders['$id'] = true;\n";
- }
- }
-
- if (isset($this->_options['keys']) && is_array($this->_options['keys'])) {
- $keys = array();
- foreach($this->_options['keys'] as $key) {
- $keys[] = "'$key'";
- }
- $js .= "LTShowKeys['$id'] = [" . join(', ', $keys) . "];\n";
- }
-
- $js .= "</script>";
- return $js;
- }
-
-
- /**
- * Calcs the canonical path of the specified file
- *
- * @param string $filename
- * @param string $path
- * @return string
- */
- function _getFileLocation($filename, $path)
- {
- if ($path) {
- $location = realpath($path . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
- } else {
- $path = '@data-dir@'.DIRECTORY_SEPARATOR.'HTML_QuickForm_LiveText'.DIRECTORY_SEPARATOR;
- if(strpos($path, '@'.'data-dir@') === 0) {
- $path = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
- }
- $location = $path.$filename;
- }
- return $location;
- }
-
- } // end class HTML_QuickForm_LiveText
-
- if (class_exists('HTML_QuickForm')) {
- HTML_QuickForm::registerElementType('livetext', 'HTML/QuickForm/LiveText.php', 'HTML_QuickForm_LiveText');
- }
- ?>