| <?php |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class HTML_QuickForm2_Renderer_Default extends HTML_QuickForm2_Renderer |
| { |
| |
| |
| |
| |
| public $hasRequired = false; |
|
|
| |
| |
| |
| |
| public $html = array(array()); |
|
|
| |
| |
| |
| |
| public $hiddenHtml = ''; |
|
|
| |
| |
| |
| |
| public $errors = array(); |
|
|
| |
| |
| |
| |
| public $templatesForClass = array( |
| 'html_quickform2_element_inputhidden' => '<div style="display: none;">{element}</div>', |
| 'html_quickform2' => '<div class="quickform">{errors}<form{attributes}>{hidden}{content}</form><qf:reqnote><div class="reqnote">{reqnote}</div></qf:reqnote></div>', |
| 'html_quickform2_container_fieldset' => '<fieldset{attributes}><qf:label><legend id="{id}-legend">{label}</legend></qf:label>{content}</fieldset>', |
| 'special:error' => array( |
| 'prefix' => '<div class="errors"><qf:message><p>{message}</p></qf:message><ul><li>', |
| 'separator' => '</li><li>', |
| 'suffix' => '</li></ul><qf:message><p>{message}</p></qf:message></div>' |
| ), |
| 'html_quickform2_element' => '<div class="row"><label for="{id}" class="element"><qf:required><span class="required">* </span></qf:required>{label}</label><div class="element<qf:error> error</qf:error>"><qf:error><span class="error">{error}</span><br /></qf:error>{element}</div></div>', |
| 'html_quickform2_container_group' => '<div class="row"><label class="element"><qf:required><span class="required">* </span></qf:required>{label}</label><div class="element group<qf:error> error</qf:error>"><qf:error><span class="error">{error}</span><br /></qf:error>{content}</div></div>' |
| ); |
|
|
| |
| |
| |
| |
| public $templatesForId = array(); |
|
|
| |
| |
| |
| |
| |
| |
| |
| public $elementTemplatesForGroupClass = array( |
| 'html_quickform2_container' => array( |
| 'html_quickform2_element' => '{element}', |
| 'html_quickform2_container_fieldset' => '<fieldset{attributes}><qf:label><legend id="{id}-legend">{label}</legend></qf:label>{content}</fieldset>' |
| ) |
| ); |
|
|
| |
| |
| |
| |
| |
| |
| |
| public $elementTemplatesForGroupId = array(); |
|
|
| |
| |
| |
| |
| public $groupId = array(); |
|
|
| public function exportMethods() |
| { |
| return array( |
| 'reset', |
| 'setTemplateForClass', |
| 'setTemplateForId', |
| 'setErrorTemplate', |
| 'setGroupedTemplateForClass', |
| 'setElementTemplateForGroupClass', |
| 'setElementTemplateForGroupId' |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function setTemplateForClass($className, $template) |
| { |
| $this->templatesForClass[strtolower($className)] = $template; |
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function setTemplateForId($id, $template) |
| { |
| $this->templatesForId[$id] = $template; |
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function setErrorTemplate(array $template) |
| { |
| return $this->setTemplateForClass('special:error', $template); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function setElementTemplateForGroupClass($groupClass, $elementClass, $template) |
| { |
| $this->elementTemplatesForGroupClass[strtolower($groupClass)][strtolower($elementClass)] = $template; |
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function setElementTemplateForGroupId($groupId, $elementClass, $template) |
| { |
| $this->elementTemplatesForGroupId[$groupId][strtolower($elementClass)] = $template; |
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function reset() |
| { |
| $this->html = array(array()); |
| $this->hiddenHtml = ''; |
| $this->errors = array(); |
| $this->hasRequired = false; |
| $this->groupId = array(); |
|
|
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| public function __toString() |
| { |
| return (isset($this->html[0][0])? $this->html[0][0]: '') . |
| $this->hiddenHtml; |
| } |
|
|
| |
| |
| |
| |
| |
| public function renderElement(HTML_QuickForm2_Node $element) |
| { |
| $elTpl = $this->prepareTemplate($this->findTemplate($element), $element); |
| $this->html[count($this->html) - 1][] = str_replace(array('{element}', '{id}'), |
| array($element, $element->getId()), $elTpl); |
| } |
|
|
| |
| |
| |
| |
| |
| public function renderHidden(HTML_QuickForm2_Node $element) |
| { |
| if ($this->options['group_hiddens']) { |
| $this->hiddenHtml .= $element->__toString(); |
| } else { |
| $this->html[count($this->html) - 1][] = str_replace('{element}', $element, |
| $this->findTemplate($element)); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| public function startContainer(HTML_QuickForm2_Node $container) |
| { |
| $this->html[] = array(); |
| $this->groupId[] = false; |
| } |
|
|
| |
| |
| |
| |
| |
| public function finishContainer(HTML_QuickForm2_Node $container) |
| { |
| array_pop($this->groupId); |
|
|
| $cTpl = str_replace( |
| array('{attributes}', '{id}'), |
| array($container->getAttributes(true), $container->getId()), |
| $this->prepareTemplate($this->findTemplate($container, '{content}'), $container) |
| ); |
| $cHtml = array_pop($this->html); |
| $break = HTML_Common2::getOption('linebreak'); |
| $indent = str_repeat(HTML_Common2::getOption('indent'), count($this->html)); |
| $this->html[count($this->html) - 1][] = str_replace( |
| '{content}', $break . $indent . implode($break . $indent, $cHtml), $cTpl |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| public function startGroup(HTML_QuickForm2_Node $group) |
| { |
| $this->html[] = array(); |
| $this->groupId[] = $group->getId(); |
| } |
|
|
| |
| |
| |
| |
| |
| public function finishGroup(HTML_QuickForm2_Node $group) |
| { |
| $gTpl = str_replace( |
| array('{attributes}', '{id}'), |
| array($group->getAttributes(true), array_pop($this->groupId)), |
| $this->prepareTemplate($this->findTemplate($group, '{content}'), $group) |
| ); |
|
|
| $separator = $group->getSeparator(); |
| $elements = array_pop($this->html); |
| if (!is_array($separator)) { |
| $content = implode((string)$separator, $elements); |
| } else { |
| $content = ''; |
| $cSeparator = count($separator); |
| for ($i = 0, $count = count($elements); $i < $count; $i++) { |
| $content .= (0 == $i? '': $separator[($i - 1) % $cSeparator]) . |
| $elements[$i]; |
| } |
| } |
|
|
| $this->html[count($this->html) - 1][] = str_replace('{content}', $content, $gTpl); |
| } |
|
|
| |
| |
| |
| |
| |
| public function startForm(HTML_QuickForm2_Node $form) |
| { |
| $this->reset(); |
| } |
|
|
| |
| |
| |
| |
| |
| public function finishForm(HTML_QuickForm2_Node $form) |
| { |
| $formTpl = str_replace( |
| array('{attributes}', '{hidden}', '{errors}'), |
| array($form->getAttributes(true), $this->hiddenHtml, |
| $this->outputGroupedErrors()), |
| $this->findTemplate($form, '{content}') |
| ); |
| $this->hiddenHtml = ''; |
|
|
| |
| if (!$this->hasRequired || $form->toggleFrozen() || |
| empty($this->options['required_note'])) |
| { |
| $formTpl = preg_replace('!<qf:reqnote>.*</qf:reqnote>!isU', '', $formTpl); |
| } else { |
| $formTpl = str_replace( |
| array('<qf:reqnote>', '</qf:reqnote>', '{reqnote}'), |
| array('', '', $this->options['required_note']), |
| $formTpl |
| ); |
| } |
|
|
| $break = HTML_Common2::getOption('linebreak'); |
| $script = $this->getJavascriptBuilder()->__toString(); |
| $this->html[0] = array((empty($script)? '': $script . $break) . str_replace( |
| '{content}', $break . implode($break, $this->html[0]), $formTpl |
| )); |
| } |
|
|
| |
| |
| |
| |
| |
| public function outputGroupedErrors() |
| { |
| if (empty($this->errors)) { |
| return ''; |
| } |
| if (!empty($this->options['errors_prefix'])) { |
| $errorHtml = str_replace(array('<qf:message>', '</qf:message>', '{message}'), |
| array('', '', $this->options['errors_prefix']), |
| $this->templatesForClass['special:error']['prefix']); |
| } else { |
| $errorHtml = preg_replace('!<qf:message>.*</qf:message>!isU', '', |
| $this->templatesForClass['special:error']['prefix']); |
| } |
| $errorHtml .= implode($this->templatesForClass['special:error']['separator'], $this->errors); |
| if (!empty($this->options['errors_suffix'])) { |
| $errorHtml .= str_replace(array('<qf:message>', '</qf:message>', '{message}'), |
| array('', '', $this->options['errors_suffix']), |
| $this->templatesForClass['special:error']['suffix']); |
| } else { |
| $errorHtml .= preg_replace('!<qf:message>.*</qf:message>!isU', '', |
| $this->templatesForClass['special:error']['suffix']); |
| } |
| return $errorHtml; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function findTemplate(HTML_QuickForm2_Node $element, $default = '{element}') |
| { |
| if (!empty($this->templatesForId[$element->getId()])) { |
| return $this->templatesForId[$element->getId()]; |
| } |
| $class = strtolower(get_class($element)); |
| $groupId = end($this->groupId); |
| $elementClasses = array(); |
| do { |
| if (empty($groupId) && !empty($this->templatesForClass[$class])) { |
| return $this->templatesForClass[$class]; |
| } |
| $elementClasses[$class] = true; |
| } while ($class = strtolower(get_parent_class($class))); |
|
|
| if (!empty($groupId)) { |
| if (!empty($this->elementTemplatesForGroupId[$groupId])) { |
| while (list($elClass) = each($elementClasses)) { |
| if (!empty($this->elementTemplatesForGroupId[$groupId][$elClass])) { |
| return $this->elementTemplatesForGroupId[$groupId][$elClass]; |
| } |
| } |
| } |
|
|
| $group = $element->getContainer(); |
| $grClass = strtolower(get_class($group)); |
| do { |
| if (!empty($this->elementTemplatesForGroupClass[$grClass])) { |
| reset($elementClasses); |
| while (list($elClass) = each($elementClasses)) { |
| if (!empty($this->elementTemplatesForGroupClass[$grClass][$elClass])) { |
| return $this->elementTemplatesForGroupClass[$grClass][$elClass]; |
| } |
| } |
| } |
| } while ($grClass = strtolower(get_parent_class($grClass))); |
| } |
| return $default; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function prepareTemplate($elTpl, HTML_QuickForm2_Node $element) |
| { |
| |
| $elTpl = $this->markRequired($elTpl, $element->isRequired()); |
| $elTpl = $this->outputError($elTpl, $element->getError()); |
| return $this->outputLabel($elTpl, $element->getLabel()); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function markRequired($elTpl, $required) |
| { |
| if ($required) { |
| $this->hasRequired = true; |
| $elTpl = str_replace(array('<qf:required>', '</qf:required>'), |
| array('', ''), $elTpl); |
| } else { |
| $elTpl = preg_replace('!<qf:required>.*</qf:required>!isU', '', $elTpl); |
| } |
| return $elTpl; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function outputError($elTpl, $error) |
| { |
| if ($error && !$this->options['group_errors']) { |
| $elTpl = str_replace(array('<qf:error>', '</qf:error>', '{error}'), |
| array('', '', $error), $elTpl); |
| } else { |
| if ($error && $this->options['group_errors']) { |
| $this->errors[] = $error; |
| } |
| $elTpl = preg_replace('!<qf:error>.*</qf:error>!isU', '', $elTpl); |
| } |
| return $elTpl; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function outputLabel($elTpl, $label) |
| { |
| $mainLabel = is_array($label)? array_shift($label): $label; |
| $elTpl = str_replace('{label}', $mainLabel, $elTpl); |
| if (false !== strpos($elTpl, '<qf:label>')) { |
| if ($mainLabel) { |
| $elTpl = str_replace(array('<qf:label>', '</qf:label>'), array('', ''), $elTpl); |
| } else { |
| $elTpl = preg_replace('!<qf:label>.*</qf:label>!isU', '', $elTpl); |
| } |
| } |
| if (is_array($label)) { |
| foreach($label as $key => $text) { |
| $key = is_int($key)? $key + 2: $key; |
| $elTpl = str_replace(array('<qf:label_' . $key . '>', '</qf:label_' . $key . '>', '{label_' . $key . '}'), |
| array('', '', $text), $elTpl); |
| } |
| } |
| if (strpos($elTpl, '{label_')) { |
| $elTpl = preg_replace('!<qf:label_([^>]+)>.*</qf:label_\1>!isU', '', $elTpl); |
| } |
| return $elTpl; |
| } |
| } |
| ?> |
|
|