Object.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Ron McClain <ron@humaniq.com> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Object.php 6184 2005-09-07 10:08:17Z bmol $
  20. require_once('HTML/QuickForm/Renderer.php');
  21. /**
  22. * A concrete renderer for HTML_QuickForm, makes an object from form contents
  23. *
  24. * Based on HTML_Quickform_Renderer_Array code
  25. *
  26. * @access public
  27. */
  28. class HTML_QuickForm_Renderer_Object extends HTML_QuickForm_Renderer
  29. {
  30. /**
  31. * The object being generated
  32. * @var object $_obj
  33. */
  34. var $_obj= null;
  35. /**
  36. * Number of sections in the form (i.e. number of headers in it)
  37. * @var integer $_sectionCount
  38. */
  39. var $_sectionCount;
  40. /**
  41. * Current section number
  42. * @var integer $_currentSection
  43. */
  44. var $_currentSection;
  45. /**
  46. * Object representing current group
  47. * @var object $_currentGroup
  48. */
  49. var $_currentGroup = null;
  50. /**
  51. * Class of Element Objects
  52. * @var object $_elementType
  53. */
  54. var $_elementType = 'QuickFormElement';
  55. /**
  56. * Additional style information for different elements
  57. * @var array $_elementStyles
  58. */
  59. var $_elementStyles = array();
  60. /**
  61. * true: collect all hidden elements into string; false: process them as usual form elements
  62. * @var bool $_collectHidden
  63. */
  64. var $_collectHidden = false;
  65. /**
  66. * Constructor
  67. *
  68. * @param collecthidden bool true: collect all hidden elements
  69. * @access public
  70. */
  71. function HTML_QuickForm_Renderer_Object($collecthidden = false)
  72. {
  73. $this->HTML_QuickForm_Renderer();
  74. $this->_collectHidden = $collecthidden;
  75. $this->_obj = new QuickformForm;
  76. }
  77. /**
  78. * Return the rendered Object
  79. * @access public
  80. */
  81. function toObject()
  82. {
  83. return $this->_obj;
  84. }
  85. /**
  86. * Set the class of the form elements. Defaults to QuickformElement.
  87. * @param type string Name of element class
  88. * @access public
  89. */
  90. function setElementType($type)
  91. {
  92. $this->_elementType = $type;
  93. }
  94. function startForm(&$form)
  95. {
  96. $this->_obj->frozen = $form->isFrozen();
  97. $this->_obj->javascript = $form->getValidationScript();
  98. $this->_obj->attributes = $form->getAttributes(true);
  99. $this->_obj->requirednote = $form->getRequiredNote();
  100. $this->_obj->errors = new StdClass;
  101. if($this->_collectHidden) {
  102. $this->_obj->hidden = '';
  103. }
  104. $this->_elementIdx = 1;
  105. $this->_currentSection = null;
  106. $this->_sectionCount = 0;
  107. } // end func startForm
  108. function renderHeader(&$header)
  109. {
  110. $hobj = new StdClass;
  111. $hobj->header = $header->toHtml();
  112. $this->_obj->sections[$this->_sectionCount] = $hobj;
  113. $this->_currentSection = $this->_sectionCount++;
  114. }
  115. function renderElement(&$element, $required, $error)
  116. {
  117. $elObj = $this->_elementToObject($element, $required, $error);
  118. if(!empty($error)) {
  119. $name = $elObj->name;
  120. $this->_obj->errors->$name = $error;
  121. }
  122. $this->_storeObject($elObj);
  123. } // end func renderElement
  124. function renderHidden(&$element)
  125. {
  126. if($this->_collectHidden) {
  127. $this->_obj->hidden .= $element->toHtml() . "\n";
  128. } else {
  129. $this->renderElement($element, false, null);
  130. }
  131. } //end func renderHidden
  132. function startGroup(&$group, $required, $error)
  133. {
  134. $this->_currentGroup = $this->_elementToObject($group, $required, $error);
  135. if(!empty($error)) {
  136. $name = $this->_currentGroup->name;
  137. $this->_obj->errors->$name = $error;
  138. }
  139. } // end func startGroup
  140. function finishGroup(&$group)
  141. {
  142. $this->_storeObject($this->_currentGroup);
  143. $this->_currentGroup = null;
  144. } // end func finishGroup
  145. /**
  146. * Creates an object representing an element
  147. *
  148. * @access private
  149. * @param element object An HTML_QuickForm_element object
  150. * @param required bool Whether an element is required
  151. * @param error string Error associated with the element
  152. * @return object
  153. */
  154. function _elementToObject(&$element, $required, $error)
  155. {
  156. if($this->_elementType) {
  157. $ret = new $this->_elementType;
  158. }
  159. $ret->name = $element->getName();
  160. $ret->value = $element->getValue();
  161. $ret->type = $element->getType();
  162. $ret->frozen = $element->isFrozen();
  163. $labels = $element->getLabel();
  164. if (is_array($labels)) {
  165. $ret->label = array_shift($labels);
  166. foreach ($labels as $key => $label) {
  167. $key = is_int($key)? $key + 2: $key;
  168. $ret->{'label_' . $key} = $label;
  169. }
  170. } else {
  171. $ret->label = $labels;
  172. }
  173. $ret->required = $required;
  174. $ret->error = $error;
  175. if(isset($this->_elementStyles[$ret->name])) {
  176. $ret->style = $this->_elementStyles[$ret->name];
  177. $ret->styleTemplate = "styles/". $ret->style .".html";
  178. }
  179. if($ret->type == 'group') {
  180. $ret->separator = $element->_separator;
  181. $ret->elements = array();
  182. } else {
  183. $ret->html = $element->toHtml();
  184. }
  185. return $ret;
  186. }
  187. /**
  188. * Stores an object representation of an element in the form array
  189. *
  190. * @access private
  191. * @param elObj object Object representation of an element
  192. * @return void
  193. */
  194. function _storeObject($elObj)
  195. {
  196. $name = $elObj->name;
  197. if(is_object($this->_currentGroup) && $elObj->type != 'group') {
  198. $this->_currentGroup->elements[] = $elObj;
  199. } elseif (isset($this->_currentSection)) {
  200. $this->_obj->sections[$this->_currentSection]->elements[] = $elObj;
  201. } else {
  202. $this->_obj->elements[] = $elObj;
  203. }
  204. }
  205. function setElementStyle($elementName, $styleName = null)
  206. {
  207. if(is_array($elementName)) {
  208. $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
  209. } else {
  210. $this->_elementStyles[$elementName] = $styleName;
  211. }
  212. }
  213. } // end class HTML_QuickForm_Renderer_Object
  214. /**
  215. * Convenience class for the form object passed to outputObject()
  216. *
  217. * Eg.
  218. * {form.outputJavaScript():h}
  219. * {form.outputHeader():h}
  220. * <table>
  221. * <tr>
  222. * <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
  223. * </tr>
  224. * </table>
  225. * </form>
  226. */
  227. class QuickformForm
  228. {
  229. /**
  230. * Whether the form has been frozen
  231. * @var boolean $frozen
  232. */
  233. var $frozen;
  234. /**
  235. * Javascript for client-side validation
  236. * @var string $javascript
  237. */
  238. var $javascript;
  239. /**
  240. * Attributes for form tag
  241. * @var string $attributes
  242. */
  243. var $attributes;
  244. /**
  245. * Note about required elements
  246. * @var string $requirednote
  247. */
  248. var $requirednote;
  249. /**
  250. * Collected html of all hidden variables
  251. * @var string $hidden
  252. */
  253. var $hidden;
  254. /**
  255. * Set if there were validation errors.
  256. * StdClass object with element names for keys and their
  257. * error messages as values
  258. * @var object $errors
  259. */
  260. var $errors;
  261. /**
  262. * Array of QuickformElementObject elements. If there are headers in the form
  263. * this will be empty and the elements will be in the
  264. * separate sections
  265. * @var array $elements
  266. */
  267. var $elements;
  268. /**
  269. * Array of sections contained in the document
  270. * @var array $sections
  271. */
  272. var $sections;
  273. /**
  274. * Output &lt;form&gt; header
  275. * {form.outputHeader():h}
  276. * @return string &lt;form attributes&gt;
  277. */
  278. function outputHeader()
  279. {
  280. return "<form " . $this->attributes . ">\n";
  281. }
  282. /**
  283. * Output form javascript
  284. * {form.outputJavaScript():h}
  285. * @return string Javascript
  286. */
  287. function outputJavaScript()
  288. {
  289. return $this->javascript;
  290. }
  291. } // end class QuickformForm
  292. /**
  293. * Convenience class describing a form element.
  294. * The properties defined here will be available from
  295. * your flexy templates by referencing
  296. * {form.zip.label:h}, {form.zip.html:h}, etc.
  297. */
  298. class QuickformElement
  299. {
  300. /**
  301. * Element name
  302. * @var string $name
  303. */
  304. var $name;
  305. /**
  306. * Element value
  307. * @var mixed $value
  308. */
  309. var $value;
  310. /**
  311. * Type of element
  312. * @var string $type
  313. */
  314. var $type;
  315. /**
  316. * Whether the element is frozen
  317. * @var boolean $frozen
  318. */
  319. var $frozen;
  320. /**
  321. * Label for the element
  322. * @var string $label
  323. */
  324. var $label;
  325. /**
  326. * Whether element is required
  327. * @var boolean $required
  328. */
  329. var $required;
  330. /**
  331. * Error associated with the element
  332. * @var string $error
  333. */
  334. var $error;
  335. /**
  336. * Some information about element style
  337. * @var string $style
  338. */
  339. var $style;
  340. /**
  341. * HTML for the element
  342. * @var string $html
  343. */
  344. var $html;
  345. /**
  346. * If element is a group, the group separator
  347. * @var mixed $separator
  348. */
  349. var $separator;
  350. /**
  351. * If element is a group, an array of subelements
  352. * @var array $elements
  353. */
  354. var $elements;
  355. function isType($type)
  356. {
  357. return ($this->type == $type);
  358. }
  359. function notFrozen()
  360. {
  361. return !$this->frozen;
  362. }
  363. function isButton()
  364. {
  365. return ($this->type == "submit" || $this->type == "reset");
  366. }
  367. /**
  368. * XXX: why does it use Flexy when all other stuff here does not depend on it?
  369. */
  370. function outputStyle()
  371. {
  372. ob_start();
  373. HTML_Template_Flexy::staticQuickTemplate('styles/' . $this->style . '.html', $this);
  374. $ret = ob_get_contents();
  375. ob_end_clean();
  376. return $ret;
  377. }
  378. } // end class QuickformElement
  379. ?>