--- /dev/null
- return redirect($this->crud->route . '/1/edit/?locale=' . $locale);
+<?php
+
+namespace App\Http\Controllers\Admin\Operations\ContentTranslate;
+
+use App\Models\FluidbookTranslate;
+use App\Models\Quiz;
+use App\Models\QuizTranslation;
+use Cubist\Util\Files\Files;
+use Illuminate\Support\Facades\Route;
+use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
+use PhpOffice\PhpSpreadsheet\RichText\RichText;
+use Prologue\Alerts\Facades\Alert;
+// __('!! Traduction de l\'interface fluidbook')
+trait ExcelImportOperation
+{
+ protected function setupExcelImportRoutes($segment, $routeName, $controller)
+ {
+ Route::match(['post'], $segment . '/excel/import/{locale}', $controller . '@excelImport');
+ }
+
+ /**
+ * @throws \JsonException
+ */
+ protected function excelImport($locale)
+ {
+ $file = $_FILES['file'];
+
+ if ($file['error']) {
+ Alert::warning('No file were imported')->flash();
+ return;
+ }
+
+ $reader = new Xlsx();
+ $xls = $reader->load($file['tmp_name']);
+ $sheet = $xls->getActiveSheet();
+
+ $class = get_class($this->getModelInstance());
+
+ $existingTranslation = $class::getLocaleTranslations($locale);
+
+
+ $translations = [];
+ $count = 0;
+ $updated = 0;
+ foreach ($sheet->getRowIterator(2) as $row) {
+ $ri = $row->getRowIndex();
+ $k = $sheet->getCellByColumnAndRow(1, $ri)->getValue();
+ if (!FluidbookTranslate::isKey($k)) {
+ continue;
+ }
+
+ $v = $sheet->getCellByColumnAndRow(4, $ri)->getValue();
+ if($v instanceof RichText){
+ $v=$v->getPlainText();
+ }
+
+ if (!isset($existingTranslation[$k]['translation']) || $v != $existingTranslation[$k]['translation']) {
+ $translations[$k] = $v;
+ $updated++;
+ }
+ $count++;
+ }
+
+
+ if (!$count) {
+ Alert::warning('No translation were find')->flash();
+ } elseif (!$updated) {
+ Alert::warning('No translation were updated')->flash();
+ } else {
+ $class::updateTranslation($locale, $translations);
+ Alert::success('<b>' . $updated . ' translations</b> were updated (' . $count . ' total)')->flash();
+ }
++ return redirect($this->crud->route . '/1/edit/?_locale=' . $locale);
+ }
+}