Changeset 320 for trunk/patForms/Parser
- Timestamp:
- 08/13/05 10:45:45 (3 years ago)
- Files:
-
- trunk/patForms/Parser/SimpleRenderer.php (modified) (2 diffs)
- trunk/patForms/Parser/patTemplateRenderer.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/patForms/Parser/SimpleRenderer.php
r317 r320 28 28 * @license LGPL 29 29 * @copyright PHP Application Tools <http://www.php-tools.net> 30 * @version 1.031 30 */ 32 31 class patForms_Parser_SimpleRenderer extends patForms_Parser … … 42 41 function render(&$patForms, $args = null) 43 42 { 44 $serializedElements = array(); 45 46 $cnt = count($patForms->elements); 47 for ($i=0; $i < $cnt; $i++) { 48 // first, serialize the element as this also initializes the attribute collection. 49 $serialized = $patForms->elements[$i]->serialize(); 50 if ($serialized === false) { 51 patErrorManager::raiseWarning( 52 PATFORMS_PARSER_ERROR_ELEMENT_NOT_SERIALIZEABLE, 53 "Element '".get_class($patForms->elements[$i])."' could not return serialized data." 54 ); 55 continue; 56 } 57 $serializedElements[($patForms->elements[$i]->getName())] = $serialized; 58 } 59 $html = $this->getHTML(); 60 foreach ($serializedElements as $name => $element) { 61 $varName = sprintf("{PATFORMS_ELEMENT_%s}", strtoupper($name)); 62 $html = str_replace($varName, $element, $html); 63 } 64 65 $name = $patForms->getName(); 66 $varName = sprintf("{PATFORMS_FORM_%s_START}", strtoupper($name)); 67 $html = str_replace($varName, $patForms->serializeStart(), $html); 68 69 $varName = sprintf("{PATFORMS_FORM_%s_END}", strtoupper($name)); 70 $html = str_replace($varName, $patForms->serializeEnd(), $html); 71 return $html; 43 $renderer = &patForms::createRenderer('String'); 44 $renderer->setTemplate($this->getHTML()); 45 $renderer->setPlaceholder($this->_placeholder, 'id'); 46 $renderer->setFormPlaceholders($this->_placeholder_form_start, $this->_placeholder_form_end); 47 48 return $renderer->render($patForms, $args); 72 49 } 73 50 } trunk/patForms/Parser/patTemplateRenderer.php
r110 r320 50 50 * @param object patTemplate object 51 51 */ 52 function setTemplate( &$tmpl)52 function setTemplate(&$tmpl) 53 53 { 54 $this->_tmpl =&$tmpl;54 $this->_tmpl = &$tmpl; 55 55 } 56 56 … … 63 63 * @return string $html HTML code 64 64 */ 65 function render( &$patForms, $args = null)65 function render(&$patForms, $args = null) 66 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' ); 67 if ($this->_tmpl == null) { 68 $this->_tmpl = &patForms_Parser::getStaticProperty('tmpl'); 92 69 } 93 70 71 $renderer = &patForms::createRenderer('patTemplate'); 72 $renderer = &new patForms_Renderer_patTemplate(); 73 $renderer->setTemplate($this->_tmpl); 74 75 $renderer->setPlaceholder($this->_placeholder); 76 $renderer->setFormPlaceholders($this->_placeholder_form_start, $this->_placeholder_form_end); 77 94 78 // check, whether the file has been loaded 95 if( !$this->_tmpl->exists( $args['template'] ) ) 96 { 97 $this->_tmpl->readTemplatesFromFile( $this->_outputFile ); 79 if (!$this->_tmpl->exists($args['template'])) { 80 $this->_tmpl->readTemplatesFromFile($this->_outputFile); 98 81 } 99 82 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 ); 83 return $renderer->render($patForms, $args); 127 84 } 128 85 129 /**130 * render the errors131 *132 * @access private133 * @todo check for special '__form' element134 */135 function _renderErrors( &$patForms, $args = null )136 {137 if( isset( $this->errorsRendered[$patForms->getName()] ) )138 {139 return true;140 }141 142 /**143 * render the errors144 */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 the163 // added attribute collection could lead to the164 // 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 86 /** 191 87 * get the placeholder for an element
