diff --git a/.gitattributes b/.gitattributes index ac45682e1dbb0490905f60e6e672d42a7e9dfc56..57719b3dd5690e05926f03ad174513ed9bac4a98 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2220,6 +2220,7 @@ library/ZendAfi/View/Helper/Album/OsmPlayer.php -text library/ZendAfi/View/Helper/Album/RssFeedVisitor.php -text library/ZendAfi/View/Helper/Album/XspfPlaylistVisitor.php -text library/ZendAfi/View/Helper/AlbumRessourceInfos.php -text +library/ZendAfi/View/Helper/Article/FormulairesCsvVisitor.php -text library/ZendAfi/View/Helper/Avis.php -text library/ZendAfi/View/Helper/AvisCms.php -text library/ZendAfi/View/Helper/BarreNav.php -text diff --git a/application/modules/admin/controllers/ModoController.php b/application/modules/admin/controllers/ModoController.php index bb9bf7b922da4dd243c1314ed022196922d9af4a..6857877215ae6c365e57ec4fe7441969b5f0fbdf 100644 --- a/application/modules/admin/controllers/ModoController.php +++ b/application/modules/admin/controllers/ModoController.php @@ -977,4 +977,20 @@ class Admin_ModoController extends Zend_Controller_Action { $this->_redirect('admin/modo/formulaires/id_article/'.$formulaire_to_delete->getIdArticle()); } + + public function exportCsvFormulaireAction() { + $this->getHelper('ViewRenderer')->setNoRender(); + + $article = Class_Article::find((int)$this->_getParam('id_article')); + $csv = $this->view->article_FormulairesCsvVisitor($article); + + $response = $this->_response; + $response->clearAllHeaders(); + $filename='formulaire_'.$article->getId().'.csv'; + + $response->setHeader('Content-Type', 'text/csv; name="'.$filename.'"', true); + $response->setHeader('Content-Disposition', 'attachment; filename="'.$filename.'"', true); + $response->setBody($csv); + } + } \ No newline at end of file diff --git a/application/modules/admin/views/scripts/modo/formulaires.phtml b/application/modules/admin/views/scripts/modo/formulaires.phtml index a53ab0a04a5a0801d994ac859ab8dcd46543c319..42a8a29763bcc8727f47c8f21917b74f31e069e6 100644 --- a/application/modules/admin/views/scripts/modo/formulaires.phtml +++ b/application/modules/admin/views/scripts/modo/formulaires.phtml @@ -1,3 +1,14 @@ +<?php +echo sprintf('<a style="float:right;margin:4px 0px" href="%s"><img style="vertical-align: middle" src="%s">%s</a>', + $this->url(['module' => 'admin', + 'controller' => 'modo', + 'action' => 'export-csv-formulaire', + 'id_article' => $this->article->getId()], + null, + true), + URL_ADMIN_IMG.'ico/down.gif', + $this->_('Export CSV')); +?> <h1><?php echo $this->_('ModÊration des formulaires: '.$this->article->getTitre());?></h1> <?php diff --git a/library/Class/Formulaire.php b/library/Class/Formulaire.php index 371e579cae8799f1b6f3812c5e1e34cd0bb52e1c..a4e77a92bd489b75797b552e11977014a4069c6b 100644 --- a/library/Class/Formulaire.php +++ b/library/Class/Formulaire.php @@ -30,8 +30,9 @@ class Class_Formulaire extends Storm_Model_Abstract { 'referenced_in' => 'id_article']]; protected $_default_attribute_values = ['data' => 'a:0:{}']; - protected $_datas; + + public static function mergeDataNames($formulaires) { $names = []; foreach($formulaires as $formulaire) { @@ -41,8 +42,9 @@ class Class_Formulaire extends Storm_Model_Abstract { return array_unique($names); } + public function getDataNames() { - return array_keys($this->getDatas()); + return array_keys(array_change_key_case($this->getDatas())); } @@ -86,8 +88,11 @@ class Class_Formulaire extends Storm_Model_Abstract { } - public function getShortDateCreation() { - return explode(' ', $this->getDateCreation())[0]; + public function attributeValues($attributes) { + return array_map( + function($attribute) { + return $this->callGetterByAttributeName($attribute); + }, + $attributes); } - } diff --git a/library/ZendAfi/View/Helper/Article/FormulairesCsvVisitor.php b/library/ZendAfi/View/Helper/Article/FormulairesCsvVisitor.php new file mode 100644 index 0000000000000000000000000000000000000000..19c431699e4df2dd8167b96c9ce51f4f565eadfb --- /dev/null +++ b/library/ZendAfi/View/Helper/Article/FormulairesCsvVisitor.php @@ -0,0 +1,43 @@ +<?php +/** + * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved. + * + * AFI-OPAC 2.0 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). + * + * AFI-OPAC 2.0 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 AFI-OPAC 2.0; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +class ZendAfi_View_Helper_Article_FormulairesCsvVisitor extends Zend_View_Helper_Json { + public function article_formulairesCsvVisitor($article) { + $formulaires = $article->getFormulaires(); + + $attributes = array_merge( + ['date_creation', 'compte', 'libelle_bib'], + Class_Formulaire::mergeDataNames($formulaires)); + + $filename = PATH_TEMP.'formulaires.csv'; + $fp_csv = fopen($filename, 'w'); + + fputcsv($fp_csv, $attributes); + + foreach($formulaires as $formulaire) + fputcsv($fp_csv, $formulaire->attributeValues($attributes)); + + fclose($fp_csv); + + return file_get_contents($filename); + } +} + +?> \ No newline at end of file diff --git a/tests/application/modules/admin/controllers/ModoControllerFormulaireTest.php b/tests/application/modules/admin/controllers/ModoControllerFormulaireTest.php index f1171647f618fe83389918912f30a7e8e399a896..9cd0988aa4c3c6ceeddd2e5bf5cbed9dc1b91d27 100644 --- a/tests/application/modules/admin/controllers/ModoControllerFormulaireTest.php +++ b/tests/application/modules/admin/controllers/ModoControllerFormulaireTest.php @@ -121,6 +121,13 @@ class ModoControllerFormulaireForArticleListTest extends ModoControllerFormulair public function aTDShouldContainsActionToDeleteFormulaireMireille() { $this->assertXPath('//tr[2]//td/a[contains(@href, "admin/modo/delete-formulaire/id_article/12/id/5")]'); } + + + /** @test */ + public function linkToExportCsvShouldBePresent() { + $this->assertXPathContentContains('//a[contains(@href, "admin/modo/export-csv-formulaire/id_article/12")]', + 'Export CSV'); + } } @@ -149,6 +156,43 @@ class ModoControllerFormulaireForArticleDeleteTest extends ModoControllerFormula +class ModoControllerFormulaireExportCSVForArticlTest extends ModoControllerFormulaireForArticleTestCase { + public function setUp() { + parent::setUp(); + + $this->dispatch('admin/modo/export-csv-formulaire/id_article/12', true); + } + + + /** @test */ + public function secondFormulaireShouldBeCSV() { + $this->assertContains('"2012-12-06 10:00:01",zork,Annecy,Bougie,Mireille', + $this->_response->getBody()); + } + + + /** @test */ + public function csvShouldContainsAttributeNames() { + $this->assertContains('date_creation,compte,libelle_bib,nom,prenom,name,age', + $this->_response->getBody()); + } + + + /** @test */ + public function headerShouldContainsFileAttachment() { + $this->assertHeaderContains('Content-Disposition', 'attachment; filename="formulaire_12.csv"'); + } + + + /** @test */ + public function headerShouldContainsContentTypeCSV() { + $this->assertHeaderContains('Content-Type', 'text/csv; name="formulaire_12.csv"'); + } +} + + + + class ModoControllerFormulaireListTest extends Admin_AbstractControllerTestCase { public function setUp() { parent::setUp();