Skip to content
Snippets Groups Projects
Commit 497dbba6 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Je sens que ça va gueuler...

Modif Aide de vue FormControlGroup : 
- Rendu particulier des checkbox : case à cocher avant label ; sur une ligne (cf. unicaen.css).
- Refactorisation : extraction de méthodes.
parent 91be51b3
No related branches found
No related tags found
No related merge requests found
...@@ -195,6 +195,16 @@ div.multicheckbox label input[type=radio] { ...@@ -195,6 +195,16 @@ div.multicheckbox label input[type=radio] {
.form-group input[type="checkbox"] {
display: inline;
vertical-align: text-bottom;
margin-right: 5px;
}
.form-group label {
display: inline;
}
.loading { .loading {
background-image: url('//gest.unicaen.fr/images/ajax-loader-h.gif'); background-image: url('//gest.unicaen.fr/images/ajax-loader-h.gif');
background-repeat: no-repeat; background-repeat: no-repeat;
......
...@@ -6,8 +6,8 @@ use UnicaenApp\Exception\LogicException; ...@@ -6,8 +6,8 @@ use UnicaenApp\Exception\LogicException;
use UnicaenApp\Form\Element\Date; use UnicaenApp\Form\Element\Date;
use UnicaenApp\Form\Element\DateInfSup; use UnicaenApp\Form\Element\DateInfSup;
use UnicaenApp\Form\Element\SearchAndSelect; use UnicaenApp\Form\Element\SearchAndSelect;
use Zend\Form\Element;
use Zend\Form\Element\Button; use Zend\Form\Element\Button;
use Zend\Form\Element\Checkbox;
use Zend\Form\Element\MultiCheckbox; use Zend\Form\Element\MultiCheckbox;
use Zend\Form\ElementInterface; use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\AbstractHelper; use Zend\Form\View\Helper\AbstractHelper;
...@@ -60,13 +60,8 @@ class FormControlGroup extends AbstractHelper ...@@ -60,13 +60,8 @@ class FormControlGroup extends AbstractHelper
*/ */
public function render(ElementInterface $element, $pluginClass = 'formElement') public function render(ElementInterface $element, $pluginClass = 'formElement')
{ {
if (!$element instanceof Button) { $this->normalizeElement($element);
$class = $element->getAttribute('class');
$labelClass = array_key_exists('class', $tmp = (array)$element->getLabelAttributes()) ? $tmp['class'] : '';
$element
->setAttribute('class', $class . ' form-control')
->setLabelAttributes(['class' => $labelClass . ' control-label']);
}
if ($element instanceof Date) { if ($element instanceof Date) {
$helper = $this->getView()->plugin('formDate'); $helper = $this->getView()->plugin('formDate');
...@@ -78,81 +73,137 @@ class FormControlGroup extends AbstractHelper ...@@ -78,81 +73,137 @@ class FormControlGroup extends AbstractHelper
return $helper($element); return $helper($element);
} }
if (!$pluginClass) { $input = $this->inputHtml($element, $pluginClass);
$pluginClass = 'formElement';
$label = $this->labelHtml($element);
$clearButton = $this->addClearButton ?
'<span class="input-group-btn"><button class="btn btn-default btn-xs" type="button" title="Vider" onclick="$(this).siblings(\':input\').val(null).focus();"><span class="glyphicon glyphicon-remove"></span></button></span>' :
null;
$helpContentBefore = null;
if (!empty($this->helpContent['before'])) {
$helpContentBefore = sprintf('<p class="help-block help-block-before">%s<p>', $this->helpContent['before']);
}
$helpContentAfter = null;
if (!empty($this->helpContent['after'])) {
$helpContentAfter = sprintf('<p class="help-block help-block-after">%s<p>', $this->helpContent['after']);
} }
if ($element instanceof SearchAndSelect) { $errors = $this->errorsHtml($element);
/** @var FormSearchAndSelect $helper */
$helper = $this->getView()->plugin('formSearchAndSelect'); $class = [];
$helper->setAutocompleteMinLength(2); $class[] = $this->addClearButton ? 'input-group' : null;
$input = $helper($element); $class[] = $errors ? 'has-error' : null;
} else { $class = implode(' ', $class);
if (is_string($pluginClass)) {
$helper = $this->getView()->plugin($pluginClass); $divContent = $this->assembleDivContent($element, $label, $helpContentBefore, $input, $clearButton, $helpContentAfter, $errors);
$input = $helper($element);
} elseif ($pluginClass instanceof \Zend\Form\View\Helper\AbstractHelper) { $html = <<<EOT
$input = $pluginClass($element); <div class="form-group $class">
} else { $divContent
throw new LogicException('Attribut $pluginClass incorrect'); </div>
EOT;
return $html;
} }
private function normalizeElement(ElementInterface $element)
{
if (! $element instanceof Button) {
$class = $element->getAttribute('class');
$labelClass = array_key_exists('class', $tmp = (array)$element->getLabelAttributes()) ? $tmp['class'] : '';
$element
->setAttribute('class', $class . ' form-control')
->setLabelAttributes(['class' => $labelClass . ' control-label']);
} }
}
private function labelHtml(ElementInterface $element)
{
$html = '';
$label = null;
if ($this->includeLabel && $element->getLabel() && !$element instanceof Button) { if ($this->includeLabel && $element->getLabel() && !$element instanceof Button) {
$helper = $this->getView()->plugin('formLabel'); $helper = $this->getView()->plugin('formLabel');
$label = $helper($element); $html = $helper($element);
} }
if ($element->hasAttribute('info_icon')) { if ($element->hasAttribute('info_icon')) {
$info = $element->getAttribute('info_icon'); $info = $element->getAttribute('info_icon');
$label .= sprintf( $html .= sprintf(
'&nbsp;<span data-toggle="tooltip" data-placement="bottom" class="info-icon glyphicon glyphicon-info-sign" title="%s"></span>', '&nbsp;<span data-toggle="tooltip" data-placement="bottom" class="info-icon glyphicon glyphicon-info-sign" title="%s"></span>',
$info); $info);
} }
if ($element instanceof MultiCheckbox) { return $html;
$input = '<div class="multicheckbox">' . $input . '</div>';
} }
$button = $this->addClearButton ? private function inputHtml(ElementInterface $element, $pluginClass = 'formElement')
'<span class="input-group-btn"><button class="btn btn-default btn-xs" type="button" title="Vider" onclick="$(this).siblings(\':input\').val(null).focus();"><span class="glyphicon glyphicon-remove"></span></button></span>' : {
null; if (! $pluginClass) {
$pluginClass = 'formElement';
}
$helpContentBefore = null; if ($element instanceof SearchAndSelect) {
if (!empty($this->helpContent['before'])) { /** @var FormSearchAndSelect $helper */
$helpContentBefore = sprintf('<p class="help-block help-block-before">%s<p>', $this->helpContent['before']); $helper = $this->getView()->plugin('formSearchAndSelect');
$helper->setAutocompleteMinLength(2);
$html = $helper($element);
} else {
if (is_string($pluginClass)) {
$helper = $this->getView()->plugin($pluginClass);
$html = $helper($element);
} elseif ($pluginClass instanceof \Zend\Form\View\Helper\AbstractHelper) {
$html = $pluginClass($element);
} else {
throw new LogicException('Argument $pluginClass incorrect');
} }
$helpContentAfter = null;
if (!empty($this->helpContent['after'])) {
$helpContentAfter = sprintf('<p class="help-block help-block-after">%s<p>', $this->helpContent['after']);
} }
$errors = null; if ($element instanceof MultiCheckbox) {
$html = '<div class="multicheckbox">' . $html . '</div>';
}
return $html;
}
private function errorsHtml(ElementInterface $element)
{
$html = '';
if ($element->getMessages() && $this->getIncludeErrors()) { if ($element->getMessages() && $this->getIncludeErrors()) {
/** @var FormElementErrors $helper */ /** @var FormElementErrors $helper */
$helper = $this->getView()->plugin('formElementErrors'); $helper = $this->getView()->plugin('formElementErrors');
$errors = $helper($element, ['class' => 'error text-danger']); $html = $helper($element, ['class' => 'error text-danger']);
} }
$class = []; return $html;
$class[] = $this->addClearButton ? 'input-group' : null; }
$class[] = $errors ? 'has-error' : null;
$class = implode(' ', $class);
$format = <<<EOT private function assembleDivContent(ElementInterface $element, $label, $helpContentBefore, $input, $button, $helpContentAfter, $errors)
<div class="form-group $class"> {
if ($element instanceof Checkbox && ! $element instanceof MultiCheckbox) {
$html = <<<EOT
$helpContentBefore
$input
$label
$button
$helpContentAfter
$errors
EOT;
}
else {
$html = <<<EOT
$label $label
$helpContentBefore $helpContentBefore
$input $input
$button $button
$helpContentAfter $helpContentAfter
$errors $errors
</div>
EOT; EOT;
}
return $format; return $html;
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment