diff --git a/ajax/edit_translation.php b/ajax/edit_translation.php index 01afec065..0f1f9b02d 100644 --- a/ajax/edit_translation.php +++ b/ajax/edit_translation.php @@ -41,10 +41,10 @@ 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'])) { @@ -52,4 +52,4 @@ die(); } -echo PluginFormcreatorTranslation::getEditor($formLanguage, $_POST['plugin_formcreator_translations_id']); \ No newline at end of file +echo PluginFormcreatorTranslation::getEditorFieldsHtml($formLanguage, $_POST['plugin_formcreator_translations_id']); \ No newline at end of file diff --git a/ajax/translation.php b/ajax/translation.php new file mode 100644 index 000000000..703066852 --- /dev/null +++ b/ajax/translation.php @@ -0,0 +1,56 @@ +. + * --------------------------------------------------------------------- + * @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(); +} diff --git a/front/form_language.form.php b/front/form_language.form.php index 4862b6318..f5ca1896c 100644 --- a/front/form_language.form.php +++ b/front/form_language.form.php @@ -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); diff --git a/inc/form.class.php b/inc/form.class.php index 3186a838f..16fc11bb5 100644 --- a/inc/form.class.php +++ b/inc/form.class.php @@ -1237,7 +1237,6 @@ public function displayUserForm() : void { if (file_exists($phpfile)) { $TRANSLATE->addTranslationFile('phparray', $phpfile, $domain, $_SESSION['glpilanguage']); } - // form title echo "

"; echo __($this->fields['name'], $domain) . " "; @@ -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 []; } diff --git a/inc/form_language.class.php b/inc/form_language.class.php index f4607b0cf..bb1f41de1 100644 --- a/inc/form_language.class.php +++ b/inc/form_language.class.php @@ -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 = []) { @@ -202,29 +213,25 @@ public function showNewTranslation($options = []) { echo '
'; $options['formtitle'] = __('Add a translation', 'formcreator'); + $options['target'] = 'javascript:plugin_formcreator.saveNewTranslation(this);'; + $this->initForm($this->getID(), $options); - $this->showFormHeader($options); - echo ""; + //$this->showFormHeader($options); + echo '
'; + echo "
"; + echo ""; + + echo ""; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; + // echo ''; + echo PluginFormcreatorTranslation::getEditorFieldsHtml($this); echo ''; + echo ""; echo '' . '
"; echo Html::hidden('name', ['value' => $this->fields['name']]); echo " 
' . __('String to translate', 'formcreator') . ''; - echo PluginFormcreatorTranslation::dropdown([ - 'condition' => [ - self::getForeignKeyField() => $this->getID(), - 'is_translated' => false, - ], - 'on_change' => "plugin_formcreator.showTranslationEditor(this)", - ]); - echo '
'; - echo ''; + // echo '
"; + echo " 
' @@ -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 ''; + echo "
"; + echo ""; - PluginFormcreatorTranslation::getEditor($this, $input['plugin_formcreator_translations_id']); + echo PluginFormcreatorTranslation::getEditorFieldsHtml($this, $input['plugin_formcreator_translations_id']); echo '' . ''; + 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 ''; - echo ''; + $out .= ''; + $out .= ''; break; case 'text': - echo ''; - echo ''; + $out .= ''; } + $out .= Html::scriptBlock('$(\'input[name="value"]\').focus(); $(\'textarea[name="value"]\').focus();'); + + return $out; } /** diff --git a/js/scripts.js.php b/js/scripts.js.php index cea369c7f..16126294c 100644 --- a/js/scripts.js.php +++ b/js/scripts.js.php @@ -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', @@ -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'); diff --git a/setup.php b/setup.php index baee6e2c7..f0c8a73fc 100644 --- a/setup.php +++ b/setup.php @@ -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) {
' diff --git a/inc/translation.class.php b/inc/translation.class.php index 10e2a73fe..70fd52044 100644 --- a/inc/translation.class.php +++ b/inc/translation.class.php @@ -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 .= '' . __('No more string to translate', 'formcreator') . '' . $original . Html::hidden("id", ['value' => $id]) . '' . Html::input("value", ['value' => $translatedString]) . '' . $original . Html::hidden("id", ['value' => $id]) . '' . Html::input("value", ['value' => $translatedString]) . '' . Html::entity_decode_deep($original) . Html::hidden("id", ['value' => $id]) . '' . Html::textarea([ + $out .= '' . Html::entity_decode_deep($original) . Html::hidden("id", ['value' => $id]) . '' . Html::textarea([ 'name' => "value", 'value' => $translatedString, 'enable_richtext' => true, 'display' => false, ]) . '