From 9c3e75f0a1ce06f2d9b8cea80314cec899441352 Mon Sep 17 00:00:00 2001 From: Laurent Laffont <llaffont@afi-sa.fr> Date: Tue, 2 Oct 2018 16:31:14 +0200 Subject: [PATCH] dev #59497 : print link print selection if not empty --- .../opac/controllers/RechercheController.php | 2 +- .../opac/controllers/RecordsController.php | 19 +++---- library/Class/ModeleFusion.php | 17 +++++- .../Action/Helper/SearchRecords.php | 37 +++++++++++++ .../Plugin/Printer/SearchResult.php | 38 ++++++++++++++ .../ZendAfi/View/Helper/ModeleFusion/Link.php | 9 ++-- library/ZendAfi/View/Helper/Search/Header.php | 18 +++++-- .../RechercheControllerPrintActionTest.php | 52 ++++++++++--------- .../SearchSelection/SearchSelectionTest.php | 31 ++++++++++- 9 files changed, 176 insertions(+), 47 deletions(-) create mode 100644 library/ZendAfi/Controller/Action/Helper/SearchRecords.php create mode 100644 library/ZendAfi/Controller/Plugin/Printer/SearchResult.php diff --git a/application/modules/opac/controllers/RechercheController.php b/application/modules/opac/controllers/RechercheController.php index 87ed980f16e..86188d06a74 100644 --- a/application/modules/opac/controllers/RechercheController.php +++ b/application/modules/opac/controllers/RechercheController.php @@ -39,7 +39,7 @@ class RechercheController extends ZendAfi_Controller_Action { public function getPlugins() { - return ['ZendAfi_Controller_Plugin_Printer_ModelFusion', + return ['ZendAfi_Controller_Plugin_Printer_SearchResult', 'ZendAfi_Controller_Plugin_Mailer_SearchResult']; } diff --git a/application/modules/opac/controllers/RecordsController.php b/application/modules/opac/controllers/RecordsController.php index 785f9b15927..2d3530daeed 100644 --- a/application/modules/opac/controllers/RecordsController.php +++ b/application/modules/opac/controllers/RecordsController.php @@ -42,13 +42,19 @@ class RecordsController extends ZendAfi_Controller_Action { public function selectPageAction() { - $this->_selection->addAll($this->_resultFromParams()->fetchRecords()); + $this->_selection->addAll($this + ->_helper + ->searchRecords() + ->fetchRecords()); $this->_helper->json(['count' => $this->_selection->count()]); } public function selectAllAction() { - $this->_selection->addAllIds($this->_resultFromParams()->fetchAllRecordsIds()); + $this->_selection->addAllIds($this + ->_helper + ->searchRecords() + ->fetchAllRecordsIds()); $this->_helper->json(['count' => $this->_selection->count()]); } @@ -56,13 +62,4 @@ class RecordsController extends ZendAfi_Controller_Action { public function selectViewAction() { } - - - protected function _resultFromParams() { - $criteria = (new Class_CriteresRecherche()) - ->setParams($this->_request->getParams()); - - return Class_MoteurRecherche::getInstance() - ->lancerRecherche($criteria); - } } diff --git a/library/Class/ModeleFusion.php b/library/Class/ModeleFusion.php index fd129a3839f..df9105b597b 100644 --- a/library/Class/ModeleFusion.php +++ b/library/Class/ModeleFusion.php @@ -48,6 +48,11 @@ class Class_ModeleFusionLoader extends Storm_Model_Loader { } + public function canPrintRecords() { + return 0 < Class_ModeleFusion::countBy(['type' => Class_ModeleFusion::RECORDS_TEMPLATE]); + } + + public function getTrainingTemplateFor($training_page) { $names = ['EMARGEMENT' => Class_ModeleFusion::TRAINING_TEMPLATE.'EMARGEMENT', 'STAGIAIRES' => Class_ModeleFusion::TRAINING_TEMPLATE.'LISTE_STAGIAIRES', @@ -59,6 +64,15 @@ class Class_ModeleFusionLoader extends Storm_Model_Loader { } + public function getFusionForStrategyAndProfilOrDefault($strategy, $profil) { + if ($model_fusion = Class_ModeleFusion::getFusionForStrategyAndProfil($strategy, + $profil->getId())) + return $model_fusion; + + return Class_ModeleFusion::getFusionForStrategy($strategy); + } + + public function getFusionForStrategyAndProfil($strategy,$id_profil) { $models =array_filter($this->findAllBy(['type' => $strategy]), function($model) use ($id_profil) { @@ -128,7 +142,8 @@ class Class_ModeleFusion extends Storm_Model_Abstract { RECORD_TEMPLATE = 'Notice_View', LOANS_TEMPLATE = 'Loans_List'; - protected $_table_name = 'modele_fusion', + protected + $_table_name = 'modele_fusion', $_loader_class = 'Class_ModeleFusionLoader', $_table_primary = 'id', $_default_attribute_values = ['nom' => '', diff --git a/library/ZendAfi/Controller/Action/Helper/SearchRecords.php b/library/ZendAfi/Controller/Action/Helper/SearchRecords.php new file mode 100644 index 00000000000..c2351879280 --- /dev/null +++ b/library/ZendAfi/Controller/Action/Helper/SearchRecords.php @@ -0,0 +1,37 @@ +<?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_Action_Helper_SearchRecords extends Zend_Controller_Action_Helper_Abstract { + public function searchRecords() { + $criteria = (new Class_CriteresRecherche()) + ->setParams($this->getRequest()->getParams()); + + return Class_MoteurRecherche::getInstance() + ->lancerRecherche($criteria); + } + + + public function direct() { + return $this->searchRecords(); + } +} +?> \ No newline at end of file diff --git a/library/ZendAfi/Controller/Plugin/Printer/SearchResult.php b/library/ZendAfi/Controller/Plugin/Printer/SearchResult.php new file mode 100644 index 00000000000..04fca871912 --- /dev/null +++ b/library/ZendAfi/Controller/Plugin/Printer/SearchResult.php @@ -0,0 +1,38 @@ +<?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_SearchResult + extends ZendAfi_Controller_Plugin_Printer_ModelFusion { + + protected function _getPrinterConfig() { + $model_fusion = Class_ModeleFusion::getFusionForStrategyAndProfilOrDefault(Class_ModeleFusion::RECORDS_TEMPLATE, + Class_Profil::getCurrentProfil()); + + if (!$ids = (new Class_RecordSelection())->values()) + $ids = $this->_helper->searchRecords()->fetchAllRecordsIds(); + + return parent::_getPrinterConfig() + ->setStrategy($model_fusion->getType()) + ->setModelFusion($model_fusion->getId()) + ->setIds(implode(';', array_slice($ids, 0, 200))); + } +} \ No newline at end of file diff --git a/library/ZendAfi/View/Helper/ModeleFusion/Link.php b/library/ZendAfi/View/Helper/ModeleFusion/Link.php index c48e55fddd1..4323592464b 100644 --- a/library/ZendAfi/View/Helper/ModeleFusion/Link.php +++ b/library/ZendAfi/View/Helper/ModeleFusion/Link.php @@ -23,14 +23,11 @@ class ZendAfi_View_Helper_ModeleFusion_Link extends ZendAfi_View_Helper_BaseHelper { public function ModeleFusion_Link($instance) { - if(!$instance) + if (!$instance) return ''; - if(!$model_fusion = - (($model_fusion = Class_ModeleFusion::getFusionForStrategyAndProfil($instance->getStrategy(), - Class_Profil::getCurrentProfil()->getId())) - ? $model_fusion - : Class_ModeleFusion::getFusionForStrategy($instance->getStrategy()))) + if (!$model_fusion = Class_ModeleFusion::getFusionForStrategyAndProfilOrDefault($instance->getStrategy(), + Class_Profil::getCurrentProfil())) return ''; $models = $instance->getModels(); diff --git a/library/ZendAfi/View/Helper/Search/Header.php b/library/ZendAfi/View/Helper/Search/Header.php index 658ca40f87d..341d6beb2a7 100644 --- a/library/ZendAfi/View/Helper/Search/Header.php +++ b/library/ZendAfi/View/Helper/Search/Header.php @@ -114,9 +114,7 @@ class ZendAfi_View_Helper_Search_Header extends ZendAfi_View_Helper_BaseHelper { $this->_tagBookmarkSearch(), - $this->_tag('span', - $this->view->tagPrintLink($instance), - ['class' => 'print']), + $this->_tagPrintLink(), $this->_tag('span', $this->view->tagSendMail($instance), @@ -140,6 +138,20 @@ class ZendAfi_View_Helper_Search_Header extends ZendAfi_View_Helper_BaseHelper { } + protected function _tagPrintLink() { + if (!Class_ModeleFusion::canPrintRecords()) + return ''; + + return $this->_tag('span', + $this->view->tagAnchor(['controller' => 'recherche', + 'action' => 'print'], + $this->_('Imprimer'), + ['title' => $this->_('Imprimer le résultat'), + 'target' => '_blank']), + ['class' => 'print']); + } + + protected function _tagBookmarkSearch() { if(!Class_AdminVar::isBookmarkSearchesReady()) return ''; diff --git a/tests/application/modules/opac/controllers/RechercheControllerPrintActionTest.php b/tests/application/modules/opac/controllers/RechercheControllerPrintActionTest.php index a912af20b20..44182ecb668 100644 --- a/tests/application/modules/opac/controllers/RechercheControllerPrintActionTest.php +++ b/tests/application/modules/opac/controllers/RechercheControllerPrintActionTest.php @@ -30,31 +30,20 @@ class RechercheControllerPrintActionLinkTest extends AbstractControllerTestCase 'contenu' => '<p> {notices.each[<img src="{url_vignette}"/> {titre_principal} <div>{article.contenu}</div> ]}</p>', 'type' => 'Notice_List']); - - $this->fixture('Class_Catalogue', - ['id'=>3, - 'libelle' => 'Nouveautés', - 'auteur' => 'Paul']); - $mock_sql = $this->mock() ->whenCalled('fetchAll') - ->with("select id_notice, facettes from notices Where MATCH(facettes) AGAINST('+(APaul)' IN BOOLEAN MODE) order by annee desc", true, false) - ->answers([ [1, ''] ]) - - ->whenCalled('fetchAll') - ->answers([$this->fixture('Class_Notice', - ['id' => 1])->toArray()]) - ->beStrict(); + ->answers([ [1, ''] ]); Zend_Registry::set('sql', $mock_sql); - $this->dispatch('/recherche/simple/id_catalogue/3/id_module/9/aleatoire/1', true); + $this->dispatch('/recherche/simple/expressionRecherche/pomme/facettes/T3/tri/*', true); } /** @test */ - public function printLinkShouldBePresent() { - $this->assertXPathContentContains('//a[contains(@href, "/recherche/print/id_catalogue/3/id_module/9/aleatoire/1/ids/1/subject/pour+%3A+/strategy/Notice_List/modele_fusion/1")][@target="_blank"]', - 'Imprimer'); + public function pageShouldContainsPrintLinkWithSearchCriteria() { + $this->assertXPathContentContains('//a[contains(@href, "/recherche/print/expressionRecherche/pomme/facettes/T3/tri/%2A")][@target="_blank"]', + 'Imprimer', + $this->_response->getBody()); } } @@ -66,11 +55,6 @@ class RechercheControllerPrintActionWithRecordsTest extends AbstractControllerTe public function setUp() { parent::setUp(); - $this->fixture('Class_ModeleFusion', ['id' => 1, - 'nom' => 'recherche', - 'contenu' => '<p> {notices.each[<img src="{url_vignette}"/> <h1>{titre_principal}</h1> <div>{article.contenu}</div> <div>{resume}</div> -]}</p>']); - Class_Indexation_PseudoNotice::index( $this->fixture('Class_Article' , ['id' => 10, 'titre' => 'pomme', 'contenu' => '<p>blabla</p>', @@ -84,7 +68,27 @@ class RechercheControllerPrintActionWithRecordsTest extends AbstractControllerTe 'notice' => new Class_Notice(), 'type_doc_id' => Class_TypeDoc::ARTICLE])); - $this->dispatch("/recherche/print/expressionRecherche/pomme/strategy/Notice_List/ids/2;1/modele_fusion/1",true); + $mock_sql = $this->mock() + ->whenCalled('fetchAll') + ->with("select id_notice, facettes from notices Where MATCH(titres, auteurs, editeur, collection, matieres, dewey) AGAINST('+(POMME POMMES POM)' IN BOOLEAN MODE) and MATCH(facettes) AGAINST('+T3' IN BOOLEAN MODE) order by (MATCH(alpha_titre) AGAINST(' POMME') * 1.5) + (MATCH(alpha_auteur) AGAINST(' POMME')) desc", + true, + false) + ->answers([ + [2, ''], + [1, ''] + ]) + ->beStrict(); + Zend_Registry::set('sql', $mock_sql); + + $this->fixture('Class_ModeleFusion', ['id' => 1, + 'nom' => 'recherche', + 'contenu' => '<p> {notices.each[<img src="{url_vignette}"/> <h1>{titre_principal}</h1> <div>{article.contenu}</div> <div>{resume}</div> +]}</p>', + 'type' => 'Notice_List']); + + + + $this->dispatch("/recherche/print/expressionRecherche/pomme/facettes/T3/tri/*",true); } @@ -109,7 +113,7 @@ class RechercheControllerPrintActionWithRecordsTest extends AbstractControllerTe /** @test */ public function contenuShouldContainsTransmetropolitan() { - $this->assertXPathContentContains("//div//h1[1]", "transmetropolitan"); + $this->assertXPathContentContains("//h1[1]", "transmetropolitan", $this->_response->getBody()); } diff --git a/tests/scenarios/SearchSelection/SearchSelectionTest.php b/tests/scenarios/SearchSelection/SearchSelectionTest.php index 15dceee0146..2b3f12b3e01 100644 --- a/tests/scenarios/SearchSelection/SearchSelectionTest.php +++ b/tests/scenarios/SearchSelection/SearchSelectionTest.php @@ -43,7 +43,7 @@ abstract class SearchSelectionTestCase extends AbstractControllerTestCase { $this->fixture('Class_Notice', ['id' => 8, 'clef_alpha'=>'HARRY_POTTER_ROWLING_1', - 'titre_principal' => 'Harry Ppotter', + 'titre_principal' => 'Harry Potter', 'clef_oeuvre' =>'HARRY_POTTER', 'url_vignette'=>'no', 'url_image'=>'no', @@ -551,5 +551,34 @@ class SearchSelectionWithSessionViewWallModeTest extends SearchSelectionTestCase public function pageShouldNotIncludeLinkAddRecordAjaxOnBasket() { $this->assertNotXPath('//a[contains(@href, "panier/add-record-ajax/")]'); } +} + + + +class SearchSelectionPrintWithSelectionTest extends SearchSelectionTestCase { + public function setUp() { + parent::setUp(); + + Zend_Registry::get('session')->search_record_selection = [8, 10]; + $this->mock_sql + ->whenCalled('fetchAll') + ->with('select id_notice, facettes from notices Where id_notice in (8,10)', + true, false) + ->answers([ [8, ''], [10, '']]) + ->beStrict(); + + $this->fixture('Class_ModeleFusion', ['id' => 1, + 'nom' => 'recherche', + 'contenu' => '{notices.each[<h1>{titre_principal}</h1>]}', + 'type' => 'Notice_List']); + + $this->dispatch('/recherche/print/expressionRecherche/pomme', true); + } + + + /** @test */ + public function printPreviewShouldContainsInterstellar() { + $this->assertXPathContentContains('//h1', 'Interstellar', $this->_response->getBody()); + } } \ No newline at end of file -- GitLab