From 3024e764e5821c4fc9f26ed5a4878518dd64e43c Mon Sep 17 00:00:00 2001
From: Matthias Meusburger <matthias.meusburger@biblibre.com>
Date: Thu, 11 Dec 2014 11:38:39 +0100
Subject: [PATCH] dev #17629 no result when domain has no settings + display
 notices linked to domain

---
 cosmogramme/php/_init.php                     |  2 +-
 cosmogramme/sql/patch/patch_225.sql           |  6 ++
 library/Class/Catalogue.php                   | 11 +++
 library/Class/CriteresRecherche.php           | 15 +++-
 library/Class/Indexation/PseudoNotice.php     | 79 +++++++++++--------
 library/Class/MoteurRecherche.php             |  7 ++
 library/Class/Notice.php                      |  3 +-
 .../controllers/RechercheControllerTest.php   | 77 +++++++++++++++++-
 8 files changed, 163 insertions(+), 37 deletions(-)
 create mode 100644 cosmogramme/sql/patch/patch_225.sql

diff --git a/cosmogramme/php/_init.php b/cosmogramme/php/_init.php
index 653e89b2a2c..d2e8cb1dc36 100644
--- a/cosmogramme/php/_init.php
+++ b/cosmogramme/php/_init.php
@@ -1,7 +1,7 @@
 <?php
 // Constantes
 error_reporting(E_ERROR | E_PARSE);
-define("PATCH_LEVEL","224");
+define("PATCH_LEVEL","225");
 
 define("APPLI","cosmogramme");
 define("COSMOPATH", "/var/www/html/vhosts/opac2/www/htdocs");
diff --git a/cosmogramme/sql/patch/patch_225.sql b/cosmogramme/sql/patch/patch_225.sql
new file mode 100644
index 00000000000..244b16d1d71
--- /dev/null
+++ b/cosmogramme/sql/patch/patch_225.sql
@@ -0,0 +1,6 @@
+CREATE TABLE IF NOT EXISTS notice_domain (
+       id int( 11 ) NOT NULL AUTO_INCREMENT ,
+       notice_id int( 11 ) NOT NULL default 0,
+       domain_id int( 11 ) NOT NULL default 0,
+       PRIMARY KEY ( id )
+) ENGINE = MYISAM DEFAULT CHARSET = utf8;
diff --git a/library/Class/Catalogue.php b/library/Class/Catalogue.php
index d271c14c7ca..5fc8d107760 100644
--- a/library/Class/Catalogue.php
+++ b/library/Class/Catalogue.php
@@ -1111,11 +1111,22 @@ class Class_Catalogue extends Storm_Model_Abstract {
 																	'multipleSelection' => false]];
 
 	}
+
+
+	public function hasNoSettings() {
+		$attributs = $this->_default_attribute_values;
+		unset($attributs['libelle']);
+		foreach($attributs as $attribut) {
+			if($this->isAttributeEmpty($attribut))
+				return true;
+		}
+	}
 }
 
 
 
 
