Commit 576dc5db authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Renaissance du MultipageForm !

parent 52d30854
Pipeline #6746 failed with stage
in 9 seconds
......@@ -76,7 +76,7 @@ class CaractEvtFieldset extends \Zend\Form\Fieldset implements \Zend\InputFilter
L'aide de vue (view helper) qui est utilisée pour générer le code HTML complet de cet élément (labels, champs et erreurs) est "[[develop:unicaen2:moduleunicaenunicaenapp:viewhelpers:FormRowDateInfSup|FormRowDateInfSup]]" (qui elle-même fait appel à l'aide de vue "[[develop:unicaen2:moduleunicaenunicaenapp:viewhelpers:FormDateInfSup|FormDateInfSup]]").
</note>
====== MultipageFormNav ======
====== MultipageFormNavElement ======
Élément composite de navigation au sein d'un formulaire multipage (formulaire en plusieurs étapes) [[develop:unicaen2:moduleunicaenunicaenapp:form:MultipageForm|MultipageForm]].
......
......@@ -88,7 +88,7 @@ code HTML complet de cet élément (labels, champs et erreurs) est
\"[FormDateInfSup](/develop/unicaen2/moduleunicaenunicaenapp/viewhelpers/FormDateInfSup)\").
\</note\>
MultipageFormNav
MultipageFormNavElement
================
Élément composite de navigation au sein d\'un formulaire multipage
......
<?php
namespace UnicaenApp\Form\Element;
use Zend\Form\Element;
use Zend\Form\Element\Submit;
/**
* Élément composite de navigation au sein d'un formulaire multi-page.
* Boutons présents selon le contexte : "Précédent", "Suivant", "Terminer", "Annuler".
*
* @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
*/
class MultipageFormNav extends \Zend\Form\Element
class MultipageFormNavElement extends Element
{
const NAME = '_nav';
const PREFIX = '_';
const NEXT = '_next';
const PREVIOUS = '_previous';
const SUBMIT = '_submit';
const CANCEL = '_cancel';
const CONFIRM = '_confirm';
/**
* @var Submit
*/
protected $nextButton;
/**
* @var Submit
*/
protected $previousButton;
/**
* @var Submit
*/
protected $cancelButton;
/**
* @var Submit
*/
protected $submitButton;
/**
* @var Submit
*/
protected $confirmButton;
/**
* @var boolean
*/
......@@ -39,78 +66,195 @@ class MultipageFormNav extends \Zend\Form\Element
private $activateConfirm = false;
/**
* Constructeur.
*
* @param null|int|string $name Optional name for the element
* @param array $options Optional options for the element
* @throws Exception\InvalidArgumentException
* {@inheritDoc}
*/
public function __construct($name = self::NAME, $options = array())
{
parent::__construct($name, $options);
$this->setActivatePrevious(false)
->setActivateNext(true)
->setActivateSubmit(false)
->setActivateCancel(true)
->setActivateConfirm(false);
$this->setPreviousEnabled(false)
->setNextEnabled(true)
->setSubmitEnabled(false)
->setCancelEnabled(true)
->setConfirmEnabled(false);
}
/**
* @return Submit
*/
public function getNextButton() : Submit
{
if ($this->nextButton === null) {
$name = $this->getName();
$labelNext = ("Suivant >");
$titleNext = ("Passer à l'étape suivante");
$this->nextButton = new Submit(
$name . '[' . MultipageFormNavElement::NEXT . ']'
);
$this->nextButton->setValue($labelNext);
$this->nextButton->setAttributes([
'title' => $titleNext,
'class' => 'multipage-nav next']);
}
return $this->nextButton;
}
/**
* @return Submit
*/
public function getPreviousButton() : Submit
{
if ($this->previousButton === null) {
$name = $this->getName();
$labelPrev = ("< Précédent");
$titlePrev = ("Revenir à l'étape précédente");
$this->previousButton = new Submit(
$name . '[' . MultipageFormNavElement::PREVIOUS . ']',
['label' => $labelPrev]
);
$this->previousButton->setValue($labelPrev);
$this->previousButton->setAttributes([
'title' => $titlePrev,
'class' => 'multipage-nav previous', 'style'=>'float: left;']);
}
return $this->previousButton;
}
/**
* @return Submit
*/
public function getCancelButton() : Submit
{
if ($this->cancelButton === null) {
$name = $this->getName();
$labelCancel = ("Annuler");
$titleCancel = ("Abandonner définitivement la saisie");
$this->cancelButton = new Submit(
$name . '[' . MultipageFormNavElement::CANCEL . ']',
['label' => $labelCancel]
);
$this->cancelButton->setValue($labelCancel);
$this->cancelButton->setAttributes([
'title' => $titleCancel,
'class' => 'multipage-nav cancel',
'onclick' => 'askConfirmation(this)',
]);
}
return $this->cancelButton;
}
/**
* @return Submit
*/
public function getSubmitButton() : Submit
{
if ($this->submitButton === null) {
$name = $this->getName();
$labelSubmit = ("Terminer");
$titleSubmit = ("Terminer la saisie");
$this->submitButton = new Submit(
$name . '[' . MultipageFormNavElement::SUBMIT . ']',
['label' => $labelSubmit]
);
$this->submitButton->setValue($labelSubmit);
$this->submitButton->setAttributes([
'title' => $titleSubmit,
'class' => 'multipage-nav submit']);
}
return $this->submitButton;
}
/**
* @return Submit
*/
public function getConfirmButton() : Submit
{
if ($this->confirmButton === null) {
$name = $this->getName();
$labelConfirm = ("Confirmer et enregistrer");
$titleConfirm = ("Confirmer et enregistrer la saisie");
$this->confirmButton = new Submit(
$name . '[' . MultipageFormNavElement::CONFIRM . ']',
['label' => $labelConfirm]
);
$this->confirmButton->setValue($labelConfirm);
$this->confirmButton->setAttributes([
'title' => $titleConfirm,
'class' => 'multipage-nav confirm']);
}
return $this->confirmButton;
}
/**
* Autorise ou non le bouton "Précédent".
* Active ou non le bouton "Précédent".
*
* @param boolean $value
* @return MultipageFormNav
* @return MultipageFormNavElement
*/
public function setActivatePrevious($value = true)
public function setPreviousEnabled($value = true)
{
$this->activatePrevious = $value;
return $this;
}
/**
* Autorise ou non le bouton "Suivant".
* Active ou non le bouton "Suivant".
*
* @param boolean $value
* @return MultipageFormNav
* @return MultipageFormNavElement
*/
public function setActivateNext($value = true)
public function setNextEnabled($value = true)
{
$this->activateNext = $value;
return $this;
}
/**
* Autorise ou non le bouton "Terminer".
* Active ou non le bouton "Terminer".
*
* @param boolean $value
* @return MultipageFormNav
* @return MultipageFormNavElement
*/
public function setActivateSubmit($value = true)
public function setSubmitEnabled($value = true)
{
$this->activateSubmit = $value;
return $this;
}
/**
* Autorise ou non le bouton "Annuler".
* Active ou non le bouton "Annuler".
*
* @param boolean $value
* @return MultipageFormNav
* @return MultipageFormNavElement
*/
public function setActivateCancel($value = true)
public function setCancelEnabled($value = true)
{
$this->activateCancel = $value;
return $this;
}
/**
* Autorise ou non le bouton "Confirmer".
* Active ou non le bouton "Confirmer et enregistrer".
*
* @param boolean $value
* @return MultipageFormNav
* @return MultipageFormNavElement
*/
public function setActivateConfirm($value = true)
public function setConfirmEnabled($value = true)
{
$this->activateConfirm = $value;
return $this;
......@@ -121,7 +265,7 @@ class MultipageFormNav extends \Zend\Form\Element
*
* @return boolean
*/
public function getActivatePrevious()
public function isPreviousEnabled()
{
return $this->activatePrevious;
}
......@@ -131,7 +275,7 @@ class MultipageFormNav extends \Zend\Form\Element
*
* @return boolean
*/
public function getActivateNext()
public function isNextEnabled()
{
return $this->activateNext;
}
......@@ -141,7 +285,7 @@ class MultipageFormNav extends \Zend\Form\Element
*
* @return boolean
*/
public function getActivateSubmit()
public function isSubmitEnabled()
{
return $this->activateSubmit;
}
......@@ -151,7 +295,7 @@ class MultipageFormNav extends \Zend\Form\Element
*
* @return boolean
*/
public function getActivateCancel()
public function isCancelEnabled()
{
return $this->activateCancel;
}
......@@ -161,7 +305,7 @@ class MultipageFormNav extends \Zend\Form\Element
*
* @return boolean
*/
public function getActivateConfirm()
public function isConfirmEnabled()
{
return $this->activateConfirm;
}
......
This diff is collapsed.
......@@ -271,12 +271,13 @@ class FormControlGroup extends AbstractHelper
if ($element instanceof MultiCheckbox) {
$html = '<div class="multicheckbox">' . $html . '</div>';
} elseif ($element instanceof Checkbox) {
$title = $element->getLabelAttributes()['title'] ?? $element->getAttributes()['title'] ?? null;
$html = sprintf('
<div class="checkbox">
<label>
%s <span class="checkbox-label">%s</span>
%s <span class="checkbox-label" title="%s">%s</span>
</label>
</div>', $html, $element->getLabel());
</div>', $html, $title, $element->getLabel());
}
return $html;
......
......@@ -46,7 +46,10 @@ class FormDateTime extends \Zend\Form\View\Helper\FormDateTime
$inputAttrs['type'] = $this->getType($element);
$inputAttrs['value'] = $this->getStringValue();
$inputAttrs['placeholder'] = $this->convertPHPToHumanFormat($this->getFormat());
$inputAttrs['id'] = uniqid('bdtpicker_');
if (! $element->getAttribute('id')) {
$inputAttrs['id'] = uniqid('bdtpicker_');
}
$value = $element->getValue();
......
<?php
namespace UnicaenApp\Form\View\Helper;
use Application\View\Renderer\PhpRenderer;
use UnicaenApp\Form\Element\MultipageFormNavElement;
use UnicaenApp\Form\MultipageForm;
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\Form\View\Helper\AbstractHelper;
use Zend\View\Exception\InvalidArgumentException;
......@@ -8,19 +13,32 @@ use Zend\View\Exception\InvalidArgumentException;
/**
* Aide de vue générant un fieldset de formulaire multi-pages.
*
* @property PhpRenderer $view
*
* @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
*/
class MultipageFormFieldset extends AbstractHelper
{
/**
* @var Fieldset
*/
private $fieldset;
/**
* @var string
*/
private $partial;
/**
* Point d'entrée.
*
* @param Fieldset $fieldset Eventuel fieldset concerné. Sinon utilisation de $this->getView()->fieldset
* @return string
* @return self
*/
public function __invoke()
{
if (!($fieldset = $this->getView()->fieldset)) {
$fieldset = $this->view->get('fieldset');
if (! $fieldset) {
throw new InvalidArgumentException("Aucun fieldset trouvé dans la vue.");
}
if (!$fieldset instanceof Fieldset) {
......@@ -29,24 +47,55 @@ class MultipageFormFieldset extends AbstractHelper
if (!count($fieldset->getElements())) {
throw new InvalidArgumentException("Le fieldset spécifié dans la vue ne possède aucun élément.");
}
return $this->render($fieldset);
$this->fieldset = $fieldset;
return $this;
}
/**
* @param string $partial
* @return self
*/
public function setPartial($partial)
{
$this->partial = $partial;
return $this;
}
/**
* @return string
*/
public function __toString()
{
return $this->render();
}
/**
* @return MultipageFormNav
*/
public function multipageFormNav()
{
/** @var MultipageFormNav $helper */
$helper = $this->view->plugin('multipageFormNav');
return $helper;
}
/**
* Génère le code HTML.
*
* @param Fieldset $fieldset Fieldset concerné
*
* @return string
* @return string
*/
public function render(Fieldset $fieldset)
public function render()
{
$fieldset = $this->fieldset;
$template = '<form method="POST"><fieldset><legend>%s</legend>%s</fieldset></form>';
$elements = '';
foreach ($fieldset as $element) {
$elements .= $this->getView()->multipageFormRow($element);
}
$label = $fieldset->getLabel();
$step = "Étape";
$on = "sur";
......@@ -57,16 +106,53 @@ class MultipageFormFieldset extends AbstractHelper
$on = $translator->translate($on, $this->getTranslatorTextDomain());
}
$html = sprintf($template, $label, $elements);
$html = sprintf($template, $label, $this->renderFieldset($fieldset));
if (isset($this->getView()->stepIndex)) {
$step .= " " . $this->getView()->stepIndex;
if (isset($this->getView()->stepCount)) {
$step .= " $on " . $this->getView()->stepCount;
if ($stepIndex = $this->view->get('stepIndex')) {
$step .= " " . $stepIndex;
if ($stepCount = $this->view->get('stepCount')) {
$step .= " $on " . $stepCount;
}
$html = "<h2>$step</h2>" . PHP_EOL . $html;
}
return $html;
}
/**
* @param Fieldset $fieldset
* @return string
*/
private function renderFieldset(Fieldset $fieldset)
{
$html = '';
if ($this->partial) {
// fieldset
$html .= $this->view->partial($this->partial);
// navigation
$navElement = $this->getNavigationElement();
$html .= $this->view->multipageFormNav($navElement);
} else {
/** @var Element $element */
foreach ($fieldset->getIterator() as $element) {
$html .= $this->view->multipageFormRow($element);
}
}
return $html;
}
/**
* @return MultipageFormNavElement
*/
protected function getNavigationElement()
{
$fieldset = $this->fieldset;
/** @var MultipageFormNavElement $element */
$element = $fieldset->get(MultipageForm::NAME_NAV);
return $element;
}
}
\ No newline at end of file
<?php
namespace UnicaenApp\Form\View\Helper;
use UnicaenApp\Form\Element\MultipageFormNav as MultipageFormNavElement;
use Zend\Form\Element\Submit;
use Zend\Form\Exception\InvalidArgumentException;
use Application\View\Renderer\PhpRenderer;
use UnicaenApp\Form\Element\MultipageFormNavElement as MultipageFormNavElement;
use Zend\Form\View\Helper\AbstractHelper;
use Zend\View\Resolver\AggregateResolver;
use Zend\View\Resolver\TemplatePathStack;
/**
* Aide de vue générant l'élément de navigation au sein d'un formulaire multi-pages.
*
* @property PhpRenderer $view
*
* @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
*/
class MultipageFormNav extends AbstractHelper
{
protected $partial = 'multipage-form-nav';
/**
* @var MultipageFormNavElement
*/
protected $navElement;
/**
* Spécifie la vue à utiliser pour la génération HTML des éléments de navigation.
*
* @param string $partial
* @return MultipageFormNav
*/
public function setPartial(string $partial): self
{
$this->partial = $partial;
return $this;
}
/**
* Point d'entrée.
*
*
* @param MultipageFormNavElement|null $element
* @return self
*/
public function __invoke(?MultipageFormNavElement $element = null) : self
{
$this->navElement = $element;
/** @var AggregateResolver $resolver */
$resolver = $this->view->resolver();
$resolver->attach(new TemplatePathStack(['script_paths' => [__DIR__ . '/partial']]));
return $this;
}
/