From 6ed0aefcf749937f922c77d1680b97db1fead604 Mon Sep 17 00:00:00 2001 From: mrflos Date: Mon, 12 Aug 2024 11:51:48 +0300 Subject: [PATCH] feat(duplication): ui to check if distant form is present --- handlers/DuplicateHandler.php | 5 +++- includes/services/DuplicationManager.php | 4 +-- javascripts/handlers/duplicate.js | 31 ++++++++++++++++++++++++ lang/yeswikijs_fr.php | 5 +++- templates/handlers/duplicate.twig | 9 ++++++- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/handlers/DuplicateHandler.php b/handlers/DuplicateHandler.php index 4cb72252b..c3829555d 100644 --- a/handlers/DuplicateHandler.php +++ b/handlers/DuplicateHandler.php @@ -2,6 +2,7 @@ use YesWiki\Bazar\Controller\EntryController; use YesWiki\Bazar\Service\EntryManager; +use YesWiki\Bazar\Service\FormManager; use YesWiki\Bazar\Service\ListManager; use YesWiki\Core\Controller\AuthController; use YesWiki\Core\Service\AclService; @@ -84,6 +85,7 @@ public function run() $pageTitle = $originalContent['bf_titre']; $proposedTag = $this->wiki->GetPageTag(); $originalContent = $this->wiki->page['body']; + $form = $this->getService(FormManager::class)->getOne($this->getService(EntryManager::class)->getOne($proposedTag)['id_typeannonce']); } else { $pageTitle = $originalContent['bf_titre'] . ' (' . _t('DUPLICATE') . ')'; $proposedTag = genere_nom_wiki($pageTitle); @@ -133,8 +135,9 @@ public function run() 'originalContent' => $originalContent ?? '', 'totalSize' => $this->duplicationManager->humanFilesize($totalSize ?? 0), 'type' => $type ?? '', + 'form' => $form ?? '', 'baseUrl' => preg_replace('/\?$/Ui', '', $this->wiki->config['base_url']), 'toExternalWiki' => $toExternalWiki, ]); } -} \ No newline at end of file +} diff --git a/includes/services/DuplicationManager.php b/includes/services/DuplicationManager.php index eca8e5232..053bc7bc1 100644 --- a/includes/services/DuplicationManager.php +++ b/includes/services/DuplicationManager.php @@ -319,7 +319,7 @@ public function importDistantContent($tag, $request) $entry = json_decode($newBody, true); $entry['id_fiche'] = $tag; $entry['antispam'] = 1; - $this->wiki->services->get(EntryManager::class)->create($entry['id_typeannonce'], $entry); + $this->wiki->services->get(EntryManager::class)->create($entry['id_typeannonce'], $entry, false, $req['sourceUrl']); } } @@ -354,4 +354,4 @@ public function humanFilesize($bytes, $decimals = 2) return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor - 1] . 'B'; } -} \ No newline at end of file +} diff --git a/javascripts/handlers/duplicate.js b/javascripts/handlers/duplicate.js index cd68fcfeb..2710ef041 100644 --- a/javascripts/handlers/duplicate.js +++ b/javascripts/handlers/duplicate.js @@ -9,6 +9,10 @@ function isValidUrl(string) { } } +function arrayIncludesAllRequiredFields(arr, fields) { + return fields.every((v) => arr.some((i) => i.id === v.id && i.type === v.type)) +} + function blockDuplicationName(tag) { $('[name=duplicate-action]').attr('disabled', 'disabled').addClass('disabled') $('#newTag').parents('.form-group').removeClass('has-success').addClass('has-error') @@ -126,6 +130,33 @@ document.addEventListener('DOMContentLoaded', () => { url }).done((data) => { handleLoginResponse(data) + + // if case of entry, we need to check if form id is available and compatible + // or propose another id + const formId = $('#form-id').val() + if (typeof formId !== 'undefined') { + const formUrl = `${shortUrl}/?api/forms/${formId}` + $.ajax({ + method: 'GET', + url: formUrl + }).done((form) => { + const requiredFields = form.prepared.filter((field) => field.required === true) + // we check if the found formId is compatible + if (arrayIncludesAllRequiredFields(window.sourceForm.prepared, requiredFields)) { + $('#form-message').removeClass('has-error').addClass('has-success').find('.help-block') + .html(_t('FORM_ID_IS_COMPATIBLE', { id: formId })) + } else { + $('#form-message').removeClass('has-success').addClass('has-error').find('.help-block') + .html(_t('FORM_ID_NOT_AVAILABLE', { id: formId })) + } + }).fail((jqXHR) => { + if (jqXHR.status === 404) { + // the formId is available + $('#form-message').removeClass('has-error').addClass('has-success').find('.help-block') + .html(_t('FORM_ID_AVAILABLE', { id: formId })) + } + }) + } }).fail((jqXHR) => { if (jqXHR.status === 401) { $('#login-message').html(`
${_t('NOT_CONNECTED')}
`) diff --git a/lang/yeswikijs_fr.php b/lang/yeswikijs_fr.php index 0f9a015fe..e67d8ebb9 100644 --- a/lang/yeswikijs_fr.php +++ b/lang/yeswikijs_fr.php @@ -159,4 +159,7 @@ 'CONNECTED_AS_ADMIN' => 'L\'utilisateurice {user} est bien connecté.e sur le YesWiki de destination, avec des droits d\'administration.', 'CONNECTED_BUT_NOT_ADMIN' => 'L\'utilisateurice {user} est bien connecté.e sur le YesWiki de destination, mais n\'a pas les droits d\'administration. Veuillez-vous connecter avec un compte admin.', 'NOT_WIKI_OR_OLD_WIKI' => 'Le site indiqué ne semble pas être un YesWiki, ou sa version est antérieure à 4.5.0', -]; + 'FORM_ID_AVAILABLE' => 'Le formulaire avec l\'identifiant {id} n\'existant pas encore sur l\'url indiqué, il sera créé.', + 'FORM_ID_NOT_AVAILABLE' => 'Le formulaire avec l\'identifiant {id} n\'est plus disponible sur l\'url indiqué, il faut changer d\'identifiant.', + 'FORM_ID_IS_COMPATIBLE' => 'Un formulaire du même identifiant ({id}) existe sur l\'url indiqué et semble avoir au moins les champs obligatoires similaires. Il sera utilisé pour la duplication.', +]; \ No newline at end of file diff --git a/templates/handlers/duplicate.twig b/templates/handlers/duplicate.twig index 4c43e2f98..50153e007 100644 --- a/templates/handlers/duplicate.twig +++ b/templates/handlers/duplicate.twig @@ -36,6 +36,13 @@ + {% if form %} + + +
+ {% endif %}
{# hide the field while not connected #} {% endif %}
@@ -46,7 +53,7 @@
- {% endif %} + {% endif %}