ObjectFlexy.php 8.4 KB

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