HTML_QuickForm_LiveText
[ class tree: HTML_QuickForm_LiveText ] [ index: HTML_QuickForm_LiveText ] [ all elements ]

Source for file livetext.php

Documentation is available at livetext.php

  1. <?php
  2. /* vim: set number autoindent tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5. * This package add live-text type to HTML_QuickForm
  6. *
  7. * A live-text is an HTML input text that intercept key-typing
  8. * to perform an ajax request and shows results.
  9. *
  10. * PHP versions 4 and 5
  11. *
  12. * LICENSE: This source file is subject to version 3.0 of the PHP license
  13. * that is available through the world-wide-web at the following URI:
  14. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  15. * the PHP License and are unable to obtain it through the web, please
  16. * send a note to license@php.net so we can mail you a copy immediately.
  17. *
  18. * @category HTML
  19. * @package HTML_QuickForm_LiveText
  20. * @author Fabio Ambrosanio <fabio@ambrosanio.com>
  21. * @license http://www.php.net/license/3_01.txt PHP
  22. * @version @package_version@
  23. *
  24. * $Id: livetext.php,v 1.7 2007/04/11 16:17:07 fabamb Exp $
  25. */
  26. require_once 'HTML/QuickForm/text.php';
  27.  
  28. /**
  29. * This class represents a live-text element of HTML_QuickForm framework.
  30. *
  31. * @author Fabio Ambrosanio <fabio@ambrosanio.com>
  32. * @category HTML
  33. * @package HTML_QuickForm_LiveText
  34. * @version @package_version@
  35. * @license http://www.php.net/license/3_01.txt PHP
  36. */
  37. class HTML_QuickForm_LiveText extends HTML_QuickForm_text
  38. {
  39. // {{{ properties
  40.  
  41.  
  42. /**
  43. * Filename of css stylesheet
  44. *
  45. * @access private
  46. * @var string
  47. */
  48. var $_css = 'livetext.css';
  49.  
  50. /**
  51. * Path of css stylesheet
  52. *
  53. * @access private
  54. * @var string
  55. */
  56. var $_css_path = false;
  57.  
  58. /**
  59. * Filenames javascript libraries
  60. *
  61. * @access private
  62. * @var string
  63. */
  64. var $_js = array('livetext.js');
  65.  
  66. /**
  67. * Options for the LiveText element
  68. * You can modify behaviors of element
  69. *
  70. * @var array
  71. * @access private
  72. */
  73. var $_options = array(
  74. 'server' => 'auto_server.php', // name of the AJAX server
  75. 'stub' => 'LiveText', // AJAX stub/class provided by the server
  76. 'method' => 'search', // method of the stub that perform the search
  77. 'minSearch' => 3, // minimum chars in input text befor start the search
  78. 'resultClass' => 'LiveTextResult', // CSS class of result container
  79. 'optionClass' => 'LiveTextOption', // CSS class of result single option
  80. 'showAsTable' => false, // if true results are showed in a table
  81. 'showHeaders' => true, // if true result keys are shows atop the table
  82. // (if showAsTable is true)
  83. 'keys' => false, // shows only specified keys
  84.  
  85.  
  86. );
  87.  
  88. // }}}
  89.  
  90. // {{{ constructor
  91.  
  92.  
  93. /**
  94. * Class constructor
  95. *
  96. * @param string $elementName (required)Input field name attribute.
  97. * @param string $elementLabel (required)Input field label in form.
  98. * @param array $options (optional)An associative array which elements specify different
  99. * aspects and behaviors of the searchnox.
  100. * @param mixed $attributes (optional)Either a typical HTML attribute string
  101. * or an associative array. Date format is passed along the attributes.
  102. * @access public
  103. * @return HTML_QuickForm_LiveText
  104. */
  105. function HTML_QuickForm_LiveText($elementName = null, $elementLabel = null, $options = null, $attributes = null)
  106. {
  107. $this->HTML_QuickForm_text($elementName, $elementLabel, $attributes);
  108. $this->_persistantFreeze = true;
  109. $id = $this->getAttribute('id');
  110. if ("$id" == '') {
  111. $this->updateAttributes(array(
  112. 'id' => $elementName
  113. ));
  114. }
  115.  
  116. // autocomplete is off by default
  117. if (!isset($attributes['autocomplete'])) {
  118. $this->updateAttributes(array(
  119. 'autocomplete' => 'off'
  120. ));
  121. }
  122.  
  123. // update element options
  124. if ($options && is_array($options)) {
  125. $this->_options = array_merge($this->_options, $options);
  126. }
  127. } //end constructor
  128.  
  129. // }}}
  130.  
  131. // {{{ toHtml()
  132.  
  133.  
  134. /**
  135. * Returns Html for the LiveText input element
  136. *
  137. * @access public
  138. * @return string the HTML string representing the searchbox
  139. */
  140. function toHtml()
  141. {
  142. if ($this->_flagFrozen) {
  143. // input froze: return parent html
  144. $html = parent::toHtml();
  145. } else {
  146. // write out style classes and javascript functions
  147. $html = $this->_getCSS();
  148. $html .= $this->_getJS();
  149.  
  150. $id = $this->getAttribute('id');
  151. $minSearch = $this->_options['minSearch'];
  152.  
  153. // build input text field
  154. $this->updateAttributes(array(
  155. 'onKeyUp' => "LTKeyPress('$id', event, $minSearch)",
  156. 'onblur' => "LTSearchHide('$id');"
  157. ));
  158. $input = parent::toHtml();
  159.  
  160. // build result container
  161. $resultClass = $this->_options['resultClass'];
  162. $html .= "<div id='$id.result' class='$resultClass'></div>" . $input;
  163. }
  164.  
  165. return $html;
  166. }// end func toHtml
  167.  
  168. // }}}
  169.  
  170. // {{{ setCSS($filename, $path)
  171.  
  172.  
  173. /**
  174. * Sets new CSS file
  175. *
  176. * @param string $filename filename of CSS file
  177. * @param string $path path of CSS file
  178. * @param string $options associative array which elements specify CSS classes for different aspects cf the searchbox
  179. */
  180. function setCSS($filename, $path = false, $options = false)
  181. {
  182. $this->_css = $filename;
  183. $this->_css_path = $path;
  184. if ($options && is_array($options)) {
  185. $this->_options = array_merge($this->_options, $options);
  186. }
  187. }// end func setCSS
  188.  
  189. // }}}
  190.  
  191.  
  192. /**
  193. * Returns CSS styles
  194. *
  195. * @return string
  196. * @access private
  197. */
  198. function _getCSS()
  199. {
  200. if (defined('HTML_QUICKFORM_LIVETEXT_CSS_' . $this->_css_path . $this->_css)) return '';
  201. define('HTML_QUICKFORM_LIVETEXT_CSS_' . $this->_css_path . $this->_css, true);
  202.  
  203. $location = $this->_getFileLocation($this->_css, $this->_css_path);
  204. $css = "<style type=\"text/css\">";
  205. $css .= "/* ";
  206. if ($this->_css_path) $css .= $this->_css_path . DIRECTORY_SEPARATOR;
  207. $css .= $this->_css;
  208. $css .= " */";
  209. $css .= file_get_contents($location);
  210. $css .= "</style>";
  211. return $css;
  212. }
  213.  
  214. /**
  215. * Returns javascript functions
  216. *
  217. * @return string
  218. * @access private
  219. */
  220. function _getJS()
  221. {
  222. $id = $this->getAttribute('id');
  223. $server = $this->_options['server'];
  224. $stub = $this->_options['stub'];
  225. $method = $this->_options['method'];
  226.  
  227. // create common javascript
  228. if (!defined('HTML_QUICKFORM_LIVETEXT_JS')) {
  229. define('HTML_QUICKFORM_LIVETEXT_JS', true);
  230.  
  231. $js .= "<script src='$server?client=all'></script>";
  232.  
  233. foreach($this->_js as $filename) {
  234. $location = $this->_getFileLocation($filename, false);
  235. $js .= "<script type='text/javascript'>" . file_get_contents($location) . "</script>";
  236. }
  237. }
  238.  
  239. // create javascript for import stubs
  240. $stub_js_sentinel = "HTML_QUICKFORM_LIVETEXT_" . $server ."_" . $stub . "_JS";
  241. if (!defined($stub_js_sentinel)) {
  242. define($stub_js_sentinel, true);
  243. $js .= "<script src='$server?stub=all'></script>";
  244. }
  245.  
  246. // create javascript to handle this element
  247. $optionClass = $this->_options['optionClass'];
  248. $js .= <<<JS
  249. <script>
  250. LTCallbacks['$id'] = {
  251. $method: function(result) {
  252. //alert(HTML_AJAX_Util.quickPrint(result));
  253. LTBuildResult('$id', result);
  254. }
  255. };
  256.  
  257. LTHelpers['$id'] = new $stub(LTCallbacks['$id']);
  258. LTHelpers['$id'].dispatcher.queue = 'ordered';
  259. LTSearchers['$id'] = function(what) {
  260. LTHelpers['$id'].$method(what);
  261. };
  262.  
  263. LTOptionClasses['$id'] = '$optionClass';
  264. JS;
  265.  
  266. // if this element need a map between ids and result keys
  267. // add it to global structure
  268. if (isset($this->_options['map'])) {
  269. $js .= "LTMaps['$id'] = [];\n";
  270. foreach($this->_options['map'] as $k => $v) {
  271. $js .= "LTMaps['$id']['$k'] = '$v';\n";
  272. }
  273. }
  274.  
  275. if (isset($this->_options['showAsTable']) && $this->_options['showAsTable']) {
  276. $js .= "LTShowAsTable['$id'] = true;\n";
  277. if (isset($this->_options['showHeaders']) && $this->_options['showHeaders']) {
  278. $js .= "LTShowHeaders['$id'] = true;\n";
  279. }
  280. }
  281.  
  282. if (isset($this->_options['keys']) && is_array($this->_options['keys'])) {
  283. $keys = array();
  284. foreach($this->_options['keys'] as $key) {
  285. $keys[] = "'$key'";
  286. }
  287. $js .= "LTShowKeys['$id'] = [" . join(', ', $keys) . "];\n";
  288. }
  289.  
  290. $js .= "</script>";
  291. return $js;
  292. }
  293.  
  294.  
  295. /**
  296. * Calcs the canonical path of the specified file
  297. *
  298. * @param string $filename
  299. * @param string $path
  300. * @return string
  301. */
  302. function _getFileLocation($filename, $path)
  303. {
  304. if ($path) {
  305. $location = realpath($path . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
  306. } else {
  307. $path = '@data-dir@'.DIRECTORY_SEPARATOR.'HTML_QuickForm_LiveText'.DIRECTORY_SEPARATOR;
  308. if(strpos($path, '@'.'data-dir@') === 0) {
  309. $path = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  310. }
  311. $location = $path.$filename;
  312. }
  313. return $location;
  314. }
  315.  
  316. } // end class HTML_QuickForm_LiveText
  317.  
  318. if (class_exists('HTML_QuickForm')) {
  319. HTML_QuickForm::registerElementType('livetext', 'HTML/QuickForm/LiveText.php', 'HTML_QuickForm_LiveText');
  320. }
  321. ?>

Documentation generated on Thu, 12 Apr 2007 08:37:35 +0200 by phpDocumentor 1.3.0RC3