Skip to content

Commit

Permalink
feat(translation): make translations UI faster to use
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Feb 15, 2021
1 parent fa45b47 commit b765943
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 60 deletions.
10 changes: 5 additions & 5 deletions ajax/edit_translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
die();
}

if (!isset($_POST['plugin_formcreator_translations_id'])) {
http_response_code(400);
die();
}
// if (!isset($_POST['plugin_formcreator_translations_id'])) {
// http_response_code(400);
// die();
// }

$formLanguage = new PluginFormcreatorForm_Language();
if (!$formLanguage->getFromDB((int) $_POST['plugin_formcreator_form_languages_id'])) {
http_response_code(400);
die();
}

echo PluginFormcreatorTranslation::getEditor($formLanguage, $_POST['plugin_formcreator_translations_id']);
echo PluginFormcreatorTranslation::getEditorFieldsHtml($formLanguage, $_POST['plugin_formcreator_translations_id']);
56 changes: 56 additions & 0 deletions ajax/translation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2021 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/

include ('../../../inc/includes.php');
// Check if plugin is activated...
if (!(new Plugin())->isActivated('formcreator')) {
Html::displayNotFoundError();
}

Session::checkRight('entity', UPDATE);

if (!isset($_POST['plugin_formcreator_forms_languages_id'])) {
http_response_code(400);
die();
}
if (!isset($_POST['id'])) {
http_response_code(400);
die();
}
if (!isset($_POST['value'])) {
http_response_code(400);
die();
}

