Commit 2489355f authored by Patrick Barroca's avatar Patrick Barroca 😁
Browse files

hotline #95190 : clean album resources csv export

parent de85e0d5
Pipeline #8644 passed with stage
in 41 minutes and 2 seconds
- ticket #95190 : Bibliothèque numérique : Export des medias au format csv
\ No newline at end of file
......@@ -156,15 +156,14 @@ class Admin_AlbumController extends ZendAfi_Controller_Action {
->setAttrib('enctype', 'multipart/form-data')
->setAction($this->view->url(['action' => 'import_ead']))
->addElement($this->view->newFormElementFile('ead', 'xml'), 'ead')
->addElement('submit', 'submit', ['label' => 'Importer le fichier EAD']);
->addElement('submit', 'submit', ['label' => $this->_('Importer le fichier EAD')]);
}
protected function _formExport($type='ead') {
return $this->view
->newForm(['id' => 'export_ead', 'class' => 'form'])
->newForm(['id' => 'export_' . $type, 'class' => 'form'])
->setMethod('post')
->setAttrib('enctype', 'multipart/form-data')
->setAction($this->view->url(['action' => 'export-'.$type]))
->addElement('select',
'cat_id',
......@@ -173,29 +172,17 @@ class Admin_AlbumController extends ZendAfi_Controller_Action {
'required' => true,
'multiOptions' => ['' => $this->_('Choisissez une catégorie')]
+ Class_AlbumCategorie::getAllLibelles()])
->addElement('submit', 'submit', ['label' => 'Exporter en ' . strtoupper($type)]);
->addElement('submit', 'submit', ['label' => $this->_('Exporter en %s', strtoupper($type))]);
}
public function exportCsvAction() {
$description = (new Class_TableDescription('ressources'))
->addColumn($this->_('Categorie'), 'album_category_absolute_path')
->addColumn($this->_('Titre Album'), 'titre_album')
->addColumn($this->_('Description Album'), 'description_album')
->addColumn($this->_('Date'), 'annee')
->addColumn($this->_('Créateur'), 'auteur')
->addColumn($this->_('Editeur'), 'editeur')
->addColumn($this->_('Titre'), 'titre')
->addColumn($this->_('Description'), 'description')
->addColumn($this->_('Ressource'), 'play_ressource_url');
$categories = [Class_AlbumCategorie::find($this->_getParam('cat_id',1))];
$datas = [];
foreach(Class_AlbumCategorie::getAlbumsFromCategories([$this->_getParam('cat_id',1)]) as $album)
$datas = array_merge($album->getRessources(),$datas);
return $this->_helper->csv('export.csv',
$this->view->renderCsv($description, $datas));
$datas = Class_AlbumCategorie::getResourcesFromCategories([$this->_getParam('cat_id',1)]);
return $this->_helper
->csv('export.csv',
$this->view->renderCsv(new Class_TableDescription_AlbumsExport('ressources'),
$datas));
}
......
......@@ -2,6 +2,8 @@
echo $this->tag('h2', $this->_('Import'));
echo $this->renderForm($this->form_import_ead);
echo $this->tag('h2', $this->_('Export'));
echo $this->tag('p',
$this->_('ATTENTION : suivant la configuration de votre serveur d\'hébergement, l\'export de grandes collections peut échouer.'),
['class' => 'warning']);
echo $this->renderForm($this->form_export_ead);
echo $this->renderForm($this->form_export_csv);
?>
......@@ -114,6 +114,15 @@ class AlbumCategorieLoader extends Storm_Model_Loader {
}
public function getResourcesFromCategories($ids) {
$datas = [];
foreach(Class_AlbumCategorie::getAlbumsFromCategories($ids) as $album)
$datas = array_merge($album->getRessources(), $datas);
return $datas;
}
public function getAlbumsIdsFromCategories($ids) {
$albums = new Storm_Model_Collection(Class_AlbumCategorie::getAlbumsFromCategories($ids));
return $albums->collect('id')->getArrayCopy();
......@@ -122,6 +131,7 @@ class AlbumCategorieLoader extends Storm_Model_Loader {
class Class_AlbumCategorie extends Storm_Model_Abstract {
use Trait_TreeNode;
......
<?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 Class_TableDescription_AlbumsExport extends Class_TableDescription {
public function init() {
$this
->addColumn($this->_('Categorie'), 'album_category_absolute_path')
->addColumn($this->_('Titre Album'), 'titre_album')
->addColumn($this->_('Description Album'), 'description_album')
->addColumn($this->_('Date'), 'annee')
->addColumn($this->_('Créateur'), 'auteur')
->addColumn($this->_('Editeur'), 'editeur')
->addColumn($this->_('Titre'), 'titre')
->addColumn($this->_('Description'), 'description')
->addColumn($this->_('Ressource'), 'play_ressource_url')
;
}
}
......@@ -37,7 +37,7 @@ class ZendAfi_View_Helper_RenderCsv extends ZendAfi_View_Helper_BaseHelper {
$this->_renderDatas($models);
rewind($this->_fp_csv);
return stream_get_contents($this->_fp_csv);
return stream_get_contents($this->_fp_csv);
}
......@@ -73,7 +73,7 @@ class ZendAfi_View_Helper_RenderCsv extends ZendAfi_View_Helper_BaseHelper {
->_description
->columnsCollect(function($column) use ($model)
{
return $column->renderModelOn($model,$this);
return $column->renderModelOn($model, $this);
});
return $this->_values;
}
......@@ -91,4 +91,3 @@ class ZendAfi_View_Helper_RenderCsv extends ZendAfi_View_Helper_BaseHelper {
public function renderModelAction($model, $description) { }
}
?>
......@@ -2,44 +2,26 @@
error_reporting(E_ERROR | E_PARSE);
require(__DIR__.'/../console.php');
class Scripts_ExportCsvCategory extends ZendAfi_Controller_Action{
if (2 > $argc) {
print "usage : php scripts/export_csv_category.php catid [filename]
public function __construct($cat_id, $filename){
$usage = "usage : php export_category_csv.php catid filename.csv
catid = id category à utiliser pour l'export
filename = nom du fichier à générer
catid : album category id from which select albums to export
filename = optional, file in which export, replace if exists, defaults to export.csv
";
if (!isset($cat_id) || !($categories = Class_AlbumCategorie::find($cat_id))){
print $usage;
exit(255);
}
if (!$category = Class_AlbumCategorie::find((int)$argv[1])) {
printf("Unknown category with id %s\n", $argv[1]);
exit(255);
}
$description = (new Class_TableDescription('ressources'))
->addColumn($this->_('Categorie'), 'album_category_absolute_path')
->addColumn($this->_('Titre Album'), 'titre_album')
->addColumn($this->_('Description Album'), 'description_album')
->addColumn($this->_('Date'), 'annee')
->addColumn($this->_('Créateur'), 'auteur')
->addColumn($this->_('Editeur'), 'editeur')
->addColumn($this->_('Titre'), 'titre')
->addColumn($this->_('Description'), 'description')
->addColumn($this->_('Ressource'), 'play_ressource_url');
$datas = [];
foreach(Class_AlbumCategorie::getAlbumsFromCategories([$cat_id]) as $album)
$datas = array_merge( $album->getRessources(),$datas);
$handle = fopen($filename?$filename:'export.csv','w');
$datas = Class_AlbumCategorie::getResourcesFromCategories([$category->getId()]);
$helper = new ZendAfi_View_Helper_RenderCsv();
$filename = isset($argv[2]) ? $argv[2] : 'export.csv';
$zf_render_csv = new ZendAfi_View_Helper_RenderCsv();
fwrite($handle, $zf_render_csv->renderCsv($description, $datas));
fclose($handle);
file_put_contents($filename,
$helper->renderCsv(new Class_TableDescription_AlbumsExport('ressources'),
$datas));
}
}
new Scripts_ExportCsvCategory($argv[1], $argv[2]);
?>
printf("%d lines exported in %s\n", count($datas), $filename);
\ No newline at end of file
......@@ -2711,6 +2711,25 @@ class Admin_AlbumControllerImportEADTest extends Admin_AlbumControllerTestCase {
$this->assertXPathContentContains('//form[contains(@action, "export-ead")]//select[@name="cat_id"]/option',
'Favoris');
}
/** @test */
public function pageShouldContainsFormToExportCsv() {
$this->assertXPath('//form[contains(@action, "/admin/album/export-csv")]');
}
/** @test */
public function formExportCsvShouldHaveSelectorForAlbumCategories() {
$this->assertXPathContentContains('//form[contains(@action, "export-csv")]//select[@name="cat_id"]/option',
'Favoris');
}
/** @test */
public function pageShouldContainsLargeCollectionsWarning() {
$this->assertXPathContentContains('//p[@class="warning"]', 'peut échouer');
}
}
......@@ -2974,16 +2993,14 @@ class AlbumControllerMultiMediasExportCSV extends ControllerAlbumMultiMediasTest
->AddEditor('Dargaud')
->setAnnee('2019');
$this->postDispatch('/admin/album/export-csv',
['cat_id' => 34],
true);
$this->postDispatch('/admin/album/export-csv', ['cat_id' => 34]);
$this->_csv = array_map(function($line) { return str_getcsv($line, ";", '"'); },
explode("\n", rtrim($this->_response->getBody())));
}
/** @test */
public function responseBodyShouldBeAttachment() {
public function responseContentDispositionShouldBeAttachment() {
$this->assertHeaderContains('Content-Disposition', 'attachment');
}
......@@ -2994,12 +3011,25 @@ class AlbumControllerMultiMediasExportCSV extends ControllerAlbumMultiMediasTest
}
/** @test */
public function firstRowShouldContainsAllHeaders() {
// cannot assertEquals entire row due to array sort diff between php5 and php7
foreach(['Categorie', "Titre Album", "Description Album", 'Date', 'Créateur', 'Editeur', 'Titre', 'Description', 'Ressource']
as $header)
$this->assertContains($header, $this->_csv[0]);
public function expectedHeaders() {
return [['Categorie'],
['Titre Album'],
['Description Album'],
['Date'],
['Créateur'],
['Editeur'],
['Titre'],
['Description'],
['Ressource']];
}
/**
* @test
* @dataProvider expectedheaders
*/
public function firstRowShouldBeContainsHeader($header) {
$this->assertContains($header, $this->_csv[0]);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment