--- /dev/null
+<?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;
+ }
+}