]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6937 @4
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 3 Jun 2024 14:39:52 +0000 (16:39 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 3 Jun 2024 14:39:52 +0000 (16:39 +0200)
app/Models/ToolWebflow.php
app/Services/Webflow.php
app/SubForms/Webflow/SEOPage.php

index 1ef0030a78cfa4c3ea6db9b90dcf041131486d93..c29e84d5735f2c0e79e3cf66131719cbfaa282bf 100644 (file)
@@ -92,10 +92,16 @@ class ToolWebflow extends ToolboxTranslatableModel
         WebflowPublish::dispatch($this->id, $mode);
     }
 
+    public function onSaving(): bool
+    {
+        $this->saveDataInWebflow();
+        return parent::onSaving();
+    }
+
     public function onSaved(): bool
     {
+
         $this->onPublish('auto');
-        $this->saveDataInWebflow();
         return parent::onSaved();
     }
 
@@ -104,27 +110,56 @@ class ToolWebflow extends ToolboxTranslatableModel
         Webflow::setToken($this->webflow_api_token);
 
         $mainLocale = $this->getMainLocale();
-        $api = json_decode($this->api, true);
+        if (is_string($this->api)) {
+            $api = json_decode($this->api, true);
+        }
+        $seo = $this->getTranslations('seo');
 
-        $clearCache = false;
-        if (!$this->_compareObjects($this->api['seo'], $this->seo[$mainLocale])) {
-            dd('seo has changed');
-            $clearCache = true;
+        $hasChanged = false;
+
+        foreach ($seo[$mainLocale] as $page) {
+
+            if ($page['og_title_copied']) {
+                $page['og_title'] = $page['seo_title'];
+            }
+            if ($page['og_description_copied']) {
+                $page['og_description'] = $page['seo_description'];
+            }
+
+            if (!$this->_compareArrays($api['seo'][$page['id']], $page)) {
+                $api['seo'][$page['id']] = $page;
+                $hasChanged = true;
+                Webflow::savePageMeta($page);
+            }
         }
 
-        if ($clearCache) {
-            Webflow::clearCache();
-            $this->refreshFormDataFromAPI();
+        if ($hasChanged) {
+            $this->api = json_encode($api);
+            $this->saveQuietly();
         }
+
     }
 
-    protected function _compareObjects($a, $b)
+    protected function _compareArrays($a, $b, $params = null)
     {
-        return $a === $b;
+        if (null === $params) {
+            $params = array_keys($a);
+        }
+        foreach ($params as $p) {
+            $va = $a[$p] ?? null;
+            $vb = $b[$p] ?? null;
+
+            if ($va != $vb) {
+                return false;
+            }
+        }
+
+        return true;
     }
 
     protected function refreshFormDataFromAPI()
     {
+        Webflow::clearCache();
         $this->api = Webflow::getEditableData($this->webflow);
 
         $mainLocale = $this->getMainLocale();
@@ -137,13 +172,12 @@ class ToolWebflow extends ToolboxTranslatableModel
             if (!isset($seo[$locale])) {
                 $seo[$locale] = [];
             }
-            foreach ($seo[$mainLocale] as $id => $s) {
-                $seo[$locale][$id] = $s;
+            foreach ($seo[$mainLocale] as $s) {
+                $seo[$locale][] = $s;
             }
         }
 
         $this->setTranslations('seo', $seo);
-
         $this->saveQuietly();
     }
 
@@ -155,7 +189,6 @@ class ToolWebflow extends ToolboxTranslatableModel
     public function mirror($slow = true, $force = false)
     {
         Webflow::setToken($this->webflow_api_token);
-        Webflow::clearCache();
         $this->refreshFormDataFromAPI();
 
         $path = $this->getMirrorPath();
@@ -264,9 +297,6 @@ class ToolWebflow extends ToolboxTranslatableModel
             $locales = json_decode($locales, true);
         }
         Cache::put('webflow_' . $this->id . '_locales', $locales);
-        Webflow::setToken($this->webflow_api_token);
-        $this->refreshFormDataFromAPI();
-
 
         return parent::onRetrieved();
     }
index 2bf0a653c9da2066db162f1e186074c5dc310b38..a76a3562a3cc98aea6393322bd52379b7a70f49a 100644 (file)
@@ -195,9 +195,7 @@ class Webflow
         ];
 
         $url = 'pages/' . $page['id'];
-        $res = self::request($url, $data, 'put', 0, true);
-
-        return $res;
+        return self::request($url, $data, 'put', 0, true);
     }
 
     protected static function _arrayKey($array)
index 11f9d992902c109110b2fcef2686594b22bd281a..01e9b41daea2ba5b94651ea19d901172e35cf865 100644 (file)
@@ -17,7 +17,8 @@ class SEOPage extends SubForm
 
         $this->addField('id', HiddenVisible::class, __('#'));
         $this->addField('url', HiddenVisible::class, __('URL'));
-        $this->addField('type', Hidden::class, __('Type'));
+        $this->addField('type', HiddenVisible::class, __('Type'));
+        $this->addField('draft', Checkbox::class, __('Brouillon'));
         $this->addField('slug', Text::class, __('Slug') . ' (' . __('slug') . ')', ['when' => ['url' => ['operator' => '!', 'value' => '/index.html']]]);
         $this->addField('seo_title', Text::class, __('Titre'), ['when' => ['type' => 'page']]);
         $this->addField('og_title_copied', Checkbox::class, __('Utiliser le titre ci-dessus pour les réseaux sociaux'), ['when' => ['type' => 'page']]);