From d471c069a33e234654a98ac9abebef8f43015616 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Fri, 12 Dec 2014 18:04:05 +0100
Subject: [PATCH] sandbox WIP

---
 .../opac/controllers/RechercheController.php  | 36 ++++++++
 .../scripts/recherche/resultatRecherche.phtml |  1 +
 library/Class/ModeleFusion.php                | 82 +++++++++++++++----
 library/Class/MoteurRecherche/Resultat.php    | 14 ++--
 .../controllers/RechercheControllerTest.php   | 17 +++-
 5 files changed, 130 insertions(+), 20 deletions(-)

diff --git a/application/modules/opac/controllers/RechercheController.php b/application/modules/opac/controllers/RechercheController.php
index 2d25197022f..d50a213091d 100644
--- a/application/modules/opac/controllers/RechercheController.php
+++ b/application/modules/opac/controllers/RechercheController.php
@@ -743,6 +743,7 @@ class RechercheController extends ZendAfi_Controller_Action {
 	}
 
 
+
 	protected function newCriteresRecherches($params) {
 		$criteres = (new Class_CriteresRecherche())->setParams($params);
 
@@ -751,5 +752,40 @@ class RechercheController extends ZendAfi_Controller_Action {
 
 		return $criteres;
 	}
+
+
+	public function printAction() {
+		$params = $this->_request->getParams();
+
+		$criteres_recherche = new Class_CriteresRecherche();
+		$criteres_recherche->setParams($params);
+		$ret = $this->moteur->lancerRecherche($criteres_recherche);
+		$this->view->notices = $this->_getListNotices($ret['req_liste']);
+		$this->view->lettre = Class_ModeleFusion::getBibliorecord();
+		$this->view->lettre
+			->setDataSource(['moteurRecherche_resultat' => new Class_MoteurRecherche_Resultat($this->view->notices)]);
+
+
+		$this->_renderLettreFusion($this->view->lettre->getContenuFusionne());
+
+	}
+
+
+	public function _renderLettreFusion($fusion_strategy) {
+		$this->_helper->getHelper('viewRenderer')->setLayoutScript('empty.phtml');
+
+		$this->renderScript('recherche/print-fusion.phtml');
+
+	}
+}
+
+
+class AbstractSessionFusionStrategy {
+	protected $_modele_fusion;
+
+	public function __construct($nom) {
+		$this->_modele_fusion = Class_ModeleFusion::get($nom);
+	}
 }
+
 ?>
\ 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 6bcc8e1cf44..59b4b26be59 100644
--- a/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml
+++ b/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml
@@ -16,6 +16,7 @@ if ($this->is_pertinence) {
 	$this->titre .= $this->_(" (recherche élargie triée par pertinence)");
 }
 ?>
+<div class="print"><a href="<?php echo $this->url(['action' => 'print']); ?>">imprimer</a></div>
 <div class="resultats_page">
 	<?php
 	echo $this->tagTriRecherche($this->criteres_recherche);
diff --git a/library/Class/ModeleFusion.php b/library/Class/ModeleFusion.php
index b5d57194ecc..c52cf4fb03c 100644
--- a/library/Class/ModeleFusion.php
+++ b/library/Class/ModeleFusion.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_ModeleFusion extends Storm_Model_Abstract {
@@ -28,6 +28,15 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 		return self::getLoader()->findFirstBy(array('nom' => $name));
 	}
 
+
+	public static function getBibliorecord() {
+		$modele = new self;
+		$modele->setContenu('<p> {moteurRecherche_resultat.notices[<img src="{url_vignette}"/>  titrePrincipal>]}</p>')->setNom('bibliorecord');
+
+//		$modele->setContenu('<p> {moteurRecherche_resultat.notices["titre":titrePrincipal>]}</p>')->setNom('bibliorecord');
+//		$modele->save();
+		return $modele;
+	}
 	public static function getLoader() {
 		return self::getLoaderFor(__CLASS__);
 	}
@@ -39,6 +48,7 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 
 
 	public function getContenuFusionne() {
+		xdebug_break();
 		$contenu = $this->getContenu();
 		$contenu_decode = preg_replace_callback('/\{[^\}]+\}/',
 																						create_function('$matches',
@@ -52,9 +62,9 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 									   '\[([^\]]+)\]'.     // [nom, prenom]
 									 ')?'.
 									 '\}'. //delimiteur
-									 '/', 
-									 $contenu_decode, 
-									 $matches, 
+									 '/',
+									 $contenu_decode,
+									 $matches,
 									 PREG_SET_ORDER);
 
 		foreach ($matches as $match) {
@@ -64,7 +74,7 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 				$tag_value = $this->getTagValueForString($match);
 			} catch (Storm_Model_Exception $e) {
 				continue;
-			} 
+			}
 
 			$contenu_decode = str_replace($tag,
 																		$tag_value,
@@ -79,9 +89,10 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 		$attributes = explode('.',array_shift($match));
 
 		$model = $this->getDataSourceNamed(array_shift($attributes));
-
-		if (is_array($value = $this->getValue($model, $attributes))) 
-			return $this->buildTable($value, array_shift($match));
+		xdebug_break();
+		if (is_array($value = $this->getValue($model, $attributes)))
+			return $this->buildSection($value,array_shift($match));
+//			return $this->buildTable($value, array_shift($match));
 
 		return $this->htmlize($value);
 	}
@@ -94,20 +105,63 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 		return $in_utf8;
 	}
 
