/source/Plug-in/xajax/xajax_controls/form.inc.php

http://prosporous.googlecode.com/ · PHP · 314 lines · 167 code · 34 blank · 113 comment · 28 complexity · c9acb898abd58f1fbfd55ba2b83739fb MD5 · raw file

  1. <?php
  2. /*
  3. File: form.inc.php
  4. HTML Control Library - Form Level Tags
  5. Title: xajax HTML control class library
  6. Please see <copyright.inc.php> for a detailed description, copyright
  7. and license information.
  8. */
  9. /*
  10. @package xajax
  11. @version $Id: form.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
  12. @copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
  13. @license http://www.xajaxproject.org/bsd_license.txt BSD License
  14. */
  15. /*
  16. Section: Description
  17. This file contains the class declarations for the following HTML Controls:
  18. - form
  19. - input, textarea, select, optgroup, option
  20. - label
  21. - fieldset, legend
  22. The following tags are deprecated as of HTML 4.01, therefore, they will not
  23. be supported:
  24. - isindex
  25. */
  26. class clsForm extends xajaxControlContainer
  27. {
  28. function clsForm($aConfiguration=array())
  29. {
  30. if (false == isset($aConfiguration['attributes']))
  31. $aConfiguration['attributes'] = array();
  32. if (false == isset($aConfiguration['attributes']['method']))
  33. $aConfiguration['attributes']['method'] = 'POST';
  34. if (false == isset($aConfiguration['attributes']['action']))
  35. $aConfiguration['attributes']['action'] = '#';
  36. xajaxControlContainer::xajaxControlContainer('form', $aConfiguration);
  37. }
  38. }
  39. class clsInput extends xajaxControl
  40. {
  41. function clsInput($aConfiguration=array())
  42. {
  43. xajaxControl::xajaxControl('input', $aConfiguration);
  44. }
  45. }
  46. class clsInputWithLabel extends clsInput
  47. {
  48. var $objLabel;
  49. var $sWhere;
  50. var $objBreak;
  51. function clsInputWithLabel($sLabel, $sWhere, $aConfiguration=array())
  52. {
  53. clsInput::clsInput($aConfiguration);
  54. $this->objLabel =& new clsLabel(array(
  55. 'child' => new clsLiteral($sLabel)
  56. ));
  57. $this->objLabel->setControl($this);
  58. $this->sWhere = $sWhere;
  59. $this->objBreak =& new clsBr();
  60. }
  61. function printHTML($sIndent='')
  62. {
  63. if ('left' == $this->sWhere || 'above' == $this->sWhere)
  64. $this->objLabel->printHTML($sIndent);
  65. if ('above' == $this->sWhere)
  66. $this->objBreak->printHTML($sIndent);
  67. clsInput::printHTML($sIndent);
  68. if ('below' == $this->sWhere)
  69. $this->objBreak->printHTML($sIndent);
  70. if ('right' == $this->sWhere || 'below' == $this->sWhere)
  71. $this->objLabel->printHTML($sIndent);
  72. }
  73. }
  74. /*
  75. Class: clsSelect
  76. A <xajaxControlContainer> derived class that assists in the construction
  77. of an HTML select control.
  78. This control can only accept <clsOption> controls as children.
  79. */
  80. class clsSelect extends xajaxControlContainer
  81. {
  82. /*
  83. Function: clsSelect
  84. Construct and initialize an instance of the class. See <xajaxControlContainer>
  85. for details regarding the aConfiguration parameter.
  86. */
  87. function clsSelect($aConfiguration=array())
  88. {
  89. xajaxControlContainer::xajaxControlContainer('select', $aConfiguration);
  90. }
  91. /*
  92. Function: addOption
  93. Used to add a single option to the options list.
  94. sValue - (string): The value that is returned as the form value
  95. when this option is the selected option.
  96. sText - (string): The text that is displayed in the select box when
  97. this option is the selected option.
  98. */
  99. function addOption($sValue, $sText)
  100. {
  101. $optionNew =& new clsOption();
  102. $optionNew->setValue($sValue);
  103. $optionNew->setText($sText);
  104. $this->addChild($optionNew);
  105. }
  106. /*
  107. Function: addOptions
  108. Used to add a list of options.
  109. aOptions - (associative array): A list of key/value pairs that will
  110. be passed to <clsSelect->addOption>.
  111. */
  112. function addOptions($aOptions, $aFields=array())
  113. {
  114. if (0 == count($aFields))
  115. foreach ($aOptions as $sValue => $sText)
  116. $this->addOption($sValue, $sText);
  117. else if (1 < count($aFields))
  118. foreach ($aOptions as $aOption)
  119. $this->addOption($aOption[$aFields[0]], $aOption[$aFields[1]]);
  120. else
  121. trigger_error('Invalid list of fields passed to clsSelect::addOptions; should be array of two strings.'
  122. . $this->backtrace(),
  123. E_USER_ERROR
  124. );
  125. }
  126. }
  127. /*
  128. Class: clsOptionGroup
  129. A <xajaxControlContainer> derived class that can be used around a list of <clsOption>
  130. objects to help the user find items in a select list.
  131. */
  132. class clsOptionGroup extends xajaxControlContainer
  133. {
  134. function clsOptionGroup($aConfiguration=array())
  135. {
  136. xajaxControlContainer::xajaxControlContainer('optgroup', $aConfiguration);
  137. }
  138. /*
  139. Function: addOption
  140. Used to add a single option to the options list.
  141. sValue - (string): The value that is returned as the form value
  142. when this option is the selected option.
  143. sText - (string): The text that is displayed in the select box when
  144. this option is the selected option.
  145. */
  146. function addOption($sValue, $sText)
  147. {
  148. $optionNew =& new clsOption();
  149. $optionNew->setValue($sValue);
  150. $optionNew->setText($sText);
  151. $this->addChild($optionNew);
  152. }
  153. /*
  154. Function: addOptions
  155. Used to add a list of options.
  156. aOptions - (associative array): A list of key/value pairs that will
  157. be passed to <clsSelect->addOption>.
  158. */
  159. function addOptions($aOptions, $aFields=array())
  160. {
  161. if (0 == count($aFields))
  162. foreach ($aOptions as $sValue => $sText)
  163. $this->addOption($sValue, $sText);
  164. else if (1 < count($aFields))
  165. foreach ($aOptions as $aOption)
  166. $this->addOption($aOption[$aFields[0]], $aOption[$aFields[1]]);
  167. else
  168. trigger_error('Invalid list of fields passed to clsOptionGroup::addOptions; should be array of two strings.'
  169. . $this->backtrace(),
  170. E_USER_ERROR
  171. );
  172. }
  173. }
  174. /*
  175. Class: clsOption
  176. A <xajaxControlContainer> derived class that assists with the construction
  177. of HTML option tags that will be assigned to an HTML select tag.
  178. This control can only accept <clsLiteral> objects as children.
  179. */
  180. class clsOption extends xajaxControlContainer
  181. {
  182. /*
  183. Function: clsOption
  184. Constructs and initializes an instance of this class. See <xajaxControlContainer>
  185. for more information regarding the aConfiguration parameter.
  186. */
  187. function clsOption($aConfiguration=array())
  188. {
  189. xajaxControlContainer::xajaxControlContainer('option', $aConfiguration);
  190. }
  191. /*
  192. Function: setValue
  193. Used to set the value associated with this option. The value is sent as the
  194. value of the select control when this is the selected option.
  195. */
  196. function setValue($sValue)
  197. {
  198. $this->setAttribute('value', $sValue);
  199. }
  200. /*
  201. Function: setText
  202. Sets the text to be shown in the select control when this is the
  203. selected option.
  204. */
  205. function setText($sText)
  206. {
  207. $this->clearChildren();
  208. $this->addChild(new clsLiteral($sText));
  209. }
  210. }
  211. class clsTextArea extends xajaxControlContainer
  212. {
  213. function clsTextArea($aConfiguration=array())
  214. {
  215. xajaxControlContainer::xajaxControlContainer('textarea', $aConfiguration);
  216. $this->sClass = '%block';
  217. }
  218. }
  219. class clsLabel extends xajaxControlContainer
  220. {
  221. var $objFor;
  222. function clsLabel($aConfiguration=array())
  223. {
  224. xajaxControlContainer::xajaxControlContainer('label', $aConfiguration);
  225. }
  226. function setControl(&$objControl)
  227. {
  228. if (false == is_a($objControl, 'xajaxControl'))
  229. trigger_error(
  230. 'Invalid control passed to clsLabel::setControl(); should be xajaxControl.'
  231. . $this->backtrace(),
  232. E_USER_ERROR);
  233. $this->objFor =& $objControl;
  234. }
  235. function printHTML($sIndent='')
  236. {
  237. $this->aAttributes['for'] = $this->objFor->aAttributes['id'];
  238. xajaxControlContainer::printHTML($sIndent);
  239. }
  240. }
  241. class clsFieldset extends xajaxControlContainer
  242. {
  243. function clsFieldset($aConfiguration=array())
  244. {
  245. xajaxControlContainer::xajaxControlContainer('fieldset', $aConfiguration);
  246. $this->sClass = '%block';
  247. }
  248. }
  249. class clsLegend extends xajaxControlContainer
  250. {
  251. function clsLegend($aConfiguration=array())
  252. {
  253. xajaxControlContainer::xajaxControlContainer('legend', $aConfiguration);
  254. $this->sClass = '%inline';
  255. }
  256. }