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