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

Source for file combobox.php

Documentation is available at combobox.php

  1. <?php
  2. /* vim: set number autoindent tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5. * This package add combobox type to HTML_QuickForm
  6. *
  7. * A combobox is an element composed by an input text and a dropdown list:
  8. * you can either select an option from the list or write into the text field.
  9. * PHP versions 4 and 5
  10. *
  11. * LICENSE: This source file is subject to version 3.0 of the PHP license
  12. * that is available through the world-wide-web at the following URI:
  13. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  14. * the PHP License and are unable to obtain it through the web, please
  15. * send a note to license@php.net so we can mail you a copy immediately.
  16. *
  17. * @category HTML
  18. * @package HTML_QuickForm_ComboBox
  19. * @author Fabio Ambrosanio <fabio@ambrosanio.com>
  20. * @license http://www.php.net/license/3_01.txt PHP
  21. * @version @package_version@
  22. *
  23. * $Id: combobox.php,v 1.2 2006/11/29 21:14:39 fabamb Exp $
  24. */
  25. require_once 'HTML/QuickForm/text.php';
  26.  
  27. /**
  28. * This class represents a combobox element of HTML_QuickForm framework.
  29. *
  30. * @author Fabio Ambrosanio <fabio@ambrosanio.com>
  31. * @category HTML
  32. * @package HTML_QuickForm_ComboBox
  33. * @version @package_version@
  34. * @license http://www.php.net/license/3_01.txt PHP
  35. */
  36. class HTML_QuickForm_ComboBox extends HTML_QuickForm_text
  37. {
  38. // {{{ properties
  39.  
  40.  
  41. /**
  42. * Elements of the dropdown list
  43. *
  44. * @var array
  45. * @access private
  46. */
  47. var $_elements = null;
  48.  
  49. /**
  50. * Filename of css stylesheet
  51. *
  52. * @var string
  53. */
  54. var $_css = 'combobox.css';
  55.  
  56. /**
  57. * Path of css stylesheet
  58. *
  59. * @var unknown_type
  60. */
  61. var $_css_path = false;
  62.  
  63. /**
  64. * Filename and path of javascript library
  65. */
  66. var $_js = 'combobox.js';
  67.  
  68. /**
  69. * Options for element's UI.
  70. * You can modify the appearence of combobox (see below for the list of elements and classes.
  71. *
  72. * If options contains a value for arrowImage key, an image is used instead of a button.
  73. *
  74. * @var array
  75. * @access private
  76. */
  77. var $_options = array(
  78. 'boxClass' => 'comboBox', // style class of external box
  79. 'inputClass' => 'comboBoxInput', // style class of input text field
  80. 'buttonClass' => 'comboBoxButton', // style class of dropdown button
  81. 'arrowClass' => 'comboBoxArrow', // style class of dropdown image
  82. 'containerClass' => 'comboBoxContainer', // style class of dropdown container
  83. 'optionClass' => 'comboBoxOption', // style class of dropdown options
  84. 'optionClassOver' => 'comboBoxOptionOver', // style class of dropdown options when highlighted
  85. 'buttonValue' => '*' // value of dropdown button
  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 $elements (optional)Combobox elements.
  99. * @param array $options (optional)An associative array which elements specify different
  100. * aspects of the combobox.
  101. * @param mixed $attributes (optional)Either a typical HTML attribute string
  102. * or an associative array. Date format is passed along the attributes.
  103. * @access public
  104. * @return void
  105. *
  106. * With $options you can specify the CSS class to use with different aspects of the combobox.
  107. * The compobox's aspects that you can change assigning different CSS class are:
  108. * <pre>
  109. * Aspect Description Default class
  110. * boxClass external box comboBox
  111. * inputClass input textfield comboBoxInput
  112. * buttonClass dropdown button (if used) comboBoxButton
  113. * arrowClass dropdown arrow (if used) comboBoxArrow
  114. * containerClass dropdown container comboBoxContainer
  115. * optionClass single option inside dropdown container comboBoxOption
  116. * optionClassOver single option when mouse is over comboBoxOptionOver
  117. * </pre>
  118. *
  119. * You can change the button (value)label with buttonValue option, es.: butonValue => 'V' or
  120. * specify images to use to render the button:
  121. * <pre>
  122. * arrowImage image used to render the button in normal state
  123. * arrowImageOver image used when the mouse is over the button
  124. * arrowImageDown image used when the button in clicked
  125. * </pre>
  126. */
  127. function HTML_QuickForm_ComboBox($elementName = null, $elementLabel = null, $elements = null, $options = null, $attributes = null)
  128. {
  129. $this->HTML_QuickForm_text($elementName, $elementLabel, $attributes);
  130. $this->_persistantFreeze = true;
  131. if (isset($elements)) {
  132. $this->_elements = $elements;
  133. }
  134. $id = $this->getAttribute('id');
  135. if ("$id" == '') {
  136. $this->updateAttributes(array(
  137. 'id' => $elementName
  138. ));
  139. }
  140.  
  141. // update element options
  142. if ($options && is_array($options)) {
  143. $this->_options = array_merge($this->_options, $options);
  144. }
  145. } //end constructor
  146.  
  147. // }}}
  148.  
  149. // {{{ toHtml()
  150.  
  151.  
  152. /**
  153. * Returns Html for the Combobox input element
  154. *
  155. * @access public
  156. * @return string the HTML string representing the combobox
  157. */
  158. function toHtml()
  159. {
  160. // generate an unique id for all UI elements
  161. $uid = uniqid('cb');
  162.  
  163. if ($this->_flagFrozen) {
  164. // input froze: return parent html
  165. $html = parent::toHtml();
  166. } else {
  167. // eventually write style classes and javascript functions
  168. $html = $this->_getCSS();
  169. $html .= $this->_getJS();
  170.  
  171. // build input text field
  172. $this->updateAttributes(array(
  173. 'class' => $this->_options['inputClass']
  174. ));
  175. $input = parent::toHtml();
  176.  
  177. // wich type of button? input-button or image?
  178. if (!$this->_options['arrowImage']) {
  179. // build simple button
  180. $buttonClass = $this->_options['buttonClass'];
  181. $buttonValue = $this->_options['buttonValue'];
  182. $button = "<input value=\"$buttonValue\" type=\"button\" id=\"$uid.button\" class=\"$buttonClass\" onClick=\"comboBoxShowOptions('$uid')\" onblur=\"comboBoxHideOptions('$uid')\"/>";
  183. } else {
  184. // build button implemented with an image
  185. $arrowImage = $this->_options['arrowImage'];
  186.  
  187. // have we an image for mouse-over?
  188. $arrowImageOver = $this->_options['arrowImageOver'];
  189. if (!$arrowImageOver) $arrowImageOver = $arrowImage;
  190.  
  191. // have we an image for mouse-down?
  192. $arrowImageDown = $this->_options['arrowImageDown'];
  193. if (!$arrowImageDown) $arrowImageDown = $arrowImage;
  194.  
  195. $arrowClass = $this->_options['arrowClass'];
  196. $button = "<input type=\"image\" src=\"$arrowImage\" id=\"$uid.arrow\" class=\"$arrowClass\" align=\"top\" onClick=\"comboBoxShowOptions('$uid'); return false;\" onMouseOver=\"comboBoxSwitchImage('$uid')\" onMouseOut=\"comboBoxSwitchImage('$uid')\" arrowimage=\"$arrowImage\" arrowimageover=\"$arrowImageOver\" arrowimagedown=\"$arrowImageDown\" onblur=\"comboBoxHideOptions('$uid')\" />";
  197. }
  198.  
  199. // build elements list
  200. $options = '';
  201. if (is_array($this->_elements)) {
  202. $optionClass = $this->_options['optionClass'];
  203. if ($this->_is_assoc_array($this->_elements)) {
  204. foreach ($this->_elements as $k => $e) {
  205. $options .= "<div class=\"$optionClass\" width=\"500px\" value=\"$k\">$e</div>";
  206. }
  207. } else {
  208. foreach ($this->_elements as $e) {
  209. $options .= "<div class=\"$optionClass\" width=\"500px\">$e</div>";
  210. }
  211. }
  212. }
  213.  
  214. // build the whole combobox
  215. $boxClass = $this->_options['boxClass'];
  216. $containerClass = $this->_options['containerClass'];
  217. $html .= "<div id=\"$uid.box\" class=\"$boxClass\" width=\"500px\">$input$button</div><div id=\"$uid.container\" class=\"$containerClass\" width=\"500px\">$options</div>";
  218.  
  219. // add this combobox to a javascript array:
  220. // when the page is loaded a script will resize each combobox
  221. $html .= "<script type=\"text/javascript\">var aCombo = new Object();aCombo[\"id\"] = '$uid';combos[combos.length++] = aCombo;</script>";
  222. }
  223.  
  224. return $html;
  225. }// end func toHtml
  226.  
  227. // }}}
  228.  
  229. // {{{ setCSS($filename, $path)
  230.  
  231.  
  232. /**
  233. * Sets new CSS file
  234. *
  235. * @param string $filename filename of CSS file
  236. * @param string $path path of CSS file
  237. * @param string $options associative array which elements specify CSS classes for different aspects cf the combobox
  238. */
  239. function setCSS($filename, $path = false, $options = false)
  240. {
  241. $this->_css = $filename;
  242. $this->_css_path = $path;
  243. if ($options && is_array($options)) {
  244. $this->_options = array_merge($this->_options, $options);
  245. }
  246. }// end func setCSS
  247.  
  248. // }}}
  249.  
  250.  
  251. /**
  252. * Returns CSS styles
  253. *
  254. * @return string
  255. * @access private
  256. */
  257. function _getCSS()
  258. {
  259. if (defined('HTML_QUICKFORM_COMBOBOX_CSS_' . $this->_css_path . $this->_css)) return '';
  260. define('HTML_QUICKFORM_COMBOBOX_CSS_' . $this->_css_path . $this->_css, true);
  261.  
  262. $location = $this->_getFileLocation($this->_css, $this->_css_path);
  263. $css = "<style type=\"text/css\">";
  264. $css .= "/* "; if ($this->_css_path) $css .= $this->_css_path . DIRECTORY_SEPARATOR;
  265. $css .= $this->_css . " */";
  266. $css .= file_get_contents($location) . "</style>";
  267. return $css;
  268. }
  269.  
  270. /**
  271. * Returns javascript functions
  272. *
  273. * @return string
  274. * @access private
  275. */
  276. function _getJS()
  277. {
  278. if (defined('HTML_QUICKFORM_COMBOBOX_JS')) return '';
  279. define('HTML_QUICKFORM_COMBOBOX_JS', true);
  280.  
  281. $location = $this->_getFileLocation($this->_js, false);
  282. $js = "<script type=\"text/javascript\">" . file_get_contents($location) . "</script>";
  283. return $js;
  284. }
  285.  
  286.  
  287. /**
  288. * Calcs the canonical path of the specified file
  289. *
  290. * @param string $filename
  291. * @param string $path
  292. * @return string
  293. */
  294. function _getFileLocation($filename, $path)
  295. {
  296. if ($path) {
  297. $location = realpath($path . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
  298. } else {
  299. $path = '@data-dir@'.DIRECTORY_SEPARATOR.'HTML_QuickForm_ComboBox'.DIRECTORY_SEPARATOR;
  300. if(strpos($path, '@'.'data-dir@') === 0) {
  301. $path = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  302. }
  303. $location = $path.$filename;
  304. }
  305. return $location;
  306. }
  307.  
  308. /**
  309. * Checks if an array is associative
  310. *
  311. * @param array $php_val
  312. * @return true if $php_val is an associative array, false otherwise
  313. */
  314. function _is_assoc_array( $php_val )
  315. {
  316. if( !is_array( $php_val ) ){
  317. # Neither an associative, nor non-associative array.
  318. return false;
  319. }
  320.  
  321. $given_keys = array_keys( $php_val );
  322. $non_assoc_keys = range( 0, count( $php_val ) );
  323.  
  324. if ( function_exists( 'array_diff_assoc' ) ) { # PHP > 4.3.0
  325. return array_diff_assoc( $given_keys, $non_assoc_keys );
  326. } else {
  327. return array_diff( $given_keys, $non_assoc_keys ) and array_diff( $non_assoc_keys, $given_keys );
  328. }
  329. }
  330.  
  331. } // end class HTML_QuickForm_ComboBox
  332.  
  333. if (class_exists('HTML_QuickForm')) {
  334. HTML_QuickForm::registerElementType('combobox', 'HTML/QuickForm/combobox.php', 'HTML_QuickForm_ComboBox');
  335. }
  336. ?>

Documentation generated on Wed, 29 Nov 2006 22:46:18 +0100 by phpDocumentor 1.3.0RC3