]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6248 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 29 Sep 2023 10:27:09 +0000 (12:27 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 29 Sep 2023 10:27:09 +0000 (12:27 +0200)
app/Fields/ElearningTranslatedString.php [new file with mode: 0644]
app/Fields/ElearningTranslationOverwrite.php [new file with mode: 0644]
app/Models/ElearningTranslate.php
app/SubForms/ElearningTranslationOverwrite.php [new file with mode: 0644]
resources/views/fields/elearning_translation_overwrite.blade.php [new file with mode: 0644]

diff --git a/app/Fields/ElearningTranslatedString.php b/app/Fields/ElearningTranslatedString.php
new file mode 100644 (file)
index 0000000..7a1a1fd
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Fields;
+
+use App\Models\ElearningTranslate;
+use App\Models\FluidbookTranslate;
+use Cubist\Backpack\Magic\Fields\SelectFromArray;
+use Cubist\Util\Str;
+
+class ElearningTranslatedString extends SelectFromArray
+{
+    protected static $__options = null;
+    protected $_ajax = false;
+    protected $_allowsHTML = true;
+
+    /**
+     * @throws \JsonException
+     */
+    public function getOptions()
+    {
+        if (null === self::$__options) {
+            self::$__options = $this->_getOptions();
+        }
+        return self::$__options;
+    }
+
+    protected function _getOptions()
+    {
+        $locale = 'en';
+
+        $translations = ElearningTranslate::getLocaleTranslations($locale);
+        $res = [];
+        foreach ($translations as $k => $translation) {
+            if ($k === 'k' || $k === 'nsis') {
+                continue;
+            }
+            $res[$translation['str']] = $translation['str'];
+        }
+        return $res;
+    }
+}
diff --git a/app/Fields/ElearningTranslationOverwrite.php b/app/Fields/ElearningTranslationOverwrite.php
new file mode 100644 (file)
index 0000000..6b94d94
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Fields;
+
+use Cubist\Backpack\Magic\Fields\KeyValueBunchOfFieldsMultiple;
+
+class ElearningTranslationOverwrite extends KeyValueBunchOfFieldsMultiple
+{
+    protected $_adminType = 'elearning_translation_overwrite';
+    protected $_viewNamespace = 'fields';
+
+    public function getDefaultAttributes()
+    {
+        return array_merge(parent::getDefaultAttributes(), ['bunch' => \App\SubForms\ElearningTranslationOverwrite::class, "add_label" => __('Nouvelle traduction')]);
+    }
+}
index b4285a782b6e2abd11a5eb173ebfa787e411585d..5d84a2803fae515a59eaba324f7a3ea618ead1c7 100644 (file)
@@ -27,5 +27,7 @@ class ElearningTranslate extends ToolboxContentTranslate
         'plural' => 'traductions',
         'oneinstance' => true];
 
+    protected static $_permissionBase = 'elearning-translate';
+
     protected $_operations = [ExcelExportOperation::class, ExcelImportOperation::class];
 }
diff --git a/app/SubForms/ElearningTranslationOverwrite.php b/app/SubForms/ElearningTranslationOverwrite.php
new file mode 100644 (file)
index 0000000..1327c64
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\SubForms;
+
+use App\Fields\ElearningTranslatedString;
+use Cubist\Backpack\Magic\Fields\Text;
+use Cubist\Backpack\Magic\SubForm;
+// __('!! Paramètres des fluidbooks')
+class ElearningTranslationOverwrite extends SubForm
+{
+    public function init()
+    {
+        parent::init();
+        $this->addField('key', ElearningTranslatedString::class, __('Texte original'));
+        $this->addField('value', Text::class, __('Nouvelle traduction'));
+    }
+}
diff --git a/resources/views/fields/elearning_translation_overwrite.blade.php b/resources/views/fields/elearning_translation_overwrite.blade.php
new file mode 100644 (file)
index 0000000..15b5462
--- /dev/null
@@ -0,0 +1,79 @@
+{{-- __('!! Paramètres des fluidbooks') --}}
+@if (!isset($seenTranslationOverwrite))
+    @php $seenTranslationOverwrite=true;@endphp
+    @push('crud_fields_scripts')
+        <script>
+            var allTranslations =@json(\App\Models\FluidbookTranslate::getAllTranslations());
+            var locale = $('select[name="locale"]').val();
+            $(document).on('change', 'select[name="locale"]', function () {
+                locale = $('select[name="locale"]').val()
+                updateTranslationOverwrite();
+            });
+
+            $(document).on('change', '[data-bunch-name="translations"] select', function () {
+                var text = $(this).closest('.bunchfields').find('input');
+                var selectVal = $(this).val();
+                if ($(text).val() === '') {
+                    var k = allTranslations['locale'];
+                    $.each(allTranslations[locale], function (k, t) {
+                        if (k === 'k') {
+                            return;
+                        }
+                        var str = window.atob(k.substring(2));
+                        if (selectVal === str) {
+                            $(text).val(t.translation);
+                            return true;
+                        }
+                    });
+                }
+            });
+
+            setTimeout(function () {
+                updateTranslationOverwrite();
+            }, 1000);
+
+            function updateTranslationOverwrite() {
+                function escapeStr(str) {
+                    if (str) {
+                        return str.replace(/([ #;?%&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
+                    }
+                    return str;
+                }
+
+
+                $('[data-bunch-name="translations"]').each(function () {
+                    $(this).find('.item').each(function () {
+                        var s = $(this).find('select');
+                        $.each(allTranslations[locale], function (k, t) {
+                            if (k === 'k') {
+                                return;
+                            }
+                            var str = window.atob(k.substring(2));
+                            if (k === 'nsis') {
+                                return;
+                            }
+                            var d = t.translation;
+                            if (t.translation !== str) {
+                                d += '<i style="font-size: 75%;display: block;margin-top: -5px;">' + str + '</i>';
+                            }
+                            $(s).find('option[value="' + escapeStr(str) + '"]').text(d);
+                        });
+                        $(s).removeClass('select2-hidden-accessible');
+                        $(this).find('.select2').remove();
+                    });
+                });
+                initUntriggeredSelectFromArray($);
+            }
+        </script>
+    @endpush
+
+    @push('crud_fields_styles')
+        <style>
+            [data-bunch-name="translations"] .select2-selection__rendered {
+                margin-top: -5px;
+            }
+        </style>
+    @endpush
+@endif
+@include('cubist_back::fields.bunch_keyvalue_multiple')
+