]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6717 @0:20
authorsoufiane <soufiane@cubedesigners.com>
Mon, 12 Feb 2024 15:03:19 +0000 (16:03 +0100)
committersoufiane <soufiane@cubedesigners.com>
Mon, 12 Feb 2024 15:03:19 +0000 (16:03 +0100)
app/Console/Commands/FluidbookSettingsExport.php [new file with mode: 0644]

diff --git a/app/Console/Commands/FluidbookSettingsExport.php b/app/Console/Commands/FluidbookSettingsExport.php
new file mode 100644 (file)
index 0000000..4d2de1f
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Console\Commands\Base\ToolboxCommand;
+use App\Models\FluidbookPublication;
+use App\Models\User;
+use App\Notifications\ToolboxNotification;
+use Cubist\Util\ArrayUtil;
+use Cubist\Util\Files\Files;
+use Illuminate\Support\Facades\Mail;
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Style\Alignment;
+use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
+use App\Mail\Base;
+
+class FluidbookSettingsExport extends ToolboxCommand
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'fluidbook:settings:export {id}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Export fluidbook settings to excel';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle()
+    {
+        $userID = $this->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 : <a href='.$url.' rel="noopener">'.$url.'</a>';
+
+        $user->notify(new ToolboxNotification("test", $notification, [], true));
+    }
+}