ObjectFlexy.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Ron McClain <ron@humaniq.com> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: ObjectFlexy.php 20456 2009-05-10 17:27:44Z ivantcholakov $
  20. require_once("HTML/QuickForm/Renderer/Object.php");
  21. /**
  22. * QuickForm renderer for Flexy template engine, static version.
  23. *
  24. * A static renderer for HTML_Quickform. Makes a QuickFormFlexyObject
  25. * from the form content suitable for use with a Flexy template
  26. *
  27. * Usage:
  28. * $form =& new HTML_QuickForm('form', 'POST');
  29. * $template =& new HTML_Template_Flexy();
  30. * $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
  31. * $renderer->setHtmlTemplate("html.html");
  32. * $renderer->setLabelTemplate("label.html");
  33. * $form->accept($renderer);
  34. * $view = new StdClass;
  35. * $view->form = $renderer->toObject();
  36. * $template->compile("mytemplate.html");
  37. *
  38. * Based on the code for HTML_QuickForm_Renderer_ArraySmarty
  39. *
  40. * @see QuickFormFlexyObject
  41. * @access public
  42. */
  43. class HTML_QuickForm_Renderer_ObjectFlexy extends HTML_QuickForm_Renderer_Object
  44. {
  45. /**
  46. * HTML_Template_Flexy instance
  47. * @var object $_flexy
  48. */
  49. var $_flexy;
  50. /**
  51. * Current element index
  52. * @var integer $_elementIdx
  53. */
  54. var $_elementIdx;
  55. /**
  56. * The current element index inside a group
  57. * @var integer $_groupElementIdx
  58. */
  59. var $_groupElementIdx = 0;
  60. /**
  61. * Name of template file for form html
  62. * @var string $_html
  63. * @see setRequiredTemplate()
  64. */
  65. var $_html = '';
  66. /**
  67. * Name of template file for form labels
  68. * @var string $label
  69. * @see setErrorTemplate()
  70. */
  71. var $label = '';
  72. /**
  73. * Class of the element objects, so you can add your own
  74. * element methods
  75. * @var string $_elementType
  76. */
  77. var $_elementType = 'QuickformFlexyElement';
  78. /**
  79. * Constructor
  80. *
  81. * @param $flexy object HTML_Template_Flexy instance
  82. * @public
  83. */
  84. function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy)
  85. {
  86. $this->HTML_QuickForm_Renderer_Object(true);
  87. $this->_obj = new QuickformFlexyForm();
  88. $this->_flexy =& $flexy;
  89. } // end constructor
  90. function renderHeader(&$header)
  91. {
  92. if($name = $header->getName()) {
  93. $this->_obj->header->$name = $header->toHtml();
  94. } else {
  95. $this->_obj->header[$this->_sectionCount] = $header->toHtml();
  96. }
  97. $this->_currentSection = $this->_sectionCount++;
  98. } // end func renderHeader
  99. function startGroup(&$group, $required, $error)
  100. {
  101. parent::startGroup($group, $required, $error);
  102. $this->_groupElementIdx = 1;
  103. } //end func startGroup
  104. /**
  105. * Creates an object representing an element containing
  106. * the key for storing this
  107. *
  108. * @access private
  109. * @param element object An HTML_QuickForm_element object
  110. * @param required bool Whether an element is required
  111. * @param error string Error associated with the element
  112. * @return object
  113. */
  114. function _elementToObject(&$element, $required, $error)
  115. {
  116. $ret = parent::_elementToObject($element, $required, $error);
  117. if($ret->type == 'group') {
  118. $ret->html = $element->toHtml();
  119. unset($ret->elements);
  120. }
  121. if(!empty($this->_label)) {
  122. $this->_renderLabel($ret);
  123. }
  124. if(!empty($this->_html)) {
  125. $this->_renderHtml($ret);
  126. $ret->error = $error;
  127. }
  128. // Create an element key from the name
  129. if (false !== ($pos = api_strpos($ret->name, '[')) || is_object($this->_currentGroup)) {
  130. if (!$pos) {
  131. $keys = '->{\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret->name) . '\'}';
  132. } else {
  133. $keys = '->{\'' . str_replace(
  134. array('\\', '\'', '[', ']'), array('\\\\', '\\\'', '\'}->{\'', ''),
  135. $ret->name
  136. ) . '\'}';
  137. }
  138. // special handling for elements in native groups
  139. if (is_object($this->_currentGroup)) {
  140. // skip unnamed group items unless radios: no name -> no static access
  141. // identification: have the same key string as the parent group
  142. if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type) {
  143. return false;
  144. }
  145. // reduce string of keys by remove leading group keys
  146. if (0 === api_strpos($keys, $this->_currentGroup->keys)) {
  147. $keys = api_substr_replace($keys, '', 0, api_strlen($this->_currentGroup->keys));
  148. }
  149. }
  150. } elseif (0 == api_strlen($ret->name)) {
  151. $keys = '->{\'element_' . $this->_elementIdx . '\'}';
  152. } else {
  153. $keys = '->{\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret->name) . '\'}';
  154. }
  155. // for radios: add extra key from value
  156. if ('radio' == $ret->type && '[]' != api_substr($keys, -2)) {
  157. $keys .= '->{\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret->value) . '\'}';
  158. }
  159. $ret->keys = $keys;
  160. $this->_elementIdx++;
  161. return $ret;
  162. }
  163. /**
  164. * Stores an object representation of an element in the
  165. * QuickformFormObject instance
  166. *
  167. * @access private
  168. * @param elObj object Object representation of an element
  169. * @return void
  170. */
  171. function _storeObject($elObj)
  172. {
  173. if ($elObj) {
  174. $keys = $elObj->keys;
  175. unset($elObj->keys);
  176. if(is_object($this->_currentGroup) && ('group' != $elObj->type)) {
  177. $code = '$this->_currentGroup' . $keys . ' = $elObj;';
  178. } else {
  179. $code = '$this->_obj' . $keys . ' = $elObj;';
  180. }
  181. eval($code);
  182. }
  183. }
  184. /**
  185. * Set the filename of the template to render html elements.
  186. * In your template, {html} is replaced by the unmodified html.
  187. * If the element is required, {required} will be true.
  188. * Eg.
  189. * {if:error}
  190. * <font color="red" size="1">{error:h}</font><br />
  191. * {end:}
  192. * {html:h}
  193. *
  194. * @access public
  195. * @param template string Filename of template
  196. * @return void
  197. */
  198. function setHtmlTemplate($template)
  199. {
  200. $this->_html = $template;
  201. }
  202. /**
  203. * Set the filename of the template to render form labels
  204. * In your template, {label} is replaced by the unmodified label.
  205. * {error} will be set to the error, if any. {required} will
  206. * be true if this is a required field
  207. * Eg.
  208. * {if:required}
  209. * <font color="orange" size="1">*</font>
  210. * {end:}
  211. * {label:h}
  212. *
  213. * @access public
  214. * @param template string Filename of template
  215. * @return void
  216. */
  217. function setLabelTemplate($template)
  218. {
  219. $this->_label = $template;
  220. }
  221. function _renderLabel(&$ret)
  222. {
  223. $this->_flexy->compile($this->_label);
  224. $ret->label = $this->_flexy->bufferedOutputObject($ret);
  225. }
  226. function _renderHtml(&$ret)
  227. {
  228. $this->_flexy->compile($this->_html);
  229. $ret->html = $this->_flexy->bufferedOutputObject($ret);
  230. }
  231. } // end class HTML_QuickForm_Renderer_ObjectFlexy
  232. /**
  233. * Adds nothing to QuickformForm, left for backwards compatibility
  234. */
  235. class QuickformFlexyForm extends QuickformForm
  236. {
  237. }
  238. /**
  239. * Adds nothing to QuickformElement, left for backwards compatibility
  240. */
  241. class QuickformFlexyElement extends QuickformElement
  242. {
  243. }
  244. ?>