]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6944 @7
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 5 Jun 2024 11:01:29 +0000 (13:01 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 5 Jun 2024 11:01:29 +0000 (13:01 +0200)
app/Models/ToolWebflow.php
app/Services/Webflow.php
app/SubForms/Webflow/WebflowText.php [deleted file]
resources/views/fields/webflow/seo.blade.php [deleted file]
resources/views/fields/webflow/texts.blade.php

index 1f26ece7b7f0d3505f29bd220caf0cd5a7703446..b26e42ec0acf0ea060a0548a5e2c515e47d42672 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\Models;
 
 use App\Fields\Webflow\WebflowImages;
+use App\Fields\Webflow\WebflowTexts;
 use App\Http\Controllers\Admin\Operations\Tools\StaticSiteUploader;
 use App\Http\Controllers\Admin\Operations\Tools\WebflowOperation;
 use App\Jobs\WebflowPublish;
@@ -13,7 +14,6 @@ use App\SubForms\Webflow\WebflowText;
 use Cubist\Backpack\Magic\Fields\BunchOfFieldsMultiple;
 use Cubist\Backpack\Magic\Fields\Code;
 use Cubist\Backpack\Magic\Fields\Hidden;
-use Cubist\Backpack\Magic\Fields\KeyValueBunchOfFieldsMultiple;
 use Cubist\Backpack\Magic\Fields\SelectFromArray;
 use Cubist\Backpack\Magic\Fields\Table;
 use Cubist\Backpack\Magic\Fields\Text;
@@ -22,6 +22,7 @@ use Cubist\Backpack\Magic\Fields\Textarea;
 use Cubist\Util\CommandLine;
 use Cubist\Util\Files\Files;
 use Cubist\Util\Json;
+use Illuminate\Contracts\Cache\Lock;
 use Illuminate\Support\Facades\Cache;
 
 // __('!! Outils')
@@ -56,7 +57,7 @@ class ToolWebflow extends ToolboxTranslatableModel
         $this->addField('upload', SelectFromArray::class, __('Uploader sur'), ['translatable' => false, 'options' => $sites, 'tab' => __('Paramètres')]);
         $this->addField('js', Code::class, __('Javascript complémentaire'), ['translatable' => false, 'language' => 'js', 'tab' => __('Code'), 'hint' => __('Code ajouté à toutes les pages')]);
         $this->addField('css', Code::class, __('CSS complémentaire'), ['translatable' => false, 'language' => 'css', 'tab' => __('Code'), 'hint' => __('Code ajouté à toutes les pages')]);
-        $this->addField('texts', KeyValueBunchOfFieldsMultiple::class, '', ['tab' => __('Textes'), 'edit_label' => '%url | %seo_title', 'allows_add' => false, 'allows_delete' => false, 'allows_clone' => false, 'allows_reorder' => false, 'bunch' => WebflowText::class, 'translatable' => true]);
+        $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('api', Hidden::class);
@@ -158,28 +159,46 @@ class ToolWebflow extends ToolboxTranslatableModel
         return true;
     }
 
-    protected function refreshFormDataFromAPI()
+    protected function getLock(): Lock
     {
-        Webflow::clearCache();
-        $this->api = Webflow::getEditableData($this->webflow);
+        return Cache::lock('webflow_api_refresh_' . $this->id);
+    }
 
