From: Vincent Vanwaelscappel Date: Wed, 6 Sep 2023 08:35:57 +0000 (+0200) Subject: wip #6248 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=ec8ff59d238141100bf534f760997495e8b41294;p=fluidbook-toolbox.git wip #6248 @0.5 --- diff --git a/app/Fields/FluidbookTranslatedString.php b/app/Fields/FluidbookTranslatedString.php index d93de4317..3082627b9 100644 --- a/app/Fields/FluidbookTranslatedString.php +++ b/app/Fields/FluidbookTranslatedString.php @@ -23,6 +23,9 @@ class FluidbookTranslatedString extends SelectFromArray return self::$__options; } + /** + * @throws \JsonException + */ protected function _getOptions() { $locale = 'en'; diff --git a/app/Http/Controllers/Admin/ElearningTranslateCrudController.php b/app/Http/Controllers/Admin/ElearningTranslateCrudController.php new file mode 100644 index 000000000..78c1833fc --- /dev/null +++ b/app/Http/Controllers/Admin/ElearningTranslateCrudController.php @@ -0,0 +1,30 @@ +getRawOriginal('content_translatable'), true); + + $t = $class::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, $class::getName() . '_translate_' . $locale . '.xlsx')->deleteFileAfterSend(); + } + + protected function _getTranslations($alldata, $locale) + { + if (isset($alldata[$locale])) { + $data = $alldata[$locale]; + } else { + $e = explode('_', $locale); + if (isset($alldata[$e[0]])) { + $data = $alldata[$e[0]]; + } else { + $data = $alldata['en']; + } + } + $res = []; + foreach ($data as $k => $v) { + if ($k === 'k') { + continue; + } + $res[$k] = $v; + } + return $res; + } +} diff --git a/app/Http/Controllers/Admin/Operations/ContentTranslate/ExcelImportOperation.php b/app/Http/Controllers/Admin/Operations/ContentTranslate/ExcelImportOperation.php new file mode 100644 index 000000000..0f37129ba --- /dev/null +++ b/app/Http/Controllers/Admin/Operations/ContentTranslate/ExcelImportOperation.php @@ -0,0 +1,73 @@ +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($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 { + 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/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php deleted file mode 100644 index 8b67bbb8a..000000000 --- a/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelExportOperation.php +++ /dev/null @@ -1,106 +0,0 @@ -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) - { - if (isset($alldata[$locale])) { - $data = $alldata[$locale]; - } else { - $e = explode('_', $locale); - if (isset($alldata[$e[0]])) { - $data = $alldata[$e[0]]; - } else { - $data = $alldata['en']; - } - } - $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 deleted file mode 100644 index 62fdbcff3..000000000 --- a/app/Http/Controllers/Admin/Operations/FluidbookTranslate/ExcelImportOperation.php +++ /dev/null @@ -1,73 +0,0 @@ -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($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 { - 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/Http/Controllers/Admin/TranslateCrudController.php b/app/Http/Controllers/Admin/TranslateCrudController.php new file mode 100644 index 000000000..9764a9c91 --- /dev/null +++ b/app/Http/Controllers/Admin/TranslateCrudController.php @@ -0,0 +1,29 @@ +_availableLocales = \Cubist\Locale\Locale::getList(App::getLocale()); + $this->addEditAction('content_translate.excel_export'); + $this->addEditAction('content_translate.excel_import'); + + parent::__construct($attributes); + } + + public static function getName() + { + return static::$_name; + } protected function _getLanguageFile($locale) { - return self::getLanguageFile($locale); + return static::getLanguageFile($locale); } public static function getLanguageFile($locale) @@ -59,12 +79,12 @@ class ToolboxContentTranslate extends Translate }); } - return self::$_allTranslations; + return static::$_allTranslations; } public static function getCompiledTranslations() { - $raw = self::getAllTranslations(); + $raw = static::getAllTranslations(); $res = []; foreach ($raw as $code => $data) { $res[$code] = []; @@ -97,13 +117,11 @@ class ToolboxContentTranslate extends Translate /** * @param string $locale * @return array[]|null - * @throws \JsonException */ - public static function getTranslation($locale) + public static function getLocaleTranslations($locale) { - $all = static::getAllTranslations(); - $res = $all[$locale] ?? null; - return $res; + $all = static::getAllTranslations(false); + return $all[$locale] ?? []; } } diff --git a/app/Models/ElearningTranslate.php b/app/Models/ElearningTranslate.php index 6a5655aab..6abe4c465 100644 --- a/app/Models/ElearningTranslate.php +++ b/app/Models/ElearningTranslate.php @@ -3,8 +3,8 @@ namespace App\Models; -use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelExportOperation; -use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelImportOperation; +use App\Http\Controllers\Admin\Operations\ContentTranslate\ExcelExportOperation; +use App\Http\Controllers\Admin\Operations\ContentTranslate\ExcelImportOperation; use App\Models\Base\ToolboxContentTranslate; use App\Models\Traits\QuizBranches; use Cubist\Backpack\Facades\App; @@ -17,7 +17,7 @@ class ElearningTranslate extends ToolboxContentTranslate use QuizBranches; protected $table = 'elearning_translate'; - protected static $_name='elearning'; + protected static string $_name='elearning'; protected $_enableRevisions = false; @@ -29,17 +29,6 @@ class ElearningTranslate extends ToolboxContentTranslate protected $_operations = [ExcelExportOperation::class, ExcelImportOperation::class]; - public function __construct(array $attributes = []) - { - PHP::neverStop(); - - $this->_availableLocales = \Cubist\Locale\Locale::getList(App::getLocale()); - $this->addEditAction('elearning_translate.excel_export'); - $this->addEditAction('elearning_translate.excel_import'); - - parent::__construct($attributes); - } - public function getPaths() { $res = []; diff --git a/app/Models/FluidbookTranslate.php b/app/Models/FluidbookTranslate.php index ea8bde0f7..34df1d4e5 100644 --- a/app/Models/FluidbookTranslate.php +++ b/app/Models/FluidbookTranslate.php @@ -4,8 +4,8 @@ namespace App\Models; use App\Fields\NSISLocale; -use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelExportOperation; -use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelImportOperation; +use App\Http\Controllers\Admin\Operations\ContentTranslate\ExcelExportOperation; +use App\Http\Controllers\Admin\Operations\ContentTranslate\ExcelImportOperation; use App\Models\Base\ToolboxContentTranslate; use App\Models\Traits\FluidbookPlayerBranches; use Cubist\Backpack\Facades\App; @@ -19,7 +19,7 @@ class FluidbookTranslate extends ToolboxContentTranslate use FluidbookPlayerBranches; protected $table = 'fluidbook_translate'; - protected static $_name = 'elearning'; + protected static string $_name = 'elearning'; protected static $_allTranslations = null; @@ -30,20 +30,6 @@ class FluidbookTranslate extends ToolboxContentTranslate 'plural' => 'traductions', 'oneinstance' => true]; - protected $_operations = [ExcelExportOperation::class, ExcelImportOperation::class]; - - - public function __construct(array $attributes = []) - { - PHP::neverStop(); - - $this->_availableLocales = \Cubist\Locale\Locale::getList(App::getLocale()); - $this->addEditAction('fluidbook_translate.excel_export'); - $this->addEditAction('fluidbook_translate.excel_import'); - - parent::__construct($attributes); - } - public function setFields() { $this->addField(['name' => 'nsis', 'type' => NSISLocale::class, 'label' => __('Langue de l\'installeur') . ' (' . __('Version offline windows') . ')', 'translatable' => true, 'default' => 'English']); @@ -67,7 +53,7 @@ class FluidbookTranslate extends ToolboxContentTranslate { return static::getAllTranslations($force); } - + /** * @param string $locale * @return array[]|null @@ -75,7 +61,7 @@ class FluidbookTranslate extends ToolboxContentTranslate */ public static function getFluidbookTranslation($locale) { - return static::getTranslation($locale); + return static::getLocaleTranslations($locale); } } diff --git a/resources/views/content_translate/excel_export.blade.php b/resources/views/content_translate/excel_export.blade.php new file mode 100644 index 000000000..5fef725cb --- /dev/null +++ b/resources/views/content_translate/excel_export.blade.php @@ -0,0 +1,5 @@ +{{-- __('!! Traduction des fluidbooks') --}} + {{__('Export Excel')}} + diff --git a/resources/views/content_translate/excel_import.blade.php b/resources/views/content_translate/excel_import.blade.php new file mode 100644 index 000000000..e0fee160d --- /dev/null +++ b/resources/views/content_translate/excel_import.blade.php @@ -0,0 +1,29 @@ +{{-- __('!! Traduction des fluidbooks') --}} +@push("after_form") + +@endpush + + + +@push('after_scripts') + +@endpush diff --git a/resources/views/fluidbook_translate/excel_export.blade.php b/resources/views/fluidbook_translate/excel_export.blade.php deleted file mode 100644 index 5fef725cb..000000000 --- a/resources/views/fluidbook_translate/excel_export.blade.php +++ /dev/null @@ -1,5 +0,0 @@ -{{-- __('!! Traduction des fluidbooks') --}} - {{__('Export Excel')}} - diff --git a/resources/views/fluidbook_translate/excel_import.blade.php b/resources/views/fluidbook_translate/excel_import.blade.php deleted file mode 100644 index e0fee160d..000000000 --- a/resources/views/fluidbook_translate/excel_import.blade.php +++ /dev/null @@ -1,29 +0,0 @@ -{{-- __('!! Traduction des fluidbooks') --}} -@push("after_form") - -@endpush - - - -@push('after_scripts') - -@endpush diff --git a/resources/views/vendor/backpack/base/inc/sidebar_content.blade.php b/resources/views/vendor/backpack/base/inc/sidebar_content.blade.php index c588e3fff..b94f683a7 100644 --- a/resources/views/vendor/backpack/base/inc/sidebar_content.blade.php +++ b/resources/views/vendor/backpack/base/inc/sidebar_content.blade.php @@ -73,10 +73,7 @@ @endcan - @can('quiztranslation:read') - - @endcan + @can('elearning-media:read') @endcan - + @can('quiztranslation:read') + + @endcan @endcanany