]> _ Git - fluidbook-toolbox.git/commitdiff
wait #4626 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 28 Jul 2021 17:27:15 +0000 (19:27 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 28 Jul 2021 17:27:15 +0000 (19:27 +0200)
app/Http/Controllers/Admin/Base/FluidbookTranslateController.php [new file with mode: 0644]
app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php [new file with mode: 0644]
app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelImportOperation.php [new file with mode: 0644]
app/Models/FluidbookTranslate.php
resources/views/fluidbook_translate/excel_export.blade.php

diff --git a/app/Http/Controllers/Admin/Base/FluidbookTranslateController.php b/app/Http/Controllers/Admin/Base/FluidbookTranslateController.php
new file mode 100644 (file)
index 0000000..0d11080
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Http\Controllers\Admin\Base;
+
+use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelExportOperation;
+use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelImportOperation;
+use Cubist\Backpack\Magic\Controllers\CubistMagicController;
+
+class FluidbookTranslateController extends CubistMagicController
+{
+    use ExcelExportOperation;
+    use ExcelImportOperation;
+}
diff --git a/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php
new file mode 100644 (file)
index 0000000..292b862
--- /dev/null
@@ -0,0 +1,97 @@
+<?php
+
+namespace App\Http\Controllers\Admin\Operations\FluidbookTranslate;
+
+use App\Models\FluidbookTranslate;
+use Cubist\Util\Files\Files;
+use Illuminate\Support\Facades\Route;
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Style\Protection;
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
+
+trait ExcelExportOperation
+{
+    protected function setupExcelExportRoutes($segment, $routeName, $controller)
+    {
+
+        Route::match(['get'], $segment . '/excel/{locale}', $controller . '@excelExport');
+    }
+
+    protected function excelExport($locale)
+    {
+        $alldata = json_decode(FluidbookTranslate::find(1)->getRawOriginal('content_translatable'), true);
+
+        $t = FluidbookTranslate::find(1);
+        if ($locale === 'en') {
+            $ref = 'fr';
+        } else {
+            $ref = 'en';
+        }
+
+        $tref = $this->_getTranslations($alldata, $ref);
+        $tr = $this->_getTranslations($alldata, $locale);
+
+        $xls = new Spreadsheet();
+
+        $worksheet = $xls->getActiveSheet();
+        $worksheet->setTitle($locale);
+
+        $worksheet->getProtection()->setSheet(true);
+        $xls->getDefaultStyle()->getProtection()->setLocked(false);
+
+        $worksheet->setCellValueByColumnAndRow(1, 1, 'ID');
+        $worksheet->setCellValueByColumnAndRow(2, 1, 'Reference string');
+        $worksheet->setCellValueByColumnAndRow(3, 1, 'Reference translation');
+        $worksheet->setCellValueByColumnAndRow(4, 1, 'Translation');
+        for ($i = 2; $i <= 4; $i++) {
+            $style = $worksheet->getStyleByColumnAndRow($i, 1);
+            $style->getAlignment()->setWrapText(true);
+            $style->getFont()->setBold(true);
+            $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
+        }
+
+        $row = 2;
+        foreach ($tref as $k => $v) {
+            $str = base64_decode(explode('t_', $k)[1]);
+            $worksheet->setCellValueByColumnAndRow(1, $row, $k);
+            $worksheet->setCellValueByColumnAndRow(2, $row, $str);
+            $worksheet->setCellValueByColumnAndRow(3, $row, $v);
+
+            for ($i = 1; $i <= 3; $i++) {
+                $style = $worksheet->getStyleByColumnAndRow($i, $row);
+                $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
+            }
+            for ($i = 2; $i <= 4; $i++) {
+                $style = $worksheet->getStyleByColumnAndRow($i, $row);
+                $style->getAlignment()->setWrapText(true);
+            }
+            $worksheet->setCellValueByColumnAndRow(4, $row, $tr[$k] ?? '');
+            $row++;
+        }
+
+        $width = 100;
+        $worksheet->getColumnDimensionByColumn(1)->setVisible(false);
+        $worksheet->getColumnDimensionByColumn(2)->setWidth($width);
+        $worksheet->getColumnDimensionByColumn(3)->setWidth($width);
+        $worksheet->getColumnDimensionByColumn(4)->setWidth($width);
+
+        $writer = new Xlsx($xls);
+        $tmp = Files::tempnam();
+        $writer->save($tmp);
+
+        return response()->download($tmp, 'fluidbook_translate_' . $locale . '.xlsx')->deleteFileAfterSend();
+    }
+
+    protected function _getTranslations($alldata, $locale)
+    {
+        $data = $alldata[$locale];
+        $res = [];
+        foreach ($data as $k => $v) {
+            if ($k === 'k') {
+                continue;
+            }
+            $res[$k] = $v;
+        }
+        return $res;
+    }
+}
diff --git a/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelImportOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelImportOperation.php
new file mode 100644 (file)
index 0000000..5b9992c
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Http\Controllers\Admin\Operations\FluidbookTranslate;
+
+use Illuminate\Support\Facades\Route;
+
+trait ExcelImportOperation
+{
+    protected function setupExcelImportRoutes($segment, $routeName, $controller)
+    {
+        Route::match(['post'], $segment . '/excel', $controller . '@excelImport');
+    }
+
+    protected function excelImport(){
+
+    }
+}
index 98c61d2f54e9f3053308275afd782229cba18909..b975a4060de6c39f021212292df217f57155caec 100644 (file)
@@ -4,11 +4,14 @@
 namespace App\Models;
 
 use App\Fields\NSISLocale;
+use App\Http\Controllers\Admin\Base\FluidbookTranslateController;
 use Cubist\Backpack\Facades\App;
 use Cubist\Backpack\Magic\Models\Translate;
 
 class FluidbookTranslate extends Translate
 {
+    protected $_baseController = FluidbookTranslateController::class;
+
     protected $table = 'fluidbook_translate';
 
     protected $_options = ['name' => 'fluidbook-translate',
index 684bb7fa5a6a614d255a3e936602c0a2b4ad90e6..4fae68cdb9153c4fd2b5f9f8d996501a849c6b18 100644 (file)
@@ -1,2 +1,2 @@
-<a href="/fluidbook-translate/excel?locale={{request()->get('locale')}}" class="btn btn-outline-notice"><span
+<a href="/fluidbook-translate/excel/{{request()->get('locale')}}" class="btn btn-outline-notice"><span
         class="la la-file-excel"></span> {{__('Export Excel')}}</a>