]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6946 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 5 Jun 2024 15:58:07 +0000 (17:58 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 5 Jun 2024 15:58:07 +0000 (17:58 +0200)
app/Fields/Webflow/WebflowPage.php [new file with mode: 0644]
app/Models/ToolWebflow.php
app/SubForms/Webflow/Redirection.php [new file with mode: 0644]

diff --git a/app/Fields/Webflow/WebflowPage.php b/app/Fields/Webflow/WebflowPage.php
new file mode 100644 (file)
index 0000000..ff8f36d
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Fields\Webflow;
+
+use Cubist\Backpack\Magic\Fields\SelectFromArray;
+
+class WebflowPage extends SelectFromArray
+{
+
+}
index 0fe896eaed2336bc7ec3ecaa23ac6135bb0dc139..cc7037949e63d23678cbca4545e4477d1757b079 100644 (file)
@@ -9,6 +9,7 @@ use App\Http\Controllers\Admin\Operations\Tools\WebflowOperation;
 use App\Jobs\WebflowPublish;
 use App\Models\Base\ToolboxTranslatableModel;
 use App\Services\Webflow;
+use App\SubForms\Webflow\Redirection;
 use App\SubForms\Webflow\SEOPage;
 use App\SubForms\Webflow\WebflowText;
 use Cubist\Azure\Translate\Api;
@@ -61,6 +62,7 @@ class ToolWebflow extends ToolboxTranslatableModel
         $this->addField('texts', WebflowTexts::class, '', ['tab' => __('Textes'), 'translatable' => true, 'hint' => __('Modifier un texte ici ne produira aucun changement sur webflow')]);
         $this->addField('images', WebflowImages::class, '', ['tab' => __('Images'), 'translatable' => true]);
         $this->addField('seo', BunchOfFieldsMultiple::class, '', ['translatable' => true, 'edit_label' => '%url | %seo_title', 'allows_add' => false, 'allows_delete' => false, 'allows_clone' => false, 'allows_reorder' => false, 'bunch' => SEOPage::class, 'tab' => __('SEO')]);
+        $this->addField('redirections', BunchOfFieldsMultiple::class, '', ['translatable' => false, 'bunch' => Redirection::class, 'tab' => __('Redirections')]);
         $this->addField('api', Hidden::class);
     }
 
@@ -177,26 +179,42 @@ class ToolWebflow extends ToolboxTranslatableModel
             $mainLocale = $this->getMainLocale();
             $locales = $this->getLocalesCodes();
 
-            $props = ['seo'];
-            foreach ($props as $prop) {
-                $data = Json::decode($this->$prop, Json::TYPE_ARRAY);
-                $data[$mainLocale] = $this->api[$prop];
-                foreach ($locales as $locale) {
-                    if ($locale === $mainLocale) {
-                        continue;
-                    }
-                    if (!isset($data[$locale])) {
-                        $data[$locale] = [];
+            Api::setCache(Files::mkdir(protected_path('cache/azure_translate')));
+            $translateAPI = new Api(env('AZURE_TRANSLATE_API_KEY'));
+
+            $data = Json::decode($this->seo, Json::TYPE_ARRAY);
+            $data[$mainLocale] = $this->api['seo'];
+            $seoTranslateProps = ['og_title', 'og_description', 'seo_title', 'seo_description'];
+            foreach ($locales as $locale) {
+                if ($locale === $mainLocale) {
+                    continue;
+                }
+                if (!isset($data[$locale])) {
+                    $data[$locale] = [];
+                }
+                foreach ($data[$mainLocale] as $d) {
+                    $found = false;
+                    foreach ($data[$locale] as $l) {
+                        if ($d['id'] == $l['id']) {
+                            $found = $l;
+                            break;
+                        }
                     }
-                    foreach ($data[$mainLocale] as $d) {
-                        $data[$locale][] = $d;
+                    if (!$found) {
+                        $t = $d;
+                        foreach ($seoTranslateProps as $seoTranslateProp) {
+                            if (!$t[$seoTranslateProp]) {
+                                continue;
+                            }
+                            $t[$seoTranslateProp] = $translateAPI->translate($t[$seoTranslateProp], $locale, $mainLocale);
+                        }
+
+                        $data[$locale][] = $t;
                     }
                 }
-                $this->setTranslations($prop, $data);
             }
+            $this->setTranslations('seo', $data);
 
-            Api::setCache(Files::mkdir(protected_path('cache/azure_translate')));
-            $translateAPI = new Api(env('AZURE_TRANSLATE_API_KEY'));
 
             $translations = $this->getTranslations('texts');
             foreach ($locales as $locale) {
diff --git a/app/SubForms/Webflow/Redirection.php b/app/SubForms/Webflow/Redirection.php
new file mode 100644 (file)
index 0000000..798a35a
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\SubForms\Webflow;
+
+use App\Fields\Webflow\WebflowPage;
+use Cubist\Backpack\Magic\Fields\Text;
+use Cubist\Backpack\Magic\SubForm;
+
+class Redirection extends SubForm
+{
+    public function init()
+    {
+        parent::init();
+        $this->addField('from', Text::class, __('Ancienne URL'));
+        $this->addField('to', WebflowPage::class, __('RedirigĂ©e vers'));
+    }
+}