From dbe4b39594986293b56ca7b76608115ddb43a6d8 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Fri, 4 Sep 2015 14:15:54 +0200
Subject: [PATCH] hotline #29082 fix articles and sito indexation into domains

Conflicts:
	library/storm
---
 VERSIONS_HOTLINE/#29082                            |  1 +
 library/Class/Catalogue.php                        |  7 +++++++
 library/Class/Notice.php                           |  5 +----
 library/Trait/HasManyDomaines.php                  | 11 ++++-------
 library/Trait/Indexable.php                        | 14 ++++++++++----
 library/storm                                      |  2 +-
 .../AdminAvisModerationControllerTest.php          |  2 +-
 tests/library/Class/SitothequeTest.php             | 10 +++++++++-
 8 files changed, 34 insertions(+), 18 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/#29082

diff --git a/VERSIONS_HOTLINE/#29082 b/VERSIONS_HOTLINE/#29082
new file mode 100644
index 00000000000..fc21cdc8e13
--- /dev/null
+++ b/VERSIONS_HOTLINE/#29082
@@ -0,0 +1 @@
+ - ticket #29082 : correction de l'indexation des articles et des sitothèques dans les domaines
\ No newline at end of file
diff --git a/library/Class/Catalogue.php b/library/Class/Catalogue.php
index 98082e25cfc..79f20e0e498 100644
--- a/library/Class/Catalogue.php
+++ b/library/Class/Catalogue.php
@@ -537,6 +537,13 @@ class CatalogueLoader extends Storm_Model_Loader {
 
     return $thesaurus;
   }
+
+
+  public function getIds($domains) {
+    return (new Storm_Model_Collection($domains))
+      ->collect('id')
+      ->getArrayCopy();
+  }
 }
 
 
diff --git a/library/Class/Notice.php b/library/Class/Notice.php
index 2dc8bdc4951..75f92e8c4d1 100644
--- a/library/Class/Notice.php
+++ b/library/Class/Notice.php
@@ -143,10 +143,7 @@ class Class_Notice extends Storm_Model_Abstract {
   protected $_notice_unimarc;
   protected $_has_many = ['exemplaires' => ['model' => 'Class_Exemplaire',
                                             'role' => 'notice',
-                                            'dependents' => 'delete'],
-                          'notice_domain' => ['model' => 'Class_NoticeDomain',
-                                              'role' => 'domain',
-                                              'dependents' => 'delete']];
+                                            'dependents' => 'delete']];
 
   protected
     $_titre_principal,
diff --git a/library/Trait/HasManyDomaines.php b/library/Trait/HasManyDomaines.php
index 1d74ef8edee..879feab62ce 100644
--- a/library/Trait/HasManyDomaines.php
+++ b/library/Trait/HasManyDomaines.php
@@ -31,17 +31,14 @@ trait Trait_HasManyDomaines {
 
   public function getDomaineIdsAsArray() {
     $domaines = $this->_get('domaine_ids') ;
-    return array_filter(explode(';',$domaines));
+    return array_filter(explode(';', $domaines));
   }
 
 
   public function getDomaines() {
-    $ids = $this->getDomaineIdsAsArray();
-    $domaines = [];
-    foreach ($ids as $id) {
-      $domaines[] = Class_Catalogue::find($id);
-    }
-    return $domaines;
+    return empty($domains = $this->getDomaineIdsAsArray())
+      ? []
+      : Class_Catalogue::findAllBy(['id_catalogue' => $domains]);
   }
 
 
diff --git a/library/Trait/Indexable.php b/library/Trait/Indexable.php
index 5d35e0f6b87..48e4947df09 100644
--- a/library/Trait/Indexable.php
+++ b/library/Trait/Indexable.php
@@ -30,15 +30,19 @@ trait Trait_Indexable {
 
 
   protected function indexIntoDomain() {
-    if(!$domains_ids = $this->getDomaineIdsAsArray())
-       return $this;
-
     if(!$alpha_key = $this->getAlphaKey()) {
       return $this;
     }
 
+    $domains_ids = Class_Catalogue::getIds($this->getDomaines());
+
     $existing = Class_NoticeDomain::findAllBy(['record_alpha_key' => $alpha_key]);
-    $existing_ids = array_map(function ($item) { return $item->getDomainId();}, $existing);
+
+    $existing_domains = array_filter(array_map(function ($item) {
+                                                                   return $item->getDomain();
+                                                                 }, $existing));
+
+    $existing_ids = array_map(function ($item) { return $item->getId();}, $existing_domains);
 
     if ($to_delete = array_diff($existing_ids, $domains_ids))
       Class_NoticeDomain::deleteBy(['record_alpha_key' => $alpha_key,
@@ -56,6 +60,8 @@ trait Trait_Indexable {
     foreach(Class_NoticeDomain::findAllBy(['record_alpha_key' => $alpha_key]) as $notice_domain)
       $notice_domain->updateFacette();
 
+    $this->setDomaineIds($domains_ids)->save();
+
     return $this;
   }
 
diff --git a/library/storm b/library/storm
index 08c073b2bfd..e8b3587ac7d 160000
--- a/library/storm
+++ b/library/storm
@@ -1 +1 @@
-Subproject commit 08c073b2bfde86b0d4d4f4ed3ef8503543fa4506
+Subproject commit e8b3587ac7dd17e67ce4d968cc7d3e1c1b9a1d5d
diff --git a/tests/application/modules/admin/controllers/AdminAvisModerationControllerTest.php b/tests/application/modules/admin/controllers/AdminAvisModerationControllerTest.php
index 9a77e0098ca..2f932411269 100644
--- a/tests/application/modules/admin/controllers/AdminAvisModerationControllerTest.php
+++ b/tests/application/modules/admin/controllers/AdminAvisModerationControllerTest.php
@@ -362,7 +362,7 @@ class AdminAvisModerationControllerCmsWithAvisTest extends AdminAvisModerationCo
   /** @test */
   public function pageShouldContainsLinkToArticle() {
     $this->assertXPathContentContains('//a[contains(@href, "/admin/cms/viewcms/id/9")]',
-                                      "Tintin à Annecy",
+                                     "Tintin à Annecy",
                                       $this->_response->getBody());
   }
 
diff --git a/tests/library/Class/SitothequeTest.php b/tests/library/Class/SitothequeTest.php
index 5711941b9cb..5d8250a6f4b 100644
--- a/tests/library/Class/SitothequeTest.php
+++ b/tests/library/Class/SitothequeTest.php
@@ -71,7 +71,6 @@ class SitothequeIndexAllTest extends Storm_Test_ModelTestCase {
                     'domaine_ids' => '1']);
 
     Class_Sitotheque::indexAll();
-    Class_Sitotheque::find(3)->setDomaineIds('1')->save();
   }
 
 
@@ -97,9 +96,18 @@ class SitothequeIndexAllTest extends Storm_Test_ModelTestCase {
   public function domainShouldHaveMySito2Record() {
     $this->assertNotNull(Class_NoticeDomain::findFirstBy(['record_alpha_key' => 'MYSITO2------10']));
   }
+
+
+  /** @test */
+  public function removingMyDomainShouldRemoveFacetInMySito() {
+    Class_Catalogue::find(1)->delete();
+    Class_Sitotheque::indexAll();
+    $this->assertNotContains('1', Class_Sitotheque::find(2)->getDomaineIdsAsArray());
+  }
 }
 
 
+
 class SitothequeUnindexTest extends Storm_Test_ModelTestCase {
   public function setUp() {
     parent::setUp();
-- 
GitLab