From a4500176a651c35546b6642758ce83e879436ffa Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Thu, 17 Oct 2013 14:02:02 +0000
Subject: [PATCH] =?UTF-8?q?Ajout=20d'un=20catalogue=20directement=20depuis?=
 =?UTF-8?q?=20un=20kiosque=20+=20mise=20=C3=A0=20jour=20de=20la=20config?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../admin/controllers/CatalogueController.php | 16 ++++-
 .../admin/views/scripts/catalogue/form.phtml  |  5 +-
 .../ZendAfi/View/Helper/Accueil/Kiosque.php   | 27 ++++++--
 .../controllers/CatalogueControllerTest.php   | 64 +++++++++++++++----
 .../View/Helper/Accueil/KiosqueTest.php       | 14 ++++
 5 files changed, 102 insertions(+), 24 deletions(-)

diff --git a/application/modules/admin/controllers/CatalogueController.php b/application/modules/admin/controllers/CatalogueController.php
index 08ff2f45239..991c78d09d3 100644
--- a/application/modules/admin/controllers/CatalogueController.php
+++ b/application/modules/admin/controllers/CatalogueController.php
@@ -76,8 +76,11 @@ class Admin_CatalogueController extends ZendAfi_Controller_Action {
 		
 
 		if ($this->isSaved($catalogue)) {
+			if($id_module = $this->_getParam('id_module')) 
+				$this->updateConfigKiosque($id_module,$catalogue);
+
 			$this->_helper->notify($this->_('Domaine %s ajouté', $catalogue->getLibelle()));
-			$this->_redirect('admin/catalogue/index');
+			$this->_redirect('admin/catalogue/edit/id_catalogue/'.$catalogue->getId());
 			return;
 		}
 
@@ -85,6 +88,17 @@ class Admin_CatalogueController extends ZendAfi_Controller_Action {
 		$this->view->titre = $this->_('Ajout de domaine');
 		$this->getHelper('ViewRenderer')->renderScript('catalogue/form.phtml');
 	}
+
+
+
+	protected function updateConfigKiosque($id_module, $catalogue){
+		$profil = Class_Profil::getCurrentProfil();
+		$module_config = $profil->getModuleAccueilConfig($id_module);
+		$module_config['preferences']['id_catalogue'] = $catalogue->getId();
+		$profil->updateModuleConfigAccueil($id_module, $module_config);
+		$profil->save();
+		return $this;
+	}
 	
 
 	//----------------------------------------------------------------------------------
diff --git a/application/modules/admin/views/scripts/catalogue/form.phtml b/application/modules/admin/views/scripts/catalogue/form.phtml
index 6942fc10800..b3a6a896ff7 100644
--- a/application/modules/admin/views/scripts/catalogue/form.phtml
+++ b/application/modules/admin/views/scripts/catalogue/form.phtml
@@ -1,9 +1,6 @@
 <center>
 <div class="form" align="center" >
-	<form name="form" method="post" action="<?php echo $this->url(['module' => 'admin',
-																																 'controller' => 'catalogue',
-																																 'action' => 'edit',
-																																 'id_catalogue' => $this->catalogue->getId()]); ?>">
+	<form name="form" method="post" action="<?php echo $this->url(); ?>">
 
 		<fieldset>
 			<legend><?php echo $this->traduire('Domaine'); ?> </legend>
diff --git a/library/ZendAfi/View/Helper/Accueil/Kiosque.php b/library/ZendAfi/View/Helper/Accueil/Kiosque.php
index 492bfc80e3c..7a3f9b010cd 100644
--- a/library/ZendAfi/View/Helper/Accueil/Kiosque.php
+++ b/library/ZendAfi/View/Helper/Accueil/Kiosque.php
@@ -73,15 +73,28 @@ class ZendAfi_View_Helper_Accueil_Kiosque extends ZendAfi_View_Helper_Accueil_Ba
 		if (!Class_Users::isCurrentUserCanAccesBackend())
 			return '';
 
-		if (!$this->preferences['id_catalogue'])
+		if (!($this->preferences['id_catalogue'] && ($catalogue = Class_Catalogue::find($this->preferences['id_catalogue']))))
 			return '';
 
-		return $this->view->tagAnchor($this->view->url(['module' => 'admin',
-																										'controller' => 'catalogue',
-																										'action' => 'edit',
-																										'id_catalogue' => $this->preferences['id_catalogue']]),
-																  $this->view->tagImg(URL_ADMIN_IMG.'ico/edit.gif',['title' => $this->view->_('Modifier la sélection')]), 
-																	['data-popup' => 'true']);
+		$edit_link =  $this->view->tagAnchor($this->view->url(['module' => 'admin',
+																													 'controller' => 'catalogue',
+																													 'action' => 'edit',
+																													 'id_catalogue' => $this->preferences['id_catalogue']]),
+																				 $this->view->tagImg(URL_ADMIN_IMG.'ico/edit.gif',['title' => $this->view->_('Modifier la sélection')]), 
+																				 ['data-popup' => 'true']);
+
+		if (!$domaine_parent = $catalogue->getDomaineParent())
+			return $edit_link;
+
+		$add_link = $this->view->tagAnchor($this->view->url(['module' => 'admin',
+																												 'controller' => 'catalogue',
+																												 'action' => 'add',
+																												 'id_catalogue' => $domaine_parent->getId(),
+																												 'id_module' => $this->id_module]),
+																			 $this->view->tagImg(URL_ADMIN_IMG.'ico/add.gif',['title' => $this->view->_('Modifier la sélection')]), 
+																			 ['data-popup' => 'true']);
+
+		return $edit_link.$add_link;
 	}
 
 
