]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6568 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 11 Dec 2023 14:14:58 +0000 (15:14 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 11 Dec 2023 14:14:58 +0000 (15:14 +0100)
app/Http/Controllers/Admin/Operations/ContentTranslate/ExcelExportOperation.php
app/Models/Base/ToolboxBaseTranslate.php [new file with mode: 0644]
app/Models/Base/ToolboxContentTranslate.php
app/Models/ToolboxTranslate.php

index 01f147c02c656a72acdb9ec1ff7cd60cd7952f61..2fa581285469ef760e405795b2445fcc2fcacf40 100644 (file)
@@ -14,7 +14,6 @@ trait ExcelExportOperation
 {
     protected function setupExcelExportRoutes($segment, $routeName, $controller)
     {
-
         Route::match(['get'], $segment . '/excel/export/{locale}', $controller . '@excelExport');
     }
 
diff --git a/app/Models/Base/ToolboxBaseTranslate.php b/app/Models/Base/ToolboxBaseTranslate.php
new file mode 100644 (file)
index 0000000..cfc6f5c
--- /dev/null
@@ -0,0 +1,153 @@
+<?php
+
+namespace App\Models\Base;
+
+use App\Http\Controllers\Admin\Operations\ContentTranslate\ExcelExportOperation;
+use App\Http\Controllers\Admin\Operations\ContentTranslate\ExcelImportOperation;
+use Cubist\Backpack\Facades\App;
+use Cubist\Backpack\Magic\Models\Translate;
+use Cubist\Util\Files\Files;
+use Cubist\Util\PHP;
+use Illuminate\Support\Facades\Cache;
+
+class ToolboxBaseTranslate extends Translate
+{
+    protected static $_allTranslations = null;
+    protected $_operations = [ExcelExportOperation::class, ExcelImportOperation::class];
+
+    public function __construct(array $attributes = [])
+    {
+        PHP::neverStop();
+
+        $this->_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 static::getLanguageFile($locale);
+    }
+
+    public static function getLanguageFile($locale)
+    {
+        $n = '';
+        if (static::$_name) {
+            $n = static::$_name . '.';
+        }
+        return resource_path('lang/' . $n . $locale . '.json');
+    }
+
+    public function getExtensions()
+    {
+        $res = parent::getExtensions();
+        $res[] = 'js';
+        return $res;
+    }
+
+    protected static function _getCacheKey()
+    {
+        return 'all_' . Files::hashFileAttributes(__FILE__) . '_' . static::$_name . '_translations';
+    }
+
+    public static function getAllTranslations($force = true)
+    {
+        if (null === static::$_allTranslations) {
+
+
+            $cacheKey = static::_getCacheKey();
+            if ($force) {
+                Cache::forget($cacheKey);
+            }
+            static::$_allTranslations = Cache::remember($cacheKey, 3600, function () {
+                start_measure('Get all ' . static::$_name . ' translations !');
+                $t = static::find(1);
+                try {
+                    $json = json_decode($t->getRawOriginal('content_translatable'), true, 512, JSON_THROW_ON_ERROR);
+                } catch (\Exception $e) {
+                    $json = [];
+                }
+
+                $res = [];
+                foreach ($json as $code => $tr) {
+                    $res[$code] = [];
+                    foreach ($tr as $k => $v) {
+                        $res[$code][$k] = ['str' => static::keyToStr($k), 'translation' => $v];
+                    }
+                }
+                stop_measure('Get all ' . static::$_name . ' translations !');
+                return $res;
+
+            });
+        }
+        return static::$_allTranslations;
+    }
+
+    public static function getCompiledTranslations()
+    {
+        $raw = static::getAllTranslations();
+        $res = [];
+        foreach ($raw as $code => $data) {
+            $res[$code] = [];
+            foreach ($data as $k => $v) {
+                if (is_string($v)) {
+                    $res[$code][$k] = $v;
+                } else {
+                    $res[$code][$v['str']] = $v['translation'];
+                }
+            }
+        }
+        return $res;
+    }
+
+    /**
+     * @throws \JsonException
+     */
+    public static function updateTranslation($locale, $translations)
+    {
+        /** @var static $t */
+        $t = static::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();
+    }
+
+    public function onSaved(): bool
+    {
+        Cache::forget(static::_getCacheKey());
+        return parent::onSaved();
+    }
+
+    /**
+     * @param string $locale
+     * @return array[]|null
+     */
+    public static function getLocaleTranslations($locale, $compiled = false)
+    {
+        $all = static::getAllTranslations(false);
+        $res = $all[$locale] ?? [];
+        if (!$compiled) {
+            return $res;
+        }
+        $comp = [];
+        foreach ($res as $k => $v) {
+            if (is_string($v)) {
+                $comp[$k] = $v;
+            } else {
+                $comp[$v['str']] = $v['translation'];
+            }
+        }
+        return $comp;
+    }
+
+}
index f28d0835ecd120b57eb4b0d7fcef7fff7ad1f6f4..8d0b2b3b3ca11bf9256db051edfcb0abb426b1b5 100644 (file)
@@ -10,145 +10,12 @@ use Cubist\Util\Files\Files;
 use Cubist\Util\PHP;
 use Illuminate\Support\Facades\Cache;
 
-class ToolboxContentTranslate extends Translate
+class ToolboxContentTranslate extends ToolboxBaseTranslate
 {
 
-    protected static $_allTranslations = null;
 
     protected static $_basePath = '';
 
-    protected $_operations = [ExcelExportOperation::class, ExcelImportOperation::class];
-
-    public function __construct(array $attributes = [])
-    {
-        PHP::neverStop();
-
-        $this->_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 static::getLanguageFile($locale);
-    }
-
-    public static function getLanguageFile($locale)
-    {
-        return resource_path('lang/' . static::$_name . '.' . $locale . '.json');
-    }
-
-    public function getExtensions()
-    {
-        $res = parent::getExtensions();
-        $res[] = 'js';
-        return $res;
-    }
-
-    protected static function _getCacheKey()
-    {
-        return 'all_' . Files::hashFileAttributes(__FILE__) . '_' . static::$_name . '_translations';
-    }
-
-    public static function getAllTranslations($force = true)
-    {
-        if (null === static::$_allTranslations) {
-
-
-            $cacheKey = static::_getCacheKey();
-            if ($force) {
-                Cache::forget($cacheKey);
-            }
-            static::$_allTranslations = Cache::remember($cacheKey, 3600, function () {
-                start_measure('Get all ' . static::$_name . ' translations !');
-                $t = static::find(1);
-                try {
-                    $json = json_decode($t->getRawOriginal('content_translatable'), true, 512, JSON_THROW_ON_ERROR);
-                } catch (\Exception $e) {
-                    $json = [];
-                }
-
-                $res = [];
-                foreach ($json as $code => $tr) {
-                    $res[$code] = [];
-                    foreach ($tr as $k => $v) {
-                        $res[$code][$k] = ['str' => static::keyToStr($k), 'translation' => $v];
-                    }
-                }
-                stop_measure('Get all ' . static::$_name . ' translations !');
-                return $res;
-
-            });
-        }
-        return static::$_allTranslations;
-    }
-
-    public static function getCompiledTranslations()
-    {
-        $raw = static::getAllTranslations();
-        $res = [];
-        foreach ($raw as $code => $data) {
-            $res[$code] = [];
-            foreach ($data as $k => $v) {
-                if (is_string($v)) {
-                    $res[$code][$k] = $v;
-                } else {
-                    $res[$code][$v['str']] = $v['translation'];
-                }
-            }
-        }
-        return $res;
-    }
-
-    /**
-     * @throws \JsonException
-     */
-    public static function updateTranslation($locale, $translations)
-    {
-        /** @var static $t */
-        $t = static::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();
-    }
-
-    public function onSaved(): bool
-    {
-        Cache::forget(static::_getCacheKey());
-        return parent::onSaved();
-    }
-
-    /**
-     * @param string $locale
-     * @return array[]|null
-     */
-    public static function getLocaleTranslations($locale, $compiled = false)
-    {
-        $all = static::getAllTranslations(false);
-        $res = $all[$locale] ?? [];
-        if (!$compiled) {
-            return $res;
-        }
-        $comp = [];
-        foreach ($res as $k => $v) {
-            if (is_string($v)) {
-                $comp[$k] = $v;
-            } else {
-                $comp[$v['str']] = $v['translation'];
-            }
-        }
-        return $comp;
-    }
 
     public function getPaths()
     {
index 6052edde15e312e934fc0245e1807bccb43c4e1a..55eba677a31a21131dd63c088224e5b5ca2acfd1 100644 (file)
@@ -3,10 +3,9 @@
 
 namespace App\Models;
 
-use App\Models\Traits\FluidbookPlayerBranches;
-use Cubist\Backpack\Magic\Models\Translate;
+use App\Models\Base\ToolboxBaseTranslate;
 
-class ToolboxTranslate extends Translate
+class ToolboxTranslate extends ToolboxBaseTranslate
 {
     protected $table = 'toolbox_translate';
 
@@ -15,6 +14,7 @@ class ToolboxTranslate extends Translate
         'plural' => 'traductions',
         'oneinstance' => true];
 
+    protected static $_name='';
 
     protected $_enableTrackEmptyValues = true;