From 82a1d89f8da072c1424630de4bfc8c44e5e1c411 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Fri, 9 Jan 2015 17:37:25 +0100
Subject: [PATCH] sandbox fusion search result : add/edit modele fusion

---
 .../admin/controllers/PrintController.php     | 55 +++++++++++++++++++
 .../admin/views/scripts/print/add.phtml       |  1 +
 .../admin/views/scripts/print/edit.phtml      |  1 +
 .../opac/controllers/RechercheController.php  |  2 +-
 .../scripts/recherche/print-fusion.phtml      |  3 +
 .../opac/views/scripts/recherche/print.phtml  | 36 ++++++++++++
 .../scripts/recherche/resultatRecherche.phtml |  3 +-
 library/Class/ModeleFusion.php                | 52 +++++++++++-------
 library/Class/Notice.php                      |  1 +
 library/ZendAfi/Form/ModeleFusion.php         | 50 +++++++++++++++++
 .../opac/controllers/PrintControllerTest.php  | 34 ++++++++++++
 .../controllers/RechercheControllerTest.php   | 20 ++++++-
 12 files changed, 234 insertions(+), 24 deletions(-)
 create mode 100644 application/modules/admin/controllers/PrintController.php
 create mode 100644 application/modules/admin/views/scripts/print/add.phtml
 create mode 100644 application/modules/admin/views/scripts/print/edit.phtml
 create mode 100644 application/modules/opac/views/scripts/recherche/print-fusion.phtml
 create mode 100644 application/modules/opac/views/scripts/recherche/print.phtml
 create mode 100644 library/ZendAfi/Form/ModeleFusion.php
 create mode 100644 tests/application/modules/opac/controllers/PrintControllerTest.php

