Skip to content

Commit b765943

Browse files
committed
feat(translation): make translations UI faster to use
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent fa45b47 commit b765943

File tree

8 files changed

+152
-60
lines changed

8 files changed

+152
-60
lines changed

ajax/edit_translation.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141
die();
4242
}
4343

44-
if (!isset($_POST['plugin_formcreator_translations_id'])) {
45-
http_response_code(400);
46-
die();
47-
}
44+
// if (!isset($_POST['plugin_formcreator_translations_id'])) {
45+
// http_response_code(400);
46+
// die();
47+
// }
4848

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

55-
echo PluginFormcreatorTranslation::getEditor($formLanguage, $_POST['plugin_formcreator_translations_id']);
55+
echo PluginFormcreatorTranslation::getEditorFieldsHtml($formLanguage, $_POST['plugin_formcreator_translations_id']);

ajax/translation.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* ---------------------------------------------------------------------
4+
* Formcreator is a plugin which allows creation of custom forms of
5+
* easy access.
6+
* ---------------------------------------------------------------------
7+
* LICENSE
8+
*
9+
* This file is part of Formcreator.
10+
*
11+
* Formcreator is free software; you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation; either version 2 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* Formcreator is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
23+
* ---------------------------------------------------------------------
24+
* @copyright Copyright © 2011 - 2021 Teclib'
25+
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
26+
* @link https://github.com/pluginsGLPI/formcreator/
27+
* @link https://pluginsglpi.github.io/formcreator/
28+
* @link http://plugins.glpi-project.org/#/plugin/formcreator
29+
* ---------------------------------------------------------------------
30+
*/
31+
32+
include ('../../../inc/includes.php');
33+
// Check if plugin is activated...
34+
if (!(new Plugin())->isActivated('formcreator')) {
35+
Html::displayNotFoundError();
36+
}
37+
38+
Session::checkRight('entity', UPDATE);
39+
40+
if (!isset($_POST['plugin_formcreator_forms_languages_id'])) {
41+
http_response_code(400);
42+
die();
43+
}
44+
if (!isset($_POST['id'])) {
45+
http_response_code(400);
46+
die();
47+
}
48+
if (!isset($_POST['value'])) {
49+
http_response_code(400);
50+
die();
51+
}
52+
53+
if (!(new PluginFormcreatorTranslation())->add($_POST)) {
54+
http_response_code(400);
55+
die();
56+
}

front/form_language.form.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,6 @@
4545
} else if (isset($_POST['update'])) {
4646
$formLanguage->update($_POST);
4747
Html::back();
48-
} else if (isset($_POST['save_translation'])) {
49-
if (!isset($_POST['plugin_formcreator_forms_languages_id'])) {
50-
http_response_code(400);
51-
die();
52-
}
53-
if (!isset($_POST['id'])) {
54-
http_response_code(400);
55-
die();
56-
}
57-
if (!isset($_POST['value'])) {
58-
http_response_code(400);
59-
die();
60-
}
61-
if (!(new PluginFormcreatorTranslation())->add($_POST)) {
62-
http_response_code(400);
63-
die();
64-
}
65-
Html::back();
6648
} else if (isset($_POST['delete'])) {
6749
if ($formLanguage->getFromDB((int) $_POST['id'])) {
6850
$formLanguage->massDeleteTranslations($_POST);

inc/form.class.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,6 @@ public function displayUserForm() : void {
12371237
if (file_exists($phpfile)) {
12381238
$TRANSLATE->addTranslationFile('phparray', $phpfile, $domain, $_SESSION['glpilanguage']);
12391239
}
1240-
12411240
// form title
12421241
echo "<h1 class='form-title'>";
12431242
echo __($this->fields['name'], $domain) . "&nbsp;";
@@ -2621,7 +2620,17 @@ public function getTranslations(string $language) : array {
26212620
return [];
26222621
}
26232622

2624-
$translations = include_once($file);
2623+
// $lockHandle = fopen($file, 'r');
2624+
// $isLocked = flock($lockHandle, LOCK_SH);
2625+
// $lockTries = 1;
2626+
// while (!$isLocked && $lockTries < 20) {
2627+
// $isLocked = flock($lockHandle, LOCK_SH);
2628+
// $lockTries++;
2629+
// usleep(50000);
2630+
// }
2631+
opcache_invalidate($file, true);
2632+
$translations = include($file);
2633+
// fclose($lockHandle);
26252634
if (!is_array($translations)) {
26262635
return [];
26272636
}

inc/form_language.class.php

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,27 @@ public function pre_deleteItem() {
133133
return true;
134134
}
135135

136-
public function massDeleteTRanslations($post) {
136+
public function massDeleteTranslations($post) {
137+
global $TRANSLATE;
138+
139+
$form = new PluginFormcreatorForm();
140+
if (!$form->getFromDB($this->fields['plugin_formcreator_forms_id'])) {
141+
return;
142+
}
143+
$translations = $form->getTranslations($this->fields['name']);
137144
foreach ($post['plugin_formcreator_translation'] as $translationId => $checked) {
138145
if ($checked != '1') {
139146
continue;
140147
}
141-
$translation = new PluginFormcreatorTranslation();
142-
$translation->delete($this, [
143-
'id' => $translationId,
148+
$translated = $form->getTranslatableStrings([
149+
'id' => $translationId,
150+
'language' => $this->fields['name'],
144151
]);
152+
$original = $translated[$translated['id'][$translationId]][$translationId];
153+
unset($translations[$original]);
145154
}
155+
$form->setTranslations($this->fields['name'], $translations);
156+
$TRANSLATE->clearCache('formcreator', $this->fields['name']);
146157
}
147158

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

203214
echo '<div data-itemtype="PluginFormcreatorForm_Language" data-id="' . $this->getID() . '">';
204215
$options['formtitle'] = __('Add a translation', 'formcreator');
216+
$options['target'] = 'javascript:plugin_formcreator.saveNewTranslation(this);';
217+
205218
$this->initForm($this->getID(), $options);
206-
$this->showFormHeader($options);
207-
echo "<tr class='tab_bg_1'>";
219+
//$this->showFormHeader($options);
220+
echo '<form name="plugin_formcreator_translation" onsubmit="plugin_formcreator.saveNewTranslation(this); return false;" >';
221+
echo "<div class='spaced' id='tabsbody'>";
222+
echo "<table class='tab_cadre_fixe' id='mainformtable'>";
223+
224+
echo "<tr class='tab_bg_1'><td>";
208225
echo Html::hidden('name', ['value' => $this->fields['name']]);
209226
echo "</td><td width='50%'>&nbsp;</td></tr>";
210227

211-
echo '<tr>';
212-
echo '<td>' . __('String to translate', 'formcreator') . '</td>';
213-
echo '<td>';
214-
echo PluginFormcreatorTranslation::dropdown([
215-
'condition' => [
216-
self::getForeignKeyField() => $this->getID(),
217-
'is_translated' => false,
218-
],
219-
'on_change' => "plugin_formcreator.showTranslationEditor(this)",
220-
]);
221-
echo '</td>';
222-
echo '</tr>';
223-
224228
echo '<tr id="plugin_formcreator_editTranslation">';
225-
echo '<td>';
226-
echo '</td>';
229+
// echo '<td>';
230+
// echo '</td>';
231+
echo PluginFormcreatorTranslation::getEditorFieldsHtml($this);
227232
echo '</tr>';
233+
echo "<tr class='tab_bg_1'><td>";
234+
echo "</td><td width='50%'>&nbsp;</td></tr>";
228235

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

247-
PluginFormcreatorTranslation::getEditor($this, $input['plugin_formcreator_translations_id']);
256+
echo PluginFormcreatorTranslation::getEditorFieldsHtml($this, $input['plugin_formcreator_translations_id']);
248257

249258
echo '<tr class="tab_bg_2">'
250259
. '<td class="center" colspan="4">'

inc/translation.class.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,42 +143,57 @@ public static function getDropdownValue($post, $json = true) {
143143
* @param string $id
144144
* @return void
145145
*/
146-
public static function getEditor(PluginFormcreatorForm_Language $formLanguage, string $id) {
146+
public static function getEditorFieldsHtml(PluginFormcreatorForm_Language $formLanguage, string $id = '') {
147+
$out = '';
147148
$form = new PluginFormcreatorForm();
148149
$form->getFromDB($formLanguage->fields['plugin_formcreator_forms_id']);
149150

150-
// Find the string from its ID
151+
// Find the strings to translate
151152
$translatableString = $form->getTranslatableStrings([
152-
'id' => $id,
153-
'language' => $formLanguage->fields['name'],
153+
'language' => $formLanguage->fields['name'],
154+
'is_translated' => ($id != ''),
154155
]);
155-
156+
if (count($translatableString['id']) < 1) {
157+
$out .= '<td colspan="2">' . __('No more string to translate', 'formcreator') . '</td>';
158+
return $out;
159+
}
160+
if ($id == '') {
161+
// find the first string to translate
162+
reset($translatableString['id']);
163+
$id = key($translatableString['id']);
164+
}
156165
if (!isset($translatableString['id'][$id])) {
166+
// Show nothing if string definitively not found
167+
// Should not happen
157168
return '';
158169
}
159170

160-
$type = $translatableString['id'][$id] ?? 'text';
171+
$type = $translatableString['id'][$id] ?? 'string';
161172
$original = $translatableString[$type][$id];
162173

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

166178
switch ($type) {
167179
case 'itemlink':
168180
case 'string':
169-
echo '<td>' . $original . Html::hidden("id", ['value' => $id]) . '</td>';
170-
echo '<td>' . Html::input("value", ['value' => $translatedString]) . '</td>';
181+
$out .= '<td>' . $original . Html::hidden("id", ['value' => $id]) . '</td>';
182+
$out .= '<td>' . Html::input("value", ['value' => $translatedString]) . '</td>';
171183
break;
172184

173185
case 'text':
174-
echo '<td>' . Html::entity_decode_deep($original) . Html::hidden("id", ['value' => $id]) . '</td>';
175-
echo '<td>' . Html::textarea([
186+
$out .= '<td>' . Html::entity_decode_deep($original) . Html::hidden("id", ['value' => $id]) . '</td>';
187+
$out .= '<td>' . Html::textarea([
176188
'name' => "value",
177189
'value' => $translatedString,
178190
'enable_richtext' => true,
179191
'display' => false,
180192
]) . '</td>';
181193
}
194+
$out .= Html::scriptBlock('$(\'input[name="value"]\').focus(); $(\'textarea[name="value"]\').focus();');
195+
196+
return $out;
182197
}
183198

184199
/**

js/scripts.js.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,15 +1138,19 @@ function buildTiles(list) {
11381138

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

11471148
this.newTranslation = function (formLanguageId) {
11481149
var modal = $(this.spinner)
1149-
.dialog(this.modalSetings);
1150+
.dialog(this.modalSetings)
1151+
.on('dialogclose', function (e, ui) {
1152+
reloadTab();
1153+
});
11501154
modal.load(
11511155
'../ajax/form_language.php', {
11521156
action: 'newTranslation',
@@ -1164,6 +1168,22 @@ function buildTiles(list) {
11641168
)
11651169
}
11661170

1171+
this.saveNewTranslation = function () {
1172+
var that = this;
1173+
var form = document.querySelector('form[name="plugin_formcreator_translation"]');
1174+
$.ajax({
1175+
url: '../ajax/translation.php',
1176+
type: 'POST',
1177+
data: $(form).serialize()
1178+
}).fail(function () {
1179+
// fix for GLPI <= 9.5.2
1180+
$('[id^="message_after_redirect_"]').remove();
1181+
displayAjaxMessageAfterRedirect();
1182+
}).success(function () {
1183+
that.showTranslationEditor(form);
1184+
});
1185+
}
1186+
11671187
this.showUpdateTranslationForm = function (object) {
11681188
var formLanguageId = $(object).closest('[data-itemtype="PluginFormcreatorForm_Language"][data-id]').attr('data-id');
11691189
var translationId = $(object.closest('[data-itemtype="PluginFormcreatorTranslation"]')).attr('data-id');

setup.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ function plugin_init_formcreator() {
264264
FORMCREATOR_ROOTDOC . '/front/form.form.php',
265265
FORMCREATOR_ROOTDOC . '/front/formanswer.form.php',
266266
FORMCREATOR_ROOTDOC . '/front/issue.form.php',
267+
FORMCREATOR_ROOTDOC . '/front/form_language.form.php',
267268
];
268269
foreach ($pages as $page) {
269270
if (strpos($_SERVER['REQUEST_URI'], $page) !== false) {

0 commit comments

Comments
 (0)