Commit 58a7ad4a authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

UserUsurpationHelper : ajout de la possibilité de dessiner un simple bouton

parent a9ec1509
Pipeline #9135 passed with stage
in 50 seconds
......@@ -3,11 +3,14 @@
namespace UnicaenAuth\View\Helper;
use UnicaenApp\Form\View\Helper\FormControlGroup;
use UnicaenAuth\Entity\Db\AbstractUser;
use UnicaenAuth\Options\ModuleOptions;
use Zend\Form\Element\Hidden;
use Zend\Form\Element\Submit;
use Zend\Form\Element\Text;
use Zend\Form\Form;
use Zend\Form\View\Helper\Form as FormHelper;
use Zend\Form\View\Helper\FormElement;
use Zend\View\Renderer\PhpRenderer;
/**
......@@ -37,6 +40,11 @@ class UserUsurpationHelper extends UserAbstract
*/
private $usurpationEnabled = false;
/**
* @var bool
*/
private $asButton = false;
/**
* Point d'entrée.
*
......@@ -49,55 +57,90 @@ class UserUsurpationHelper extends UserAbstract
/**
* Retourne le code HTML généré par cette aide de vue.
* Equivalent à {@see renderAsTextfield()}.
*
* @return string
*/
public function __toString()
{
if (! $this->usurpationEnabled) {
return $this->renderAsTextfield();
}
/**
* @return string
*/
public function renderAsButton(AbstractUser $user, string $buttonLabel = 'Usurper')
{
if (!$this->usurpationEnabled) {
return '';
}
$this->asButton = true;
$form = $this->createForm();
$identity = $form->get('identity');
$submit = $form->get('submit');
$identity->setValue($user->getUsername());
$submit
->setValue($buttonLabel)
->setAttribute('title', "Usurper l'identité de " . $user);
/** @var FormHelper $formHelper */
$formHelper = $this->view->plugin('form');
/** @var FormControlGroup $formControlGroupHelper */
$formControlGroupHelper = $this->view->plugin('formControlGroup');
/** @var FormElement $formElementHelper */
$formElementHelper = $this->view->plugin('formElement');
$form = new Form('user-usurpation-form');
$form->setAttributes([
'class' => 'user-usurpation-form disable-ajax-submit',
'action' => $this->url,
]);
$html = '';
$html .= $formHelper->openTag($form);
$html .= $formElementHelper->__invoke($identity);
$html .= $formElementHelper->__invoke($submit);
$html .= $formHelper->closeTag();
$input = new Text('identity');
$input->setAttributes([
'id' => 'user-usurpation-input',
'placeholder' => "Identifiant utilisateur",
]);
return $html;
}
$submit = new Submit('submit');
$submit->setValue("Usurper");
$submit->setAttributes([
'class' => 'user-usurpation-submit btn btn-info',
]);
/**
* @return string
*/
public function renderAsTextfield()
{
if (!$this->usurpationEnabled) {
return '';
}
$this->asButton = false;
$form = $this->createForm();
$identity = $form->get('identity');
$submit = $form->get('submit');
/** @var FormHelper $formHelper */
$formHelper = $this->view->plugin('form');
/** @var FormControlGroup $formControlGroupHelper */
$formControlGroupHelper = $this->view->plugin('formControlGroup');
$html = '';
$html .= $formHelper->openTag($form);
$html .= "<div><strong>Usurpation d'identité :</strong></div>";
$html .= $formControlGroupHelper->__invoke($input);
$html .= $formControlGroupHelper->__invoke($identity);
$html .= $formControlGroupHelper->__invoke($submit);
$html .= $formHelper->closeTag();
$formId = $form->getAttribute('id');
$html .= <<<EOS
<script>
$(".user-usurpation-form").submit(function() {
var form = $("#$formId").submit(function() {
$("body *").css('cursor', 'wait');
});
var input = $("#user-usurpation-input").on('input', function() {
var input = form.find(".user-usurpation-input").on('input', function() {
updateUsurpationSubmit();
});
function updateUsurpationSubmit() {
$(".user-usurpation-submit").prop("disabled", input.val().length === 0);
form.find(".user-usurpation-submit").prop("disabled", input.val().length === 0);
}
updateUsurpationSubmit();
</script>
......@@ -106,6 +149,41 @@ EOS;
return $html;
}
protected function createForm()
{
$form = new Form('user-usurpation-form');
$form->setAttributes([
'id' => $formId = uniqid('user-usurpation-form'),
'class' => 'user-usurpation-form disable-ajax-submit',
'action' => $this->url,
]);
if ($this->asButton) {
$identity = null;
$identity = new Hidden('identity');
$identity->setAttributes([
'id' => 'user-usurpation-hidden',
]);
} else {
$identity = new Text('identity');
$identity->setAttributes([
'class' => 'user-usurpation-input',
'placeholder' => "Identifiant utilisateur",
]);
}
$submit = new Submit('submit');
$submit->setValue("Usurper");
$submit->setAttributes([
'class' => 'user-usurpation-submit btn btn-danger',
]);
$form->add($identity);
$form->add($submit);
return $form;
}
/**
* @param string $url
* @return $this
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment