From 0c0bb9495660e1b8e17ae08960063e9ba396a462 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Tue, 8 Sep 2015 18:13:44 +0200
Subject: [PATCH] dev #17782 : notice view notice , refacto

---
 .../opac/controllers/RechercheController.php  |  7 --
 .../scripts/recherche/resultatRecherche.phtml |  2 +-
 .../views/scripts/recherche/viewnotice.phtml  |  5 ++
 library/Class/ModeleFusion.php                | 25 +++++--
 library/ZendAfi/Controller/Action.php         | 23 ++++--
 .../RechercheControllerPrintActionTest.php    | 74 ++++++++++++++++++-
 6 files changed, 110 insertions(+), 26 deletions(-)

diff --git a/application/modules/opac/controllers/RechercheController.php b/application/modules/opac/controllers/RechercheController.php
index af07ef1389c..7c521007b78 100644
--- a/application/modules/opac/controllers/RechercheController.php
+++ b/application/modules/opac/controllers/RechercheController.php
@@ -751,13 +751,6 @@ class RechercheController extends ZendAfi_Controller_Action {
   }
 
 
-	public function _renderLettreFusion($fusion_strategy) {
-		$this->_helper->getHelper('viewRenderer')->setLayoutScript('empty.phtml');
-
-		$this->renderScript('recherche/print-fusion.phtml');
-
-	}
-
 }
 
 ?>
\ No newline at end of file
diff --git a/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml b/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml
index eaef32e1238..f7f3d2c487f 100644
--- a/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml
+++ b/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml
@@ -13,7 +13,7 @@ if ($this->is_pertinence) {
 ?>
 <div class="print">
 <?php
-  echo $this->tagPrintLink($this->search_result->fetchRecords(), 'Notice');
+  echo $this->tagPrintLink($this->search_result->fetchRecords(), 'Notice_List');
 ?>
 </div>
 <div class="resultats_page">
diff --git a/application/modules/opac/views/scripts/recherche/viewnotice.phtml b/application/modules/opac/views/scripts/recherche/viewnotice.phtml
index a7fda1b98e3..bc881ad2642 100644
--- a/application/modules/opac/views/scripts/recherche/viewnotice.phtml
+++ b/application/modules/opac/views/scripts/recherche/viewnotice.phtml
@@ -25,6 +25,11 @@ $script_loader = Class_ScriptLoader::getInstance()
 	<a class="retour" href="<?php echo $this->url($this->criteres_recherche->getUrlRetourListe(), null, true).'#'.$this->notice->getId(); ?>">
     <?php echo $this->_('Retour à la liste') ?>
   </a>
+<div class="print">
+<?php
+  echo $this->tagPrintLink($this->notice, 'Notice_View');
+?>
+</div>
 
 	<?php echo $this->tagAnchor($this->url_panier,
 															$this->_('Ajouter au Panier'),
diff --git a/library/Class/ModeleFusion.php b/library/Class/ModeleFusion.php
index 9e2de8345c4..6060529f367 100644
--- a/library/Class/ModeleFusion.php
+++ b/library/Class/ModeleFusion.php
@@ -41,8 +41,10 @@ class Class_ModeleFusionLoader extends Storm_Model_Loader {
 
 	public function getAllTypes() {
 		return ['none' => 'Aucun',
-					  'Article' => 'Page d\'articles',
-						'Notice' => 'Resultats de recherche'];
+					  'Article_List' => 'Page d\'articles',
+						'Notice_List' => 'Resultats de recherche',
+            'Notice_View' => 'Page de notice'
+    ];
 	}
 }
 
@@ -65,6 +67,7 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 	public static function getBibliorecord($id) {
 		return Class_ModeleFusion::find($id);
 	}
+
 	public static function getLoader() {
 		return self::getLoaderFor(__CLASS__);
 	}
@@ -76,6 +79,7 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 
 
 	public function getContenuFusionne() {
+    xdebug_break();
 		$contenu = $this->getContenu();
 		$contenu_decode = preg_replace_callback('/\{[^\}]+\}/',
 																						create_function('$matches',
@@ -161,16 +165,20 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 			}
 
 			else $attributes =[ $match];
-
+    xdebug_break();
     $model = $this->getDataSourceNamed(array_shift($attributes));
 
 		$value = $this->getValue($model, $attributes);
 
-		if (is_array($value) && ($this->getNom() === 'recherche' || $this->getNom() === 'article'))
-			return $this->buildSection($value,array_shift($match));
-
+    $result=null;
+    $next_match=array_shift($match);
 		if (is_array($value))
-			return $this->buildTable($value, array_shift($match));
+			$result= $this->buildTable($value, $next_match);
+    if ($result)
+      return $result;
+    if (is_array($value) && ($this->getNom() === 'recherche' || $this->getNom() === 'article'))
+			return $this->buildSection($value,$next_match);
+
 
     return $this->htmlize($value);
   }
@@ -255,7 +263,8 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 		$matches = array();
 
     preg_match_all('/(?:\"([^\"]+)\"\:)([\w|\.]+)?(?:\s*,\s*)?/', $columns_def_string, $matches, PREG_SET_ORDER);
-
+    if (!$matches)
+      return null;
     $columns = array();
     foreach ($matches as $match)
       $columns[$match[1]] = count($match)>2 ? $match[2] : '';
diff --git a/library/ZendAfi/Controller/Action.php b/library/ZendAfi/Controller/Action.php
index badde382341..844ea672e56 100644
--- a/library/ZendAfi/Controller/Action.php
+++ b/library/ZendAfi/Controller/Action.php
@@ -418,18 +418,27 @@ class ZendAfi_Controller_Action extends Zend_Controller_Action {
 
 	public function printAction() {
 		$models=[];
-		$strategy=$this->_getParam('strategy','Article');
-		$model='Class_'.$strategy;
-		$id_name='id_'.$strategy;
-		foreach(explode(';',$this->_getParam('ids',0)) as $id)
-			$models[]=$model::find($id);
+    xdebug_break();
+		$strategy=$this->_getParam('strategy','Article_List');
+    list($class_name,$type)=explode('_',$strategy);
+		$model='Class_'.$class_name;
+		$id_name='id_'.$class_name;
 
 
 		$this->view->lettre = Class_ModeleFusion::getBibliorecord($this->_getParam('modele_fusion'));
+    if ($type=="List") {
+      foreach(explode(';',$this->_getParam('ids',0)) as $id)
+        $models[]=$model::find($id);
 
-		$this->view->lettre
-			->setDataSource([strtolower(Storm_Inflector::pluralize($strategy)) => new Class_CollectionFusion($models)]);
+      $this->view->lettre
+        ->setDataSource([strtolower(Storm_Inflector::pluralize($class_name)) => new Class_CollectionFusion($models)]);
+    }
+    else {
+      $object_fusion = $model::find($this->_getParam('id',0));
 
+      $this->view->lettre
+        ->setDataSource([strtolower($class_name) => $object_fusion]);
+    }
 		$this->_helper->getHelper('viewRenderer')->setLayoutScript('empty.phtml');
 		$this->renderScript('print.phtml');
 
diff --git a/tests/application/modules/opac/controllers/RechercheControllerPrintActionTest.php b/tests/application/modules/opac/controllers/RechercheControllerPrintActionTest.php
index 86a30f0201e..55d32e5e08b 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerPrintActionTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerPrintActionTest.php
@@ -29,7 +29,7 @@ class RechercheControllerPrintActionLinkTest extends AbstractControllerTestCase
 																				'nom' => 'recherche',
 																				'contenu' => '<p> {notices.each[<img src="{url_vignette}"/>  {titre_principal} <div>{article.contenu}</div>
 ]}</p>',
-																				'type' => 'Notice']);
+																				'type' => 'Notice_List']);
 
 		$this->fixture('Class_Catalogue',
 									 ['id'=>3,
@@ -51,7 +51,7 @@ class RechercheControllerPrintActionLinkTest extends AbstractControllerTestCase
 
   /** @test */
 	public function printLinkShouldBePresent() {
-		$this->assertXPathContentContains('//a[contains(@href, "/recherche/print/id_catalogue/3/id_module/9/aleatoire/1/filtres/APaul%3B/ids/1/strategy/Notice/modele_fusion/1")]', 'Imprimer',$this->_response->getBody());
+		$this->assertXPathContentContains('//a[contains(@href, "/recherche/print/id_catalogue/3/id_module/9/aleatoire/1/filtres/APaul%3B/ids/1/strategy/Notice_List/modele_fusion/1")]', 'Imprimer',$this->_response->getBody());
 	}
 }
 
@@ -81,7 +81,7 @@ class RechercheControllerPrintActionWithRecordsTest extends AbstractControllerTe
 																																					'notice' => new Class_Notice(),
 																																					'type_doc_id' => Class_TypeDoc::ARTICLE]));
 
