From 18bacf395216febcd955f7be6d866644de35d00d Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Mon, 9 Jan 2017 17:29:31 +0100
Subject: [PATCH] migrate print action to plugin

---
 .../opac/controllers/CmsController.php        |  5 ++
 .../opac/controllers/RechercheController.php  |  5 ++
 library/ZendAfi/Controller/Action.php         | 27 ----------
 .../Controller/Plugin/Printer/ModelFusion.php | 50 +++++++++++++++++++
 .../CmsControllerPrintActionTest.php          |  8 ++-
 5 files changed, 67 insertions(+), 28 deletions(-)
 create mode 100644 library/ZendAfi/Controller/Plugin/Printer/ModelFusion.php

diff --git a/application/modules/opac/controllers/CmsController.php b/application/modules/opac/controllers/CmsController.php
index b0f3d434c69..e1cca78a7d1 100644
--- a/application/modules/opac/controllers/CmsController.php
+++ b/application/modules/opac/controllers/CmsController.php
@@ -23,6 +23,11 @@ class CmsController extends ZendAfi_Controller_Action {
 	use Trait_TimeSource;
 
 
+  public function getPlugins() {
+    return ['ZendAfi_Controller_Plugin_Printer_ModelFusion'];
+  }
+
+
   public function init() {
     parent::init();
 
diff --git a/application/modules/opac/controllers/RechercheController.php b/application/modules/opac/controllers/RechercheController.php
index 3498df89531..e309320f438 100644
--- a/application/modules/opac/controllers/RechercheController.php
+++ b/application/modules/opac/controllers/RechercheController.php
@@ -25,6 +25,11 @@ class RechercheController extends ZendAfi_Controller_Action {
     $preferences;
 
 
+  public function getPlugins() {
+    return ['ZendAfi_Controller_Plugin_Printer_ModelFusion'];
+  }
+
+
   public function init() {
     $this->moteur = Class_MoteurRecherche::getInstance();
     $this->view->resultat = [];
diff --git a/library/ZendAfi/Controller/Action.php b/library/ZendAfi/Controller/Action.php
index b75920f4f49..9915f9e2e86 100644
--- a/library/ZendAfi/Controller/Action.php
+++ b/library/ZendAfi/Controller/Action.php
@@ -241,33 +241,6 @@ class ZendAfi_Controller_Action extends Zend_Controller_Action {
   }
 
 
-  public function printAction() {
-    if ($this->_response->isRedirect())
-      return;
-
-    $models = [];
-    $strategy = $this->_getParam('strategy','Article_List');
-    list($class_name,$type) = explode('_',$strategy);
-    $model = 'Class_'.$class_name;
-    $id_name = 'id_'.$class_name;
-
-    $source_key = strtolower($class_name);
-    $data = $model::find($this->_getParam('id',0));
-
-    $this->view->fusion = Class_ModeleFusion::find($this->_getParam('modele_fusion'));
-
-    if ($type == 'List') {
-      $models = array_map([$model, 'find'], explode(';', $this->_getParam('ids', 0)));
-      $data = new Class_CollectionFusion($models);
-      $source_key = Storm_Inflector::pluralize($source_key);
-    }
-
-    $this->view->fusion->setDataSource([$source_key => $data]);
-    $this->_helper->getHelper('viewRenderer')->setLayoutScript('empty.phtml');
-    $this->renderScript('print.phtml');
-  }
-
-
   public function getOsmService() {
     return (new Class_WebService_OpenStreetMap())
       ->onCoordinatesNotFound(
diff --git a/library/ZendAfi/Controller/Plugin/Printer/ModelFusion.php b/library/ZendAfi/Controller/Plugin/Printer/ModelFusion.php
new file mode 100644
index 00000000000..2e47e26efa8
--- /dev/null
+++ b/library/ZendAfi/Controller/Plugin/Printer/ModelFusion.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, 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_Controller_Plugin_Printer_ModelFusion extends ZendAfi_Controller_Plugin_Abstract {
+  public function printAction() {
+    if ($this->_response->isRedirect())
+      return;
+
+    $models = [];
+    $strategy = $this->_getParam('strategy', 'Article_List');
+    list($class_name,$type) = explode('_',$strategy);
+    $model = 'Class_'.$class_name;
+    $id_name = 'id_'.$class_name;
+
+    $source_key = strtolower($class_name);
+    $data = $model::find($this->_getParam('id',0));
+
+    $this->_view->fusion = Class_ModeleFusion::find($this->_getParam('modele_fusion'));
+
+    if ($type == 'List') {
+      $models = array_map([$model, 'find'], explode(';', $this->_getParam('ids', 0)));
+      $data = new Class_CollectionFusion($models);
+      $source_key = Storm_Inflector::pluralize($source_key);
+    }
+
+    $this->_view->fusion->setDataSource([$source_key => $data]);
+    $this->_helper->getHelper('viewRenderer')->setLayoutScript('empty.phtml');
+    $this->renderScript('print.phtml');
+  }
+}
+?>
\ No newline at end of file
diff --git a/tests/application/modules/opac/controllers/CmsControllerPrintActionTest.php b/tests/application/modules/opac/controllers/CmsControllerPrintActionTest.php
index 5c004abfd22..d71608cc3fc 100644
--- a/tests/application/modules/opac/controllers/CmsControllerPrintActionTest.php
+++ b/tests/application/modules/opac/controllers/CmsControllerPrintActionTest.php
@@ -55,7 +55,7 @@ class CmsControllerPrintActionArticleviewByDate extends AbstractControllerTestCa
 
   $this->fixture('Class_ModeleFusion', ['id' => 1,
                                         'nom' => 'article',
-                                        'contenu' => '<p><div>{notices.each[{contenu}]}</div></p>',
+                                        'contenu' => '<p><div>{articles.each[{contenu}]}</div></p>',
                                         'type' => 'Article_List']);
 
   }
@@ -74,5 +74,11 @@ class CmsControllerPrintActionArticleviewByDate extends AbstractControllerTestCa
     $this->assertXPathContentContains('//a[contains(@href, "/cms/print/ids/2241%3B245/strategy/Article_List/modele_fusion/1")]', 'Imprimer', $this->_response->getBody());
   }
 
+
+  /** @test */
+  public function dispatchPrintCmsShouldContainsArticle() {
+    $this->dispatch('/cms/print/ids/2241%3B245/strategy/Article_List/modele_fusion/1', true);
+    $this->assertXPathContentContains('//div[@class="print_fusion"]//div', 'an appetizing feast');
+  }
 }
 ?>
\ No newline at end of file
-- 
GitLab