diff --git a/application/modules/admin/controllers/PrintController.php b/application/modules/admin/controllers/PrintController.php
new file mode 100644
index 00000000000..32804a1fef8
--- /dev/null
+++ b/application/modules/admin/controllers/PrintController.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * 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
+ */
+
+
+class Admin_PrintController extends ZendAfi_Controller_Action {
+	use Trait_Translator;
+	public function getRessourceDefinitions() {
+		return [
+						'model' => [
+												'class' => 'Class_ModeleFusion',
+												'name' => 'modele_fusion',
+												'order' => 'id'],
+
+						'messages' => [
+													 'successful_save' => $this->_('Modèle "%s" sauvegardé'),
+													 'successful_add' => $this->_('Le modèle "%s" a été sauvegardé'),
+													 'successful_delete' => $this->_('Modèle "%s" supprimé')],
+
+						'actions' => [
+													'add' => ['title' => $this->_("Ajouter un modèle")],
+													'edit' => ['title' => $this->_("Modifier le modèle: %s")]],
+
+						'form_class_name' => 'ZendAfi_Form_ModeleFusion',
+
+						'after_delete' => function() { $this->_redirect('/admin/print/index');},
+
+						'after_add' => function($modele) { $this->afterAdd($modele); }
+		];
+	}
+
+	public function indexAction() {
+		$this->_redirectToIndex();
+	}
+
+
+}
+?>
\ No newline at end of file
diff --git a/application/modules/admin/views/scripts/print/add.phtml b/application/modules/admin/views/scripts/print/add.phtml
new file mode 100644
index 00000000000..c52ca489f90
--- /dev/null
+++ b/application/modules/admin/views/scripts/print/add.phtml
@@ -0,0 +1 @@
+<?php echo $this->renderForm($this->form); ?>
diff --git a/application/modules/admin/views/scripts/print/edit.phtml b/application/modules/admin/views/scripts/print/edit.phtml
new file mode 100644
index 00000000000..c52ca489f90
--- /dev/null
+++ b/application/modules/admin/views/scripts/print/edit.phtml
@@ -0,0 +1 @@
+<?php echo $this->renderForm($this->form); ?>
diff --git a/application/modules/opac/controllers/RechercheController.php b/application/modules/opac/controllers/RechercheController.php
index d50a213091d..6f7c7470684 100644
--- a/application/modules/opac/controllers/RechercheController.php
+++ b/application/modules/opac/controllers/RechercheController.php
@@ -761,7 +761,7 @@ class RechercheController extends ZendAfi_Controller_Action {
 		$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 = Class_ModeleFusion::getBibliorecord($this->_getParam('modele_fusion'));
 		$this->view->lettre
 			->setDataSource(['moteurRecherche_resultat' => new Class_MoteurRecherche_Resultat($this->view->notices)]);
 
diff --git a/application/modules/opac/views/scripts/recherche/print-fusion.phtml b/application/modules/opac/views/scripts/recherche/print-fusion.phtml
new file mode 100644
index 00000000000..21af2be2829
--- /dev/null
+++ b/application/modules/opac/views/scripts/recherche/print-fusion.phtml
@@ -0,0 +1,3 @@
+<div class="lettre_fusion">
+	 <?php echo $this->lettre->getContenuFusionne(); ?>
+</div>
diff --git a/application/modules/opac/views/scripts/recherche/print.phtml b/application/modules/opac/views/scripts/recherche/print.phtml
new file mode 100644
index 00000000000..7b53270012e
--- /dev/null
+++ b/application/modules/opac/views/scripts/recherche/print.phtml
@@ -0,0 +1,36 @@
+
+
+<style>
+.cke_contents {min-height: 500px;}
+</style>
+
+<div class="impressions tree">
+	<?php foreach($this->impressions as $libelle => $impressions) { ?>
+	<div><?php echo $libelle ?>
+		<span class="actions">
+		<?php
+			 echo $this->tagAnchor('#', $this->boutonIco("type=edit"));
+			 echo $this->tagAnchor(array('action' => $impressions['action']),
+															 $this->boutonIco("picto=picto/printer.png", "bulle=Imprimer"));
+		?>
+		</span>
+
+		<?php
+			echo sprintf('<div class="fusion_form" style="display:none">%s</div>',
+									 $this->renderForm($impressions['form']));
+		?>
+	</div>
+  <?php } ?>
+</div>
+
+
+<?php
+Class_ScriptLoader::getInstance()->addJQueryReady(
+'$("div.impressions .actions a:first-child").click(function(event){
+    event.preventDefault();
+    $(this).closest("div").find(".fusion_form").slideToggle();
+  });
+
+$("div.impressions .actions a:first-child + a").attr("target", "_blank")'
+);
+?>
diff --git a/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml b/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml
index 59b4b26be59..3d32cf2e566 100644
--- a/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml
+++ b/application/modules/opac/views/scripts/recherche/resultatRecherche.phtml
@@ -16,7 +16,8 @@ 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="print"><a href="<?php echo $this->url(['action' => 'print',
+																									 'modele_fusion' => Class_ModeleFusion::findAll()[0]->getId()]); ?>">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 c52cf4fb03c..c0cbaaed54a 100644
--- a/library/Class/ModeleFusion.php
+++ b/library/Class/ModeleFusion.php
@@ -22,20 +22,19 @@
 class Class_ModeleFusion extends Storm_Model_Abstract {
 	protected $_table_name = 'modele_fusion';
 	protected $_table_primary = 'id';
-
+	protected $_default_attribute_values = ['nom' => '',
+																					'contenu' => ''];
 
 	public static function get($name) {
 		return self::getLoader()->findFirstBy(array('nom' => $name));
 	}
 
+	public function getLibelle() {
+		return $this->getNom();
+	}
 
-	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 getBibliorecord($id) {
+		return Class_ModeleFusion::find($id);
 	}
 	public static function getLoader() {
 		return self::getLoaderFor(__CLASS__);
@@ -85,6 +84,12 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 	}
 
 
+	public function getTagValueForInstance($match,$instance) {
+		return $instance->callGetterByAttributeName($match);
+
+	}
+
+
 	public function getTagValueForString($match) {
 		$attributes = explode('.',array_shift($match));
 
@@ -138,25 +143,30 @@ class Class_ModeleFusion extends Storm_Model_Abstract {
 									 $matches,
 									 PREG_SET_ORDER);
 
+		$result='';
+			foreach($items as $item) {
+				$decode=$contenu_decode;
+				foreach ($matches as $match) {
 
-		foreach ($matches as $match) {
-			$tag = array_shift($match);
 
-			try {
-				$tag= str_replace('{','',$tag);
-				$tag= str_replace('}','',$tag);
+				$tag = array_shift($match);
+				try {
+					$tag= str_replace('{','',$tag);
+					$tag= str_replace('}','',$tag);
 
-				$tag_value = $this->getTagValueForString($tag);
-			} catch (Storm_Model_Exception $e) {
-				continue;
-			}
+					$tag_value = $this->getTagValueForInstance($tag,$item);
+				} catch (Storm_Model_Exception $e) {
+					continue;
+				}
 
-			$contenu_decode = str_replace($tag,
-																		$tag_value,
-																		$contenu_decode);
+				$decode = str_replace('{'.$tag.'}',
+															 $tag_value,
+															 $decode);
+				}
+				$result.=$decode;
 		}
 
-		return $contenu_decode;
+		return $result;
 
 
 
diff --git a/library/Class/Notice.php b/library/Class/Notice.php
index 5a630ad51d1..b4d4b1e58b1 100644
--- a/library/Class/Notice.php
+++ b/library/Class/Notice.php
@@ -813,6 +813,7 @@ class Class_Notice extends Storm_Model_Abstract {
 	public function getTitrePrincipal($type_doc = false, $tome = false) {
 		// 200$a
 		$titre = '';
+		xdebug_break();
 		if ($titres = $this->get_subfield('200', 'a'))
 			$titre = trim($titres[0]);
 
diff --git a/library/ZendAfi/Form/ModeleFusion.php b/library/ZendAfi/Form/ModeleFusion.php
new file mode 100644
index 00000000000..d280db7bfcb
--- /dev/null
+++ b/library/ZendAfi/Form/ModeleFusion.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * 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
+ */
+
+class ZendAfi_Form_ModeleFusion extends ZendAfi_Form {
+
+
+	public static function newWith($datas=[], $custom_form=null) {
+		$form_name='MODELE_FUSION';
+//		$modele_fusion = Class_ModeleFusion::get($form_name);
+//		if (!$modele_fusion)
+		if (!$datas)
+			$datas= (new Class_ModeleFusion())->toArray();
+
+		$form = new self();
+		$form->setMethod('post')
+				 ->addElement('text',
+											'nom',
+											['label' => 'Nom'])
+				 ->addElement('ckeditor',
+											'contenu',
+
+											array(
+														'label' => $datas['nom'],
+														'value' => $datas['contenu'],
+														'required' => true,
+														'allowEmpty' => false));
+
+		return $form;
+	}
+
+}
+?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/PrintControllerTest.php b/tests/application/modules/opac/controllers/PrintControllerTest.php
new file mode 100644
index 00000000000..1fc757fc89f
--- /dev/null
+++ b/tests/application/modules/opac/controllers/PrintControllerTest.php
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * 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
+ */
+require_once 'AbstractControllerTestCase.php';
+
+class PrintControllerTest extends AbstractControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+
+		$this->dispatch('/admin/print/add',true);
+	}
+
+ /** @test */
+	public function contextShouldExpectation() {
+		$this->assertXPathContentContains('//p', 'toto',$this->_response->getBody());
+	}
+}
\ 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 e43dac6d2ad..dded174a815 100644
--- a/tests/application/modules/opac/controllers/RechercheControllerTest.php
+++ b/tests/application/modules/opac/controllers/RechercheControllerTest.php
@@ -53,6 +53,7 @@ abstract class RechercheControllerNoticeTestCase extends AbstractControllerTestC
 																										 'type_doc' => Class_TypeDoc::LIVRE,
 																										 'url_vignette' => 'NO',
 																										 'url_image' => 'NO',
+																										 'titres' => 'POMME',
 																										 'facettes' => 'A123 F2 G1 P4 Y2 B1 Lfre M1 Z3 D1',
 																										 'date_creation' => '2013-12-31',
 																										 'unimarc' => "01570nam0 2200325   450 0010007000000100033000070200017000400210027000571000041000841010008001251020007001331050018001401060006001582000106001642100075002702150044003452250023003893000125004123000020005373000137005573300265006943450018009594100051009775120027010286060033010556060060010886760012011487000045011608010039012052218529  a2-86642-370-4bbr.d8,95 EUR  aFRb00347575  aFRbDLE-20031204-51138  a20031107d2003    m  h0frey0103    ba| afre  aFR  ay   z   000y|  ar1 aCinéma d'animationbTexte impriméedessin animé, marionnettes, images de synthèsefBernard Génin  a[Paris]c\"Cahiers du cinéma\"cSCEREN-CNDPdcop. 2003gimpr. en Italie  a95 p.cill., couv. ill. en coul.d19 cm2 aLes petits cahiers  aLa couv. porte en plus : \"du crayon à l'ordinateur, pour ou contre Disney, Europe-Japon : le dessin animé aujourd'hui\"  aBibliogr. p. 93  aSCEREN = Services, cultures, éditions, ressources pour l'éducation nationale. CNDP = Centre national de documentation pédagogique  aPrésente un historique du cinéma d'animation, un survol des différentes productions nationales à travers le monde (Etats-Unis, Japon, France, Canada), les techniques du volume animé, l'image de synthèse, mais aussi l'oeuvre de Disney et le film d'auteur.  b9782866423704 032525826tLes Petits cahiers (Paris)x1633-90531 aLe cinéma d'animation| 31053394aAnimation (cinéma)| 31031625aDessins animés32195497xHistoire et critique  a791.431 |32547161aGéninbBernardf1946-....4070 0aFRbBNFc20031107gAFNOR2intermrc"])
@@ -2740,13 +2741,30 @@ class RechercheControlleSimpleActionWithEmptyDomainSettingsAndArticlesLinkedTest
 class RechercheControllerPrintActionTest extends AbstractControllerTestCase {
 	public function setUp() {
   parent::setUp();
-  $this->dispatch("/recherche/print/expressionRecherche/pomme",true);
+	$this->fixture('Class_ModeleFusion', ['id' => 1,
+																				'nom' => 'pomme',
+																				'contenu' => '<p> {moteurRecherche_resultat.notices[<img src="{url_vignette}"/>  {titre_principal} {article_cms.contenu}
+]}</p>']);
+	Class_Indexation_PseudoNotice::index(	$this->fixture('Class_Article' , ['id' => 10,
+																																					'titre' => 'pomme',
+																																					'contenu' => 'blabla',
+																																					'notice' => new Class_Notice(),
+																																					'type_doc_id' => Class_TypeDoc::ARTICLE]));
+
+
+  $this->dispatch("/recherche/print/expressionRecherche/pomme/modele_fusion/1",true);
 	}
 
 	/** @test */
 	public function displayShouldprintTitle() {
 		$this->assertXPathContentContains("//p", "pomme",$this->_response->getBody());
 	}
+
+/** @test */
+	public function contenuShouldContains() {
+		$this->assertXPathContentContains("//p", "blabla",$this->_response->getBody());
+
+	}
 }
 
 ?>
\ No newline at end of file
-- 
GitLab