+
 class AllNoticesCatalogue extends Class_Catalogue {
 	public function isMatchingAllNotices() {
 		return true;
diff --git a/library/Class/CriteresRecherche.php b/library/Class/CriteresRecherche.php
index f2978a4bab7..abaf509af84 100644
--- a/library/Class/CriteresRecherche.php
+++ b/library/Class/CriteresRecherche.php
@@ -120,7 +120,6 @@ class Class_CriteresRecherche {
 	public function setParams($rech) {
 		$this->_params = $this->filterParams($rech);
 		return $this;
-
 	}
 
 
@@ -155,6 +154,17 @@ class Class_CriteresRecherche {
 	}
 
 
+	public function hasEmptyDomain() {
+		if( !$catalogue = Class_Catalogue::find($this->getParam('id_catalogue')))
+			return false;
+
+		if($catalogue->hasNoSettings() && !Class_NoticeDomain::getClesNoticesForDomain($catalogue->getId()))
+			return true;
+
+		return false;
+	}
+
+
 	public function getGeoZone(){
 		return $this->getParam('geo_zone',false);
 	}
@@ -292,6 +302,9 @@ class Class_CriteresRecherche {
 
 		if ($catalogue->getAnneeDebut() || $catalogue->getAnneeFin())
 			$visitor->visitAnneeDebutFin($catalogue->getAnneeDebut(),$catalogue->getAnneeFin());
+
+		if($cles_notices = Class_NoticeDomain::getClesNoticesForDomain($catalogue->getId()))
+			$visitor->visitClesNotices($cles_notices);
 	}
 
 
diff --git a/library/Class/Indexation/PseudoNotice.php b/library/Class/Indexation/PseudoNotice.php
index 4171837f3d5..be752b49620 100644
--- a/library/Class/Indexation/PseudoNotice.php
+++ b/library/Class/Indexation/PseudoNotice.php
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with BOKEH; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 class Class_Indexation_PseudoNotice {
@@ -45,8 +45,8 @@ class Class_Indexation_PseudoNotice {
 
 	public static function newWith($type_doc, $datas) {
 		// types simples
-		$mapping = [self::TYPE_CMS => 'Cms', 
-								self::TYPE_RSS => 'Rss', 
+		$mapping = [self::TYPE_CMS => 'Cms',
+								self::TYPE_RSS => 'Rss',
 								self::TYPE_SITO=> 'Sito'];
 
 		if (array_key_exists($type_doc, $mapping)) {
@@ -64,7 +64,7 @@ class Class_Indexation_PseudoNotice {
 		$this->_type_doc = (int)$type_doc;
 		$this->_indexation = new Class_Indexation();
 		$this->_datas = array_change_key_case($datas, CASE_LOWER);
-		$this->_model = call_user_func_array([$this->_model_name, 'find'], 
+		$this->_model = call_user_func_array([$this->_model_name, 'find'],
 																				 [$this->_datas[$this->_id]]);
 	}
 
@@ -86,7 +86,6 @@ class Class_Indexation_PseudoNotice {
 
 	public function save() {
 		$this->_prepare();
-
 		$notice = Class_Notice::newInstance(['type_doc' => $this->_type_doc]);
 		if (!$notice->save())
 			return false;
@@ -147,21 +146,21 @@ class Class_Indexation_PseudoNotice {
 		$this->_notice->updateAttributes($data);
 
 		$authors = $this->_getAuthorNames();
-		
+
 		$this->_notice
-			->setMatieres($this->extractFullTextFromCodif($data, 
-																										'matiere', 
+			->setMatieres($this->extractFullTextFromCodif($data,
+																										'matiere',
 																										'Class_CodifMatiere'))
 			->setAlphaTitre($this->_indexation->codeAlphaTitre($this->_datas["titre"]))
-			->setClefAlpha($this->_indexation->getClefAlpha($this->_notice->getTypeDoc(), 
-																											$this->_datas["titre"], 
-																											'', 
+			->setClefAlpha($this->_indexation->getClefAlpha($this->_notice->getTypeDoc(),
+																											$this->_datas["titre"],
+																											'',
 																											implode('-', $authors),
 																											'',
 																											$this->_datas["editeur"],
 																											$this->_datas["annee"]))
 			->setClefOeuvre($this->_indexation->getClefOeuvre($this->_datas["titre"],
-																												'', 
+																												'',
 																												$this->_datas["auteur"],
 																												''))
 			->setTitres($this->_indexation->getfullText($this->extractTitles()))
@@ -175,7 +174,7 @@ class Class_Indexation_PseudoNotice {
 			->setDateMaj(date('Y-m-d H:i:s'));
 
 
-		if ($this->_dataExist("editeur")) 
+		if ($this->_dataExist("editeur"))
 			$this->_notice->setEditeur($this->_indexation->getfullText($this->_datas["editeur"]));
 
 		if ($this->_dataExist("fichier") && !empty($this->_datas['fichier'])) {
@@ -200,11 +199,27 @@ class Class_Indexation_PseudoNotice {
 		$cote = '';
 		if ($this->_dataExist("cote"))
 			$cote = $this->_datas["cote"];
-		
+
 		$this->_exemplaire
 			->setGenre($genre)
 			->setCote($cote)
 			->save();
+
+		$this->saveDomains();
+	}
+
+
+	protected function saveDomains() {
+		if(!array_key_exists('domaine_ids', $this->_datas))
+			 return $this;
+
+		$domaine_ids = explode(';', $this->_datas['domaine_ids']);
+
+		foreach($domaine_ids as $domain) {
+			$notice_domain = Class_NoticeDomain::newInstance(['domain_id' => $domain,
+																												'notice_id' => $this->_notice->getId()]);
+			$notice_domain->save();
+		}
 	}
 
 
@@ -235,26 +250,26 @@ class Class_Indexation_PseudoNotice {
 
 
 	protected function extractFullTextFromCodif($data, $field, $codif_class)   {
-		if (!$this->_dataExist($field)) 
+		if (!$this->_dataExist($field))
 			return '';
-      	 
+
 		$ids = array_filter(array_map('trim', explode(';', $this->_datas[$field])));
 		$fulltext = [];
 		foreach($ids as $id) {
 			if ($model = $codif_class::find($id))
 				$fulltext[] = $model->getLibelle();
 		}
-      
+
 		return $this->_indexation->getfullText(implode(' ', $fulltext));
 	}
 
 
 	protected function indexDeweyAndPCDM4($data)	{
-		$pcdm4 = $this->extractFullTextFromCodif($data, 
-																						 'pcdm4', 
+		$pcdm4 = $this->extractFullTextFromCodif($data,
+																						 'pcdm4',
 																						 'Class_CodifPcdm4');
-		$dewey = $this->extractFullTextFromCodif($data, 
-																						 'dewey', 
+		$dewey = $this->extractFullTextFromCodif($data,
+																						 'dewey',
 																						 'Class_CodifDewey');
 
 		$data["dewey"] = implode(' ', array_filter([$pcdm4, $dewey]));
@@ -267,9 +282,9 @@ class Class_Indexation_PseudoNotice {
 			return $this->url_site;
 
 		$adresse = Class_CosmoVar::get('url_site');
-		if (strtolower(substr($adresse, 0, 7)) != 'http://') 
+		if (strtolower(substr($adresse, 0, 7)) != 'http://')
 			$adresse = 'http://' . $adresse;
-		if (substr($adresse, -1, 1) != '/') 
+		if (substr($adresse, -1, 1) != '/')
 			$adresse .= '/';
 		return $this->url_site = $adresse;
 	}
@@ -322,7 +337,7 @@ class Class_Indexation_PseudoNotice {
 	}
 
 	protected function _getCodeBarres() {
-		return str_repeat('0', (4 - strlen($this->_datas['id_bib']))) 
+		return str_repeat('0', (4 - strlen($this->_datas['id_bib'])))
 			. $this->_datas['id_bib'] . '-' . $this->_notice->getId();
 	}
 
@@ -350,7 +365,7 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
 		parent::_prepare();
 
 		if ($this->_datas['id_origine'])
-			$this->_datas['url'] = $this->getUrlSite() . 'bib-numerique/notice/ido/' 
+			$this->_datas['url'] = $this->getUrlSite() . 'bib-numerique/notice/ido/'
 				. $this->_datas['id_origine'];
 
 		if ($poster = $this->_model->getPoster())
@@ -361,7 +376,7 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
 
 
 	protected function _modelIdAcceptVisitor($visitor) {}
-	
+
 	/** @return array */
 	protected function extractAuthors() {
 		$authors = parent::extractAuthors();
@@ -374,23 +389,23 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
 		return array_unique($authors);
 	}
 
-	
+
 	protected function _addAuthorArrayAsNoticeAuthor($datas, &$authors) {
 		foreach($datas as $data)
-			$authors []= new Class_Notice_Author($data['a'], 
+			$authors []= new Class_Notice_Author($data['a'],
 																					 isset($data['4']) ? $data['4'] : '');
 	}
 
-	
+
 	protected function extractEditors() {
-		return array_unique(array_merge(parent::extractEditors(), 
+		return array_unique(array_merge(parent::extractEditors(),
 																		$this->_model->getEditors()));
 	}
 
 
 	/** @return array */
 	protected function extractCollections() {
-	  return array_unique(array_merge(parent::extractCollections(), 
+	  return array_unique(array_merge(parent::extractCollections(),
 																		$this->_model->getCollections()));
 	}
 
@@ -418,7 +433,7 @@ class Class_Indexation_PseudoNotice_Album extends Class_Indexation_PseudoNotice{
 			->visitGenre($this->getValuesFromModel('genre'))
 			->visitPcdm4($this->getValuesFromModel('pcdm4'));
 
-		foreach ($this->_model->getRessources() as $ressource) 
+		foreach ($this->_model->getRessources() as $ressource)
 			$visitor->visitRessource($ressource);
 	}
 
diff --git a/library/Class/MoteurRecherche.php b/library/Class/MoteurRecherche.php
index ae55b792585..3e0ffb8bf6c 100644
--- a/library/Class/MoteurRecherche.php
+++ b/library/Class/MoteurRecherche.php
@@ -263,6 +263,13 @@ class Class_MoteurRecherche {
 			$this->setCondition("MATCH(facettes) AGAINST('" . trim($this->all_facettes)."' IN BOOLEAN MODE)");
 		}
 
+
+		if($this->criteres_recherche->hasEmptyDomain()) {
+			$ret["statut"] = 'erreur';
+			$ret["erreur"] = $this->_('Domaine non paramétré');
+			return $ret;
+		}
+
 		$where = '';
 		if ($this->conditions)
 			$where = 'Where ' . $this->conditions;
diff --git a/library/Class/Notice.php b/library/Class/Notice.php
index 67b81992a84..c298c643174 100644
--- a/library/Class/Notice.php
+++ b/library/Class/Notice.php
@@ -1562,7 +1562,6 @@ class Class_Notice extends Storm_Model_Abstract {
 			return trim(str_replace('|',' ', $auteur));
 		}
 		return '';
-		}
 	}
-
+}
 ?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/RechercheControllerTest.php b/tests/application/modules/opac/controllers/RechercheControllerTest.php
index 88531dafada..6b54b663c22 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerTest.php
@@ -1505,7 +1505,6 @@ class RechercheControllerSimpleActionWithCatalogueAndDomainBrowserWidgetTest ext
 										'domaine_parent' => $this->fixture('Class_Catalogue',
 																											 ['id'=>3,
 																												'libelle' => 'Nouveautés'])]);
-
 	}
 
 
@@ -2263,4 +2262,80 @@ class RechercheControllerViewnoticeWithBreadcrumbAndIdPanierParamTest extends Re
 	public function pageTitleShouldBeAsExpected() {
 		$this->assertXPathContentContains('//head', 'my panier - Cinéma d\'animation', $this->_response->getBody());
 	}
+}
+
+
+
+class RechercheControlleSimpleActionWithEmptyDomainSettingsTest extends AbstractControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+		$this->fixture('Class_Catalogue',
+									 ['id'=>3,
+										'libelle' => 'Nouveautés',
+										'auteur' => 'Paul']);
+		$this->dispatch('/recherche/simple/id_catalogue/3/id_module/9/aleatoire/1', true);
+	}
+
+
+  /** @test */
+	public function onRechercheSimpleCatalogue3IdModule9ResultsShouldBeEmpty() {
+		$this->assertXPathContentContains('//div[@class="info-recherche"]', 'Aucun résultat trouvé',											 $this->_response->getBody());
+	}
+}
+
+
+class RechercheControlleSimpleActionWithEmptyDomainSettingsAndSiteLinkedTest extends AbstractControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+		$this->fixture('Class_Catalogue',
+									 ['id'=>3,
+										'libelle' => 'Nouveautés']);
+		$this->fixture('Class_Sitotheque',
+									 ['id' => 1,
+										'titre' => 'Mon lien',
+										'url' => 'http://monlien.com',
+										'domaine_ids' => 3,
+									 ]);
+
+		Class_Notice::beVolatile();
+		Class_NoticeDomain::beVolatile();
+		Class_Sitotheque::find(1)->index();
+
+
+		$mock_sql = $this->mock()
+										 ->whenCalled('fetchAllByColumn')
+										 ->with("Select id_notice from notices Where notices.clef_alpha in('MONLIEN------10')")
+										 ->answers([1])
+										 ->whenCalled('fetchOne')
+										 ->answers(1)
+										 ->whenCalled('fetchAll')
+										 ->answers([Class_Notice::find(1)->toArray()]);
+
+
+
+		Zend_Registry::set('sql', $mock_sql);
+		Class_Profil::getCurrentProfil()
+			->setCfgModules(['recherche' => ['resultatsimple' => [
+																														'liste_format' => 			Class_Systeme_ModulesAppli::LISTE_FORMAT_MUR]]]);
+
+		$this->dispatch('/recherche/simple/id_catalogue/3/id_module/9/aleatoire/1', true);
+	}
+
+
+	/** @test */
+	public function monLienShouldExistsAsANotice() {
+		$this->assertEquals('MON LIEN', Class_Notice::find(1)->getAlphaTitre());
+	}
+
+
+	/** @test */
+	public function sitoDomainEShouldExists() {
+		$this->assertNotNull(Class_NoticeDomain::find(1));
+	}
+
+
+  /** @test */
+	public function onRechercheSimpleCatalogueSitoShouldBePresent() {
+		$this->assertXPathContentContains('//div[@class="resultat_recherche"]//a', 'Mon lien',											 $this->_response->getBody());
+	}
 }
\ No newline at end of file
-- 
GitLab