ObjectFlexy.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * QuickForm renderer for Flexy template engine, static version.
  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: ObjectFlexy.php,v 1.10 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. require_once 'HTML/QuickForm/Renderer/Object.php';
  26. /**
  27. * QuickForm renderer for Flexy template engine, static version.
  28. *
  29. * A static renderer for HTML_Quickform. Makes a QuickFormFlexyObject
  30. * from the form content suitable for use with a Flexy template
  31. *
  32. * Usage:
  33. * <code>
  34. * $form =& new HTML_QuickForm('form', 'POST');
  35. * $template =& new HTML_Template_Flexy();
  36. * $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
  37. * $renderer->setHtmlTemplate("html.html");
  38. * $renderer->setLabelTemplate("label.html");
  39. * $form->accept($renderer);
  40. * $view = new StdClass;
  41. * $view->form = $renderer->toObject();
  42. * $template->compile("mytemplate.html");
  43. * </code>
  44. *
  45. * Based on the code for HTML_QuickForm_Renderer_ArraySmarty
  46. *
  47. * @category HTML
  48. * @package HTML_QuickForm
  49. * @author Ron McClain <ron@humaniq.com>
  50. * @version Release: 3.2.11
  51. * @since 3.1.1
  52. */
  53. class HTML_QuickForm_Renderer_ObjectFlexy extends HTML_QuickForm_Renderer_Object
  54. {
  55. /**#@+
  56. * @access private
  57. */
  58. /**
  59. * HTML_Template_Flexy instance
  60. * @var object $_flexy
  61. */
  62. var $_flexy;
  63. /**
  64. * Current element index
  65. * @var integer $_elementIdx
  66. */
  67. var $_elementIdx;
  68. /**
  69. * The current element index inside a group
  70. * @var integer $_groupElementIdx
  71. */
  72. var $_groupElementIdx = 0;
  73. /**
  74. * Name of template file for form html
  75. * @var string $_html
  76. * @see setRequiredTemplate()
  77. */
  78. var $_html = '';
  79. /**
  80. * Name of template file for form labels
  81. * @var string $label
  82. * @see setErrorTemplate()
  83. */
  84. var $label = '';
  85. /**
  86. * Class of the element objects, so you can add your own
  87. * element methods
  88. * @var string $_elementType
  89. */
  90. var $_elementType = 'QuickformFlexyElement';
  91. /**#@-*/
  92. /**
  93. * Constructor
  94. *
  95. * @param HTML_Template_Flexy template object to use
  96. * @public
  97. */
  98. function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy)
  99. {
  100. $this->HTML_QuickForm_Renderer_Object(true);
  101. $this->_obj = new QuickformFlexyForm();
  102. $this->_flexy =& $flexy;
  103. } // end constructor
  104. function renderHeader(&$header)
  105. {
  106. if($name = $header->getName()) {
  107. $this->_obj->header->$name = $header->toHtml();
  108. } else {
  109. $this->_obj->header[$this->_sectionCount] = $header->toHtml();
  110. }
  111. $this->_currentSection = $this->_sectionCount++;
  112. } // end func renderHeader
  113. function startGroup(&$group, $required, $error)
  114. {
  115. parent::startGroup($group, $required, $error);
  116. $this->_groupElementIdx = 1;
  117. } //end func startGroup
  118. /**
  119. * Creates an object representing an element containing
  120. * the key for storing this
  121. *
  122. * @access private
  123. * @param HTML_QuickForm_element form element being rendered
  124. * @param bool Whether an element is required
  125. * @param string Error associated with the element
  126. * @return object
  127. */
  128. function _elementToObject(&$element, $required, $error)
  129. {
  130. $ret = parent::_elementToObject($element, $required, $error);
  131. if($ret->type == 'group') {
  132. $ret->html = $element->toHtml();
  133. unset($ret->elements);
  134. }
  135. if(!empty($this->_label)) {
  136. $this->_renderLabel($ret);
  137. }
  138. if(!empty($this->_html)) {
  139. $this->_renderHtml($ret);
  140. $ret->error = $error;
  141. }
  142. // Create an element key from the name
  143. if (false !== ($pos = strpos($ret->name, '[')) || is_object($this->_currentGroup)) {
  144. if (!$pos) {
  145. $keys = '->{\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret->name) . '\'}';
  146. } else {
  147. $keys = '->{\'' . str_replace(
  148. array('\\', '\'', '[', ']'), array('\\\\', '\\\'', '\'}->{\'', ''),
  149. $ret->name
  150. ) . '\'}';
  151. }
  152. // special handling for elements in native groups
  153. if (is_object($this->_currentGroup)) {
  154. // skip unnamed group items unless radios: no name -> no static access
  155. // identification: have the same key string as the parent group
  156. if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type) {
  157. return false;
  158. }
  159. // reduce string of keys by remove leading group keys
  160. if (0 === strpos($keys, $this->_currentGroup->keys)) {
  161. $keys = substr_replace($keys, '', 0, strlen($this->_currentGroup->keys));
  162. }
  163. }
  164. } elseif (0 == strlen($ret->name)) {
  165. $keys = '->{\'element_' . $this->_elementIdx . '\'}';
  166. } else {
  167. $keys = '->{\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret->name) . '\'}';
  168. }
  169. // for radios: add extra key from value
  170. if ('radio' == $ret->type && '[]' != substr($keys, -2)) {
  171. $keys .= '->{\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret->value) . '\'}';
  172. }
  173. $ret->keys = $keys;
  174. $this->_elementIdx++;
  175. return $ret;
  176. }
  177. /**
  178. * Stores an object representation of an element in the
  179. * QuickformFormObject instance
  180. *
  181. * @access private
  182. * @param QuickformElement Object representation of an element
  183. * @return void
  184. */
  185. function _storeObject($elObj)
  186. {
  187. if ($elObj) {
  188. $keys = $elObj->keys;
  189. unset($elObj->keys);
  190. if(is_object($this->_currentGroup) && ('group' != $elObj->type)) {
  191. $code = '$this->_currentGroup' . $keys . ' = $elObj;';
  192. } else {
  193. $code = '$this->_obj' . $keys . ' = $elObj;';
  194. }
  195. eval($code);
  196. }
  197. }
  198. /**
  199. * Set the filename of the template to render html elements.
  200. * In your template, {html} is replaced by the unmodified html.
  201. * If the element is required, {required} will be true.
  202. * Eg.
  203. * <pre>
  204. * {if:error}
  205. * <font color="red" size="1">{error:h}</font><br />
  206. * {end:}
  207. * {html:h}
  208. * </pre>
  209. *
  210. * @access public
  211. * @param string Filename of template
  212. * @return void
  213. */
  214. function setHtmlTemplate($template)
  215. {
  216. $this->_html = $template;
  217. }
  218. /**
  219. * Set the filename of the template to render form labels
  220. * In your template, {label} is replaced by the unmodified label.
  221. * {error} will be set to the error, if any. {required} will
  222. * be true if this is a required field
  223. * Eg.
  224. * <pre>
  225. * {if:required}
  226. * <font color="orange" size="1">*</font>
  227. * {end:}
  228. * {label:h}
  229. * </pre>
  230. *
  231. * @access public
  232. * @param string Filename of template
  233. * @return void
  234. */
  235. function setLabelTemplate($template)
  236. {
  237. $this->_label = $template;
  238. }
  239. function _renderLabel(&$ret)
  240. {
  241. $this->_flexy->compile($this->_label);
  242. $ret->label = $this->_flexy->bufferedOutputObject($ret);
  243. }
  244. function _renderHtml(&$ret)
  245. {
  246. $this->_flexy->compile($this->_html);
  247. $ret->html = $this->_flexy->bufferedOutputObject($ret);
  248. }
  249. } // end class HTML_QuickForm_Renderer_ObjectFlexy
  250. /**
  251. * Adds nothing to QuickformForm, left for backwards compatibility
  252. *
  253. * @category HTML
  254. * @package HTML_QuickForm
  255. * @ignore
  256. */
  257. class QuickformFlexyForm extends QuickformForm
  258. {
  259. }
  260. /**
  261. * Adds nothing to QuickformElement, left for backwards compatibility
  262. *
  263. * @category HTML
  264. * @package HTML_QuickForm
  265. * @ignore
  266. */
  267. class QuickformFlexyElement extends QuickformElement
  268. {
  269. }
  270. ?>