From 1253e380cbb7d6df1eb996e327cce9dde378fdda Mon Sep 17 00:00:00 2001 From: soufiane Date: Mon, 12 Feb 2024 16:03:19 +0100 Subject: [PATCH] wip #6717 @0:20 --- .../Commands/FluidbookSettingsExport.php | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 app/Console/Commands/FluidbookSettingsExport.php diff --git a/app/Console/Commands/FluidbookSettingsExport.php b/app/Console/Commands/FluidbookSettingsExport.php new file mode 100644 index 000000000..4d2de1f30 --- /dev/null +++ b/app/Console/Commands/FluidbookSettingsExport.php @@ -0,0 +1,105 @@ +getUser()->id; + $user = User::withoutGlobalScopes()->find(5908); + + // + $listID = ArrayUtil::parseRange($this->argument('id')); + $settings = FluidbookPublication::whereIn('id',$listID)->get("settings")->map(function($i){ + return json_decode($i->settings, true); + })->toArray(); + + $keys = array_keys(array_merge(...$settings)); + + + $excel = new Spreadsheet(); + $excel->getDefaultStyle() + ->getNumberFormat() + ->setFormatCode( + NumberFormat::FORMAT_TEXT + ); + $sheet = $excel->getActiveSheet(); + $sheetname = "test"; + $sheet->setTitle($sheetname); + + $line = 1; + $columns = 0; + $c = 0; + + foreach ($keys as $key) { + $c++; + $columns = max($columns, $c); + $cell = $sheet->getCellByColumnAndRow($c, $line); + $cell->setValue($key); + $style = $sheet->getStyleByColumnAndRow($c, $line); + $style->getFont()->setBold(true); + $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); + } + + for ($i = 0; $i < sizeof($settings); $i++) { + $c = 1; + $line++; + foreach ($keys as $key) { + $columns = max($columns, $c); + if (array_key_exists($key, $settings[$i])) { + $value = $settings[$i][$key] ?? ""; + $value = is_array($value) ? json_encode($value) : $value; + $sheet->getCellByColumnAndRow($c, $line)->setValue($value); + $style = $sheet->getStyleByColumnAndRow($c, $line); + $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); + $style->getAlignment()->setWrapText(true); + } + $c++; + } + } + + for ($i = 0; $i <= $columns; $i++) { + $sheet->getColumnDimensionByColumn($i)->setAutoSize(true); + } + + $tmpfile = Files::tempnam() . '.xlsx'; + $writer = new Xlsx($excel); + $writer->save($tmpfile); + + $url = route('download_settings', ['file' => base64_encode($tmpfile)]); + $notification = 'Cliquez sur ce lien pour télécharger l\'export des paramètres : '.$url.''; + + $user->notify(new ToolboxNotification("test", $notification, [], true)); + } +} -- 2.39.5