From d5f1ba0d2ff7b61400fd0e395801d5523a67da51 Mon Sep 17 00:00:00 2001 From: Florian Schmitt Date: Sun, 19 Jan 2025 15:59:08 +0100 Subject: [PATCH] fix(duplication): hide duplication to another wiki (i ll be back....) --- includes/controllers/ApiController.php | 16 +- javascripts/handlers/duplicate.js | 211 +++++++++++------- .../templates/barreredaction_basic.twig | 16 +- 3 files changed, 152 insertions(+), 91 deletions(-) diff --git a/includes/controllers/ApiController.php b/includes/controllers/ApiController.php index d1d76befe..73ee57d99 100644 --- a/includes/controllers/ApiController.php +++ b/includes/controllers/ApiController.php @@ -42,25 +42,25 @@ public function getDocumentation() '

' . _t('LIST') . ' ' . _t('USERS') . '

' . "\n" . '

GET ' . $urlUser . '

' . "\n" . '

' . _t('GET') . ' ' . _t('USER') . '

' . "\n" . - '

GET ' . $urlUser . '/{userId}' . '

' . "\n" . '

' . _t('CREATE') . ' ' . _t('USER') . '

' . "\n" . + '

GET ' . $urlUser . '/{userId}

' . "\n" . '

' . _t('CREATE') . ' ' . _t('USER') . '

' . "\n" . '

POST ' . $urlUser . '

' . "\n" . - '

' . 'name=…&email=…' . '

' . "\n" . + '

name=…&email=…

' . "\n" . '

' . _t('DELETE') . ' ' . _t('USER') . '

' . "\n" . - '

POST ' . $urlUser . '/{userId}/delete' . '

' . "\n"; + '

POST ' . $urlUser . '/{userId}/delete

' . "\n"; $urlGroup = $this->wiki->Href('', 'api/groups'); $output .= '

' . _t('GROUPS') . '

' . "\n" . '

' . _t('LIST') . ' ' . _t('GROUPS') . '

' . "\n" . '

GET ' . $urlGroup . '

' . "\n" . '

' . _t('GET') . ' ' . _t('GROUP') . '

' . "\n" . - '

GET ' . $urlGroup . '/{group_name}' . '

' . "\n" . '

' . _t('CREATE') . ' ' . _t('GROUP') . '

' . "\n" . + '

GET ' . $urlGroup . '/{group_name}

' . "\n" . '

' . _t('CREATE') . ' ' . _t('GROUP') . '

' . "\n" . '

POST ' . $urlGroup . '

' . "\n" . - '

' . 'name=…&users[0]=…&users[1]' . '

' . "\n" . + '

name=…&users[0]=…&users[1]

' . "\n" . '

' . _t('DELETE') . ' ' . _t('GROUP') . '

' . "\n" . - '

POST ' . $urlGroup . '/{group_name}/delete' . '

' . "\n" . + '

POST ' . $urlGroup . '/{group_name}/delete

' . "\n" . '

' . _t('UPDATE') . ' ' . _t('GROUP') . '

' . "\n" . - '

POST ' . $urlGroup . '/{group_name}/update' . '

' . "\n" . - '

' . 'users[0]=…&users[1]' . '

' . "\n"; + '

POST ' . $urlGroup . '/{group_name}/update

' . "\n" . + '

users[0]=…&users[1]

' . "\n"; $urlPages = $this->wiki->Href('', 'api/pages'); $output .= '

' . _t('PAGES') . '

