From: Vincent Vanwaelscappel Date: Wed, 5 Jun 2024 11:01:29 +0000 (+0200) Subject: wip #6944 @7 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=898dbe4f3016522f17e747cc0cc3a0c62213bd1b;p=fluidbook-toolbox.git wip #6944 @7 --- diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index 1f26ece7b..b26e42ec0 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -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(); } diff --git a/app/Services/Webflow.php b/app/Services/Webflow.php index a76a3562a..8af2a9ae3 100644 --- a/app/Services/Webflow.php +++ b/app/Services/Webflow.php @@ -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 index 1c97d4419..000000000 --- a/app/SubForms/Webflow/WebflowText.php +++ /dev/null @@ -1,15 +0,0 @@ -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 index 1a2b4b19a..000000000 --- a/resources/views/fields/webflow/seo.blade.php +++ /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') - -@stack('seo_forms') - -@include('crud::fields.inc.wrapper_end') diff --git a/resources/views/fields/webflow/texts.blade.php b/resources/views/fields/webflow/texts.blade.php index e69de29bb..8e7c46a68 100644 --- a/resources/views/fields/webflow/texts.blade.php +++ b/resources/views/fields/webflow/texts.blade.php @@ -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') + +@include('crud::fields.inc.translatable_icon') + +@foreach($texts as $text=>$details) + + + + @if (isset($field['hint'])) +

{{__(':nb occurences',['nb'=>$details['occurences']])}}

+ @endif +@endforeach +{{-- HINT --}} + +@include('crud::fields.inc.wrapper_end')