From 6ec0892ac6e0cc4e6cb69168d4a0a3a4b07b4052 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Tue, 17 Feb 2015 15:11:42 +0100 Subject: [PATCH] Allowing export for reports --- Controller/ReportController.php | 127 +++++++++++++++--- Resources/config/routing.yml | 16 ++- Resources/views/Report/export.csv.twig | 23 ++++ .../views/Report/select_report_type.html.twig | 2 - .../select_report_type_for_export.html.twig | 33 +++++ 5 files changed, 183 insertions(+), 18 deletions(-) create mode 100644 Resources/views/Report/export.csv.twig create mode 100644 Resources/views/Report/select_report_type_for_export.html.twig diff --git a/Controller/ReportController.php b/Controller/ReportController.php index aa2c7499b..546549d39 100644 --- a/Controller/ReportController.php +++ b/Controller/ReportController.php @@ -33,10 +33,12 @@ use Chill\ReportBundle\Form\ReportType; */ class ReportController extends Controller { - /** - * Lists all Report entities. + * List all the report entities for a given person. * + * @param integer $person_id The id of the person. + * @param Request $request The request + * @return Response The web page. */ public function listAction($person_id, Request $request) { @@ -52,9 +54,12 @@ class ReportController extends Controller )); } - /** - * Select the type of the Report + * Display a form for selecting which type of report to add for a given person + * + * @param integer $person_id The id of the person. + * @param Request $request The request + * @return Response The web page. */ public function selectReportTypeAction($person_id, Request $request) { @@ -68,19 +73,15 @@ class ReportController extends Controller $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); - $cFGroups = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup') ->findByEntity('Chill\ReportBundle\Entity\Report'); - if(count($cFGroups) === 1 ){ return $this->redirect( $this->generateUrl('report_new', array('person_id' => $person_id, 'cf_group_id' => $cFGroups[0]->getId()))); } - $cFGroupsChoice = array(); foreach ($cFGroups as $cFGroup) { @@ -97,6 +98,8 @@ class ReportController extends Controller )) ->getForm(); + $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); + return $this->render('ChillReportBundle:Report:select_report_type.html.twig', array( 'form' => $form->createView(), 'person' => $person @@ -104,8 +107,87 @@ class ReportController extends Controller } /** - * Displays a form to create a new Report entity. + * Display a form for selecting which type of report to export + * (a csv file with all the report of this type) * + * @param Request $request The request + * @return Response The web page. + */ + public function selectReportTypeForExportAction(Request $request) + { + $cFGroupId = $request->query->get('cFGroup'); + + if($cFGroupId) { + return $this->redirect( + $this->generateUrl('report_export_list', + array('cf_group_id' => $cFGroupId))); + } + + $em = $this->getDoctrine()->getManager(); + + $cFGroups = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup') + ->findByEntity('Chill\ReportBundle\Entity\Report'); + + if(count($cFGroups) === 1 ){ + return $this->redirect( + $this->generateUrl('report_export_list', + array('cf_group_id' => $cFGroups[0]->getId()))); + } + + $cFGroupsChoice = array(); + + foreach ($cFGroups as $cFGroup) { + $cFGroupsChoice[$cFGroup->getId()] = $cFGroup->getName($request->getLocale()); + } + + $form = $this->get('form.factory') + ->createNamedBuilder(null, 'form', null, array( + 'method' => 'GET', + 'csrf_protection' => false + )) + ->add('cFGroup', 'choice', array( + 'choices' => $cFGroupsChoice + )) + ->getForm(); + + return $this->render('ChillReportBundle:Report:select_report_type_for_export.html.twig', array( + 'form' => $form->createView(), + 'layout_name' => "ChillMainBundle::Export/layout.html.twig" + )); + } + + /** + * Return a csv file with all the reports of a given type + * + * @param integer $cf_group_id The id of the report type to export + * @param Request $request The request + * @return A csv file with all the reports of the selected type + */ + public function exportAction($cf_group_id, Request $request) + { + $em = $this->getDoctrine()->getManager(); + + $cFGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($cf_group_id); + $reports = $em->getRepository('ChillReportBundle:Report')->findByCFGroup($cFGroup); + + + $response = $this->render('ChillReportBundle:Report:export.csv.twig', array( + 'reports' => $reports, + 'cf_group' => $cFGroup + )); + + $response->headers->set('Content-Type', 'text/csv'); + $response->headers->set('Content-Disposition', 'attachment; filename="export.csv"'); + return $response; + } + + /** + * Display a form for creating a new report for a given person and of a given type + * + * @param integer $person_id The id of the person. + * @param integer $cf_group_id The id of the report type. + * @param Request $request The request + * @return Response The web page. */ public function newAction($person_id, $cf_group_id, Request $request) { @@ -130,8 +212,12 @@ class ReportController extends Controller } /** - * Creates a new Report entity. + * Create a new report for a given person and of a given type * + * @param integer $person_id The id of the person. + * @param integer $cf_group_id The id of the report type. + * @param Request $request The request containing the form data (from the newAction) + * @return Response The web page. */ public function createAction($person_id, $cf_group_id, Request $request) { @@ -183,6 +269,8 @@ class ReportController extends Controller * Creates a form to create a Report entity. * * @param Report $entity The entity + * @param integer $person_id The id of the person. + * @param integer $cf_group_id The id of the report type. * * @return \Symfony\Component\Form\Form The form */ @@ -200,8 +288,11 @@ class ReportController extends Controller } /** - * Finds and displays a Report entity. + * Find and display a report. * + * @param integer $report_id The id of the report. + * @param integer $person_id The id of the person. + * @return Response The web page. */ public function viewAction($report_id, $person_id) { @@ -223,8 +314,11 @@ class ReportController extends Controller } /** - * Displays a form to edit an existing Report entity. + * Display a form to edit an existing Report entity. * + * @param integer $person_id The id of the person. + * @param integer $report_id The id of the report. + * @return Response The web page. */ public function editAction($person_id, $report_id, Request $request) { @@ -255,8 +349,8 @@ class ReportController extends Controller /** * Creates a form to edit a Report entity. * - * @param Report $entity The entity - * + * @param Report $entity The report to edit. + * @param integer $person_id The id of the person. * @return \Symfony\Component\Form\Form The form */ private function createEditForm(Report $entity, $person_id) @@ -273,8 +367,11 @@ class ReportController extends Controller } /** - * Edits an existing Report entity. + * Web page for editing an existing report. * + * @param integer $person_id The id of the person. + * @param integer $report_id The id of the report. + * @return Response The web page. */ public function updateAction($person_id, $report_id, Request $request) { diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index 187bc6986..77d410d90 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -34,4 +34,18 @@ report_edit: report_update: path: /{_locale}/person/{person_id}/report/{report_id}/update defaults: { _controller: "ChillReportBundle:Report:update" } - requirements: { _method: post|put } \ No newline at end of file + requirements: { _method: post|put } + +report_export_list: + path: /{_locale}/export/report/cfgroup/{cf_group_id} + defaults: { _controller: "ChillReportBundle:Report:export" } + + +report_export_select_type: + pattern: /{_locale}/export/report/select/type + defaults: {_controller: "ChillReportBundle:Report:selectReportTypeForExport" } + options: + menus: + export: + order: 100 + label: Export reports \ No newline at end of file diff --git a/Resources/views/Report/export.csv.twig b/Resources/views/Report/export.csv.twig new file mode 100644 index 000000000..54548f001 --- /dev/null +++ b/Resources/views/Report/export.csv.twig @@ -0,0 +1,23 @@ +"{{ 'Person'|trans }}",{# +#}"{{ 'Date'|trans }}",{# +#}"{{ 'User'|trans }}",{# +#}"{{ 'Report type'|trans }}",{# +#}{% for customField in cf_group.customFields %}{# + #}"{{ chill_custom_field_label(customField) }}"{% if not loop.last %},{% endif %}{# +#}{% endfor %} + +{% for report in reports %}{# + #}"{{ report.person }}",{# + #}"{{ report.date|localizeddate('long', 'none') }}",{# + #}"{{ report.user }}",{# + #}"{{ report.cFGroup.getName(app.request.locale) }}",{# + #}{% for customField in report.cFGroup.customFields %}{# + #}{% if customField.type == 'title' %}{# + #}""{# + #}{% else %}{# + #}"{{ chill_custom_field_widget(report.cFData , customField, 'csv') }}"{# + #}{% endif %}{# + #}{% if not loop.last %},{% endif %}{# + #}{% endfor %} + +{% endfor %} \ No newline at end of file diff --git a/Resources/views/Report/select_report_type.html.twig b/Resources/views/Report/select_report_type.html.twig index 581c63e52..b37b1f2ba 100644 --- a/Resources/views/Report/select_report_type.html.twig +++ b/Resources/views/Report/select_report_type.html.twig @@ -22,7 +22,6 @@ {% block personcontent %} - {{ form_start(form) }} {{ form_widget(form.cFGroup) }} {{ form_end(form) }} - {% endblock %} \ No newline at end of file diff --git a/Resources/views/Report/select_report_type_for_export.html.twig b/Resources/views/Report/select_report_type_for_export.html.twig new file mode 100644 index 000000000..05a42e954 --- /dev/null +++ b/Resources/views/Report/select_report_type_for_export.html.twig @@ -0,0 +1,33 @@ +{# + * Copyright (C) 2014, Champs Libres Cooperative SCRLFS, + * + * This program 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, either version 3 of the + * License, or (at your option) any later version. + * + * This program 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 this program. If not, see . +#} +{% extends "ChillMainBundle::Export/layout.html.twig" %} + +{% set activeRouteKey = 'report_export_list' %} + +{% block title %}{{ 'Reports export'|trans() }}{% endblock title %} + +{% block export_content %} + +{{ form_start(form) }} + {{ form_widget(form.cFGroup) }} + +{{ form_end(form) }} + +{% endblock %} \ No newline at end of file