]> _ Git - fluidbook-toolbox.git/commitdiff
wait #5672
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 9 Jan 2023 17:15:49 +0000 (18:15 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 9 Jan 2023 17:15:49 +0000 (18:15 +0100)
app/Http/Controllers/Admin/Operations/Tools/Excel2JSON.php

index 0079eec50a270813451e8734d8da190b01aee0ed..59fbbf795a2f85e106699ef5b8844c2a0fc45092 100644 (file)
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin\Operations\Tools;
 
 use Cubist\Backpack\Magic\Fields\SelectFromArray;
 use Cubist\Backpack\Magic\Fields\StandardFile;
+use Cubist\Backpack\Magic\Fields\Text;
 use Cubist\Backpack\Magic\Form;
 use Cubist\Excel\ExcelToArray;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -16,7 +17,7 @@ trait Excel2JSON
     {
         return [
             'excelToArray' => ['label' => __('Conforme à la source'), 'function' => 'excelToArrayRaw'],
-            'excelToArrayFirstSheet' => ['label' => __('Conforme à la source') . ' (' . __('Première feuille uniquement') . ')', 'function' => 'excelToArrayFirstSheet'],
+            'excelToArrayFirstSheet' => ['label' => __('Conforme à la source') . ' (' . __('Une feuille uniquement') . ')', 'function' => 'excelToArrayFirstSheet'],
             'excelToArrayKeyVars' => ['label' => __('Liste nommée par le titre de colonne') . ' (' . __('Clés = valeur de la colonne A') . ')'],
             'excelToArrayIndexKeyVars' => ['label' => __('Liste nommée par le titre de colonne') . ' (' . __('Clés = nombres séquentiels') . ')'],
             'excelToArrayKeyVal' => ['label' => __('Couples clé (colonne A) / valeur (colonne B)')],
@@ -39,6 +40,7 @@ trait Excel2JSON
         }
 
         $form->addField('dataorg', SelectFromArray::class, __('Type'), ['allows_null' => false, 'default' => 'excelToArray', 'options' => $options]);
+        $form->addField('sheet', Text::class, __('Feuille'), ['hint' => __('Laisser vide pour utiliser la première feuille'), 'when' => ['dataorg' => ['excelToArrayFirstSheet', 'excelToArrayIndexKeyVars', 'excelToArrayIndexKeyVars', 'excelToArrayKeyVal', 'excelToArrayKeyValMulti']]]);
         $form->addField('format', SelectFromArray::class, __('Format de sortie'), ['allows_null' => false, 'default' => 'json', 'options' =>
                 [
                     'json' => 'JSON',
@@ -46,6 +48,7 @@ trait Excel2JSON
                 ]
             ]
         );
+        $form->addField('varname', Text::class, __('Nom de la variable'), ['when' => ['format' => 'js'], 'default' => 'DATA']);
         return view('tools.form', ['form' => $form]);
     }
 
@@ -64,13 +67,13 @@ trait Excel2JSON
         }
         $function = $dataOrgs[$dataorg]['function'] ?? $dataorg;
 
-        $res = ExcelToArray::$function($file->getPathname());
+        $res = ExcelToArray::$function($file->getPathname(), request()->input('sheet', ''));
 
         if ($format === 'json') {
             return response()->json($res);
         }
         if ($format === 'js') {
-            return response('var DATA=' . json_encode($res) . ';')->header('Content-Type', 'text/plain');
+            return response('var ' . request()->input('varname', 'DATA') . '=' . json_encode($res) . ';')->header('Content-Type', 'text/plain');
         }
     }
 }