Skip to content

Commit

Permalink
fix(duplication): hide duplication to another wiki (i ll be back....)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrflos committed Jan 19, 2025
1 parent cbf33d4 commit d5f1ba0
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 91 deletions.
16 changes: 8 additions & 8 deletions includes/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ public function getDocumentation()
'<h4>' . _t('LIST') . ' ' . _t('USERS') . '</h4>' . "\n" .
'<p><code>GET ' . $urlUser . '</code></p>' . "\n" .
'<h4>' . _t('GET') . ' ' . _t('USER') . '</h4>' . "\n" .
'<p><code>GET ' . $urlUser . '/{userId}' . '</code></p>' . "\n" . '<h4>' . _t('CREATE') . ' ' . _t('USER') . '</h4>' . "\n" .
'<p><code>GET ' . $urlUser . '/{userId}</code></p>' . "\n" . '<h4>' . _t('CREATE') . ' ' . _t('USER') . '</h4>' . "\n" .
'<p><code>POST ' . $urlUser . '</code></p>' . "\n" .
'<p><code> ' . 'name=…&email=…' . '</code></p>' . "\n" .
'<p><code> name=…&email=…</code></p>' . "\n" .
'<h4>' . _t('DELETE') . ' ' . _t('USER') . '</h4>' . "\n" .
'<p><code>POST ' . $urlUser . '/{userId}/delete' . '</code></p>' . "\n";
'<p><code>POST ' . $urlUser . '/{userId}/delete</code></p>' . "\n";

$urlGroup = $this->wiki->Href('', 'api/groups');
$output .= '<h2>' . _t('GROUPS') . '</h2>' . "\n" .
'<h4>' . _t('LIST') . ' ' . _t('GROUPS') . '</h4>' . "\n" .
'<p><code>GET ' . $urlGroup . '</code></p>' . "\n" .
'<h4>' . _t('GET') . ' ' . _t('GROUP') . '</h4>' . "\n" .
'<p><code>GET ' . $urlGroup . '/{group_name}' . '</code></p>' . "\n" . '<h4>' . _t('CREATE') . ' ' . _t('GROUP') . '</h4>' . "\n" .
'<p><code>GET ' . $urlGroup . '/{group_name}</code></p>' . "\n" . '<h4>' . _t('CREATE') . ' ' . _t('GROUP') . '</h4>' . "\n" .
'<p><code>POST ' . $urlGroup . '</code></p>' . "\n" .
'<p><code> ' . 'name=…&users[0]=…&users[1]' . '</code></p>' . "\n" .
'<p><code> name=…&users[0]=…&users[1]</code></p>' . "\n" .
'<h4>' . _t('DELETE') . ' ' . _t('GROUP') . '</h4>' . "\n" .
'<p><code>POST ' . $urlGroup . '/{group_name}/delete' . '</code></p>' . "\n" .
'<p><code>POST ' . $urlGroup . '/{group_name}/delete</code></p>' . "\n" .
'<h4>' . _t('UPDATE') . ' ' . _t('GROUP') . '</h4>' . "\n" .
'<p><code>POST ' . $urlGroup . '/{group_name}/update' . '</code></p>' . "\n" .
'<p><code> ' . 'users[0]=…&users[1]' . '</code></p>' . "\n";
'<p><code>POST ' . $urlGroup . '/{group_name}/update</code></p>' . "\n" .
'<p><code> users[0]=…&users[1]</code></p>' . "\n";

$urlPages = $this->wiki->Href('', 'api/pages');
$output .= '<h2>' . _t('PAGES') . '</h2>' . "\n" .
Expand Down
211 changes: 136 additions & 75 deletions javascripts/handlers/duplicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,56 @@ 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 }))
}

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')
Expand All @@ -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('')
Expand All @@ -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(`<div class="text-danger">${_t('NOT_CONNECTED')}</div>`)
$('.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(
`<div class="text-danger">${_t('NOT_CONNECTED')}</div>`
)
$('.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
})

Expand All @@ -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(`<div class="text-danger">${_t('NOT_CONNECTED')}</div>`)
$('.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(
`<div class="text-danger">${_t('NOT_CONNECTED')}</div>`
)
$('.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')
}
Expand Down
16 changes: 8 additions & 8 deletions tools/templates/templates/barreredaction_basic.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
<i class="fa fa-folder-open"></i>
</a>
{% endif %}
<span class="dropup">
{#<span class="dropup">#}
<a
title="{{ _t('TEMPLATE_DUPLICATE_PAGE') ~ ' ' ~ page }}"
title="{{ _t('TEMPLATE_DUPLICATE_PAGE') ~ ' ' ~ page }}"
class="link-duplicate dropdown-toggle"
class="link-duplicate {#dropdown-toggle#}"
id="duplicate-dropup"
data-toggle="dropdown"
href="{{ linkduplicate }}"
{# data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
aria-expanded="false" #}
>
<i class="fas fa-copy"></i><span>{{ _t('TEMPLATE_DUPLICATE') }}</span>
<span class="caret"></span>
{#<span class="caret"></span>#}
</a>
<ul class="dropdown-menu" aria-labelledby="duplicate-dropup">
{#<ul class="dropdown-menu" aria-labelledby="duplicate-dropup">
<li>
<a
href="{{ linkduplicate }}"
Expand All @@ -48,7 +48,7 @@
</a>
</li>
</ul>
</span>
</span>#}
{% if linkdeletepage is defined %}
<a
href="{{ linkdeletepage }}"
Expand Down

0 comments on commit d5f1ba0

Please sign in to comment.