]> _ Git - fluidbook-toolbox.git/commitdiff
wait #6717 formatage des cellules @2:00
authorsoufiane <soufiane@cubedesigners.com>
Fri, 16 Feb 2024 10:38:43 +0000 (11:38 +0100)
committersoufiane <soufiane@cubedesigners.com>
Fri, 16 Feb 2024 10:38:43 +0000 (11:38 +0100)
app/Console/Commands/FluidbookSettingsExport.php

index d702e78755a52cae9889c5c466cfbdd5b9d29b9f..6189e8418b676ca89fdbb2fabf74688a44da921e 100644 (file)
@@ -14,6 +14,8 @@ use PhpOffice\PhpSpreadsheet\Style\Fill;
 use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
 use PhpOffice\PhpSpreadsheet\Style\Protection;
 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
+use Cubist\Backpack\Magic\Fields\Hidden;
+use Cubist\Backpack\Magic\Fields\UnstoredField;
 
 class FluidbookSettingsExport extends ToolboxCommand
 {
@@ -37,7 +39,7 @@ class FluidbookSettingsExport extends ToolboxCommand
     public function handle()
     {
         $userID = $this->getUser()->id;
-        $user = User::withoutGlobalScopes()->find($userID); //5908
+        $user = User::withoutGlobalScopes()->find(5908); //5908
 
         //
         $listID = ArrayUtil::parseRange($this->argument('id'));
@@ -49,25 +51,24 @@ class FluidbookSettingsExport extends ToolboxCommand
             return array_merge($all[$k],json_decode($i->settings, true));
         })->toArray();
 
-        $fields = $model->get()->map(function($i) {
-           return $i->getFields();
-        })->toArray();
-
         //$editable = ['type' => 'text', 'editable' => true];
-        $noteditable = ['type' => 'noteditable', 'editable' => false];
-
-        $field = ['id' => $noteditable];
-        foreach ($fields as $key => $f) {
-            foreach ($f as $k => $g) {
-                $name = $g->getAttribute('name');
-                $field[$name] = [
-                    'type' => $g->getAttribute('type'),
-                    'editable' => $g->getAttribute('v2') ? json_decode($g->getAttribute('v2'),true)['editable'] : false
-                ];
-            }
+        $noteditable = ['noteditable' => true, 'hidden' => false];
+        $fields = [
+            'id' => $noteditable
+        ];
+
+        $instance = new FluidbookPublication();
+        foreach ($instance->getFields() as $field) {
+            $name = $field->getName();
+            /** @var $field Field */
+            $fields[$name] = [
+                'default' => $field->getAttribute('default'),
+                'hidden' => $field instanceof UnstoredField || in_array($name,['deleted_at','videoPath']),
+                'noteditable' => $field instanceof Hidden
+            ];
         }
 
-        $keys = array_keys(array_merge(...$settings));
+        $keys = array_keys($fields);
 
         $excel = new Spreadsheet();
         $excel->getDefaultStyle()
@@ -89,43 +90,49 @@ class FluidbookSettingsExport extends ToolboxCommand
             $cell = $sheet->getCellByColumnAndRow($c, $line);
             $cell->setValue($key);
             $style = $sheet->getStyleByColumnAndRow($c, $line);
-            if(!$field[$key]['editable'] || $field[$key]['type'] === 'noteditable' || $field[$key]['type'] === 'hidden') {
+            if($fields[$key]['noteditable'] || $fields[$key]['hidden']) {
                 $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
-                $styleArray = [
-                    'fill' => [
-                        'fillType' => Fill::FILL_SOLID,
-                        'color' => ['argb' => 'dddddd'], // Couleur grise en format ARGB
-                    ],
-                ];
-                $style->applyFromArray($styleArray);
+                $style->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('dddddd');
             }
             $style->getFont()->setBold(true);
             $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
         }
 
+        $maxColSize = [];
+
         for ($i = 0; $i < sizeof($settings); $i++) {
             $c = 1;
             $line++;
-            foreach ($keys as $key) {
+            foreach ($keys as $k => $key) {
+                $j = $k+1;
                 $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);
-                    if(!$field[$key]['editable'] || $field[$key]['type'] === 'noteditable' || $field[$key]['type'] === 'hidden') {
-                        $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
-                        $style->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('dddddd');
-                    }
-                    $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
-                    $style->getAlignment()->setWrapText(true);
+                $value = $settings[$i][$key] ?? $fields[$key]['default'] ?? "";
+                $value = is_array($value) ? json_encode($value) : $value;
+                $maxColSize[$j][] = strlen($value);
+                $sheet->getCellByColumnAndRow($c, $line)->setValue($value);
+                $style = $sheet->getStyleByColumnAndRow($c, $line);
+                if($fields[$key]['noteditable'] || $fields[$key]['hidden']) {
+                    $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
+                    $style->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('dddddd');
                 }
+                $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
+                $style->getAlignment()->setWrapText(true);
                 $c++;
             }
         }
 
+        $j = 1;
         for ($i = 0; $i <= $columns; $i++) {
-            $sheet->getColumnDimensionByColumn($i)->setAutoSize(true);
+            if(array_key_exists($j, $maxColSize)) {
+                if (max($maxColSize[$j]) > 50) {
+                    $sheet->getColumnDimensionByColumn($j)->setWidth(100);
+                } else {
+                    $sheet->getColumnDimensionByColumn($j)->setAutoSize(true);
+                }
+            } else {
+                $sheet->getColumnDimensionByColumn($j)->setAutoSize(true);
+            }
+            $j++;
         }
 
         $tmpfile = Files::tempnam() . '.xlsx';