partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 8e1adb6e617f59cca529a9f83956f1092e927435
parent 8dcaf2e0c8261d8ce4c89019615a1f96638c5155
Author: Felicitus <felicitus@felicitus.org>
Date:   Fri,  6 Nov 2015 19:04:54 +0100

Added exporter which takes input from a JSON array and outputs it as the specified format

Diffstat:
Mapp/config/routing.yml | 3+++
Asrc/PartKeepr/ExportBundle/Controller/ExportController.php | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/ExportBundle/Resources/config/routing.yml | 3+++
3 files changed, 63 insertions(+), 0 deletions(-)

diff --git a/app/config/routing.yml b/app/config/routing.yml @@ -26,6 +26,9 @@ PartKeeprPartBundle: _frontend: resource: "@PartKeeprFrontendBundle/Resources/config/routing.yml" +_export: + resource: "@PartKeeprExportBundle/Resources/config/routing.yml" + _setup: resource: "@PartKeeprSetupBundle/Resources/config/routing.yml" diff --git a/src/PartKeepr/ExportBundle/Controller/ExportController.php b/src/PartKeepr/ExportBundle/Controller/ExportController.php @@ -0,0 +1,57 @@ +<?php +namespace PartKeepr\ExportBundle\Controller; + +use Exporter\Writer\CsvWriter; +use Exporter\Writer\XmlExcelWriter; +use Sensio\Bundle\FrameworkExtraBundle\Configuration as Routing; +use FOS\RestBundle\Controller\Annotations\View; +use FOS\RestBundle\Controller\FOSRestController; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; + +class ExportController extends FOSRestController +{ + /** + * Exports the given data to a given format + * + * @Routing\Route("/api/export", defaults={"method" = "post","_format" = "json"}) + * @View() + * + * @return array + */ + public function exportAction (Request $request) { + $contentTypes = $request->getAcceptableContentTypes(); + + $exporter = false; + $file = tempnam(sys_get_temp_dir(), "partkeepr_export"); + unlink($file); + + foreach ($contentTypes as $contentType) { + switch ($contentType) { + case "text/comma-separated-values": + $exporter = new CsvWriter($file, ',', '"', '\\', false); + break; + case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": + $exporter = new XmlExcelWriter($file, false); + break; + } + } + + if ($exporter === false) { + throw new \Exception("No or invalid format specified"); + } + + $content = json_decode($request->getContent(),true); + + $exporter->open(); + foreach ($content as $item) { + $exporter->write($item); + } + + $exporter->close(); + + $exportData = file_get_contents($file); + + return new Response($exportData, 200); + } +} diff --git a/src/PartKeepr/ExportBundle/Resources/config/routing.yml b/src/PartKeepr/ExportBundle/Resources/config/routing.yml @@ -0,0 +1,3 @@ +_index: + resource: "@PartKeeprExportBundle/Controller/ExportController.php" + type: annotation