From 62184b3ec1098fe0f5593f82d00c02b0171d7b5d Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Sat, 19 Aug 2017 17:07:41 +0200
Subject: [PATCH] hotline#61462 fix hold item while not logged

---
 library/ZendAfi/Controller/Action.php         |  3 ++
 .../ZendAfi/Form/Decorator/DisplayMode.php    |  6 +---
 .../View/Helper/Admin/ImageViewersOptions.php |  4 +--
 library/ZendAfi/View/Helper/Bouton.php        |  2 --
 library/ZendAfi/View/Helper/Button/Submit.php |  9 +-----
 library/ZendAfi/View/Helper/FormSubmit.php    |  1 -
 library/ZendAfi/View/Helper/RenderForm.php    |  5 +--
 .../View/Helper/TagObjetsImgProperties.php    |  3 +-
 public/opac/js/subModal.js                    |  2 +-
 .../opac/controllers/AuthControllerTest.php   | 32 +++++++++++++++++++
 10 files changed, 41 insertions(+), 26 deletions(-)

diff --git a/library/ZendAfi/Controller/Action.php b/library/ZendAfi/Controller/Action.php
index 4bca7fa6568..c1ba829f25d 100644
--- a/library/ZendAfi/Controller/Action.php
+++ b/library/ZendAfi/Controller/Action.php
@@ -175,6 +175,9 @@ class ZendAfi_Controller_Action extends Zend_Controller_Action {
 
 
   public function renderPopup($popup_url, $location = null) {
+    if ($this->isPopupRequest())
+      return $this->_redirect($popup_url);
+
     $this->getHelper('flashMessenger')
          ->addMessage([ZendAfi_Controller_Action_Helper_FlashMessenger::POPUP => ['url' => $popup_url]]);
 
diff --git a/library/ZendAfi/Form/Decorator/DisplayMode.php b/library/ZendAfi/Form/Decorator/DisplayMode.php
index 144926f3486..7c05409a30f 100644
--- a/library/ZendAfi/Form/Decorator/DisplayMode.php
+++ b/library/ZendAfi/Form/Decorator/DisplayMode.php
@@ -24,11 +24,7 @@ class ZendAfi_Form_Decorator_DisplayMode extends Zend_Form_Decorator_Abstract {
   public function render($content) {
     $view = $this->_element->getView();
 
-    $submit_script = sprintf('$("select[name=\'%s\']").change(function() {$("#styles_reload").val("1");var form=$(this).closest("form"); %s form.submit();});',
-                             'mode-affichage',
-                             $view->isPopup()
-                             ? 'initializeAjaxFormSubmit(form);'
-                             : '');
+    $submit_script = '$("select[name=\'mode-affichage\']").change(function() {$("#styles_reload").val("1");var form=$(this).closest("form"); form.submit();});';
 
     Class_ScriptLoader::getInstance()
       ->addJQueryReady($submit_script);
diff --git a/library/ZendAfi/View/Helper/Admin/ImageViewersOptions.php b/library/ZendAfi/View/Helper/Admin/ImageViewersOptions.php
index a093a362cf5..69e95f3b6bb 100644
--- a/library/ZendAfi/View/Helper/Admin/ImageViewersOptions.php
+++ b/library/ZendAfi/View/Helper/Admin/ImageViewersOptions.php
@@ -64,9 +64,7 @@ class ZendAfi_View_Helper_Admin_ImageViewersOptions
 
 
   protected function _getComboStyles() {
-    $submit_script = sprintf('$("select[name=\'style_liste\']").change(function() {$("#styles_reload").val("1");var form=$(this).closest("form"); %s form.submit();});',
-
-                             $this->view->isPopup() ? 'initializeAjaxFormSubmit(form);' : '');
+    $submit_script = '$("select[name=\'style_liste\']").change(function() {$("#styles_reload").val("1");var form=$(this).closest("form"); form.submit();});';
     Class_ScriptLoader::getInstance()
       ->addJQueryReady($submit_script);
 
diff --git a/library/ZendAfi/View/Helper/Bouton.php b/library/ZendAfi/View/Helper/Bouton.php
index 15a040143b8..831aef217fb 100644
--- a/library/ZendAfi/View/Helper/Bouton.php
+++ b/library/ZendAfi/View/Helper/Bouton.php
@@ -74,8 +74,6 @@ class ZendAfi_View_Helper_Bouton extends ZendAfi_View_Helper_BaseHelper {
               $largeur="120px";
 
             $onclick = 'var form=$(this).parents(\'form\'); if (!form.size()) form=$(this).parents(\'.boutons\').prevAll(\'form\');if (!form.size()) form=$(this).parents(\'.boutons\').nextAll(\'form\');';
-            if ($this->_render_popup)
-              $onclick .= 'initializeAjaxFormSubmit(form);';
             $onclick .= 'form.submit(); return false;';
           }
       }
diff --git a/library/ZendAfi/View/Helper/Button/Submit.php b/library/ZendAfi/View/Helper/Button/Submit.php
index 951cb26c75c..ac869cc02a8 100644
--- a/library/ZendAfi/View/Helper/Button/Submit.php
+++ b/library/ZendAfi/View/Helper/Button/Submit.php
@@ -32,20 +32,13 @@ class ZendAfi_View_Helper_Button_Submit extends ZendAfi_View_Helper_BaseHelper {
 
 
   protected function _getDefaultAttribs() {
-    return ['onclick' => sprintf("var form=$(this).parents('form'); if (!form.size()) form=$(this).parents('.boutons, .admin-buttons').prevAll('form');if (!form.size()) form=$(this).parents('.boutons, .admin-buttons').nextAll('form');%sform.submit(); return false;", $this->_getAjax()),
+    return ['onclick' => "var form=$(this).parents('form'); if (!form.size()) form=$(this).parents('.boutons, .admin-buttons').prevAll('form');if (!form.size()) form=$(this).parents('.boutons, .admin-buttons').nextAll('form');form.submit(); return false;",
             'class' => 'bouton validate',
             'type' => 'submit',
             'title' => $this->_('Enregistrer mes modifications')];
   }
 
 
-  protected function _getAjax() {
-    return $this->view->isPopup()
-      ? 'initializeAjaxFormSubmit(form);'
-      : '';
-  }
-
-
   protected function _getDefaultButton($attribs) {
     return (new Class_Entity())
       ->setText($this->_('Valider'))
diff --git a/library/ZendAfi/View/Helper/FormSubmit.php b/library/ZendAfi/View/Helper/FormSubmit.php
index d94d37c1b7d..be84bc806eb 100644
--- a/library/ZendAfi/View/Helper/FormSubmit.php
+++ b/library/ZendAfi/View/Helper/FormSubmit.php
@@ -34,7 +34,6 @@ class ZendAfi_View_Helper_FormSubmit extends Zend_View_Helper_FormSubmit {
   public function formSubmit($name, $value = null, $attribs = []) {
     if ($this->_render_popup) {
       $onclick = 'var form=$(this).parents(\'form\'); if (!form.size()) form=$(this).parents(\'.boutons\').prevAll(\'form\');';
-      $onclick .= 'initializeAjaxFormSubmit(form);';
       $onclick .= 'form.submit(); return false;';
       $attribs['onclick'] = $onclick;
     }
diff --git a/library/ZendAfi/View/Helper/RenderForm.php b/library/ZendAfi/View/Helper/RenderForm.php
index 51b658a15c3..e7254ee41ec 100644
--- a/library/ZendAfi/View/Helper/RenderForm.php
+++ b/library/ZendAfi/View/Helper/RenderForm.php
@@ -104,11 +104,8 @@ class ZendAfi_View_Helper_RenderForm extends ZendAfi_View_Helper_BaseHelper {
 
   protected function _injectOnKeyPressSubmit($element) {
     $element->onkeypress = 'if (event.keyCode == 13) {'
-      . ($this->view->isPopup()
-         ? 'initializeAjaxFormSubmit($(this.form));'
-         : '')
       . '$(this.form).submit();'
-      . 'return false;}';
+      . 'return false; }';
   }
 
 
diff --git a/library/ZendAfi/View/Helper/TagObjetsImgProperties.php b/library/ZendAfi/View/Helper/TagObjetsImgProperties.php
index 2ce61a7a7e1..91f99b68024 100644
--- a/library/ZendAfi/View/Helper/TagObjetsImgProperties.php
+++ b/library/ZendAfi/View/Helper/TagObjetsImgProperties.php
@@ -65,8 +65,7 @@ class ZendAfi_View_Helper_TagObjetsImgProperties
 
 
   private function getComboStyles($styles, $valeur_select) {
-        $submit_script = sprintf('$("select[name=\'style_liste\']").change(function() {$("#styles_reload").val("1");var form=$(this).closest("form"); %s form.submit();});',
-                             $this->view->isPopup() ? 'initializeAjaxFormSubmit(form);' : '');
+        $submit_script = '$("select[name=\'style_liste\']").change(function() {$("#styles_reload").val("1");var form=$(this).closest("form"); form.submit();});';
 
     Class_ScriptLoader::getInstance()
       ->addJQueryReady($submit_script);
diff --git a/public/opac/js/subModal.js b/public/opac/js/subModal.js
index b418177401a..81b15480289 100644
--- a/public/opac/js/subModal.js
+++ b/public/opac/js/subModal.js
@@ -16,6 +16,7 @@
         opacDialogFromUrl(addPath(url, '/render/popup'));
       });
     current_anchor = undefined;
+    initializeAjaxFormSubmit($('#opac-dialog form'));
   }
 
 
@@ -28,7 +29,6 @@
 
 
   window.initializeAjaxFormSubmit = function(form) {
-
     form.submit(function(event) {
       updateAndDestroyCKEditor();
       formData = new FormData(form[0]);
diff --git a/tests/application/modules/opac/controllers/AuthControllerTest.php b/tests/application/modules/opac/controllers/AuthControllerTest.php
index c4182c148e3..301f735df27 100644
--- a/tests/application/modules/opac/controllers/AuthControllerTest.php
+++ b/tests/application/modules/opac/controllers/AuthControllerTest.php
@@ -1039,6 +1039,38 @@ class AuthControllerAjaxLoginPostTest extends AuthControllerPostSimpleSuccessful
 
 
 
+class AuthControllerAjaxLoginWithRedirectPopupPostTest extends AuthControllerPostSimpleSuccessfulTestCase {
+  public function setUp() {
+    parent::setUp();
+    $_SERVER['HTTP_REFERER'] = '/recherche/viewnotice';
+    $this->postDispatch('/opac/auth/ajax-login/render/popup',
+                        ['username' => 'foo',
+                         'password' => 'bar',
+                         'redirect' => '/recherche/simple/reservation-pickup-ajax/id/2'],
+                        true);
+  }
+
+
+  /** @test */
+  public function flashMessengerShouldContainsPopupForReservationPickup() {
+    $this->assertFlashMessengerContainsPopup(Class_Url::absolute('/recherche/simple/reservation-pickup-ajax/id/2/render/popup'));
+  }
+
+
+  /** @test */
+  public function responseShouldAjaxReloadPage() {
+    $this->assertXPathContentContains('//script', 'location.reload()');
+  }
+
+
+  /** @test */
+  public function responseShouldNotBeARedirect() {
+    $this->assertNotRedirect();
+  }
+}
+
+
+
 class AuthControllerBoiteLoginPostTest extends AuthControllerPostSimpleSuccessfulTestCase {
   public function setUp() {
     parent::setUp();
-- 
GitLab