]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5285 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 1 Jun 2022 15:56:47 +0000 (17:56 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 1 Jun 2022 15:56:47 +0000 (17:56 +0200)
app/Fields/FluidbookTranslatedString.php
app/Fields/FluidbookTranslationOverwrite.php [new file with mode: 0644]
app/Models/FluidbookPublication.php
app/SubForms/FluidbookTranslationOverwrite.php
resources/views/fields/fluidbook_translation_overwrite.blade.php [new file with mode: 0644]

index 1f9e5d26ae571c3a98bc4c31b98ccb8a7cf9fbfb..d93de4317926d2ce9808cb352006f6c83904b3a5 100644 (file)
@@ -9,6 +9,8 @@ use Cubist\Util\Str;
 class FluidbookTranslatedString extends SelectFromArray
 {
     protected static $__options = null;
+    protected $_ajax = false;
+    protected $_allowsHTML = true;
 
     /**
      * @throws \JsonException
@@ -31,13 +33,7 @@ class FluidbookTranslatedString extends SelectFromArray
             if ($k === 'k' || $k === 'nsis') {
                 continue;
             }
-            if (!$translation['translation']) {
-                $res[$translation['str']] = $translation['str'];
-            } else if (Str::lower($translation['translation']) == Str::lower($translation['str'])) {
-                $res[$translation['str']] = $translation['translation'];
-            } else {
-                $res[$translation['str']] = $translation['translation'] . ' // ' . $translation['str'];
-            }
+            $res[$translation['str']] = $translation['str'];
         }
         return $res;
     }
diff --git a/app/Fields/FluidbookTranslationOverwrite.php b/app/Fields/FluidbookTranslationOverwrite.php
new file mode 100644 (file)
index 0000000..d4b5e5d
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Fields;
+
+use Cubist\Backpack\Magic\Fields\Field;
+use Cubist\Backpack\Magic\Fields\KeyValueBunchOfFieldsMultiple;
+
+class FluidbookTranslationOverwrite extends KeyValueBunchOfFieldsMultiple
+{
+    protected $_adminType = 'fluidbook_translation_overwrite';
+    protected $_viewNamespace='fields';
+
+    public function getDefaultAttributes()
+    {
+        return array_merge(parent::getDefaultAttributes(), ['bunch' => \App\SubForms\FluidbookTranslationOverwrite::class, "add_label" => __('Nouvelle traduction')]);
+    }
+}
index ec342d06839c226420790212c12f481c8446e24e..1670dada4541523c90cbded97627d82b96168573 100644 (file)
@@ -62,7 +62,7 @@ class FluidbookPublication extends ToolboxModel
 
         $this->addField('section_locale', FormBigSection::class, __('Langue'));
         $this->addField('locale', FluidbookLocale::class, __('Langue principale'), ['default' => 'fr', 'allows_null' => false, 'filter' => true, 'filter_label' => 'Langue', 'column' => true, 'column_label' => '<i class="la la-language"></i>']);
-        //$this->addFieldOnFill('translations', KeyValueBunchOfFieldsMultiple::class, __('Traductions personnalisées'), ['bunch' => FluidbookTranslationOverwrite::class, 'add_label' => __('Nouvelle traduction'),]);
+        $this->addField('translations', \App\Fields\FluidbookTranslationOverwrite::class, __('Traductions personnalisées'));
 
         $this->addField('section_chapters', FormBigSection::class, __('Sommaire'));
         $this->addField('chapters', Textarea::class, __('Sommaire'));
index e361fc5f53c7cd90927b66063846f2b5469d6464..9bdbbc03d89edc98e16c3bee324f5eef73e471a1 100644 (file)
@@ -9,20 +9,10 @@ use Cubist\Backpack\Magic\SubForm;
 
 class FluidbookTranslationOverwrite extends SubForm
 {
-
-    /**
-     * @param $model CubistMagicAbstractModel
-     * @return void
-     */
-    public function setModelInstance($model)
-    {
-        $this->addField('key', FluidbookTranslatedString::class, __('Texte original'), ['locale' => $model->locale]);
-        $this->addField('value', Text::class, __('Nouvelle traduction'));
-    }
-
     public function init()
     {
         parent::init();
-
+        $this->addField('key', FluidbookTranslatedString::class, __('Texte original'));
+        $this->addField('value', Text::class, __('Nouvelle traduction'));
     }
 }
diff --git a/resources/views/fields/fluidbook_translation_overwrite.blade.php b/resources/views/fields/fluidbook_translation_overwrite.blade.php
new file mode 100644 (file)
index 0000000..6c349fa
--- /dev/null
@@ -0,0 +1,79 @@
+@if (!isset($seenTranslationOverwrite))
+    @php $seenTranslationOverwrite=true;@endphp
+    @push('crud_fields_scripts')
+        <script>
+            var allTranslations =@json(\App\Models\FluidbookTranslate::getAllFluidbookTranslations());
+            var locale = $('select[name="locale"]').val();
+            $(document).on('change', 'select[name="locale"]', function () {
+                console.log(locale);
+                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')
+