diff --git a/tests/application/modules/admin/controllers/CatalogueControllerTest.php b/tests/application/modules/admin/controllers/CatalogueControllerTest.php
index 9ba4e752554..15f07ccceb9 100644
--- a/tests/application/modules/admin/controllers/CatalogueControllerTest.php
+++ b/tests/application/modules/admin/controllers/CatalogueControllerTest.php
@@ -655,6 +655,12 @@ class CatalogueControllerAddCatalogueTest extends AdminCatalogueControllerTestCa
 		$this->assertXPathContentContains('//select[@name="parent_id"]//option[@value="200"]', 'Politique');
 		$this->assertXPathContentContains('//select[@name="parent_id"]//option[@value="300"]', 'Moyen-age');
 	}
+
+
+	/** @test */
+	public function formActionShouldBeAdminCatalogueAddIdCatalogue100() {
+		$this->assertXPath('//form[contains(@action, "/admin/catalogue/add/id_catalogue/100")]');
+	}
 }
 
 
@@ -665,11 +671,18 @@ class CatalogueControllerAddCataloguePostTest extends AdminCatalogueControllerTe
 
 	public function setUp() {
 		parent::setUp();
-		$this->postDispatch('/admin/catalogue/add', array('libelle' => 'Geeks',
-																											'pcdm4' => '5',
-																											'annee_debut' => '20',
-																											'annee_fin' => '2020',
-																											'parent_id' => ''));
+
+		Class_Catalogue::whenCalled('save')
+			->willDo(
+				function($model) {
+					return $model->setId(987);
+				});
+
+		$this->postDispatch('/admin/catalogue/add', ['libelle' => 'Geeks',
+																								 'pcdm4' => '5',
+																								 'annee_debut' => '20',
+																								 'annee_fin' => '2020',
+																								 'parent_id' => '']);
 
 		$this->_new_catalogue = Class_Catalogue::getLoader()->getFirstAttributeForLastCallOn('save');
 	}
@@ -694,8 +707,8 @@ class CatalogueControllerAddCataloguePostTest extends AdminCatalogueControllerTe
 
 
 	/** @test */
-	public function responseShouldRedirectToIndex() {
-		$this->assertRedirectTo('/admin/catalogue/index');
+	public function responseShouldRedirectToEditCatalogueId987() {
+		$this->assertRedirectTo('/admin/catalogue/edit/id_catalogue/987');
 	}
 
 
@@ -719,11 +732,30 @@ class CatalogueControllerAddCatalogueToAdultesPostTest extends AdminCatalogueCon
 
 	public function setUp() {
 		parent::setUp();
-		$this->postDispatch('/admin/catalogue/add', array('libelle' => 'Geeks',
-																											'pcdm4' => '5',
-																											'annee_debut' => '20',
-																											'annee_fin' => '2020',
-																											'parent_id' => '6'), true);
+
+
+		Class_Profil::getCurrentProfil()
+			->setCfgAccueil([
+												'modules' => [
+													'10' => ['division' => '2',
+																	 'type_module' => 'KIOSQUE']],
+														 
+												'options' => []]);
+
+
+		Class_Catalogue::whenCalled('save')
+			->willDo(
+				function($model) {
+					return $model->setId(78);
+				});
+
+
+		$this->postDispatch('/admin/catalogue/add/id_module/10', 
+												['libelle' => 'Geeks',
+												 'pcdm4' => '5',
+												 'annee_debut' => '20',
+												 'annee_fin' => '2020',
+												 'parent_id' => '6']);
 
 		$this->_new_catalogue = Class_Catalogue::getLoader()->getFirstAttributeForLastCallOn('save');
 	}
@@ -733,6 +765,14 @@ class CatalogueControllerAddCatalogueToAdultesPostTest extends AdminCatalogueCon
 	public function newCatalogueDomaineParentShouldBeAdultes() {
 		$this->assertEquals('Adultes', $this->_new_catalogue->getDomaineParent()->getLibelle());
 	}
+
+
+	/** @test **/
+	public function boiteKiosqueShouldBeUpdatedToNewCatalogue78() {
+		$this->assertEquals( 78, 
+												 Class_Profil::getCurrentProfil()
+												 ->getModuleAccueilPreferences(10)['id_catalogue']);
+	}
 }
 
 
diff --git a/tests/library/ZendAfi/View/Helper/Accueil/KiosqueTest.php b/tests/library/ZendAfi/View/Helper/Accueil/KiosqueTest.php
index 90fa8c0e297..5a56d1a71bd 100644
--- a/tests/library/ZendAfi/View/Helper/Accueil/KiosqueTest.php
+++ b/tests/library/ZendAfi/View/Helper/Accueil/KiosqueTest.php
@@ -138,6 +138,12 @@ class ZendAfi_View_Helper_Accueil_KiosqueCubeAsRedacteurWithCatalogueTest extend
 																				 ->beModoBib());
 
 
+		Class_Catalogue::newInstanceWithId(12,
+																			 ['libelle' => 'Classique',
+																				'domaine_parent' => Class_Catalogue::newInstanceWithId(89,
+																																															 ['libelle' => 'Musique'])]);
+
+
 		$this->_html = $this->_helper->getBoite();
 	}
 
@@ -156,6 +162,14 @@ class ZendAfi_View_Helper_Accueil_KiosqueCubeAsRedacteurWithCatalogueTest extend
 											 '//a[contains(@href, "admin/catalogue/edit/id_catalogue/12")]',
 											 $this->_html);
 	}
+
+
+	/** @test */
+	public function linkToAddCatalogueId89ShouldBeVisible() {
+		$this->assertXPath($this->_html,
+											 '//a[contains(@href, "admin/catalogue/add/id_catalogue/89/id_module/3")]',
+											 $this->_html);
+	}
 }
 
 
-- 
GitLab