namespace App\Models;
use App\Fields\Webflow\WebflowImages;
-use App\Fields\Webflow\WebflowSEO;
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;
-use App\Models\Base\ToolboxModel;
+use App\Models\Base\ToolboxTranslatableModel;
use App\Services\Webflow;
use App\SubForms\Webflow\SEOPage;
use Cubist\Backpack\Magic\Fields\BunchOfFieldsMultiple;
use Cubist\Backpack\Magic\Fields\Code;
+use Cubist\Backpack\Magic\Fields\Hidden;
use Cubist\Backpack\Magic\Fields\SelectFromArray;
use Cubist\Backpack\Magic\Fields\Table;
use Cubist\Backpack\Magic\Fields\Text;
use Cubist\Backpack\Magic\Fields\Textarea;
use Cubist\Util\CommandLine;
use Cubist\Util\Files\Files;
+use Cubist\Util\Json;
use Illuminate\Support\Facades\Cache;
// __('!! Outils')
-class ToolWebflow extends ToolboxModel
+class ToolWebflow extends ToolboxTranslatableModel
{
protected static $_permissionBase = 'webflow';
{
parent::setFields();
- $this->addField('name', Text::class, __('Projet'), ['column' => true, 'tab' => __('Paramètres')]);
- $this->addField('webflow', Text::class, __('URL du projet webflow'), ['prefix' => 'https://', 'suffix' => '.webflow.io', 'column' => true, 'hint' => __('ex : :url', ['url' => 'https://projet.webflow.io']), 'tab' => __('Paramètres')]);
- $this->addField('webflow_api_token', Text::class, __('Token de l\'API Webflow'), ['tab' => __('Paramètres')]);
- $this->addField('webflow_api_login', LinkButton::class, __('Récupérer un token'), ['when' => ["webflow_api_token" => ''], 'tab' => __('Paramètres'), 'value' => 'https://webflow.com/oauth/authorize?response_type=code&client_id=' . env('WEBFLOW_CLIENT_ID') . '&state=$id&scope=assets%3Aread%20assets%3Awrite%20authorized_user%3Aread%20cms%3Aread%20cms%3Awrite%20custom_code%3Aread%20custom_code%3Awrite%20forms%3Aread%20forms%3Awrite%20pages%3Aread%20pages%3Awrite%20sites%3Aread%20sites%3Awrite']);
- $this->addField('domains', Textarea::class, __('Domaines à télécharger'), ['tab' => __('Paramètres')]);
- $this->addField('locales', Table::class, __('Langues'), ['columns' => ['locale' => __('Code langue'), 'url' => __('URL')], 'tab' => __('Paramètres')]);
- $this->addField('slack', Text::class, __('Notification slack'), ['tab' => __('Paramètres')]);
+ $this->addField('name', Text::class, __('Projet'), ['translatable' => false, 'column' => true, 'tab' => __('Paramètres')]);
+ $this->addField('webflow', Text::class, __('URL du projet webflow'), ['translatable' => false, 'prefix' => 'https://', 'suffix' => '.webflow.io', 'column' => true, 'hint' => __('ex : :url', ['url' => 'https://projet.webflow.io']), 'tab' => __('Paramètres')]);
+ $this->addField('webflow_api_token', Text::class, __('Token de l\'API Webflow'), ['translatable' => false, 'tab' => __('Paramètres')]);
+ $this->addField('webflow_api_login', LinkButton::class, __('Récupérer un token'), ['translatable' => false, 'when' => ["webflow_api_token" => ''], 'tab' => __('Paramètres'), 'value' => 'https://webflow.com/oauth/authorize?response_type=code&client_id=' . env('WEBFLOW_CLIENT_ID') . '&state=$id&scope=assets%3Aread%20assets%3Awrite%20authorized_user%3Aread%20cms%3Aread%20cms%3Awrite%20custom_code%3Aread%20custom_code%3Awrite%20forms%3Aread%20forms%3Awrite%20pages%3Aread%20pages%3Awrite%20sites%3Aread%20sites%3Awrite']);
+ $this->addField('domains', Textarea::class, __('Domaines à télécharger'), ['translatable' => false, 'tab' => __('Paramètres')]);
+ $this->addField('locales_domains', Table::class, __('Langues'), ['translatable' => false, 'columns' => ['locale' => __('Code langue'), 'url' => __('URL')], 'tab' => __('Paramètres')]);
+ $this->addField('slack', Text::class, __('Notification slack'), ['translatable' => false, 'tab' => __('Paramètres')]);
$s = StaticSiteUploader::getSites();
$sites = [];
foreach ($s as $k => $item) {
$sites[$k] = $item['label'];
}
- $this->addField('upload', SelectFromArray::class, __('Uploader sur'), ['options' => $sites, 'tab' => __('Paramètres')]);
- $this->addField('js', Code::class, __('Javascript complémentaire'), ['language' => 'js', 'tab' => __('Code'), 'hint' => __('Code ajouté à toutes les pages')]);
- $this->addField('css', Code::class, __('CSS complémentaire'), ['language' => 'css', 'tab' => __('Code'), 'hint' => __('Code ajouté à toutes les pages')]);
+ $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', WebflowTexts::class, '', ['tab' => __('Textes'), 'translatable' => true]);
$this->addField('images', WebflowImages::class, '', ['tab' => __('Images'), 'translatable' => true]);
- $this->addField('seo', BunchOfFieldsMultiple::class, '', ['edit_label' => '%url | %seo_title', 'allows_add' => false, 'allows_delete' => false, 'allows_clone' => false, 'allows_reorder' => false, 'bunch' => SEOPage::class, 'tab' => __('SEO')]);
+ $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);
}
protected function _parsePages()
return Cache::get('webflow_' . $this->id . '_locales', []);
}
+ public function getLocalesCodes()
+ {
+ $res = [];
+ foreach ($this->getLocales() as $locale) {
+ $res[] = $locale['locale'];
+ }
+ return $res;
+ }
+
+ public function getMainLocale()
+ {
+ foreach ($this->getLocales() as $locale) {
+ return $locale['locale'];
+ }
+ }
public function onPublish($mode = 'manual')
{
protected function saveDataInWebflow()
{
Webflow::setToken($this->webflow_api_token);
- // SEO
- foreach ($this->seo as $page) {
- Webflow::savePageMeta($page);
+
+ $mainLocale = $this->getMainLocale();
+ $api = json_decode($this->api, true);
+
+ $clearCache = false;
+ if (!$this->_compareObjects($this->api['seo'], $this->seo[$mainLocale])) {
+ dd('seo has changed');
+ $clearCache = true;
}
- Webflow::clearCache();
- $this->refreshFormDataFromAPI();
+
+ if ($clearCache) {
+ Webflow::clearCache();
+ $this->refreshFormDataFromAPI();
+ }
+ }
+
+ protected function _compareObjects($a, $b)
+ {
+ return $a === $b;
}
protected function refreshFormDataFromAPI()
{
- $extra_locales=$this->locales;
- array_shift($extra_locales);
+ $this->api = Webflow::getEditableData($this->webflow);
+
+ $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 $id => $s) {
+ $seo[$locale][$id] = $s;
+ }
+ }
+
+ $this->setTranslations('seo', $seo);
- $editable = Webflow::getEditableData($this->webflow);
- $this->seo = $editable['seo'];
$this->saveQuietly();
}
public function onRetrieved(): bool
{
- $locales = $this->locales;
+ $locales = $this->locales_domains;
if (is_string($locales)) {
$locales = json_decode($locales, true);
}
Cache::put('webflow_' . $this->id . '_locales', $locales);
Webflow::setToken($this->webflow_api_token);
$this->refreshFormDataFromAPI();
+
+
return parent::onRetrieved();
}
return Webflow::getEditableData($this->webflow);
}
+ public function getAvailableLocales()
+ {
+ $locales = $this->getLocales();
+ if (null === $locales) {
+ return [];
+ }
+ $res = [];
+ foreach ($locales as $locale) {
+ $res[$locale['locale']] = \Cubist\Locale\Locale::translate($locale['locale'], $locale['locale']);
+ }
+ return $res;
+ }
+
}