From 9b30339be486677bee24a19056e236f7fa50f66e Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Thu, 15 Oct 2020 11:21:07 +0200 Subject: [PATCH] feat: translate answers for targets and notifications Signed-off-by: Thierry Bugier --- inc/field/actorfield.class.php | 3 ++- inc/field/checkboxesfield.class.php | 5 +++-- inc/field/datefield.class.php | 2 +- inc/field/datetimefield.class.php | 2 +- inc/field/descriptionfield.class.php | 5 +++-- inc/field/dropdownfield.class.php | 3 ++- inc/field/filefield.class.php | 3 ++- inc/field/floatfield.class.php | 2 +- inc/field/hiddenfield.class.php | 2 +- inc/field/hostnamefield.class.php | 2 +- inc/field/ipfield.class.php | 2 +- inc/field/radiosfield.class.php | 9 +++------ inc/field/requesttypefield.class.php | 2 +- inc/field/tagfield.class.php | 3 ++- inc/field/textareafield.class.php | 2 +- inc/field/textfield.class.php | 2 +- inc/field/timefield.class.php | 6 ++++-- inc/field/urgencyfield.class.php | 3 ++- inc/fieldinterface.class.php | 5 +++-- inc/formanswer.class.php | 3 ++- 20 files changed, 37 insertions(+), 29 deletions(-) diff --git a/inc/field/actorfield.class.php b/inc/field/actorfield.class.php index e87f434b5..a19bcd8df 100644 --- a/inc/field/actorfield.class.php +++ b/inc/field/actorfield.class.php @@ -213,7 +213,8 @@ public function getValueForDesign(): string { return implode("\r\n", $value); } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string + { $value = []; foreach ($this->value as $item) { if (filter_var($item, FILTER_VALIDATE_EMAIL) !== false) { diff --git a/inc/field/checkboxesfield.class.php b/inc/field/checkboxesfield.class.php index 71eae494c..5ae7ff74d 100644 --- a/inc/field/checkboxesfield.class.php +++ b/inc/field/checkboxesfield.class.php @@ -280,7 +280,8 @@ public function hasInput($input): bool { return isset($input['formcreator_field_' . $this->question->getID()]); } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string + { $value = []; $values = $this->getAvailableValues(); @@ -290,7 +291,7 @@ public function getValueForTargetText($richText): string { foreach ($this->value as $input) { if (in_array($input, $values)) { - $value[] = $input; + $value[] = __($input, $domain); } } diff --git a/inc/field/datefield.class.php b/inc/field/datefield.class.php index fc34bdb78..f628d511f 100644 --- a/inc/field/datefield.class.php +++ b/inc/field/datefield.class.php @@ -117,7 +117,7 @@ public function getValueForDesign(): string { return $this->value; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string { return Html::convDate($this->value); } diff --git a/inc/field/datetimefield.class.php b/inc/field/datetimefield.class.php index 8cd9662b7..c0784ede7 100644 --- a/inc/field/datetimefield.class.php +++ b/inc/field/datetimefield.class.php @@ -123,7 +123,7 @@ public function hasInput($input): bool { return isset($input['formcreator_field_' . $this->question->getID()]); } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string { return Html::convDateTime($this->value); } diff --git a/inc/field/descriptionfield.class.php b/inc/field/descriptionfield.class.php index 51a75bead..f642359c3 100644 --- a/inc/field/descriptionfield.class.php +++ b/inc/field/descriptionfield.class.php @@ -72,10 +72,11 @@ public function getValueForDesign(): string { return ''; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string + { $text = $this->question->fields['description']; if (!$richText) { - $text = nl2br(strip_tags(html_entity_decode($text))); + $text = nl2br(strip_tags(html_entity_decode(__($text, $domain)))); } return $text; diff --git a/inc/field/dropdownfield.class.php b/inc/field/dropdownfield.class.php index f0d650a27..312f8bda9 100644 --- a/inc/field/dropdownfield.class.php +++ b/inc/field/dropdownfield.class.php @@ -406,7 +406,8 @@ public function getValueForDesign(): string { return $this->value; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string + { $DbUtil = new DbUtils(); $itemtype = $this->getSubItemtype(); if ($itemtype == User::class) { diff --git a/inc/field/filefield.class.php b/inc/field/filefield.class.php index e04aa5e9b..e23953c18 100644 --- a/inc/field/filefield.class.php +++ b/inc/field/filefield.class.php @@ -102,7 +102,8 @@ public function getValueForDesign(): string { return ''; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string + { return $this->value; } diff --git a/inc/field/floatfield.class.php b/inc/field/floatfield.class.php index f377289bc..9bdcec020 100644 --- a/inc/field/floatfield.class.php +++ b/inc/field/floatfield.class.php @@ -126,7 +126,7 @@ public function getValueForDesign(): string { return $this->value; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string { return $this->value; } diff --git a/inc/field/hiddenfield.class.php b/inc/field/hiddenfield.class.php index c3866e4e4..041db0224 100644 --- a/inc/field/hiddenfield.class.php +++ b/inc/field/hiddenfield.class.php @@ -111,7 +111,7 @@ public static function getName(): string { return _n('Hidden field', 'Hidden fields', 1); } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string { return str_replace("\n", '\r\n', $this->value); } diff --git a/inc/field/hostnamefield.class.php b/inc/field/hostnamefield.class.php index 90f3e317e..3e8e8aa8f 100644 --- a/inc/field/hostnamefield.class.php +++ b/inc/field/hostnamefield.class.php @@ -88,7 +88,7 @@ public function getValueForDesign(): string { return ''; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string { return $this->value; } diff --git a/inc/field/ipfield.class.php b/inc/field/ipfield.class.php index dde4e9d0b..22d455d0c 100644 --- a/inc/field/ipfield.class.php +++ b/inc/field/ipfield.class.php @@ -96,7 +96,7 @@ public function getValueForDesign(): string { return $this->value; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string { return $this->value; } diff --git a/inc/field/radiosfield.class.php b/inc/field/radiosfield.class.php index 6bff79701..2f7fd3241 100644 --- a/inc/field/radiosfield.class.php +++ b/inc/field/radiosfield.class.php @@ -197,12 +197,8 @@ public function parseDefaultValue($defaultValue) { $this->value = array_shift($this->value); } -<<<<<<< HEAD - public function serializeValue():string { -======= public function serializeValue(): string { ->>>>>>> f10ec68e... feat: translatable forms if ($this->value === null || $this->value === '') { return ''; } @@ -224,8 +220,9 @@ public function getValueForDesign(): string { return $this->value; } - public function getValueForTargetText($richText): string { - return $this->value; + public function getValueForTargetText($domain, $richText): string + { + return __($this->value, $domain); } public function moveUploads() { diff --git a/inc/field/requesttypefield.class.php b/inc/field/requesttypefield.class.php index 53790fd53..bcbd61125 100644 --- a/inc/field/requesttypefield.class.php +++ b/inc/field/requesttypefield.class.php @@ -171,7 +171,7 @@ public function hasInput($input): bool { return isset($input['formcreator_field_' . $this->question->getID()]); } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string { $available = $this->getAvailableValues(); return $available[$this->value]; } diff --git a/inc/field/tagfield.class.php b/inc/field/tagfield.class.php index e85a55103..ac3bce423 100644 --- a/inc/field/tagfield.class.php +++ b/inc/field/tagfield.class.php @@ -146,7 +146,8 @@ public function getValueForDesign(): string { return implode("\r\n", $this->value); } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string + { $value = Dropdown::getDropdownName(PluginTagTag::getTable(), $this->value); return $value; } diff --git a/inc/field/textareafield.class.php b/inc/field/textareafield.class.php index de0c21206..ac2cecffd 100644 --- a/inc/field/textareafield.class.php +++ b/inc/field/textareafield.class.php @@ -215,7 +215,7 @@ public function parseAnswerValues($input, $nonDestructive = false): bool { return true; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string { $value = $this->value; if (!$richText) { $value = Toolbox::unclean_cross_side_scripting_deep($value); diff --git a/inc/field/textfield.class.php b/inc/field/textfield.class.php index 22a65cccc..a3aa15bc9 100644 --- a/inc/field/textfield.class.php +++ b/inc/field/textfield.class.php @@ -131,7 +131,7 @@ public function getValueForDesign(): string { return $this->value; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string { return $this->value; } diff --git a/inc/field/timefield.class.php b/inc/field/timefield.class.php index 4d5c0b1d4..9f9aea64a 100644 --- a/inc/field/timefield.class.php +++ b/inc/field/timefield.class.php @@ -84,7 +84,8 @@ public function getDesignSpecializationField(): array { ]; } - public function getRenderedHtml($domain, $canEdit = true): string { + public function getRenderedHtml($domain, $canEdit = true): string + { if (!$canEdit) { return $this->value; } @@ -117,7 +118,8 @@ public function getValueForDesign(): string { return $this->value; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string + { $date = DateTime::createFromFormat("H:i:s", $this->value); if ($date === false) { return ' '; diff --git a/inc/field/urgencyfield.class.php b/inc/field/urgencyfield.class.php index 73589d776..1c0a6eb4e 100644 --- a/inc/field/urgencyfield.class.php +++ b/inc/field/urgencyfield.class.php @@ -175,7 +175,8 @@ public function getValueForDesign(): string { return $this->value; } - public function getValueForTargetText($richText): string { + public function getValueForTargetText($domain, $richText): string + { $available = $this->getAvailableValues(); return $available[$this->value]; } diff --git a/inc/fieldinterface.class.php b/inc/fieldinterface.class.php index 66d3e08bd..aeaad0761 100644 --- a/inc/fieldinterface.class.php +++ b/inc/fieldinterface.class.php @@ -112,11 +112,12 @@ public function getValueForDesign() : string; /** * Get the value of the field for display in a target * - * @param boolean $richText Enable rich text mode for field rendering + * @param string $domain locales domain + * @param boolean $richText Enable rich text mode for field rendering * * @return string */ - public function getValueForTargetText($richText) : string; + public function getValueForTargetText($domain, $richText): string; /** * Move uploaded files and make Document items diff --git a/inc/formanswer.class.php b/inc/formanswer.class.php index 3ad4aacdd..fba22caba 100644 --- a/inc/formanswer.class.php +++ b/inc/formanswer.class.php @@ -1094,6 +1094,7 @@ public function parseTags($content, PluginFormcreatorTargetInterface $target = n $form = $this->getForm(); $this->getQuestionFields($form->getID()); $this->deserializeAnswers(); + $domain = PluginFormcreatorForm::getTranslationDomain($form->getID()); foreach ($this->questionFields as $questionId => $field) { $question = $field->getQuestion(); @@ -1101,7 +1102,7 @@ public function parseTags($content, PluginFormcreatorTargetInterface $target = n $value = ''; if (PluginFormcreatorFields::isVisible($question, $this->questionFields)) { $name = $question->fields['name']; - $value = $this->questionFields[$questionId]->getValueForTargetText($richText); + $value = $this->questionFields[$questionId]->getValueForTargetText($domain, $richText); } $content = str_replace('##question_' . $questionId . '##', Toolbox::addslashes_deep($name), $content);