From dd3de80fa1463ebd12934a0c78356ab3977d0993 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Thu, 16 Apr 2015 14:37:12 +0200
Subject: [PATCH] hotline_#24018_fix_duplicate_id_module_in_same_page

---
 ...tline_#24018_fix_duplicate_id_in_same_page |  1 +
 library/Class/Profil.php                      | 45 ++++++++++++++++
 .../ProfilOptionsControllerTest.php           | 51 ++++++++++++++++++-
 3 files changed, 96 insertions(+), 1 deletion(-)
 create mode 100644 VERSIONS_HOTLINE/hotline_#24018_fix_duplicate_id_in_same_page

diff --git a/VERSIONS_HOTLINE/hotline_#24018_fix_duplicate_id_in_same_page b/VERSIONS_HOTLINE/hotline_#24018_fix_duplicate_id_in_same_page
new file mode 100644
index 00000000000..771e9e591fc
--- /dev/null
+++ b/VERSIONS_HOTLINE/hotline_#24018_fix_duplicate_id_in_same_page
@@ -0,0 +1 @@
+ - ticket #24018 : Correction d'un bug qui ajoutait des boîtes avec le même identifiant entre les profils partagés.
\ No newline at end of file
diff --git a/library/Class/Profil.php b/library/Class/Profil.php
index 6b651546e49..fcc9a3844cb 100644
--- a/library/Class/Profil.php
+++ b/library/Class/Profil.php
@@ -1597,9 +1597,54 @@ class Class_Profil extends Storm_Model_Abstract {
 	public function afterSave() {
 		$this->_has_parent_profil = null;
 		$this->_should_forward_attributes = [];
+		$this->syncModulesIdsWithParentProfil();
 	}
 
 
+	protected function syncModulesIdsWithParentProfil() {
+		if(!$this->hasParentProfil())
+			return;
+
+		if(!$parent_banner_modules = $this->getParentProfil()->getBoitesDivision(self::DIV_BANNIERE))
+			return;
+
+		if(!$ids_used_by_parent_profil = array_keys($parent_banner_modules))
+			return;
+
+		return $this->doNotUseIds($ids_used_by_parent_profil);
+	}
+
+
+	protected function doNotUseIds($ids) {
+		$my_cfg = $this->getCfgAccueilAsArray();
+
+		if(!isset($my_cfg['modules']))
+			return;
+
+		if(!$my_modules_ids = array_keys($my_cfg['modules']))
+			return;
+
+		if(!$duplicate_ids = array_intersect($ids, $my_modules_ids))
+			return;
+
+		$last_used_id = max(array_unique(array_merge($ids, $my_modules_ids)));
+
+		foreach($duplicate_ids as $id) {
+			$last_used_id++;
+			$this->changeIdModuleTo($id, $last_used_id);
+		}
+	}
+
+
+	protected function changeIdModuleTo($id, $new_id) {
+		$cfg_accueil = $this->getCfgAccueilAsArray();
+		$module_cfg = $cfg_accueil['modules'][$id];
+		unset($cfg_accueil['modules'][$id]);
+		$cfg_accueil['modules'][$new_id] = $module_cfg;
+		$this->setCfgAccueil($cfg_accueil);
+		return $this;
+	}
+
 
 	public function setCfgMenuHorizontal($cfg_menu_horizontal) {
 		$menus = $this->getCfgMenusAsArray();
diff --git a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
index 7de06fe9f9d..dd314b0bddd 100644
--- a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
+++ b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
@@ -2364,4 +2364,53 @@ class ProfilOptionsControllerSelectedActionInMenuVerticalWidgetInHomeTest extend
 		$this->assertXPathContentContains('//ul/li//ul/li[@class="menuGauche"]', 'Bokeh FAQ');
 	}
 }
-?>
\ No newline at end of file
+
+
+
+class ProfilOptionControllerWithHeritedBannerTest extends AbstractControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+
+		$profil_portal = $this->fixture('Class_Profil',
+																		['id' => 1,
+																		 'browser' => 'opac',
+																		 'libelle' => 'Bokeh Portal'])
+													->setCfgAccueil(['modules' => ['1' => ['division' => '4',
+																																 'type_module' => 'RECH_SIMPLE',
+																																 'preferences' => []],
+																												 '4' => ['division' => '4',
+																																 'type_module' => 'NEWS']]]);
+
+		$profil_gnu = $this->fixture('Class_profil',
+																 ['id' => 2,
+																	'parent_profil' => $profil_portal,
+																	'browser' => 'opac',
+																	'libelle' => 'A GNU Portal'])
+											 ->setCfgAccueil(['modules' => ['4' => ['division' => '1',
+																															'type_module' => 'NEWS'],
+																											'10' => ['division' => '1',
+																															 'type_module' => 'NEWS']]]);
+		$profil_gnu->save();
+		$profil_gnu->beCurrentProfil();
+
+		$this->dispatch('/opac', true);
+	}
+
+
+	/** @test */
+	public function htmlShouldCOntainsOnlyOneIdBoite4() {
+		$this->assertXPathCount('//div[@id="boite_4"]', 1);
+	}
+
+
+	/** @test */
+	public function htmlShouldCOntainsIdBoite11() {
+		$this->assertXPathCount('//div[@id="boite_11"]', 1);
+	}
+
+
+	/** @test */
+	public function profilShouldIncrementCfgIdsAndDeleteOldId() {
+		$this->assertEquals([10,11], array_keys(Class_Profil::find(2)->getCfgAccueilAsArray()['modules']));
+	}
+}
\ No newline at end of file
-- 
GitLab