From ffd0d50921535ef76d38d790aa0ff21e312c4bf9 Mon Sep 17 00:00:00 2001 From: Ghislain Loas <ghislo@sandbox.pergame.net> Date: Fri, 4 Dec 2015 12:18:28 +0100 Subject: [PATCH] dev #30973 add csv export --- .../admin/controllers/AlbumController.php | 5 +++ .../admin/views/scripts/album/dilicom.phtml | 4 ++ library/Class/Album/Item.php | 9 ++++ .../ZendAfi/Controller/Action/Helper/Csv.php | 45 +++++++++++++++++++ library/ZendAfi/View/Helper/Csv.php | 34 ++++++++++++++ .../AlbumControllerDilicomPNBTest.php | 41 +++++++++++++++++ 6 files changed, 138 insertions(+) create mode 100644 library/ZendAfi/Controller/Action/Helper/Csv.php create mode 100644 library/ZendAfi/View/Helper/Csv.php diff --git a/application/modules/admin/controllers/AlbumController.php b/application/modules/admin/controllers/AlbumController.php index 713d3f2beb8..c212ce2007f 100644 --- a/application/modules/admin/controllers/AlbumController.php +++ b/application/modules/admin/controllers/AlbumController.php @@ -95,6 +95,11 @@ class Admin_AlbumController extends ZendAfi_Controller_Action { } + public function dilicomExportCsvAction() { + $this->_helper->csv('dilicom_csv', Class_Album_Item::findAll()); + } + + public function importeadAction() { $this->view->titre = 'Import/Export EAD'; diff --git a/application/modules/admin/views/scripts/album/dilicom.phtml b/application/modules/admin/views/scripts/album/dilicom.phtml index c30ea353cd4..30818ccb98f 100644 --- a/application/modules/admin/views/scripts/album/dilicom.phtml +++ b/application/modules/admin/views/scripts/album/dilicom.phtml @@ -8,6 +8,10 @@ $title = function($model, $attrib) { }; echo $this->tag('h2', $this->_('Utilisation des ressources PNB Dilicom')) . + $this->tagAnchor($this->url(['module' => 'admin', + 'controller' => 'album', + 'action' => 'dilicom-export-csv'], null, true), + $this->_('Exporter le tableau en CSV')). $this->tagModelTable($this->dilicom_items, [$this->_('Titre'), $this->_('Nombre de prêts'), diff --git a/library/Class/Album/Item.php b/library/Class/Album/Item.php index 2ae3e5ea473..4da14b919bd 100644 --- a/library/Class/Album/Item.php +++ b/library/Class/Album/Item.php @@ -54,5 +54,14 @@ class Class_Album_Item extends Storm_Model_Abstract { protected function getAlbumUsageConstraints() { return $this->getAlbum()->getUsageConstraints(); } + + + public function toCsv() { + return [$this->getTitle(), + $this->getQuantityOnTotal(), + $this->getLiveQuantity(), + $this->getDuration(), + $this->getLicenseExpiration()]; + } } ?> \ No newline at end of file diff --git a/library/ZendAfi/Controller/Action/Helper/Csv.php b/library/ZendAfi/Controller/Action/Helper/Csv.php new file mode 100644 index 00000000000..f8e52b8a013 --- /dev/null +++ b/library/ZendAfi/Controller/Action/Helper/Csv.php @@ -0,0 +1,45 @@ +<?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_Controller_Action_Helper_Csv extends Zend_Controller_Action_Helper_Abstract { + + public function direct($filename, $data) { + Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true); + + $csv = $this->encodeCsv($data); + + $filename = $filename . '.csv'; + + $response = $this->getResponse(); + $response->clearAllHeaders(); + + $response->setHeader('Content-Type', 'text/csv; name="'.$filename.'"', true); + $response->setHeader('Content-Disposition', 'attachment; filename="'.$filename.'"', true); + $response->setBody($csv); + } + + + protected function encodeCsv($data) { + return (new ZendAfi_View_Helper_Csv())->csv($data); + } +} +?> \ No newline at end of file diff --git a/library/ZendAfi/View/Helper/Csv.php b/library/ZendAfi/View/Helper/Csv.php new file mode 100644 index 00000000000..6bee99f57f2 --- /dev/null +++ b/library/ZendAfi/View/Helper/Csv.php @@ -0,0 +1,34 @@ +<?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_View_Helper_Csv extends ZendAfi_View_Helper_BaseHelper { + public function csv($data) { + $fp_csv = fopen('php://memory', 'r+'); + + foreach($data as $element) + fputcsv($fp_csv, $element->toCsv(), ';'); + + rewind($fp_csv); + return stream_get_contents($fp_csv); + } +} +?> \ No newline at end of file diff --git a/tests/application/modules/admin/controllers/AlbumControllerDilicomPNBTest.php b/tests/application/modules/admin/controllers/AlbumControllerDilicomPNBTest.php index 8e415793596..294ad5a6486 100644 --- a/tests/application/modules/admin/controllers/AlbumControllerDilicomPNBTest.php +++ b/tests/application/modules/admin/controllers/AlbumControllerDilicomPNBTest.php @@ -113,6 +113,12 @@ class AlbumControllerDilicomPNBImportDilicomTest extends Admin_AlbumControllerDi public function nbOfRemainingDaysInLicenseShouldBe45() { $this->assertXPathContentContains('//table//tr/td', '45'); } + + + /** @test */ + public function exportCSVAnchorShouldBePresent() { + $this->assertXPathContentContains('//div[@class="modules"]/a[contains(@href, "/admin/album/dilicom-export-csv")]', 'Exporter le tableau en CSV'); + } } @@ -242,4 +248,39 @@ class AlbumControllerDilicomPNBImportDilicomPaginatorTest extends Admin_AlbumCon ]); } } +} + + + +class Admin_AlbumControllerDilicomPNBExportCsvTest extends Admin_AbstractControllerTestCase { + + public function setUp() { + parent::setUp(); + $this->dispatch('admin/album/dilicom-export-csv', true); + } + + + /** @test */ + public function filenameShouldBeDilicomCsv() { + $this->assertContains(['name' => 'Content-Type', + 'value' => 'text/csv; name="dilicom_csv.csv"', + 'replace' => true], $this->_response->getHeaders()); + } + + + /** @test */ + public function csvShouldContainsAlbumsItems() { + $this->assertEquals('"La Guerre germano-soviétique";"2 / 40";"0 / 5";56;1679 +"Aux origines des Editions du Seuil";"1 / 40";"0 / 5";56;1748 +"Le Cercle littéraire des amateurs d\'épluchures de patates";"0 / 30";"0 / 5";56;949 +"La Reine des Neiges";"6 / 30";"2 / 5";56;2042 +"La Place de l\'étoile";"0 / 30";"0 / 5";56;2155 +"Où vont les hirondelles en hiver";"1 / 30";"1 / 5";56;1060 +"Victor et Macha";"1 / 30";"0 / 5";56;2134 +"Histoire d\'Irène";"0 / 30";"0 / 5";56;2136 +"Les soldats de la honte";"1 / 30";"1 / 5";56;1060 +Ravage;"0 / 30";"0 / 5";56;2155 +"Tranchecaille. Une enquête dans les tranchées";"1 / 30";"0 / 5";56;2155 +', $this->_response->getBody()); + } } \ No newline at end of file -- GitLab