Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -3385,10 +3385,7 @@ public function listTranslationsHandlers(): array
$handlers = [];
$key = sprintf('%s_%d', self::getType(), $this->getID());
$category_name = sprintf('%s: %s', self::getTypeName(), $this->getName());
if (
!empty($this->fields['custom_helpdesk_home_title'])
&& $this->fields['custom_helpdesk_home_title'] != self::CONFIG_PARENT
) {
if ($this->fields['custom_helpdesk_home_title'] != self::CONFIG_PARENT) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_CUSTOM_HELPDESK_HOME_TITLE,
Expand Down
36 changes: 16 additions & 20 deletions src/Glpi/Form/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,23 @@ public function listTranslationsHandlers(): array
$category_name = sprintf('%s: %s', self::getTypeName(), $this->getName());
$handlers = [];

if (!empty($this->fields['name'])) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_NAME,
name: __('Comment title'),
value: $this->fields['name'],
is_rich_text: false,
category: $category_name
);
}
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_NAME,
name: __('Comment title'),
value: $this->fields['name'],
is_rich_text: false,
category: $category_name
);

if (!empty($this->fields['description'])) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_DESCRIPTION,
name: __('Comment description'),
value: $this->fields['description'],
is_rich_text: true,
category: $category_name
);
}
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_DESCRIPTION,
name: __('Comment description'),
value: $this->fields['description'],
is_rich_text: true,
category: $category_name
);

return $handlers;
}
Expand Down
54 changes: 24 additions & 30 deletions src/Glpi/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,38 +451,32 @@ public function listTranslationsHandlers(): array
$key = sprintf('%s_%d', self::getType(), $this->getID());
$category_name = __('Form properties');
$handlers = [];
if (!empty($this->fields['name'])) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_NAME,
name: __('Form title'),
value: $this->fields['name'],
is_rich_text: false,
category: $category_name
);
}
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_NAME,
name: __('Form title'),
value: $this->fields['name'],
is_rich_text: false,
category: $category_name
);

if (!empty($this->fields['header'])) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_HEADER,
name: __('Form description'),
value: $this->fields['header'],
is_rich_text: true,
category: $category_name
);
}
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_HEADER,
name: __('Form description'),
value: $this->fields['header'],
is_rich_text: true,
category: $category_name
);

if (!empty($this->fields['description'])) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_DESCRIPTION,
name: __('Service catalog description'),
value: $this->fields['description'],
is_rich_text: true,
category: $category_name
);
}
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_DESCRIPTION,
name: __('Service catalog description'),
value: $this->fields['description'],
is_rich_text: true,
category: $category_name
);

$sections_handlers = array_map(
fn($section) => $section->listTranslationsHandlers(),
Expand Down
36 changes: 16 additions & 20 deletions src/Glpi/Form/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,27 +135,23 @@ public function listTranslationsHandlers(): array
$key = sprintf('%s_%d', self::getType(), $this->getID());
$category_name = sprintf('%s: %s', self::getTypeName(), $this->getName());
$handlers = [];
if (!empty($this->fields['name'])) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_NAME,
name: __('Question name'),
value: $this->fields['name'],
is_rich_text: false,
category: $category_name
);
}
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_NAME,
name: __('Question name'),
value: $this->fields['name'],
is_rich_text: false,
category: $category_name
);

if (!empty($this->fields['description'])) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_DESCRIPTION,
name: __('Question description'),
value: $this->fields['description'],
is_rich_text: true,
category: $category_name
);
}
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_DESCRIPTION,
name: __('Question description'),
value: $this->fields['description'],
is_rich_text: true,
category: $category_name
);

$question_type = $this->getQuestionType();
if ($question_type instanceof TranslationAwareQuestionType) {
Expand Down
23 changes: 10 additions & 13 deletions src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,17 @@ public function convertExtraData(array $rawData): array
#[Override]
public function listTranslationsHandlers(Question $question): array
{
$handlers = [];
$options = $this->getOptions($question);
if ($options !== []) {
$handlers = array_map(
fn($uuid, $option) => new TranslationHandler(
item: $question,
key: sprintf('%s-%s', self::TRANSLATION_KEY_OPTION, $uuid),
name: sprintf('%s %s', self::getName(), __('Option')),
value: $option,
),
array_keys($options),
$options
);
}
$handlers = array_map(
fn($uuid, $option) => new TranslationHandler(
item: $question,
key: sprintf('%s-%s', self::TRANSLATION_KEY_OPTION, $uuid),
name: sprintf('%s %s', self::getName(), __('Option')),
value: $option,
),
array_keys($options),
$options
);

return $handlers;
}
Expand Down
16 changes: 7 additions & 9 deletions src/Glpi/Form/QuestionType/QuestionTypeLongText.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,13 @@ public function listTranslationsHandlers(Question $question): array
{
$handlers = [];

if (!empty($question->fields['default_value'])) {
$handlers[] = new TranslationHandler(
item: $question,
key: Question::TRANSLATION_KEY_DEFAULT_VALUE,
name: __('Default value'),
value: $question->fields['default_value'],
is_rich_text: true
);
}
$handlers[] = new TranslationHandler(
item: $question,
key: Question::TRANSLATION_KEY_DEFAULT_VALUE,
name: __('Default value'),
value: $question->fields['default_value'],
is_rich_text: true
);

return $handlers;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Glpi/Form/QuestionType/QuestionTypeShortText.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,12 @@ public function getConditionHandlers(
#[Override]
public function listTranslationsHandlers(Question $question): array
{
$default_value = $question->fields['default_value'] ?? '';
if (empty($default_value)) {
return [];
}

return [
new TranslationHandler(
item: $question,
key: Question::TRANSLATION_KEY_DEFAULT_VALUE,
name: __('Default value'),
value: $default_value,
value: $question->fields['default_value'] ?? '',
),
];
}
Expand Down
36 changes: 16 additions & 20 deletions src/Glpi/Form/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,23 @@ public function listTranslationsHandlers(): array
$key = sprintf('%s_%d', self::getType(), $this->getID());
$category_name = sprintf('%s: %s', self::getTypeName(), $this->getName());
if (count($form->getSections()) > 1) {
if (!empty($this->fields['name'])) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_NAME,
name: __('Section title'),
value: $this->fields['name'],
is_rich_text: false,
category: $category_name
);
}
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_NAME,
name: __('Section title'),
value: $this->fields['name'],
is_rich_text: false,
category: $category_name
);

if (!empty($this->fields['description'])) {
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_DESCRIPTION,
name: __('Section description'),
value: $this->fields['description'],
is_rich_text: true,
category: $category_name
);
}
$handlers[$key][] = new TranslationHandler(
item: $this,
key: self::TRANSLATION_KEY_DESCRIPTION,
name: __('Section description'),
value: $this->fields['description'],
is_rich_text: true,
category: $category_name
);
}