if (!(new PluginFormcreatorTranslation())->add($_POST)) {
http_response_code(400);
die();
}
18 changes: 0 additions & 18 deletions front/form_language.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,6 @@
} else if (isset($_POST['update'])) {
$formLanguage->update($_POST);
Html::back();
} else if (isset($_POST['save_translation'])) {
if (!isset($_POST['plugin_formcreator_forms_languages_id'])) {
http_response_code(400);
die();
}
if (!isset($_POST['id'])) {
http_response_code(400);
die();
}
if (!isset($_POST['value'])) {
http_response_code(400);
die();
}
if (!(new PluginFormcreatorTranslation())->add($_POST)) {
http_response_code(400);
die();
}
Html::back();
} else if (isset($_POST['delete'])) {
if ($formLanguage->getFromDB((int) $_POST['id'])) {
$formLanguage->massDeleteTranslations($_POST);
Expand Down
13 changes: 11 additions & 2 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,6 @@ public function displayUserForm() : void {
if (file_exists($phpfile)) {
$TRANSLATE->addTranslationFile('phparray', $phpfile, $domain, $_SESSION['glpilanguage']);
}

// form title
echo "<h1 class='form-title'>";
echo __($this->fields['name'], $domain) . "&nbsp;";
Expand Down Expand Up @@ -2621,7 +2620,17 @@ public function getTranslations(string $language) : array {
return [];
}

$translations = include_once($file);
// $lockHandle = fopen($file, 'r');
// $isLocked = flock($lockHandle, LOCK_SH);
// $lockTries = 1;
// while (!$isLocked && $lockTries < 20) {
// $isLocked = flock($lockHandle, LOCK_SH);
// $lockTries++;
// usleep(50000);
// }
opcache_invalidate($file, true);
$translations = include($file);
// fclose($lockHandle);
if (!is_array($translations)) {
return [];
}
Expand Down
55 changes: 32 additions & 23 deletions inc/form_language.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,27 @@ public function pre_deleteItem() {
return true;
}

public function massDeleteTRanslations($post) {
public function massDeleteTranslations($post) {
global $TRANSLATE;

$form = new PluginFormcreatorForm();
if (!$form->getFromDB($this->fields['plugin_formcreator_forms_id'])) {
return;
}
$translations = $form->getTranslations($this->fields['name']);
foreach ($post['plugin_formcreator_translation'] as $translationId => $checked) {
if ($checked != '1') {
continue;
}
$translation = new PluginFormcreatorTranslation();
$translation->delete($this, [
'id' => $translationId,
$translated = $form->getTranslatableStrings([
'id' => $translationId,
'language' => $this->fields['name'],
]);
$original = $translated[$translated['id'][$translationId]][$translationId];
unset($translations[$original]);
}
$form->setTranslations($this->fields['name'], $translations);
$TRANSLATE->clearCache('formcreator', $this->fields['name']);
}

public function showForm($ID, $options = []) {
Expand Down Expand Up @@ -202,29 +213,25 @@ public function showNewTranslation($options = []) {

echo '<div data-itemtype="PluginFormcreatorForm_Language" data-id="' . $this->getID() . '">';
$options['formtitle'] = __('Add a translation', 'formcreator');
$options['target'] = 'javascript:plugin_formcreator.saveNewTranslation(this);';

$this->initForm($this->getID(), $options);
$this->showFormHeader($options);
echo "<tr class='tab_bg_1'>";
//$this->showFormHeader($options);
echo '<form name="plugin_formcreator_translation" onsubmit="plugin_formcreator.saveNewTranslation(this); return false;" >';
echo "<div class='spaced' id='tabsbody'>";
echo "<table class='tab_cadre_fixe' id='mainformtable'>";

echo "<tr class='tab_bg_1'><td>";
echo Html::hidden('name', ['value' => $this->fields['name']]);
echo "</td><td width='50%'>&nbsp;</td></tr>";

echo '<tr>';
echo '<td>' . __('String to translate', 'formcreator') . '</td>';
echo '<td>';
echo PluginFormcreatorTranslation::dropdown([
'condition' => [
self::getForeignKeyField() => $this->getID(),
'is_translated' => false,
],
'on_change' => "plugin_formcreator.showTranslationEditor(this)",
]);
echo '</td>';
echo '</tr>';

echo '<tr id="plugin_formcreator_editTranslation">';
echo '<td>';
echo '</td>';
// echo '<td>';
// echo '</td>';
echo PluginFormcreatorTranslation::getEditorFieldsHtml($this);
echo '</tr>';
echo "<tr class='tab_bg_1'><td>";
echo "</td><td width='50%'>&nbsp;</td></tr>";

echo '<tr class="tab_bg_2">'
. '<td class="center" colspan="4">'
Expand All @@ -242,9 +249,11 @@ public function showNewTranslation($options = []) {
public function showTranslationEntry($input) : void {
$options['formtitle'] = __('Add a translation', 'formcreator');
$this->initForm($this->getID(), $options);
$this->showFormHeader($options);
echo '<form name="plugin_formcreator_translation" onsubmit="plugin_formcreator.saveNewTranslation(this); return false;" >';
echo "<div class='spaced' id='tabsbody'>";
echo "<table class='tab_cadre_fixe' id='mainformtable'>";

PluginFormcreatorTranslation::getEditor($this, $input['plugin_formcreator_translations_id']);
echo PluginFormcreatorTranslation::getEditorFieldsHtml($this, $input['plugin_formcreator_translations_id']);

echo '<tr class="tab_bg_2">'
. '<td class="center" colspan="4">'
Expand Down
35 changes: 25 additions & 10 deletions inc/translation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,42 +143,57 @@ public static function getDropdownValue($post, $json = true) {
* @param string $id
* @return void
*/
public static function getEditor(PluginFormcreatorForm_Language $formLanguage, string $id) {
public static function getEditorFieldsHtml(PluginFormcreatorForm_Language $formLanguage, string $id = '') {
$out = '';
$form = new PluginFormcreatorForm();
$form->getFromDB($formLanguage->fields['plugin_formcreator_forms_id']);

// Find the string from its ID
// Find the strings to translate
$translatableString = $form->getTranslatableStrings([
'id' => $id,
'language' => $formLanguage->fields['name'],
'language' => $formLanguage->fields['name'],
'is_translated' => ($id != ''),
]);

if (count($translatableString['id']) < 1) {
$out .= '<td colspan="2">' . __('No more string to translate', 'formcreator') . '</td>';
return $out;
}
if ($id == '') {
// find the first string to translate
reset($translatableString['id']);
$id = key($translatableString['id']);
}
if (!isset($translatableString['id'][$id])) {
// Show nothing if string definitively not found
// Should not happen
return '';
}

$type = $translatableString['id'][$id] ?? 'text';
$type = $translatableString['id'][$id] ?? 'string';
$original = $translatableString[$type][$id];

// Find the translation if any
$translations = $form->getTranslations($formLanguage->fields['name']);
$translatedString = $translations[$original] ?? '';

switch ($type) {
case 'itemlink':
case 'string':
echo '<td>' . $original . Html::hidden("id", ['value' => $id]) . '</td>';
echo '<td>' . Html::input("value", ['value' => $translatedString]) . '</td>';
$out .= '<td>' . $original . Html::hidden("id", ['value' => $id]) . '</td>';
$out .= '<td>' . Html::input("value", ['value' => $translatedString]) . '</td>';
break;

case 'text':
echo '<td>' . Html::entity_decode_deep($original) . Html::hidden("id", ['value' => $id]) . '</td>';
echo '<td>' . Html::textarea([
$out .= '<td>' . Html::entity_decode_deep($original) . Html::hidden("id", ['value' => $id]) . '</td>';
$out .= '<td>' . Html::textarea([
'name' => "value",
'value' => $translatedString,
'enable_richtext' => true,
'display' => false,
]) . '</td>';
}
$out .= Html::scriptBlock('$(\'input[name="value"]\').focus(); $(\'textarea[name="value"]\').focus();');

return $out;
}

/**
Expand Down
24 changes: 22 additions & 2 deletions js/scripts.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -1138,15 +1138,19 @@ function buildTiles(list) {

this.showTranslationEditor = function (object) {
var formlanguageId = $(object).closest('[data-itemtype="PluginFormcreatorForm_Language"][data-id]').attr('data-id');
var plugin_formcreator_translations_id = $(object).find('input[name="id"]').val();
$('#plugin_formcreator_editTranslation').load(formcreatorRootDoc + '/ajax/edit_translation.php', {
plugin_formcreator_form_languages_id: formlanguageId,
plugin_formcreator_translations_id: $(object).val()
plugin_formcreator_translations_id: ''
});
}

this.newTranslation = function (formLanguageId) {
var modal = $(this.spinner)
.dialog(this.modalSetings);
.dialog(this.modalSetings)
.on('dialogclose', function (e, ui) {
reloadTab();
});
modal.load(
'../ajax/form_language.php', {
action: 'newTranslation',
Expand All @@ -1164,6 +1168,22 @@ function buildTiles(list) {
)
}

this.saveNewTranslation = function () {
var that = this;
var form = document.querySelector('form[name="plugin_formcreator_translation"]');
$.ajax({
url: '../ajax/translation.php',
type: 'POST',
data: $(form).serialize()
}).fail(function () {
// fix for GLPI <= 9.5.2
$('[id^="message_after_redirect_"]').remove();
displayAjaxMessageAfterRedirect();
}).success(function () {
that.showTranslationEditor(form);
});
}

this.showUpdateTranslationForm = function (object) {
var formLanguageId = $(object).closest('[data-itemtype="PluginFormcreatorForm_Language"][data-id]').attr('data-id');
var translationId = $(object.closest('[data-itemtype="PluginFormcreatorTranslation"]')).attr('data-id');
Expand Down
1 change: 1 addition & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ function plugin_init_formcreator() {
FORMCREATOR_ROOTDOC . '/front/form.form.php',
FORMCREATOR_ROOTDOC . '/front/formanswer.form.php',
FORMCREATOR_ROOTDOC . '/front/issue.form.php',
FORMCREATOR_ROOTDOC . '/front/form_language.form.php',
];
foreach ($pages as $page) {
if (strpos($_SERVER['REQUEST_URI'], $page) !== false) {
Expand Down

0 comments on commit b765943

Please sign in to comment.