-  $this->dispatch("/recherche/print/expressionRecherche/pomme/strategy/Notice/ids/2;1/modele_fusion/1",true);
+  $this->dispatch("/recherche/print/expressionRecherche/pomme/strategy/Notice_List/ids/2;1/modele_fusion/1",true);
 	}
 
 
@@ -119,3 +119,71 @@ class RechercheControllerPrintActionWithRecordsTest extends AbstractControllerTe
 
 
 }
+
+
+
+
+class RechercheControllerViewNoticePrintActionWithRecordsTest extends AbstractControllerTestCase {
+	protected $_storm_default_to_volatile = true;
+
+	public function setUp() {
+  parent::setUp();
+
+	$this->fixture('Class_ModeleFusion', ['id' => 1,
+																				'nom' => 'article',
+																				'contenu' => '<p><h1> {notice.titre_principal}</h1> <div>{notice.article.contenu} </div>
+{notice.avis[<p>{avis}</p>]}
+</p>']);
+
+
+	Class_Indexation_PseudoNotice::index(	$this->fixture('Class_Article' , ['id' => 12,
+																																					'titre' => 'transmetropolitan',
+																																					'contenu' => '<p>bd</p>',
+																																					'notice' => new Class_Notice(),
+																																					'type_doc_id' => Class_TypeDoc::ARTICLE]));
+  $avis = [
+           $this->fixture('Class_AvisNotice', [
+                                               'id' => 1,
+                                               'id_user' => 1,
+                                               'avis' => 'Lies are news and truth is obsolete.',
+                                               'entete' => 'quotes',
+                                               'note' => 4,
+                                               'clef_oeuvre' => 'TRANSMETROPOLITAN---',
+                                               'notice' => 1,
+                                               'date_avis' => '2012-01-01',
+                                               ]),
+           $this->fixture('Class_AvisNotice', [
+                                               'id' => 2,
+                                               'id_user' => 1,
+                                               'avis' => "The point is, the only real tools we have are our eyes and our heads. It's not the act of seeing with our eyes alone; it's correctly comprehending what we see. Treating life as an autopsy",
+        'entete' => 'quotes',
+                                               'note' => 4,
+                                               'notice' => 1,
+                                               'clef_oeuvre' => 'TRANSMETROPOLITAN---',
+                                               'date_avis' => '2012-01-01',
+                                               ]),
+  ];
+
+
+
+  $this->dispatch("/recherche/print/expressionRecherche/pomme/strategy/Notice_View/id/1/modele_fusion/1",true);
+	}
+
+
+
+	/** @test */
+	public function displayShouldprintContent() {
+		$this->assertXPathContentContains("//div//p", "bd", $this->_response->getBody());
+	}
+
+
+  /** @test */
+	public function contenuShouldContainsTransmetropolitan() {
+		$this->assertXPathContentContains("//div//h1[1]", "transmetropolitan", $this->_response->getBody());
+	}
+
+/** @test */
+  public function avisShouldBeDisplayed() {
+    $this->assertXPathContentContains('//div//p', "Lies are news and truth is obsolete.",$this->_response->getBody());
+  }
+}
-- 
GitLab