From 74edceafc84c0eff5ff0413b61e7782081838ec4 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 29 Oct 2021 11:39:56 +0200 Subject: [PATCH] add JSON export renderer --- Application/Model/ExportRenderer/Json.php | 51 +++++++++++++++++++ .../Model/ExportRenderer/RendererBridge.php | 4 +- .../views/admin/de/d3DataWizard_lang.php | 1 + .../views/admin/en/d3DataWizard_lang.php | 1 + 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 Application/Model/ExportRenderer/Json.php diff --git a/Application/Model/ExportRenderer/Json.php b/Application/Model/ExportRenderer/Json.php new file mode 100644 index 0000000..8677b62 --- /dev/null +++ b/Application/Model/ExportRenderer/Json.php @@ -0,0 +1,51 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\DataWizard\Application\Model\ExportRenderer; + +use D3\DataWizard\Application\Model\Exceptions\RenderException; + +class Json implements RendererInterface +{ + /** + * @param $rows + * @param $fieldNames + * + * @return string + * @throws RenderException + */ + public function getContent($rows, $fieldNames): string + { + $flags = JSON_PRETTY_PRINT; + $json = json_encode( $rows, $flags ); + if ( $json === false ) { + throw oxNew( RenderException::class, json_last_error_msg(), json_last_error()); + } + return $json; + } + + public function getFileExtension(): string + { + return 'json'; + } + + /** + * @return string + */ + public function getTitleTranslationId(): string + { + return "D3_DATAWIZARD_EXPORT_FORMAT_JSON"; + } +} \ No newline at end of file diff --git a/Application/Model/ExportRenderer/RendererBridge.php b/Application/Model/ExportRenderer/RendererBridge.php index 6713bb8..a5f43b3 100644 --- a/Application/Model/ExportRenderer/RendererBridge.php +++ b/Application/Model/ExportRenderer/RendererBridge.php @@ -21,6 +21,7 @@ class RendererBridge { const FORMAT_CSV = 'CSV'; const FORMAT_PRETTY = 'Pretty'; + const FORMAT_JSON = 'JSON'; /** * @return array @@ -29,7 +30,8 @@ class RendererBridge { return [ self::FORMAT_CSV => oxNew(Csv::class), - self::FORMAT_PRETTY => oxNew(Pretty::class) + self::FORMAT_PRETTY => oxNew(Pretty::class), + self::FORMAT_JSON => oxNew(Json::class) ]; } diff --git a/Application/views/admin/de/d3DataWizard_lang.php b/Application/views/admin/de/d3DataWizard_lang.php index 992ee4a..0d9805d 100644 --- a/Application/views/admin/de/d3DataWizard_lang.php +++ b/Application/views/admin/de/d3DataWizard_lang.php @@ -39,6 +39,7 @@ $aLang = array( 'D3_DATAWIZARD_EXPORT_SUBMIT' => 'Export starten', 'D3_DATAWIZARD_EXPORT_FORMAT_CSV' => 'CSV-Format', 'D3_DATAWIZARD_EXPORT_FORMAT_PRETTY' => 'Pretty-Format', + 'D3_DATAWIZARD_EXPORT_FORMAT_JSON' => 'JSON-Format', 'D3_DATAWIZARD_ACTION_SUBMIT' => 'Aktion starten', diff --git a/Application/views/admin/en/d3DataWizard_lang.php b/Application/views/admin/en/d3DataWizard_lang.php index 9dc50c8..bb34e92 100644 --- a/Application/views/admin/en/d3DataWizard_lang.php +++ b/Application/views/admin/en/d3DataWizard_lang.php @@ -39,6 +39,7 @@ $aLang = array( 'D3_DATAWIZARD_EXPORT_SUBMIT' => 'generate export', 'D3_DATAWIZARD_EXPORT_FORMAT_CSV' => 'CSV format', 'D3_DATAWIZARD_EXPORT_FORMAT_PRETTY' => 'Pretty format', + 'D3_DATAWIZARD_EXPORT_FORMAT_JSON' => 'JSON format', 'D3_DATAWIZARD_ACTION_SUBMIT' => 'run action',