Object.php 11 KB

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