-	
+
 	public function getValue($modele, &$attributes) {
 		if (!$next_attribute = array_shift($attributes))
 			return '';
-
+		xdebug_break();
 		if (!$value_or_model = $modele->callGetterByAttributeName($next_attribute))
 			return '';
 
-		if (count($attributes)==0) 
+		if (count($attributes)==0)
 			return $value_or_model;
-		
+
 		return $this->getValue($value_or_model, $attributes);
 	}
 
+	public function buildSection($items,$html) {
+
+		$contenu_decode=preg_replace_callback('/\{[^\}]+\}/',
+													create_function('$matches',
+																					'return html_entity_decode($matches[0],ENT_QUOTES);'),
+													$html);
+		$matches = [];
+		preg_match_all('/'.  //ex: @session_formation.stagiaires[nom, prenom]@
+									 '\{'. //delimiteur
+									 '([\w|\.]+)'. //session_formation.stagiaires
+									 '(?:'.
+									   '\[([^\]]+)\]'.     // [nom, prenom]
+									 ')?'.
+									 '\}'. //delimiteur
+									 '/',
+									 $contenu_decode,
+									 $matches,
+									 PREG_SET_ORDER);
+
+
+		foreach ($matches as $match) {
+			$tag = array_shift($match);
+
+			try {
+				$tag= str_replace('{','',$tag);
+				$tag= str_replace('}','',$tag);
+
+				$tag_value = $this->getTagValueForString($tag);
+			} catch (Storm_Model_Exception $e) {
+				continue;
+			}
+
+			$contenu_decode = str_replace($tag,
+																		$tag_value,
+																		$contenu_decode);
+		}
+
+		return $contenu_decode;
+
+
+
+	}
+
 
 	public function buildTable($items, $columns_def_string) {
 		$matches = array();
@@ -123,7 +177,7 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 			$content .= '<td>'.$this->htmlize($label).'</td>';
 		$content .= '</tr>';
 
-			
+
 		foreach($items as $item)
 			$content .= $this->buildTableRow($item, $columns);
 
@@ -138,7 +192,7 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 			$requested_attributes = explode('.', $attribute);
 			$value = $this->getValue($model, $requested_attributes);
 
-			$row .= sprintf('<td>%s</td>', 
+			$row .= sprintf('<td>%s</td>',
 											$attribute ? $this->htmlize($value) : '');
 		}
 
diff --git a/library/Class/MoteurRecherche/Resultat.php b/library/Class/MoteurRecherche/Resultat.php
index 852d82c4e78..a4ae1460c6c 100644
--- a/library/Class/MoteurRecherche/Resultat.php
+++ b/library/Class/MoteurRecherche/Resultat.php
@@ -16,20 +16,24 @@
  *
  * 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_MoteurRecherche_Resultat {
-	protected $_notices;
+class Class_MoteurRecherche_Resultat extends Storm_Model_Abstract {
+	protected $notices;
 
 	public function __construct($notices) {
-		$this->_notices = $notices;
+		$this->notices = $notices;
 	}
 
 
 	public function acceptVisitor($visitor) {
-		foreach($this->_notices as $notice) 
+		foreach($this->notices as $notice)
 			$visitor->visitNotice($notice);
 	}
+
+	public function getNotices() {
+		return $this->notices;
+	}
 }
 
 ?>
\ 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 7f4d1b11825..e43dac6d2ad 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerTest.php
@@ -2591,6 +2591,7 @@ class RechercheControllerViewnoticeWithBreadcrumbAndIdPanierParamTest extends Re
 	}
 }
 
+<<<<<<< HEAD
 
 
 
@@ -2734,4 +2735,18 @@ class RechercheControlleSimpleActionWithEmptyDomainSettingsAndArticlesLinkedTest
 	}
 }
 
-?>
+
+
+class RechercheControllerPrintActionTest extends AbstractControllerTestCase {
+	public function setUp() {
+  parent::setUp();
+  $this->dispatch("/recherche/print/expressionRecherche/pomme",true);
+	}
+
+	/** @test */
+	public function displayShouldprintTitle() {
+		$this->assertXPathContentContains("//p", "pomme",$this->_response->getBody());
+	}
+}
+
+?>
\ No newline at end of file
-- 
GitLab