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)
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);
}
}
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());
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);
+ }
}
-<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>
-<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