From 026470c0fa1760add6983f723599d53b8a323921 Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Mon, 20 Aug 2012 17:00:02 +0000
Subject: [PATCH] Multimedia: suppression duplication verification workflow

---
 .gitattributes                                |   1 +
 .../opac/controllers/AbonneController.php     | 141 ++++--------------
 library/Class/Multimedia/ReservationBean.php  |  98 ++++++++++++
 .../AbonneControllerMultimediaTest.php        |  12 +-
 4 files changed, 129 insertions(+), 123 deletions(-)
 create mode 100644 library/Class/Multimedia/ReservationBean.php

diff --git a/.gitattributes b/.gitattributes
index bf18f12aee1..8af89f74bf7 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1741,6 +1741,7 @@ library/Class/Multimedia/Device.php -text
 library/Class/Multimedia/DeviceGroup.php -text
 library/Class/Multimedia/DeviceHold.php -text
 library/Class/Multimedia/Location.php -text
+library/Class/Multimedia/ReservationBean.php -text
 library/Class/Newsletter.php -text
 library/Class/NewsletterSubscription.php -text
 library/Class/Notice.php -text
diff --git a/application/modules/opac/controllers/AbonneController.php b/application/modules/opac/controllers/AbonneController.php
index bac1d8ff2c9..0ae4896344c 100644
--- a/application/modules/opac/controllers/AbonneController.php
+++ b/application/modules/opac/controllers/AbonneController.php
@@ -457,7 +457,7 @@ class AbonneController extends Zend_Controller_Action {
 
 
 	public function multimediaHoldLocationAction() {
-		$bean = MultimediaReservationBean::newInSession();
+		$bean = Class_Multimedia_ReservationBean::newInSession();
 
 		if (null != $this->_getParam('location')) {
 			$bean->location = $this->_getParam('location');
@@ -471,12 +471,7 @@ class AbonneController extends Zend_Controller_Action {
 
 
 	public function multimediaHoldDayAction() {
-		$bean = $this->_getDeviceHoldBean();
-		/* Si le site n'a pas encore été choisi, on retourne au choix du site */
-		if (null == ($location = Class_Multimedia_Location::getLoader()->find((int)$bean->location))) {
-			$this->_redirect('/abonne/multimedia-hold-location');
-			return;
-		}
+		$bean = Class_Multimedia_ReservationBean::current();
 
 		/* Vérification du quota sur le jour choisi */
 		$day = $this->_getParam('day');
@@ -505,8 +500,15 @@ class AbonneController extends Zend_Controller_Action {
 			$this->_redirect('/abonne/multimedia-hold-hours');
 			return;
 		}
+		
+		$this->getRequest()->setParam('day', 0);
+		if (!$bean->isCurrentStateValidForRequest($this->getRequest())) {
+			$this->_redirect('/abonne/'.$bean->currentState());
+			return;
+		}
 
 		/* Rendu du calendrier avec les jours sélectionnables */
+		$location = $bean->getLocation();
 		$this->view->minDate = $location->getMinDate();
 		$this->view->maxDate = $location->getMaxDate();
 		$holidayStamps = array_map(
@@ -546,17 +548,10 @@ class AbonneController extends Zend_Controller_Action {
 
 
 	public function multimediaHoldHoursAction() {
-		$bean = $this->_getDeviceHoldBean();
-		if (null == ($location = Class_Multimedia_Location::getLoader()->find((int)$bean->location))) {
-			$this->_redirect('/abonne/multimedia-hold-location');
-			return;
-		}
-
-		if ('' == $bean->day) {
-			$this->_redirect('/abonne/multimedia-hold-day');
+		if (!$bean = $this->_getDeviceHoldBean())
 			return;
-		}
 
+		$location = $bean->getLocation();
 		if ($this->_getParam('time') && $this->_getParam('duration')) {
 			$holdLoader = Class_Multimedia_DeviceHold::getLoader();
 			$start = $holdLoader->getTimeFromDayAndTime($bean->day, $this->_getParam('time'));
@@ -598,87 +593,28 @@ class AbonneController extends Zend_Controller_Action {
 
 
 	public function multimediaHoldGroupAction() {
-		$bean = $this->_getDeviceHoldBean();
-
-		if (null == ($location = Class_Multimedia_Location::getLoader()->find((int)$bean->location))) {
-			$this->_redirect('/abonne/multimedia-hold-location');
-			return;
-		}
-
-		if ('' == $bean->day) {
-			$this->_redirect('/abonne/multimedia-hold-day');
-			return;
-		}
-
-		if ('' == $bean->time || 0 == $bean->duration) {
-			$this->_redirect('/abonne/multimedia-hold-hours');
-			return;
-		}
-
-		if ($this->_getParam('group')) {
-			$bean->group = $this->_getParam('group');
-			$this->_redirect('/abonne/multimedia-hold-device');
+		if (!$bean = $this->_getDeviceHoldBean())
 			return;
-		}
 
-		$this->view->groups = $location->getGroups();
+		$this->view->groups = $bean->getGroups();
 		$this->view->timelineActions = $this->_getTimelineActions('group');
 	}
 
 
 	public function multimediaHoldDeviceAction() {
-		$bean = $this->_getDeviceHoldBean();
-
-		if (null == ($group = Class_Multimedia_DeviceGroup::getLoader()->find((int)$bean->group))) {
-			$this->_redirect('/abonne/multimedia-hold-group');
+		if (!$bean = $this->_getDeviceHoldBean())
 			return;
-		}
-
-		if ('' == $bean->day) {
-			$this->_redirect('/abonne/multimedia-hold-day');
-			return;
-		}
 
-		if ('' == $bean->time || 0 == $bean->duration) {
-			$this->_redirect('/abonne/multimedia-hold-hours');
-			return;
-		}
-
-		if ($this->_getParam('device')) {
-			$bean->device = $this->_getParam('device');
-			$this->_redirect('/abonne/multimedia-hold-confirm');
-			return;
-		}
-		
 		$this->view->timelineActions = $this->_getTimelineActions('device');
-		$this->view->devices = $group->getHoldableDevicesForDateTimeAndDuration(
-																																						$bean->day,
-																																						$bean->time,
-																																						$bean->duration);
+		$this->view->devices = $bean->getGroup()->getHoldableDevicesForDateTimeAndDuration($bean->day,
+																																											 $bean->time,
+																																											 $bean->duration);
 	}
 
 
 	public function multimediaHoldConfirmAction() {
-		$bean = $this->_getDeviceHoldBean();
-		if (null == ($location = Class_Multimedia_Location::getLoader()->find((int)$bean->location))) {
-			$this->_redirect('/abonne/multimedia-hold-location');
-			return;
-		}
-
-		if ('' == $bean->day) {
-			$this->_redirect('/abonne/multimedia-hold-day');
+		if (!$bean = $this->_getDeviceHoldBean())
 			return;
-		}
-
-		if ('' == $bean->time || 0 == $bean->duration) {
-			$this->_redirect('/abonne/multimedia-hold-hours');
-			return;
-		}
-
-		if (null == ($device = Class_Multimedia_Device::getLoader()->find((int)$bean->device))) {
-			$this->_redirect('/abonne/multimedia-hold-device');
-			return;
-		}
 
 		if ($this->_getParam('validate')) {
 			$hold = Class_Multimedia_DeviceHold::getLoader()->newFromBean($bean);
@@ -688,10 +624,12 @@ class AbonneController extends Zend_Controller_Action {
 		}
 
 		$this->view->timelineActions = $this->_getTimelineActions('confirm');
-		$this->view->location = $location->getLibelle();
+		$this->view->location = $bean->getLocation()->getLibelle();
 		$this->view->day = strftime('%d %B %Y', strtotime($bean->day));
 		$this->view->time = str_replace(':', 'h', $bean->time);
 		$this->view->duration = $bean->duration . 'mn';
+
+		$device = $bean->getDevice();
 		$this->view->device = $device->getLibelle() . ' - ' . $device->getOs();
 	}
 
@@ -757,39 +695,12 @@ class AbonneController extends Zend_Controller_Action {
 
 	/** @return stdClass */
 	protected function _getDeviceHoldBean() {
-		return MultimediaReservationBean::current();
-	}
-}
-
-
-
-
-class MultimediaReservationBean extends StdClass {
-	public static function sessionNameSpace()  {
-		return new Zend_Session_Namespace('abonneController');
-	}
-
-	public static function newInSession() {
-		$session_ns = 
-		$bean = new self();
-		self::sessionNameSpace()->holdBean = $bean;
-		return $bean;
-	}
-
+		$bean = Class_Multimedia_ReservationBean::current();
+		if (!$bean->isCurrentStateValidForRequest($this->getRequest())) {
+			$this->_redirect('/abonne/'.$bean->currentState());
+			return null;
+		}
 
-	public static function current() {
-		if (null == ($bean = self::sessionNameSpace()->holdBean))
-			return self::newInSession();
 		return $bean;
 	}
-
-
-	public function __construct() {
-		$this->location = 0;
-		$this->day = '';
-		$this->time = '';
-		$this->duration = 0;
-		$this->group = 0;
-		$this->device = 0;
-	}
 }
\ No newline at end of file
diff --git a/library/Class/Multimedia/ReservationBean.php b/library/Class/Multimedia/ReservationBean.php
new file mode 100644
index 00000000000..82b4749878b
--- /dev/null
+++ b/library/Class/Multimedia/ReservationBean.php
@@ -0,0 +1,98 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * AFI-OPAC 2.0 is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with AFI-OPAC 2.0; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ */
+
+class Class_Multimedia_ReservationBean extends StdClass {
+	public static function sessionNameSpace()  {
+		return new Zend_Session_Namespace('abonneController');
+	}
+
+	public static function newInSession() {
+		$bean = new self();
+		self::sessionNameSpace()->holdBean = $bean;
+		return $bean;
+	}
+
+
+	public static function current() {
+		if (null == ($bean = self::sessionNameSpace()->holdBean))
+			return self::newInSession();
+		return $bean;
+	}
+
+
+	public function __construct() {
+		$this->location = 0;
+		$this->day = '';
+		$this->time = '';
+		$this->duration = 0;
+		$this->group = 0;
+		$this->device = 0;
+	}
+
+
+	public function currentState() {
+		if (null == $this->getLocation())
+			return 'multimedia-hold-location';
+
+		if ('' == $this->day)
+			return 'multimedia-hold-day';
+
+		if ('' == $this->time || 0 == $this->duration)
+			return 'multimedia-hold-hours';
+
+		if (null == $this->getGroup())
+			return 'multimedia-hold-group';
+
+		if (null == $this->getDevice())
+			return 'multimedia-hold-device';
+
+		return 'multimedia-hold-confirm';
+	}
+
+
+	public function isCurrentStateValidForRequest($request) {
+		foreach(['location', 'day', 'time', 'group', 'device'] as $param)
+			$this->$param = $request->getParam($param, $this->$param);
+
+		return $request->getActionName() == $this->currentState();
+	}
+
+	
+	public function getGroups() {
+		return $this->getLocation()->getGroups();
+	}
+
+
+	public function getLocation() {
+		return Class_Multimedia_Location::find((int)$this->location);
+	}
+
+	public function getGroup() {
+		return Class_Multimedia_DeviceGroup::find((int)$this->group);
+	}
+
+
+	public function getDevice() {
+		return Class_Multimedia_Device::getLoader()->find((int)$this->device);
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php b/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php
index 79eac507b6b..fb1dcfc3c5e 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php
@@ -20,6 +20,7 @@
  */
 
 require_once 'AbstractControllerTestCase.php';
+require_once 'application/modules/opac/controllers/AbonneController.php';
 
 trait TAbonneControllerMultimediaFixtureHoldSuccessOnSept12 {
 	protected function _launch() {
@@ -400,14 +401,7 @@ abstract class AbonneControllerMultimediaHoldTestCase extends AbstractController
 	public function setUp() {
 		parent::setUp();
 		$this->_session = new Zend_Session_Namespace('abonneController');
-		$bean = new stdClass();
-		$bean->location = 0;
-		$bean->day = '';
-		$bean->time = '';
-		$bean->duration = 0;
-		$bean->device = 0;
-		$bean->group  = 0;
-		$this->_session->holdBean = $this->_bean = $bean;
+		$this->_session->holdBean = $this->_bean = new Class_Multimedia_ReservationBean();
 
 		Class_Users::getIdentity()
 			->setUserGroups([Class_UserGroup::newInstanceWithId(12)
@@ -968,6 +962,7 @@ class AbonneControllerMultimediaHoldConfirmTest extends AbonneControllerMultimed
 		$this->_prepareLocationInSession();
 		$this->_prepareDayInSession();
 		$this->_prepareTimeAndDurationInSession();
+		$this->_prepareGroupInSession();
 		$this->_bean->device = 23;
 		Class_Multimedia_Device::getLoader()->newInstanceWithId(23)
 				->setLibelle('Poste 1')
@@ -1028,6 +1023,7 @@ class AbonneControllerMultimediaHoldConfirmValidatedTest extends AbonneControlle
 		$this->_prepareLocationInSession();
 		$this->_prepareDayInSession();
 		$this->_prepareTimeAndDurationInSession();
+		$this->_prepareGroupInSession();
 		$this->_bean->device = 23;
 		Class_Multimedia_Device::getLoader()->newInstanceWithId(23)
 				->setLibelle('Poste 1')
-- 
GitLab