' . "\n" . diff --git a/javascripts/handlers/duplicate.js b/javascripts/handlers/duplicate.js index 8dbeb7cae..62cf1544d 100644 --- a/javascripts/handlers/duplicate.js +++ b/javascripts/handlers/duplicate.js @@ -14,14 +14,22 @@ function arrayIncludesAllRequiredFields(arr, fields) { } function blockDuplicationName(tag) { - $('[name=duplicate-action]').attr('disabled', 'disabled').addClass('disabled') - $('#newTag').parents('.form-group').removeClass('has-success').addClass('has-error') + $('[name=duplicate-action]') + .attr('disabled', 'disabled') + .addClass('disabled') + $('#newTag') + .parents('.form-group') + .removeClass('has-success') + .addClass('has-error') $('#pagetag-message').html(_t('PAGE_NOT_AVAILABLE', { tag })) } function validateDuplicationName(tag) { $('[name=duplicate-action]').removeAttr('disabled').removeClass('disabled') - $('#newTag').parents('.form-group').removeClass('has-error').addClass('has-success') + $('#newTag') + .parents('.form-group') + .removeClass('has-error') + .addClass('has-success') $('#pagetag-message').html(_t('PAGE_AVAILABLE', { tag })) } @@ -29,27 +37,33 @@ function checkPageExistence(url) { $.ajax({ method: 'GET', url - }).done(() => { - blockDuplicationName(url.replace(`${shortUrl}/?api/pages/`, '')) - }).fail((jqXHR) => { - if (jqXHR.status === 404) { - validateDuplicationName(url.replace(`${shortUrl}/?api/pages/`, '')) - } else { - blockDuplicationName(url.replace(`${shortUrl}/?api/pages/`, '')) - } }) + .done(() => { + blockDuplicationName(url.replace(`${shortUrl}/?api/pages/`, '')) + }) + .fail((jqXHR) => { + if (jqXHR.status === 404) { + validateDuplicationName(url.replace(`${shortUrl}/?api/pages/`, '')) + } else { + blockDuplicationName(url.replace(`${shortUrl}/?api/pages/`, '')) + } + }) } function handleLoginResponse(data) { if (data.isAdmin === true) { - $('#login-message').html(_t('CONNECTED_AS_ADMIN', { user: data.user })).parents('.form-group') + $('#login-message') + .html(_t('CONNECTED_AS_ADMIN', { user: data.user })) + .parents('.form-group') .removeClass('has-error') .addClass('has-success') $('.login-fields').addClass('hide') $('.duplication-fields').removeClass('hide') checkPageExistence(`${shortUrl}/?api/pages/${$('#newTag').val()}`) } else { - $('#login-message').html(_t('CONNECTED_BUT_NOT_ADMIN', { user: data.user })).parents('.form-group') + $('#login-message') + .html(_t('CONNECTED_BUT_NOT_ADMIN', { user: data.user })) + .parents('.form-group') .removeClass('has-success') .addClass('has-error') $('.duplication-fields').addClass('hide') @@ -58,10 +72,13 @@ function handleLoginResponse(data) { } document.addEventListener('DOMContentLoaded', () => { - $('.duplication-wiki-form, .duplication-login-form, #form-duplication').on('submit', (e) => { - e.stopPropagation() - return false - }) + $('.duplication-wiki-form, .duplication-login-form, #form-duplication').on( + 'submit', + (e) => { + e.stopPropagation() + return false + } + ) $('#url-wiki').on('change', () => { $('.login-fields, .duplication-fields').addClass('hide') $('#login-message').html('') @@ -75,36 +92,54 @@ document.addEventListener('DOMContentLoaded', () => { username: $('#username').val(), password: $('#password').val() } - }).done((data) => { - handleLoginResponse(data) - }).fail((jqXHR) => { - toastMessage(jqXHR.responseJSON.error, 3000, 'alert alert-danger') - if (jqXHR.status === 401) { - $('#login-message').html(`
${_t('NOT_CONNECTED')}
`) - $('.login-fields').removeClass('hide') - } }) + .done((data) => { + handleLoginResponse(data) + }) + .fail((jqXHR) => { + toastMessage(jqXHR.responseJSON.error, 3000, 'alert alert-danger') + if (jqXHR.status === 401) { + $('#login-message').html( + `
${_t('NOT_CONNECTED')}
` + ) + $('.login-fields').removeClass('hide') + } + }) return false }) $('[name="duplicate-action"]').on('click', (e) => { const btnAction = e.currentTarget.value - $.ajax({ - method: 'POST', + const settings = { + cache: false, + async: true, + crossDomain: true, url: `${shortUrl}/?api/pages/${$('#newTag').val()}/duplicate`, + method: 'POST', + headers: { + accept: 'application/json', + 'Access-Control-Allow-Origin': '*' + }, data: $('#form-duplication').serialize() - }).done((d) => { - if (btnAction === 'open') { - document.location = `${shortUrl}/?${d.newTag}` - } else if (btnAction === 'edit') { - document.location = `${shortUrl}/?${d.newTag}/edit` - } else { - const url = document.location.href.replace(/\/duplicate.*/, '') - document.location = url - } - }).fail((jqXHR) => { - toastMessage(`${_t('ERROR')} ${jqXHR.status}`, 3000, 'alert alert-danger') - }) + } + $.ajax(settings) + .done((d) => { + if (btnAction === 'open') { + document.location = `${shortUrl}/?${d.newTag}` + } else if (btnAction === 'edit') { + document.location = `${shortUrl}/?${d.newTag}/edit` + } else { + const url = document.location.href.replace(/\/duplicate.*/, '') + document.location = url + } + }) + .fail((jqXHR) => { + toastMessage( + `${_t('ERROR')} ${jqXHR.status}`, + 3000, + 'alert alert-danger' + ) + }) return false }) @@ -128,43 +163,69 @@ document.addEventListener('DOMContentLoaded', () => { $.ajax({ method: 'GET', 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')}
`) - $('.login-fields').removeClass('hide') - } else { - toastMessage(_t('NOT_WIKI_OR_OLD_WIKI', { url }), 3000, 'alert alert-danger') - } }) + .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')}
` + ) + $('.login-fields').removeClass('hide') + } else { + toastMessage( + _t('NOT_WIKI_OR_OLD_WIKI', { url }), + 3000, + 'alert alert-danger' + ) + } + }) } else { toastMessage(_t('NOT_VALID_URL', { url }), 3000, 'alert alert-danger') } diff --git a/tools/templates/templates/barreredaction_basic.twig b/tools/templates/templates/barreredaction_basic.twig index e347d65ec..15bdefd2b 100644 --- a/tools/templates/templates/barreredaction_basic.twig +++ b/tools/templates/templates/barreredaction_basic.twig @@ -17,20 +17,20 @@ {% endif %} - + {##} -