-        $mainLocale = $this->getMainLocale();
-        $seo = Json::decode($this->seo, true);
-        $seo[$mainLocale] = $this->api['seo'];
-        foreach ($this->getLocalesCodes() as $locale) {
-            if ($locale === $mainLocale) {
-                continue;
-            }
-            if (!isset($seo[$locale])) {
-                $seo[$locale] = [];
-            }
-            foreach ($seo[$mainLocale] as $s) {
-                $seo[$locale][] = $s;
+    protected function refreshFormDataFromAPI()
+    {
+        $lock = $this->getLock();
+        try {
+            $lock->block(360);
+
+            Webflow::clearCache();
+            $this->api = Webflow::getEditableData($this->webflow);
+
+            $mainLocale = $this->getMainLocale();
+
+            $props = ['seo'/*, 'texts', 'images'*/];
+            foreach ($props as $prop) {
+                $data = Json::decode($this->$prop, Json::TYPE_ARRAY);
+                $data[$mainLocale] = $this->api[$prop];
+                foreach ($this->getLocalesCodes() as $locale) {
+                    if ($locale === $mainLocale) {
+                        continue;
+                    }
+                    if (!isset($data[$locale])) {
+                        $data[$locale] = [];
+                    }
+                    foreach ($data[$mainLocale] as $d) {
+                        $data[$locale][] = $d;
+                    }
+                }
+                $this->setTranslations($prop, $data);
             }
-        }
 
-        $this->setTranslations('seo', $seo);
-        $this->saveQuietly();
+            $this->saveQuietly();
+        } catch (LockTimeoutException $e) {
+
+        } finally {
+            $lock?->release();
+        }
     }
 
     /**
@@ -293,11 +312,23 @@ class ToolWebflow extends ToolboxTranslatableModel
 
     public function onRetrieved(): bool
     {
-        $locales = $this->locales_domains;
-        if (is_string($locales)) {
-            $locales = json_decode($locales, true);
+
+        $lock = $this->getLock();
+        try {
+            $lock->block(1200);
+
+            $locales = $this->locales_domains;
+            if (is_string($locales)) {
+                $locales = json_decode($locales, true);
+            }
+            Cache::put('webflow_' . $this->id . '_locales', $locales);
+
+        } catch (LockTimeoutException $e) {
+
+        } finally {
+            $lock?->release();
         }
-        Cache::put('webflow_' . $this->id . '_locales', $locales);
+
 
         return parent::onRetrieved();
     }
index a76a3562a3cc98aea6393322bd52379b7a70f49a..8af2a9ae3807ad5b174fcf8795651e43f04218b3 100644 (file)
@@ -276,7 +276,7 @@ class Webflow
                     'og_description_copied' => $details['openGraph']['descriptionCopied'],
                 ];
 
-                $res['seo'][$pageID] = $seo;
+                $res['seo'][] = $seo;
 
                 $texts = [];
                 foreach ($page['contents'] as $node) {
@@ -285,9 +285,9 @@ class Webflow
                             continue;
                         }
                         if (!isset($res['texts'][$node['text']['text']])) {
-                            $res['texts'][$node['text']['text']] = [];
+                            $res['texts'][$node['text']['text']] = ['key' => base64_encode($node['text']['text']), 'occurences' => 0];
                         }
-                        $res['texts'][$node['text']['text']][] = ['node' => $node['id'], 'page' => $pageID];
+                        $res['texts'][$node['text']['text']]['occurences']++;
                     } else if ($node['type'] === 'image') {
                         if (!isset($node['image']['assetId'])) {
                             continue;
@@ -296,7 +296,7 @@ class Webflow
                         if (isset($res['images'][$assetId])) {
                             continue;
                         }
-                        $res['images'][$assetId] = ['alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']];
+                        $res['images'][] = ['id' => $assetId, 'alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']];
                     }
                 }
             }
diff --git a/app/SubForms/Webflow/WebflowText.php b/app/SubForms/Webflow/WebflowText.php
deleted file mode 100644 (file)
index 1c97d44..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-<?php
-
-namespace App\SubForms\Webflow;
-
-use Cubist\Backpack\Magic\Fields\HiddenVisible;
-use Cubist\Backpack\Magic\SubForm;
-
-class WebflowText extends SubForm
-{
-    public function init()
-    {
-        parent::init();
-        $this->addField('id', HiddenVisible::class, __('#'));
-    }
-}
diff --git a/resources/views/fields/webflow/seo.blade.php b/resources/views/fields/webflow/seo.blade.php
deleted file mode 100644 (file)
index 1a2b4b1..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-@php
-    $data=$entry->getEditableData()['seo'];
-    $fields=[
-        'slug'=>['label'=>'URL','type'=>'text']
-    ,'seo_title','seo_description']
-@endphp
-@include('crud::fields.inc.wrapper_start')
-<ul style="position: sticky;top:0px;">
-    @foreach($data as $id=>$page)
-        <li>
-            <a href="#" data-id="{{$id}}">{{$page['slug']?:'index'}}</a>
-        </li>
-        @push('seo_forms')
-            <div class="seo_page" id="seo_page_{{$id}}">
-                @foreach($fields as $name=>$f)
-
-                @endforeach
-            </div>
-        @endpush
-    @endforeach
-</ul>
-@stack('seo_forms')
-
-@include('crud::fields.inc.wrapper_end')
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8e7c46a68ec9a3c81b4286554539fa4a2c0246cb 100644 (file)
@@ -0,0 +1,29 @@
+@php
+    $value=old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? '[]';
+    $translations=\Cubist\Util\Json::decode($value,\Cubist\Util\Json::TYPE_ARRAY)??[];
+    $texts=\Cubist\Util\Json::decode($crud->entry->api,\Cubist\Util\Json::TYPE_ARRAY)['texts'];
+    foreach ($translations as $key=>$trans) {
+        $t=base64_decode($key);
+        if(!isset($texts[$t])){
+            $texts[$t]=['occurences'=>0,'key'=>$key];
+        }
+    }
+@endphp
+
+@include('crud::fields.inc.wrapper_start')
+<label>{!! $field['label'] !!}</label>
+@include('crud::fields.inc.translatable_icon')
+
+@foreach($texts as $text=>$details)
+    <label>{{$text}}</label>
+    <textarea rows="{{ceil(mb_strlen($text)/150)}}" name="{{ $field['name'] }}[{{$details['key']}}]" @include('crud::fields.inc.attributes')>
+                {{$translations[$details['key']]??''}}
+            </textarea>
+
+    @if (isset($field['hint']))
+        <p class="help-block">{{__(':nb occurences',['nb'=>$details['occurences']])}}</p>
+    @endif
+@endforeach
+{{-- HINT --}}
+
+@include('crud::fields.inc.wrapper_end')