PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/pear/HTML/QuickForm/Renderer/ITDynamic.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 333 lines | 197 code | 21 blank | 115 comment | 35 complexity | 7e0c0b7178f61bdaf1bb32a74f40fe96 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * A concrete renderer for HTML_QuickForm, using Integrated Templates.
  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 Alexey Borzov <avb@php.net>
  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: ITDynamic.php 137 2009-11-09 13:24:37Z vanpouckesven $
  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, using Integrated Templates.
  28. *
  29. * This is a "dynamic" renderer, which means that concrete form look
  30. * is defined at runtime. This also means that you can define
  31. * <b>one</b> template file for <b>all</b> your forms. That template
  32. * should contain a block for every element 'look' appearing in your
  33. * forms and also some special blocks (consult the examples). If a
  34. * special block is not set for an element, the renderer falls back to
  35. * a default one.
  36. *
  37. * @category HTML
  38. * @package HTML_QuickForm
  39. * @author Alexey Borzov <avb@php.net>
  40. * @version Release: 3.2.11
  41. * @since 3.0
  42. */
  43. class HTML_QuickForm_Renderer_ITDynamic extends HTML_QuickForm_Renderer
  44. {
  45. /**#@+
  46. * @access private
  47. */
  48. /**
  49. * A template class (HTML_Template_ITX or HTML_Template_Sigma) instance
  50. * @var HTML_Template_ITX|HTML_Template_Sigma
  51. */
  52. var $_tpl = null;
  53. /**
  54. * The errors that were not shown near concrete fields go here
  55. * @var array
  56. */
  57. var $_errors = array();
  58. /**
  59. * Show the block with required note?
  60. * @var bool
  61. */
  62. var $_showRequired = false;
  63. /**
  64. * A separator for group elements
  65. * @var mixed
  66. */
  67. var $_groupSeparator = null;
  68. /**
  69. * The current element index inside a group
  70. * @var integer
  71. */
  72. var $_groupElementIdx = 0;
  73. /**
  74. * Blocks to use for different elements
  75. * @var array
  76. */
  77. var $_elementBlocks = array();
  78. /**
  79. * Block to use for headers
  80. * @var string
  81. */
  82. var $_headerBlock = null;
  83. /**#@-*/
  84. /**
  85. * Constructor
  86. *
  87. * @param HTML_Template_ITX|HTML_Template_Sigma Template object to use
  88. */
  89. function __construct(&$tpl)
  90. {
  91. parent :: __construct();
  92. $this->_tpl = & $tpl;
  93. $this->_tpl->setCurrentBlock('qf_main_loop');
  94. }
  95. function finishForm(&$form)
  96. {
  97. // display errors above form
  98. if (! empty($this->_errors) && $this->_tpl->blockExists('qf_error_loop'))
  99. {
  100. foreach ($this->_errors as $error)
  101. {
  102. $this->_tpl->setVariable('qf_error', $error);
  103. $this->_tpl->parse('qf_error_loop');
  104. }
  105. }
  106. // show required note
  107. if ($this->_showRequired)
  108. {
  109. $this->_tpl->setVariable('qf_required_note', $form->getRequiredNote());
  110. }
  111. // assign form attributes
  112. $this->_tpl->setVariable('qf_attributes', $form->getAttributes(true));
  113. // assign javascript validation rules
  114. $this->_tpl->setVariable('qf_javascript', $form->getValidationScript());
  115. }
  116. function renderHeader(&$header)
  117. {
  118. $blockName = $this->_matchBlock($header);
  119. if ('qf_header' == $blockName && isset($this->_headerBlock))
  120. {
  121. $blockName = $this->_headerBlock;
  122. }
  123. $this->_tpl->setVariable('qf_header', $header->toHtml());
  124. $this->_tpl->parse($blockName);
  125. $this->_tpl->parse('qf_main_loop');
  126. }
  127. function renderElement(&$element, $required, $error)
  128. {
  129. $blockName = $this->_matchBlock($element);
  130. // are we inside a group?
  131. if ('qf_main_loop' != $this->_tpl->currentBlock)
  132. {
  133. if (0 != $this->_groupElementIdx && $this->_tpl->placeholderExists('qf_separator', $blockName))
  134. {
  135. if (is_array($this->_groupSeparator))
  136. {
  137. $this->_tpl->setVariable('qf_separator', $this->_groupSeparator[($this->_groupElementIdx - 1) % count($this->_groupSeparator)]);
  138. }
  139. else
  140. {
  141. $this->_tpl->setVariable('qf_separator', (string) $this->_groupSeparator);
  142. }
  143. }
  144. $this->_groupElementIdx ++;
  145. }
  146. elseif (! empty($error))
  147. {
  148. // show the error message or keep it for later use
  149. if ($this->_tpl->blockExists($blockName . '_error'))
  150. {
  151. $this->_tpl->setVariable('qf_error', $error);
  152. }
  153. else
  154. {
  155. $this->_errors[] = $error;
  156. }
  157. }
  158. // show an '*' near the required element
  159. if ($required)
  160. {
  161. $this->_showRequired = true;
  162. if ($this->_tpl->blockExists($blockName . '_required'))
  163. {
  164. $this->_tpl->touchBlock($blockName . '_required');
  165. }
  166. }
  167. // Prepare multiple labels
  168. $labels = $element->getLabel();
  169. if (is_array($labels))
  170. {
  171. $mainLabel = array_shift($labels);
  172. }
  173. else
  174. {
  175. $mainLabel = $labels;
  176. }
  177. // render the element itself with its main label
  178. $this->_tpl->setVariable('qf_element', $element->toHtml());
  179. if ($this->_tpl->placeholderExists('qf_label', $blockName))
  180. {
  181. $this->_tpl->setVariable('qf_label', $mainLabel);
  182. }
  183. // render extra labels, if any
  184. if (is_array($labels))
  185. {
  186. foreach ($labels as $key => $label)
  187. {
  188. $key = is_int($key) ? $key + 2 : $key;
  189. if ($this->_tpl->blockExists($blockName . '_label_' . $key))
  190. {
  191. $this->_tpl->setVariable('qf_label_' . $key, $label);
  192. }
  193. }
  194. }
  195. $this->_tpl->parse($blockName);
  196. $this->_tpl->parseCurrentBlock();
  197. }
  198. function renderHidden(&$element)
  199. {
  200. $this->_tpl->setVariable('qf_hidden', $element->toHtml());
  201. $this->_tpl->parse('qf_hidden_loop');
  202. }
  203. function startGroup(&$group, $required, $error)
  204. {
  205. $blockName = $this->_matchBlock($group);
  206. $this->_tpl->setCurrentBlock($blockName . '_loop');
  207. $this->_groupElementIdx = 0;
  208. $this->_groupSeparator = is_null($group->_separator) ? '&nbsp;' : $group->_separator;
  209. // show an '*' near the required element
  210. if ($required)
  211. {
  212. $this->_showRequired = true;
  213. if ($this->_tpl->blockExists($blockName . '_required'))
  214. {
  215. $this->_tpl->touchBlock($blockName . '_required');
  216. }
  217. }
  218. // show the error message or keep it for later use
  219. if (! empty($error))
  220. {
  221. if ($this->_tpl->blockExists($blockName . '_error'))
  222. {
  223. $this->_tpl->setVariable('qf_error', $error);
  224. }
  225. else
  226. {
  227. $this->_errors[] = $error;
  228. }
  229. }
  230. $this->_tpl->setVariable('qf_group_label', $group->getLabel());
  231. }
  232. function finishGroup(&$group)
  233. {
  234. $this->_tpl->parse($this->_matchBlock($group));
  235. $this->_tpl->setCurrentBlock('qf_main_loop');
  236. $this->_tpl->parseCurrentBlock();
  237. }
  238. /**
  239. * Returns the name of a block to use for element rendering
  240. *
  241. * If a name was not explicitly set via setElementBlock(), it tries
  242. * the names '{prefix}_{element type}' and '{prefix}_{element}', where
  243. * prefix is either 'qf' or the name of the current group's block
  244. *
  245. * @param HTML_QuickForm_element form element being rendered
  246. * @access private
  247. * @return string block name
  248. */
  249. function _matchBlock(&$element)
  250. {
  251. $name = $element->getName();
  252. $type = $element->getType();
  253. if (isset($this->_elementBlocks[$name]) && $this->_tpl->blockExists($this->_elementBlocks[$name]))
  254. {
  255. if (('group' == $type) || ($this->_elementBlocks[$name] . '_loop' != $this->_tpl->currentBlock))
  256. {
  257. return $this->_elementBlocks[$name];
  258. }
  259. }
  260. if ('group' != $type && 'qf_main_loop' != $this->_tpl->currentBlock)
  261. {
  262. $prefix = substr($this->_tpl->currentBlock, 0, - 5); // omit '_loop' postfix
  263. }
  264. else
  265. {
  266. $prefix = 'qf';
  267. }
  268. if ($this->_tpl->blockExists($prefix . '_' . $type))
  269. {
  270. return $prefix . '_' . $type;
  271. }
  272. elseif ($this->_tpl->blockExists($prefix . '_' . $name))
  273. {
  274. return $prefix . '_' . $name;
  275. }
  276. else
  277. {
  278. return $prefix . '_element';
  279. }
  280. }
  281. /**
  282. * Sets the block to use for element rendering
  283. *
  284. * @param mixed element name or array ('element name' => 'block name')
  285. * @param string block name if $elementName is not an array
  286. * @access public
  287. * @return void
  288. */
  289. function setElementBlock($elementName, $blockName = null)
  290. {
  291. if (is_array($elementName))
  292. {
  293. $this->_elementBlocks = array_merge($this->_elementBlocks, $elementName);
  294. }
  295. else
  296. {
  297. $this->_elementBlocks[$elementName] = $blockName;
  298. }
  299. }
  300. /**
  301. * Sets the name of a block to use for header rendering
  302. *
  303. * @param string block name
  304. * @access public
  305. * @return void
  306. */
  307. function setHeaderBlock($blockName)
  308. {
  309. $this->_headerBlock = $blockName;
  310. }
  311. }
  312. ?>