From f7a792ab9d6e53b134f4c3b64f51283b19d37449 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 29 Jul 2021 11:46:02 +0200 Subject: [PATCH] wait #4627 @1.5 --- .../ExcelExportOperation.php | 2 +- .../ExcelImportOperation.php | 55 +++++++++++- app/Models/FluidbookTranslate.php | 86 ++++++++++++++++++- .../excel_export.blade.php | 2 +- .../excel_import.blade.php | 29 ++++++- 5 files changed, 167 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php index 292b862be..f865e988a 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php @@ -14,7 +14,7 @@ trait ExcelExportOperation protected function setupExcelExportRoutes($segment, $routeName, $controller) { - Route::match(['get'], $segment . '/excel/{locale}', $controller . '@excelExport'); + Route::match(['get'], $segment . '/excel/export/{locale}', $controller . '@excelExport'); } protected function excelExport($locale) diff --git a/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelImportOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelImportOperation.php index 5b9992cf2..50b49b48a 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelImportOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelImportOperation.php @@ -2,16 +2,67 @@ namespace App\Http\Controllers\Admin\Operations\FluidbookTranslate; +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 Prologue\Alerts\Facades\Alert; trait ExcelImportOperation { protected function setupExcelImportRoutes($segment, $routeName, $controller) { - Route::match(['post'], $segment . '/excel', $controller . '@excelImport'); + Route::match(['post'], $segment . '/excel/import/{locale}', $controller . '@excelImport'); } - protected function 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(); + + $existingTranslation = FluidbookTranslate::getFluidbookTranslation($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 (!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 { + FluidbookTranslate::updateFluidbookTranslation($locale, $translations); + Alert::success('' . $updated . ' translations were updated (' . $count . ' total)')->flash(); + } + return redirect($this->crud->route . '/1/edit/?locale=' . $locale); } } diff --git a/app/Models/FluidbookTranslate.php b/app/Models/FluidbookTranslate.php index b975a4060..1e2362e21 100644 --- a/app/Models/FluidbookTranslate.php +++ b/app/Models/FluidbookTranslate.php @@ -14,11 +14,16 @@ class FluidbookTranslate extends Translate protected $table = 'fluidbook_translate'; + protected static $_allTranslations = null; + + protected $_enableRevisions = false; + protected $_options = ['name' => 'fluidbook-translate', 'singular' => 'traduction', 'plural' => 'traductions', 'oneinstance' => true]; + public function __construct(array $attributes = []) { $this->_availableLocales = \Cubist\Locale\Locale::getList(App::getLocale()); @@ -46,10 +51,87 @@ class FluidbookTranslate extends Translate protected function _getLanguageFile($locale) { - return self::getLanguageFile($locale); + return self::getLanguageFile($locale); } - public static function getLanguageFile($locale){ + public static function getLanguageFile($locale) + { return resource_path('lang/fluidbook.' . $locale . '.json'); } + + /** + * @throws \JsonException + */ + public static function getAllFluidbookTranslations() + { + if (null === self::$_allTranslations) { + $t = FluidbookTranslate::find(1); + $json = json_decode($t->getRawOriginal('content_translatable'), true, 512, JSON_THROW_ON_ERROR); + + self::$_allTranslations = []; + + foreach ($json as $code => $tr) { + $res[$code] = []; + foreach ($tr as $k => $v) { + self::$_allTranslations[$code][$k] = ['str' => self::keyToStr($k), 'translation' => $v]; + } + } + + $nsis = json_decode($t->getRawOriginal('nsis'), true, 512, JSON_THROW_ON_ERROR); + foreach ($nsis as $code => $v) { + self::$_allTranslations[$code]['nsis'] = $v; + } + } + return self::$_allTranslations; + } + + /** + * @throws \JsonException + */ + public static function updateFluidbookTranslation($locale, $translations) + { + /** @var FluidbookTranslate $t */ + $t = self::find(1); + $json = json_decode($t->getRawOriginal('content_translatable'), true, 512, JSON_THROW_ON_ERROR); + foreach ($translations as $k => $v) { + $json[$locale][$k] = $v; + } + $t->setRawAttributes(['content_translatable' => json_encode($json, JSON_THROW_ON_ERROR)]); + $t->save(); + } + + /** + * @param string $locale + * @return array[]|null + * @throws \JsonException + */ + public static function getFluidbookTranslation($locale) + { + $all = self::getAllFluidbookTranslations(); + return $all[$locale] ?? null; + } + + /** + * @param $key string + * @return string + */ + public static function keyToStr($key) + { + if (self::isKey($key)) { + return base64_decode(substr($key, 2)); + } + return $key; + } + + /** + * @param $key string + * @return bool + */ + public static function isKey($key) + { + if (!$key) { + return false; + } + return (bool)preg_match("/^t_[a-zA-Z0-9\/\+]*={0,2}$/", $key); + } } diff --git a/resources/views/fluidbook_translate/excel_export.blade.php b/resources/views/fluidbook_translate/excel_export.blade.php index 4fae68cdb..28bd093d8 100644 --- a/resources/views/fluidbook_translate/excel_export.blade.php +++ b/resources/views/fluidbook_translate/excel_export.blade.php @@ -1,2 +1,2 @@ - {{__('Export Excel')}} diff --git a/resources/views/fluidbook_translate/excel_import.blade.php b/resources/views/fluidbook_translate/excel_import.blade.php index 659b894ce..f7573b285 100644 --- a/resources/views/fluidbook_translate/excel_import.blade.php +++ b/resources/views/fluidbook_translate/excel_import.blade.php @@ -1 +1,28 @@ - +@push("after_form") + +@endpush + + + +@push('after_scripts') + +@endpush -- 2.39.5