PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/patForms/Parser/patTemplateRenderer.php

https://github.com/chregu/fluxcms
PHP | 243 lines | 127 code | 28 blank | 88 comment | 13 complexity | 0d2462b130acf0ad76905378f12180b8 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?PHP
  2. /**
  3. * Renderer based on patForms_Parser and patTemplate
  4. *
  5. * $Id$
  6. *
  7. * @author Stephan Schmidt <s.schmidt@metrix.de>
  8. * @package patForms
  9. * @subpackage Parser
  10. * @license LGPL
  11. * @copyright PHP Application Tools <http://www.php-tools.net>
  12. */
  13. /**
  14. * Renderer based on patForms_Parser and patTemplate
  15. *
  16. * Use this parser, if you want to use the forms together
  17. * with patTemplate
  18. *
  19. * Possible arguements to renderForm():
  20. * - template : name of the template (_not_ filename) in which the elements will be added
  21. * - errorTemplate : name of the template to which the error messages will be added (will be repeated, if more than one error occured)
  22. * - errorTemplateContainer : name of the template which contains the errorTemplate. If errors occured, its visibility will be set to visible
  23. *
  24. * @author Stephan Schmidt <s.schmidt@metrix.de>
  25. * @package patForms
  26. * @subpackage Parser
  27. * @license LGPL
  28. * @copyright PHP Application Tools <http://www.php-tools.net>
  29. * @version 1.0
  30. */
  31. class patForms_Parser_patTemplateRenderer extends patForms_Parser
  32. {
  33. /**
  34. * patTemplate object
  35. * @access private
  36. */
  37. var $_tmpl = null;
  38. /**
  39. * checks whether errors have been rendered or not
  40. * @access private
  41. */
  42. var $errorsRendered = array();
  43. /**
  44. * set the reference to the patTemplate object
  45. *
  46. * @access public
  47. * @param object patTemplate object
  48. */
  49. function setTemplate( &$tmpl )
  50. {
  51. $this->_tmpl = &$tmpl;
  52. }
  53. /**
  54. * gathers serialized data from all elements and replaces them in the outputFile.
  55. *
  56. * @access public
  57. * @param object &$patForms Reference to the patForms object
  58. * @param mixed $args optional arguments
  59. * @return string $html HTML code
  60. */
  61. function render( &$patForms, $args = null )
  62. {
  63. $serializedElements = array();
  64. $elementAttribs = array();
  65. $cnt = count( $patForms->elements );
  66. for( $i=0; $i < $cnt; $i++ )
  67. {
  68. // first, serialize the element as this also initializes the attribute collection.
  69. $serialized = $patForms->elements[$i]->serialize();
  70. if( $serialized === false )
  71. {
  72. patErrorManager::raiseWarning(
  73. PATFORMS_PARSER_ERROR_ELEMENT_NOT_SERIALIZEABLE,
  74. 'Element \''.get_class( $patForms->elements[$i] ).'\' could not return serialized data.'
  75. );
  76. continue;
  77. }
  78. $elName = $patForms->elements[$i]->getName();
  79. $serializedElements[$elName] = $serialized;
  80. $elementAttribs[$elName] = $patForms->elements[$i]->getAttributes();
  81. }
  82. // no template has been specified => use the default
  83. if( $this->_tmpl == null )
  84. {
  85. $this->_tmpl = &patForms_Parser::getStaticProperty( 'tmpl' );
  86. }
  87. // check, whether the file has been loaded
  88. if( !$this->_tmpl->exists( $args['template'] ) )
  89. {
  90. $this->_tmpl->readTemplatesFromFile( $this->_outputFile );
  91. }
  92. foreach( $serializedElements as $name => $element )
  93. {
  94. $this->_tmpl->addVar( $args['template'], sprintf( $this->_placeholder, $name ), $element );
  95. // copy the attribute collection
  96. $tmplVars = $elementAttribs[$name];
  97. // remove any arrays in the variables to add, as that could
  98. // lead to problems with the patTemplate output.
  99. foreach( $tmplVars as $key => $val )
  100. if( is_array( $val ) )
  101. $tmplVars[$key] = '';
  102. // add the attribute collection as vars to the template
  103. $this->_tmpl->addVars( $args['template'], $tmplVars, $name.'_' );
  104. }
  105. $this->_tmpl->addVar( $args['template'], sprintf( $this->_placeholder_form_start, $patForms->getName() ), $patForms->serializeStart() );
  106. $this->_tmpl->addVar( $args['template'], sprintf( $this->_placeholder_form_end, $patForms->getName() ), $patForms->serializeEnd() );
  107. if( !isset( $args['errorTemplate'] ) )
  108. {
  109. return true;
  110. }
  111. return $this->_renderErrors( $patForms, $args );
  112. }
  113. /**
  114. * render the errors
  115. *
  116. * @access private
  117. * @todo check for special '__form' element
  118. */
  119. function _renderErrors( &$patForms, $args = null )
  120. {
  121. if( isset( $this->errorsRendered[$patForms->getName()] ) )
  122. {
  123. return true;
  124. }
  125. /**
  126. * render the errors
  127. */
  128. if( $patForms->isSubmitted() && !$patForms->validateForm() )
  129. {
  130. if( isset( $args['errorTemplateContainer'] ) )
  131. {
  132. $this->_tmpl->setAttribute( $args['errorTemplateContainer'], 'visibility', 'visible' );
  133. }
  134. $validationErrors = $patForms->getValidationErrors();
  135. foreach( $validationErrors as $fieldName => $errors )
  136. {
  137. if( empty( $errors ) )
  138. {
  139. continue;
  140. }
  141. $field =& $patForms->getElement( $fieldName );
  142. // workaround for patTemplate Bug! - an array in the
  143. // added attribute collection could lead to the
  144. // template to be repeated too many times.
  145. $atts = $field->getAttributes();
  146. foreach( $atts as $key => $value )
  147. {
  148. if( is_array( $atts[$key] ) )
  149. {
  150. unset( $atts[$key] );
  151. }
  152. }
  153. $this->_tmpl->addVars( $args['errorTemplate'], $atts, 'FIELD_' );
  154. foreach( $errors as $error )
  155. {
  156. $error['field'] = $fieldName;
  157. $this->_tmpl->addVars( $args['errorTemplate'], $error, 'ERROR_' );
  158. $this->_tmpl->parseTemplate( $args['errorTemplate'], 'a' );
  159. }
  160. }
  161. }
  162. $this->errorsRendered[$patForms->getName()] = true;
  163. return true;
  164. }
  165. /**
  166. * get the placeholder for an element
  167. *
  168. * @access protected
  169. * @param string element name
  170. * @param string name of the placeholder template
  171. * @return string placeholder
  172. */
  173. function _getPlaceholderForElement( $element, $template = 'placeholder' )
  174. {
  175. // adjust the case
  176. switch( $this->_placeholder_case )
  177. {
  178. case 'upper':
  179. $element = strtoupper( $element );
  180. break;
  181. case 'lower':
  182. $element = strtolower( $element );
  183. break;
  184. default:
  185. break;
  186. }
  187. return sprintf( '{'.$this->{'_'.$template}.'}', $element );
  188. }
  189. /**
  190. * get the placeholder for a form tag
  191. *
  192. * @access protected
  193. * @param string name of the form
  194. * @param string type (start|end)
  195. * @return string placeholder
  196. */
  197. function _getPlaceholderForForm( $form, $type )
  198. {
  199. // adjust the case
  200. switch( $this->_placeholder_case )
  201. {
  202. case 'upper':
  203. $form = strtoupper( $form );
  204. break;
  205. case 'lower':
  206. $form = strtolower( $form );
  207. break;
  208. default:
  209. break;
  210. }
  211. $template = '_placeholder_form_'.$type;
  212. return sprintf( '{'.$this->$template.'}', $form );
  213. }
  214. }
  215. ?>