| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
define( 'PATFORMS_RENDERER_STRING_ERROR_TEMPLATEFILE_404', 'Renderer:String:001' ); |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
define('PATFORMS_RENDERER_STRING_RENDER_ERRORS', 'renderErrors'); |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
class patForms_Renderer_String extends patForms_Renderer |
|---|
| 35 |
{ |
|---|
| 36 |
|
|---|
| 37 |
* Stores the template string used to render the form |
|---|
| 38 |
* |
|---|
| 39 |
* @access private |
|---|
| 40 |
* @var string |
|---|
| 41 |
*/ |
|---|
| 42 |
var $_template = null; |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
* Stores the name of the placeholders to use to insert |
|---|
| 46 |
* the elements and element attributes. |
|---|
| 47 |
* |
|---|
| 48 |
* @access private |
|---|
| 49 |
* @var string |
|---|
| 50 |
*/ |
|---|
| 51 |
var $_placeholder = '{PATFORMS_ELEMENT_%s}'; |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* Stores the name of the place holder attribute |
|---|
| 55 |
* |
|---|
| 56 |
* @access private |
|---|
| 57 |
* @var string |
|---|
| 58 |
*/ |
|---|
| 59 |
var $_placeholderAttribute = 'id'; |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
* Stores the name of the placeholder to use for the |
|---|
| 63 |
* opening form tag. |
|---|
| 64 |
* |
|---|
| 65 |
* @access private |
|---|
| 66 |
* @var string |
|---|
| 67 |
*/ |
|---|
| 68 |
var $_placeholderFormStart = '{PATFORMS_FORM_%s_START}'; |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
* Stores the name of the placeholder to use for the |
|---|
| 72 |
* closing form tag. |
|---|
| 73 |
* |
|---|
| 74 |
* @access private |
|---|
| 75 |
* @var string |
|---|
| 76 |
*/ |
|---|
| 77 |
var $_placeholderFormEnd = '{PATFORMS_FORM_%s_END}'; |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
* Stores the name of the placeholder to use for the |
|---|
| 81 |
* form errros |
|---|
| 82 |
* |
|---|
| 83 |
* @access private |
|---|
| 84 |
* @var string |
|---|
| 85 |
*/ |
|---|
| 86 |
var $_placeholderFormErrors = '{PATFORMS_FORM_%s_ERRORS}'; |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
* Stores the name of the element attributes that will |
|---|
| 90 |
* be replaced in the template. |
|---|
| 91 |
* |
|---|
| 92 |
* @access private |
|---|
| 93 |
* @var array |
|---|
| 94 |
*/ |
|---|
| 95 |
var $_attributes = array( |
|---|
| 96 |
'label', |
|---|
| 97 |
'title', |
|---|
| 98 |
); |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
* The format used for displaying error messages for an element |
|---|
| 102 |
* |
|---|
| 103 |
* @access private |
|---|
| 104 |
* @var string |
|---|
| 105 |
*/ |
|---|
| 106 |
var $_elementErrorLineFormat = '{ELEMENT_LABEL} : {ERROR_MESSAGE}<br />'; |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
* The format used for displaying error messages for a form |
|---|
| 110 |
* |
|---|
| 111 |
* @access private |
|---|
| 112 |
* @var string |
|---|
| 113 |
*/ |
|---|
| 114 |
var $_formErrorLineFormat = '{ERROR_MESSAGE}<br />'; |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
* Sets the template string to use to render the form |
|---|
| 118 |
* |
|---|
| 119 |
* The template can be any ASCII data (HTML, text, ...) |
|---|
| 120 |
* and has to contain placeholders in the format set |
|---|
| 121 |
* in the {@link $_placeholder} property, or the custom |
|---|
| 122 |
* format set via {@link setPlaceholder}. |
|---|
| 123 |
* |
|---|
| 124 |
* @access public |
|---|
| 125 |
* @param string $template The template string |
|---|
| 126 |
*/ |
|---|
| 127 |
function setTemplate( $template ) |
|---|
| 128 |
{ |
|---|
| 129 |
$this->_template = $template; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
* Sets the template to use from a file. |
|---|
| 134 |
* |
|---|
| 135 |
* @access public |
|---|
| 136 |
* @param string $file The file to load the template from |
|---|
| 137 |
* @return mixed $success True on success, a patError object otherwise. |
|---|
| 138 |
* @see setTemplate() |
|---|
| 139 |
*/ |
|---|
| 140 |
function setTemplateFile( $file ) |
|---|
| 141 |
{ |
|---|
| 142 |
if( !file_exists( $file ) ) { |
|---|
| 143 |
return patErrorManager::raiseError( |
|---|
| 144 |
PATFORMS_RENDERER_STRING_ERROR_TEMPLATEFILE_404, |
|---|
| 145 |
'The specified template file could not be found', |
|---|
| 146 |
'Tried to open file "'.$file.'"' |
|---|
| 147 |
); |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
$tmpl = file_get_contents( $file ); |
|---|
| 151 |
|
|---|
| 152 |
$this->setTemplate( $tmpl ); |
|---|
| 153 |
|
|---|
| 154 |
return true; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
* Sets a list of attributes to replace in the template |
|---|
| 159 |
* in addition of the default attributes list. |
|---|
| 160 |
* |
|---|
| 161 |
* @access public |
|---|
| 162 |
* @param array $attributes The list of attributes |
|---|
| 163 |
* @see $_attributes |
|---|
| 164 |
*/ |
|---|
| 165 |
function setAttributes( $attributes ) |
|---|
| 166 |
{ |
|---|
| 167 |
$this->_attributes = array_merge( $this->_attributes, $attributes ); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
* Sets the placeholder to use for the elements and the |
|---|
| 172 |
* element attributes. |
|---|
| 173 |
* |
|---|
| 174 |
* Note: make sure this has a %s where you want the element |
|---|
| 175 |
* ID to be inserted so the replacement will work. |
|---|
| 176 |
* |
|---|
| 177 |
* @access public |
|---|
| 178 |
* @param string The placeholder to use |
|---|
| 179 |
* @param string Name of the attribute that will be inserted in the placeholder |
|---|
| 180 |
*/ |
|---|
| 181 |
function setPlaceholder( $placeholder, $attribute = 'id' ) |
|---|
| 182 |
{ |
|---|
| 183 |
$this->_placeholder = $placeholder; |
|---|
| 184 |
$this->_placeholderAttribute = $attribute; |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
* Sets the placeholders to use for the opening and closing |
|---|
| 189 |
* form tags. |
|---|
| 190 |
* |
|---|
| 191 |
* Note: make sure this has a %s where you want the form |
|---|
| 192 |
* name to be inserted so the replacement will work. |
|---|
| 193 |
* |
|---|
| 194 |
* @access public |
|---|
| 195 |
* @param string $placeholderStart The placeholder for the opening form tag |
|---|
| 196 |
* @param string $placeholderEnd The placeholder for the closing form tag |
|---|
| 197 |
* @param string $placeholderErrors The placeholder for the error list |
|---|
| 198 |
*/ |
|---|
| 199 |
function setFormPlaceholders($placeholderStart, $placeholderEnd, $placeholderErrors = '{PATFORMS_FORM_%s_ERRORS}') |
|---|
| 200 |
{ |
|---|
| 201 |
$this->_placeholderFormStart = $placeholderStart; |
|---|
| 202 |
$this->_placeholderFormEnd = $placeholderEnd; |
|---|
| 203 |
$this->_placeholderFormErrors = $placeholderErrors; |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
* Set the format of one error message. |
|---|
| 208 |
* |
|---|
| 209 |
* Supported placeholders in the format strings are: |
|---|
| 210 |
* - {ERROR_MESSAGE} |
|---|
| 211 |
* - {ELEMENT_LABEL} |
|---|
| 212 |
* |
|---|
| 213 |
* @access public |
|---|
| 214 |
* @param string format for an element error |
|---|
| 215 |
* @param string format for a form error |
|---|
| 216 |
*/ |
|---|
| 217 |
function setErrorLineFormat($elementError, $formError) |
|---|
| 218 |
{ |
|---|
| 219 |
$this->_elementErrorLineFormat = $elementError; |
|---|
| 220 |
$this->_formErrorLineFormat = $formError; |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
* Gets the template for the renderer |
|---|
| 225 |
* |
|---|
| 226 |
* @access public |
|---|
| 227 |
* @return string $template The template string |
|---|
| 228 |
*/ |
|---|
| 229 |
function getTemplate() |
|---|
| 230 |
{ |
|---|
| 231 |
return $this->_template; |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
* Gathers serialized data from all elements, and returns it along with all |
|---|
| 236 |
* attributes in a handy array that can directly be added to a template to |
|---|
| 237 |
* display the form. |
|---|
| 238 |
* |
|---|
| 239 |
* @access public |
|---|
| 240 |
* @param object Reference to the patForms object |
|---|
| 241 |
* @param array Arguments for the renderer |
|---|
| 242 |
* @return string The rendered form |
|---|
| 243 |
*/ |
|---|
| 244 |
function render(&$patForms, $args = array()) |
|---|
| 245 |
{ |
|---|
| 246 |
$form = $this->getTemplate(); |
|---|
| 247 |
$elements = $patForms->getElements(); |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
// placeholders and attribute placeholders. |
|---|
| 251 |
$cnt = count( $elements ); |
|---|
| 252 |
for ($i = 0; $i < $cnt; $i++) { |
|---|
| 253 |
$el = $elements[$i]->serialize(); |
|---|
| 254 |
$var = sprintf( $this->_placeholder, strtoupper( $elements[$i]->getAttribute($this->_placeholderAttribute) ) ); |
|---|
| 255 |
$form = str_replace( $var, $el, $form ); |
|---|
| 256 |
$form = $this->_replaceAttributes( $form, $elements[$i] ); |
|---|
| 257 |
} |
|---|
| 258 |
|
|---|
| 259 |
$name = $patForms->getName(); |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
$varName = sprintf( $this->_placeholderFormStart, strtoupper( $name ) ); |
|---|
| 263 |
$form = str_replace( $varName, $patForms->serializeStart(), $form ); |
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
$varName = sprintf( $this->_placeholderFormEnd, strtoupper( $name ) ); |
|---|
| 267 |
$form = str_replace( $varName, $patForms->serializeEnd(), $form ); |
|---|
| 268 |
|
|---|
| 269 |
if (!isset($args[PATFORMS_RENDERER_STRING_RENDER_ERRORS]) || $args[PATFORMS_RENDERER_STRING_RENDER_ERRORS] !== true) { |
|---|
| 270 |
return $form; |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
if (!$patForms->isSubmitted()) { |
|---|
| 275 |
$varName = sprintf($this->_placeholderFormErrors, strtoupper($name)); |
|---|
| 276 |
$form = str_replace($varName, '', $form); |
|---|
| 277 |
return $form; |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
if ($patForms->validateForm()) { |
|---|
| 282 |
$varName = sprintf($this->_placeholderFormErrors, strtoupper($name)); |
|---|
| 283 |
$form = str_replace($varName, '', $form); |
|---|
| 284 |
return $form; |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
$validationErrors = $patForms->getValidationErrors(); |
|---|
| 288 |
$errorString = ''; |
|---|
| 289 |
|
|---|
| 290 |
foreach($validationErrors as $fieldName => $errors) { |
|---|
| 291 |
if (empty($errors)) { |
|---|
| 292 |
continue; |
|---|
| 293 |
} |
|---|
| 294 |
$field = &$patForms->getElement($fieldName); |
|---|
| 295 |
|
|---|
| 296 |
foreach ($errors as $error) { |
|---|
| 297 |
|
|---|
| 298 |
if ($fieldName === '__form') { |
|---|
| 299 |
$template = $this->_formErrorLineFormat; |
|---|
| 300 |
} else { |
|---|
| 301 |
$template = $this->_elementErrorLineFormat; |
|---|
| 302 |
$template = str_replace('{ELEMENT_LABEL}', $field->getAttribute('label'), $template); |
|---|
| 303 |
} |
|---|
| 304 |
$template = str_replace('{ERROR_MESSAGE}', $error['message'], $template); |
|---|
| 305 |
$errorString = $errorString . $template; |
|---|
| 306 |
} |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
$varName = sprintf($this->_placeholderFormErrors, strtoupper($name)); |
|---|
| 311 |
$form = str_replace($varName, $errorString, $form); |
|---|
| 312 |
|
|---|
| 313 |
return $form; |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
* Replaces an element's attributes in the form template |
|---|
| 318 |
* |
|---|
| 319 |
* Note: only a selection of element attributes are replaced |
|---|
| 320 |
* per default; if you want more to be replaced, set them |
|---|
| 321 |
* with the {@link setAttributes()} method. |
|---|
| 322 |
* |
|---|
| 323 |
* Have a look at the {@link $_attributes} property to see |
|---|
| 324 |
* which attributes are replaced per default. |
|---|
| 325 |
* |
|---|
| 326 |
* @access private |
|---|
| 327 |
* @param string $form The form template |
|---|
| 328 |
* @param object &$element The element |
|---|
| 329 |
* @return string $form The form template, with all needed attributes replaced |
|---|
| 330 |
* @see $_attributes |
|---|
| 331 |
*/ |
|---|
| 332 |
function _replaceAttributes( $form, &$element ) |
|---|
| 333 |
{ |
|---|
| 334 |
foreach( $this->_attributes as $attribute ) |
|---|
| 335 |
{ |
|---|
| 336 |
$varName = sprintf( $this->_placeholder, strtoupper( $element->getId().'_'.$attribute ) ); |
|---|
| 337 |
$form = str_replace( $varName, $element->getAttribute( $attribute ), $form ); |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
return $form; |
|---|
| 341 |
} |
|---|
| 342 |
} |
|---|
| 343 |
?> |
|---|