$blocks_handlers = [];
Expand Down
8 changes: 4 additions & 4 deletions src/Glpi/ItemTranslation/Context/TranslationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class TranslationHandler
private string $name;

/** @var string The default value (in the default language) */
private string $value;
private ?string $value;

/** @var bool Whether this field contains rich text that should be edited in a rich text editor */
private bool $is_rich_text;
Expand All @@ -72,7 +72,7 @@ public function __construct(
CommonDBTM $item,
string $key,
string $name,
string $value,
?string $value,
bool $is_rich_text = false,
?string $category = null
) {
Expand Down Expand Up @@ -117,9 +117,9 @@ public function getName(): string
/**
* Get the default value (in the default language)
*
* @return string
* @return string|null
*/
public function getValue(): string
public function getValue(): ?string
{
return $this->value;
}
Expand Down
17 changes: 15 additions & 2 deletions src/Glpi/ItemTranslation/ItemTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
use Gettext\Languages\Language;
use Glpi\Form\FormTranslation;
use Glpi\ItemTranslation\Context\ProvideTranslationsInterface;
use Glpi\ItemTranslation\Context\TranslationHandler;
use LogicException;
use Override;
use Session;

use function Safe\array_walk_recursive;
use function Safe\json_decode;
use function Safe\json_encode;

Expand Down Expand Up @@ -215,7 +217,18 @@ protected function getTranslationsHandlersForStats(): array
throw new LogicException('Item does not provide translations');
}

return $item->listTranslationsHandlers();
// Filter out handlers with empty values and those that do not have a translation yet
return array_map(
fn(array $handlers) => array_filter(
$handlers,
fn(TranslationHandler $handler) => !empty($handler->getValue()) || !empty(self::getForItemKeyAndLanguage(
$item,
$handler->getKey(),
$this->fields['language']
)?->getTranslation())
),
$item->listTranslationsHandlers()
);
}

public function getTranslatedPercentage(): int
Expand Down Expand Up @@ -278,7 +291,7 @@ function ($handler) use (&$translations_to_review) {
static::$itemtype => $handler->getItem()->getType(),
'language' => $this->fields['language'],
'key' => $handler->getKey(),
'hash' => ['!=', md5($handler->getValue())],
'hash' => ['!=', md5($handler->getValue() ?? '')],
])
) {
if ($translation->isPossiblyObsolete()) {
Expand Down
39 changes: 24 additions & 15 deletions templates/pages/admin/form/form_translation.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,30 @@
'type_aria_label': _n('Category', 'Categories', 1)
}]) %}
{% for handler in handlers %}
{% set entries = entries|merge([{
'type': '<span class="fw-medium">' ~ handler.getName() ~ '</span>',
'default': handler.isRichText() ? handler.getValue()|safe_html : handler.getValue()|e,
'translation': _self.getTranslationInput(
handler,
form_translation.getForItemKeyAndLanguage(
handler.getItem(),
handler.getKey(),
form_translation.fields['language']
)
),
'type_aria_label': __('Translation name'),
'default_aria_label': __('Default value'),
'translation_aria_label': __('Translated value')
}]) %}
{% set item_translation = form_translation.getForItemKeyAndLanguage(
handler.getItem(),
handler.getKey(),
form_translation.fields['language']
) %}
{% if handler.getValue() is not empty or (item_translation is not null and item_translation.getTranslation() is not empty) %}
{% set default_value = handler.getValue() %}
{% if handler.getValue() is empty %}
{% set default_value = '<span class="text-muted fst-italic">' ~ __('No default value') ~ '</span>' %}
{% else %}
{% set default_value = handler.isRichText() ? default_value|safe_html : default_value|e %}
{% endif %}
{% set entries = entries|merge([{
'type': '<span class="fw-medium">' ~ handler.getName() ~ '</span>',
'default': default_value,
'translation': _self.getTranslationInput(
handler,
item_translation
),
'type_aria_label': __('Translation name'),
'default_aria_label': __('Default value'),
'translation_aria_label': __('Translated value')
}]) %}
{% endif %}
{% endfor %}
{% endfor %}

Expand Down
Loading
Loading