From 9b5372335c53b54d7cb1fc462040939822cd1173 Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Tue, 7 Jul 2015 17:32:14 +0200
Subject: [PATCH] dev #24821 user notices

Notifications can be rendered as popup
---
 library/Class/ScriptLoader.php                | 25 ++++++++--
 library/Class/Users.php                       |  2 +-
 .../Action/Helper/FlashMessenger.php          | 46 +++++++++++--------
 .../Controller/Action/Helper/Notify.php       |  6 ++-
 .../modules/AbstractControllerTestCase.php    | 12 +++--
 .../opac/controllers/AuthControllerTest.php   | 11 +++--
 tests/library/Class/ScriptLoaderTest.php      | 18 ++++++--
 7 files changed, 84 insertions(+), 36 deletions(-)

diff --git a/library/Class/ScriptLoader.php b/library/Class/ScriptLoader.php
index 1c5ea26c043..f7575c3f87c 100644
--- a/library/Class/ScriptLoader.php
+++ b/library/Class/ScriptLoader.php
@@ -312,9 +312,28 @@ class Class_ScriptLoader {
 	 * voir [[file:~/public_html/afi-opac3/library/ZendAfi/Controller/Action/Helper/Notify.php::class%20ZendAfi_Controller_Action_Helper_Notify%20extends%20Zend_Controller_Action_Helper_Abstract%20{][ZendAfi_Controller_Action_Helper_Notify]]
 	 */
 	public function showNotifications() {
-		$messenger = new ZendAfi_Controller_Action_Helper_FlashMessenger();
-		if ($message = implode('. ', $messenger->getNotifications()))
-			$this->notify($message);
+		$notifications = (new ZendAfi_Controller_Action_Helper_FlashMessenger())
+			->getNotifications();
+
+		if (!$notifications)
+			return $this;
+
+		$popup_messages = [];
+		$bar_messages = [];
+
+		$notifications->eachDo(
+													 function($n) use (&$popup_messages, &$bar_messages) {
+														 $n->isPopup()
+															 ? $popup_messages []= $n->getMessage()
+															 : $bar_messages []= $n->getMessage();
+													 });
+
+		if ($bar_messages)
+			$this->notify(implode('. ', $bar_messages));
+
+		if ($popup_messages)
+			$this->addJQueryReady("$('<p>" .implode('<br> ', $popup_messages). "</p>').dialog()");
+
 
 		return $this;
 	}
diff --git a/library/Class/Users.php b/library/Class/Users.php
index ba836464ace..070bcf67644 100644
--- a/library/Class/Users.php
+++ b/library/Class/Users.php
@@ -1510,7 +1510,7 @@ class Class_Users extends Storm_Model_Abstract {
 
 
 	public function registerNotificationsOn($notifiable) {
-		$this->_notifications = [];
+		$this->_notifications = ['tost'];
 		array_map([$notifiable, 'notify'], $this->_notifications);
 	}
 }
diff --git a/library/ZendAfi/Controller/Action/Helper/FlashMessenger.php b/library/ZendAfi/Controller/Action/Helper/FlashMessenger.php
index 99fc60f7d1d..42a104919b9 100644
--- a/library/ZendAfi/Controller/Action/Helper/FlashMessenger.php
+++ b/library/ZendAfi/Controller/Action/Helper/FlashMessenger.php
@@ -16,19 +16,25 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with BOKEH; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301	 USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301	 USA
  */
 
 class ZendAfi_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_FlashMessenger {
-	const TRACK_EVENT = 'track_event';
-	const POPUP = 'popup';
+	const
+		POPUP = 'popup',
+		NOTIFICATION = 'notification';
 
 	public static function reset() {
 		self::$_session = null;
 	}
 
+	public function addNotification($message, $options = []) {
+		$params = ['message' => $message] + $options;
+		$this->addMessage([ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION => $params]);
+	}
+
 	public function isNotification($message) {
-		return !is_array($message);
+		return is_array($message) && isset($message[self::NOTIFICATION]);
 	}
 
 
@@ -36,29 +42,33 @@ class ZendAfi_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Ac
 		return is_array($message) && isset($message[self::POPUP]);
 	}
 
-
-	public function isTrackEvent($message) {
-		return is_array($message) && isset($message[self::TRACK_EVENT]);
+	public function getNotifications() {
+		return (new Storm_Collection($this->getMessages()))
+			->select(function($e) {return $this->isNotification($e);})
+			->collect(function($e) {return new FlashMessengerNotification($e[self::NOTIFICATION]);});
 	}
 
-
-	public function getNotifications() {
-		return array_filter($this->getMessages(), [$this, 'isNotification']); 
+	public function getPopups() {
+		return array_filter($this->getMessages(), [$this, 'isPopup']);
 	}
+}
 
 
-	public function getPopups() {
-		return array_filter($this->getMessages(), [$this, 'isPopup']); 
+class FlashMessengerNotification {
+	protected $_params;
+
+	public function __construct($params) {
+		$this->_params = $params;
 	}
 
+	public function getMessage() {
+		return $this->_params['message'];
+	}
 
-	public function getTrackEvents() {
-		$messages = array_filter($this->getMessages(), [$this, 'isTrackEvent']); 
-		$events = [];
-		foreach($messages as $message) 
-		  $events []= $message[self::TRACK_EVENT];
-		return $events;
+	public function isPopup() {
+		return isset($this->_params['display']) && ($this->_params['display'] == 'popup');
 	}
 }
 
+
 ?>
\ No newline at end of file
diff --git a/library/ZendAfi/Controller/Action/Helper/Notify.php b/library/ZendAfi/Controller/Action/Helper/Notify.php
index 7c3ae85bb61..9f252615141 100644
--- a/library/ZendAfi/Controller/Action/Helper/Notify.php
+++ b/library/ZendAfi/Controller/Action/Helper/Notify.php
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with BOKEH; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 class ZendAfi_Controller_Action_Helper_Notify extends Zend_Controller_Action_Helper_Abstract {
@@ -24,7 +24,9 @@ class ZendAfi_Controller_Action_Helper_Notify extends Zend_Controller_Action_Hel
 	 * [[file:~/public_html/afi-opac3/library/Class/ScriptLoader.php::public%20function%20showNotifications()%20{][voir Class_ScriptLoader::showNotifications]]
 	 */
 	public function notify($message) {
-		$this->getActionController()->getHelper('flashMessenger')->addMessage($message);
+		$this->getActionController()
+				 ->getHelper('flashMessenger')
+				 ->addNotification($message);
 	}
 
 	public function direct($message) {
diff --git a/tests/application/modules/AbstractControllerTestCase.php b/tests/application/modules/AbstractControllerTestCase.php
index ec090028457..38a4542efd8 100644
--- a/tests/application/modules/AbstractControllerTestCase.php
+++ b/tests/application/modules/AbstractControllerTestCase.php
@@ -266,9 +266,15 @@ abstract class AbstractControllerTestCase extends Zend_Test_PHPUnit_ControllerTe
 
 	public function assertFlashMessengerContentContains($value, $message = '') {
 		$messages = $this->_getFlashMessengerMessages();
-		$messages = array_filter($messages, 'is_string');
-		foreach($messages as $message_value){
-			if (false!==strpos($message_value, $value))
+		$messages = array_filter($messages,
+														 function($data)
+														 {
+															 return is_array($data)
+																 && isset($data[ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION]);
+														 });
+		foreach($messages as $message){
+			if (false!==strpos($message[ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION]['message'],
+												 $value))
 				return;
 		}
 
diff --git a/tests/application/modules/opac/controllers/AuthControllerTest.php b/tests/application/modules/opac/controllers/AuthControllerTest.php
index 703dfb38a1e..b294999e4b0 100644
--- a/tests/application/modules/opac/controllers/AuthControllerTest.php
+++ b/tests/application/modules/opac/controllers/AuthControllerTest.php
@@ -780,7 +780,7 @@ class AuthControllerPostTest extends AuthControllerNobodyLoggedTestCase {
 	public function invalidAjaxAuthenticationShouldRedirectToActionReferrerWithPopupInFlash()	{
 		$this->ajaxLoginWithWrongInformation();
 		$this->assertRedirectTo($this->_referer);
-		$this->assertFlashMessengerEquals(['Identifiant ou mot de passe incorrect.',
+		$this->assertFlashMessengerEquals([[ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION => ['message' => 'Identifiant ou mot de passe incorrect.']],
 																			 [ZendAfi_Controller_Action_Helper_FlashMessenger::POPUP => ['url' => '/auth/popup-login?redirect='.urlencode('/auth/panier')]]]);
 
 	}
@@ -936,7 +936,8 @@ class AuthControllerBoiteLoginPostTest extends AuthControllerPostSimpleSuccessfu
 
 	/** @test */
 	public function flashMessengerShouldContainMessageDeNotification() {
-		$this->assertFlashMessengerContentContains('Message de notification');
+		$this->assertFlashMessengerContains(
+												[ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION => ['message' => 'Message de notification']]);
 	}
 
 	/** @test */
@@ -1964,7 +1965,7 @@ class AuthControllerTomLoggedRegisterNewsletterTestCase extends AbstractControll
 
 	/** @test */
 	public function flashMessengerShouldBeReadyWithExpectedMessage() {
-		$this->assertFlashMessengerEquals(['Vous êtes inscrit à la liste de diffusion: Daily News']);
+		$this->assertFlashMessengerContentContains('Vous êtes inscrit à la liste de diffusion: Daily News');
 	}
 
 
@@ -2022,7 +2023,7 @@ class AuthControllerNewsletterUnsubscribeWrongIdTest extends AuthControllerNobod
 
 	/** @test */
 	public function flashMessageShouldBeAsExpected() {
-		$this->assertFlashMessengerEquals(['Vous n\'êtes plus inscrit à la lettre d\'information, celle-ci n\'existe plus.']);
+		$this->assertFlashMessengerContentContains('Vous n\'êtes plus inscrit à la lettre d\'information, celle-ci n\'existe plus.');
 	}
 }
 
@@ -2065,7 +2066,7 @@ class AuthControllerNewsletterUnsubscribeTest extends AbstractControllerTestCase
 
 	/** @test */
 	public function flashMessageShouldBeAsExpected() {
-		$this->assertFlashMessengerEquals(['Vous êtes désinscrit de la liste de diffusion: Daily News']);
+		$this->assertFlashMessengerContentContains('Vous êtes désinscrit de la liste de diffusion: Daily News');
 	}
 }
 
diff --git a/tests/library/Class/ScriptLoaderTest.php b/tests/library/Class/ScriptLoaderTest.php
index 30733289cc2..c01dc055841 100644
--- a/tests/library/Class/ScriptLoaderTest.php
+++ b/tests/library/Class/ScriptLoaderTest.php
@@ -237,12 +237,14 @@ class ScriptLoaderVersionHashTest extends PHPUnit_Framework_TestCase {
 
 
 
-class ScriptLoaderNotificationsTest extends Storm_Test_ModelTestCase {
+class ScriptLoaderNotificationsBarTest extends Storm_Test_ModelTestCase {
 	public function setUp() {
 		Class_ScriptLoader::resetInstance();
 		$messenger = new ZendAfi_Controller_Action_Helper_FlashMessenger();
-		$messenger->addMessage('First message');
-		$messenger->addMessage('Second message');
+		$messenger->addNotification('First message');
+		$messenger->addNotification('Second message');
+		$messenger->addNotification('Third message', ['display' => 'popup']);
+		$messenger->addNotification('Fourth message', ['display' => 'popup']);
 
 		ZendAfi_Controller_Action_Helper_FlashMessenger::reset();
 		$this->_html = Class_ScriptLoader::getInstance()->showNotifications()->html();
@@ -250,7 +252,7 @@ class ScriptLoaderNotificationsTest extends Storm_Test_ModelTestCase {
 
 
 	/** @test */
-	public function javascriptToShowNotificationsShouldConcatenateAllMessages() {
+	public function javascriptToShowNotificationsShouldConcatenateMessagesOneAndTwo() {
 		$this->assertContains('showNotification({"message":"First message. Second message","autoClose":true,"duration":10,"type":"information"})',
 													$this->_html);
 	}
@@ -261,5 +263,13 @@ class ScriptLoaderNotificationsTest extends Storm_Test_ModelTestCase {
 		$this->assertContains('public/admin/js/notification/js/jquery_notification_v.1.js',
 													$this->_html);
 	}
+
+
+	/** @test */
+	public function messageThreeAndFourShouldBePopupedUpInJQueryDialog() {
+		$this->assertContains("$('<p>Third message<br> Fourth message</p>').dialog()",
+													$this->_html);
+	}
 }
+
 ?>
\ No newline at end of file
-- 
GitLab