From 36ac25064f7eb1cb6c8ef96c8cc8ac3db425fabb Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Thu, 26 Apr 2012 12:45:41 +0000
Subject: [PATCH] =?UTF-8?q?Formations:=20refactoring=20fusion=20lettre=20e?=
 =?UTF-8?q?=CC=81margement?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../admin/controllers/FormationController.php | 57 ++++++++++++++++---
 .../controllers/FormationControllerTest.php   |  2 +-
 2 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/application/modules/admin/controllers/FormationController.php b/application/modules/admin/controllers/FormationController.php
index b5194814c33..229f55ae0cf 100644
--- a/application/modules/admin/controllers/FormationController.php
+++ b/application/modules/admin/controllers/FormationController.php
@@ -519,19 +519,16 @@ class SessionFusionStrategy extends AbstractSessionFusionStrategy{
 
 class SessionOneLetterPerDayFusionStrategy extends AbstractSessionFusionStrategy{
 	public function getContenuFusionne($session_formation) {
-		$date_utils = new Class_Date();
-		$date_debut = $session_formation->getDateDebut();
-		$date_fin = $session_formation->getDateFin() ? $session_formation->getDateFin() : $date_debut;
-		$nb_jours = $date_utils->soustraitDates($date_fin, $date_debut);
+		$date_context = new FusionDateContext($session_formation->getDateDebut());
+		$nb_jours = $date_context->numberOfDaysTo($session_formation->getDateFin());
 
 		$lettres = array();
 		for ($i=0; $i<=$nb_jours; $i++) {
-			$jour = $date_utils->ajouterJours($session_formation->getDateDebut(), $i, 'yyyy-mm-dd');
-			$session_formation->setDateJourTexte($date_utils->humanDate($jour, 'd MMMM yyyy'));
-
 			$lettres []= $this->_modele_fusion
-				->setDataSource(array("session_formation" => $session_formation))
+				->setDataSource(array("session_formation" => $session_formation,
+															"date_context" => $date_context))
 				->getContenuFusionne();
+			$date_context->forwardOneDay();
 		}
 
 		return implode('<div style="page-break-after: always"></div>', $lettres);
@@ -539,6 +536,50 @@ class SessionOneLetterPerDayFusionStrategy extends AbstractSessionFusionStrategy
 }
 
 
+
+
+class FusionDateContext {
+	protected $_current_date;
+
+	public function __construct($datestr) {
+		$this->_current_date = $this->dateStringToZendDate($datestr);
+	}
+
+
+	public function dateStringToZendDate($datestr) {
+		return new Zend_Date($datestr, null, Zend_Registry::get('locale'));
+	}
+
+
+	public function forwardOneDay() {
+		$this->_current_date->add(1, Zend_Date::DAY);		
+		return $this;
+	}
+
+
+	public function getTexte() {
+		return $this->_current_date->toString('d MMMM yyyy');
+	}
+
+
+	public function numberOfDaysTo($other_datestr) {
+		if (!$other_datestr)
+			return 0;
+
+		$other_date = $this->dateStringToZendDate($other_datestr);
+		$other_date->sub($this->_current_date);
+		return ($other_date->toValue(Zend_Date::DAY)-1);
+	}
+
+
+	public function callGetterByAttributeName($attribute) {
+		return call_user_func(array($this, 'get'.Storm_Inflector::camelize($attribute)));
+	}
+}
+
+
+
+
 class SessionStagiairesFusionStrategy extends AbstractSessionFusionStrategy{
 	public function getContenuFusionne($session_formation) {
 		$lettres = array();
diff --git a/tests/application/modules/admin/controllers/FormationControllerTest.php b/tests/application/modules/admin/controllers/FormationControllerTest.php
index f21a2e38599..893838b1dd1 100644
--- a/tests/application/modules/admin/controllers/FormationControllerTest.php
+++ b/tests/application/modules/admin/controllers/FormationControllerTest.php
@@ -1329,7 +1329,7 @@ abstract class FormationControllerImpressionsTestCase extends Admin_FormationCon
 										<h2>{session_formation.formation.libelle}</h2>
 
 										<p>
-										{session_formation.date_jour_texte}
+										{date_context.texte}
 										</p>
 
 										<p>
-- 
GitLab