PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/www/lib/HTML/QuickForm/Renderer/Object.php

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