Skip to content

Commit

Permalink
feat(duplication): ui to check if distant form is present
Browse files Browse the repository at this point in the history
  • Loading branch information
mrflos committed Aug 12, 2024
1 parent f026991 commit 6ed0aef
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
5 changes: 4 additions & 1 deletion handlers/DuplicateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
]);
}
}
}
4 changes: 2 additions & 2 deletions includes/services/DuplicationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

Expand Down Expand Up @@ -354,4 +354,4 @@ public function humanFilesize($bytes, $decimals = 2)

return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor - 1] . 'B';
}
}
}
31 changes: 31 additions & 0 deletions javascripts/handlers/duplicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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(`<div class="text-danger">${_t('NOT_CONNECTED')}</div>`)
Expand Down
5 changes: 4 additions & 1 deletion lang/yeswikijs_fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
];
9 changes: 8 additions & 1 deletion templates/handlers/duplicate.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
</div>
</div>
</form>
{% if form %}
<script>
var sourceForm = {{ form|json_encode|raw }}
</script>
<input type="hidden" id="form-id" value="{{form.bn_id_nature}}"/>
<div id="form-message"><span class="help-block"></span></div>
{% endif %}
<div class="duplication-fields hide">{# hide the field while not connected #}
{% endif %}
<form id="form-duplication" method="post" action="{{ url({handler: 'duplicate'})}}">
Expand All @@ -46,7 +53,7 @@
<input required type="text" class="form-control" id="newTitle" name="newTitle" placeholder="{{ _t('PAGE_TITLE_TO_DUPLICATE') }}" value="{{pageTitle}}" />
</div>
</div>
{% endif %}
{% endif %}
<div class="form-group">
<label for="newTag" class="control-label">{{ _t('PAGE_TAG_TO_DUPLICATE') }}</label>
<div class="input-group">
Expand Down

0 comments on commit 6ed0aef

Please sign in to comment.