]> _ Git - fluidbook-toolbox.git/commitdiff
wait #4627 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 29 Jul 2021 09:46:02 +0000 (11:46 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 29 Jul 2021 09:46:02 +0000 (11:46 +0200)
app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php
app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelImportOperation.php
app/Models/FluidbookTranslate.php
resources/views/fluidbook_translate/excel_export.blade.php
resources/views/fluidbook_translate/excel_import.blade.php

index 292b862be728a0bf6757e64141f49b83eeee0895..f865e988a9fa87763e73a283fff328373d6c1855 100644 (file)
@@ -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)
index 5b9992cf2594f46030a1e7956862b431ea50886f..50b49b48ac54d2ecb4f11650ed59240ba1da8556 100644 (file)
@@ -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('<b>' . $updated . ' translations</b> were updated (' . $count . ' total)')->flash();
+        }
+        return redirect($this->crud->route . '/1/edit/?locale=' . $locale);
     }
 }
index b975a4060de6c39f021212292df217f57155caec..1e2362e21b2b331b47c682ce896ad60b7d9ae529 100644 (file)
@@ -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);
+    }
 }
index 4fae68cdb9153c4fd2b5f9f8d996501a849c6b18..28bd093d8ad862c15a885cd68a742a7a53a1d5fc 100644 (file)
@@ -1,2 +1,2 @@
-<a href="/fluidbook-translate/excel/{{request()->get('locale')}}" class="btn btn-outline-notice"><span
+<a href="{{$crud->route}}/excel/export/{{request()->get('locale')}}" class="btn btn-outline-notice"><span
         class="la la-file-excel"></span> {{__('Export Excel')}}</a>
index 659b894ce90904402eed76e6fc0d3cf3bcba9388..f7573b2852a8fc8972e9ea5695825c3640c7fc98 100644 (file)
@@ -1 +1,28 @@
-<button class="btn btn-outline-notice"><span class="la la-file-excel"></span> {{__('Import Excel')}}</button>
+@push("after_form")
+<form method="post" style="visibility: hidden;width: 0;height: 0;" enctype="multipart/form-data" action="{{$crud->route}}/excel/import/{{request()->get('locale')}}"
+      style="visibility:hidden;height:1px;position:absolute;top:0;" id="uploadimportform">
+    @csrf
+    <input type="file" name="file" id="uploadimport"
+           accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
+</form>
+@endpush
+
+<button class="btn btn-outline-notice" id="uploadimportbutton"><span
+        class="la la-file-excel"></span> {{__('Import Excel')}}</button>
+
+@push('after_scripts')
+    <script>
+        (function ($) {
+            $(function () {
+                $(document).on('click', "#uploadimportbutton", function () {
+                    $("#uploadimport").click();
+                    return false;
+                });
+
+                $(document).on('change', '#uploadimportform', function () {
+                    $("#uploadimportform").submit();
+                })
+            });
+        })(jQuery);
+    </script>
+@endpush