Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Fix hidden question not available in anonymous forms
- Fix `Tree Cascade Dropdown` question ignoring dropdown translations

## [1.3.1] - 2026-10-06

Expand Down
8 changes: 2 additions & 6 deletions src/Controller/TreeDropdownChildrenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Glpi\Controller\Form\Utils\CanCheckAccessPolicies;
use Glpi\Http\Firewall;
use Glpi\Security\Attribute\SecurityStrategy;
use GlpiPlugin\Advancedforms\Model\QuestionType\TreeCascadeDropdownQuestion;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
Expand Down Expand Up @@ -128,12 +129,7 @@ public function __invoke(Request $request): Response
$where[$foreign_key] = $parent_id;

$children = [];
$iterator = $DB->request([
'SELECT' => ['id', 'name'],
'FROM' => $table,
'WHERE' => $where,
'ORDER' => 'name ASC',
]);
$iterator = $DB->request(TreeCascadeDropdownQuestion::getTranslatedItemsCriteria($itemtype, $where));

foreach ($iterator as $row) {
if (!is_array($row)) {
Expand Down
72 changes: 63 additions & 9 deletions src/Model/QuestionType/TreeCascadeDropdownQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

use DBmysql;
use CommonTreeDropdown;
use DropdownTranslation;
use Glpi\DBAL\QueryExpression;
use Glpi\Application\View\TemplateRenderer;
use Glpi\DBAL\JsonFieldInterface;
use Glpi\Form\Condition\ConditionHandler\ItemAsTextConditionHandler;
Expand All @@ -47,6 +49,7 @@
use GlpiPlugin\Advancedforms\Model\QuestionType\AdvancedCategory;
use InvalidArgumentException;
use Override;
use Session;

final class TreeCascadeDropdownQuestion extends QuestionTypeItemDropdown implements ConfigurableItemInterface
{
Expand Down Expand Up @@ -139,6 +142,7 @@ public function renderEndUserTemplate(Question $question): string
$root_item = getItemForItemtype($itemtype);
if ($root_item instanceof CommonTreeDropdown && $root_item->getFromDB($root_items_id)) {
$root_item_name = is_string($root_item->fields['name'] ?? '') ? (string) ($root_item->fields['name'] ?? '') : '';
$root_item_name = (string) DropdownTranslation::getTranslatedValue($root_items_id, $itemtype, 'name', '', $root_item_name);
}
}

Expand Down Expand Up @@ -298,7 +302,7 @@ private function buildAncestorChain(

/** @var array<string, mixed> $typed_base_where */
$typed_base_where = $base_where;
$node['siblings'] = $this->getValidItemsForLevel($table, $typed_base_where, $raw_where);
$node['siblings'] = $this->getValidItemsForLevel($itemtype, $typed_base_where, $raw_where);
}

return $chain;
Expand Down Expand Up @@ -356,25 +360,22 @@ private function getFirstLevelItems(

/** @var array<string, mixed> $typed_base_where */
$typed_base_where = $base_where;
return $this->getValidItemsForLevel($table, $typed_base_where, $raw_where);
return $this->getValidItemsForLevel($itemtype, $typed_base_where, $raw_where);
}

/**
* @param class-string<CommonTreeDropdown> $itemtype
* @param array<string, mixed> $base_where
* @param array<string, mixed> $raw_where
* @return array<int, array{id: int, name: string}>
*/
private function getValidItemsForLevel(string $table, array $base_where, array $raw_where): array
private function getValidItemsForLevel(string $itemtype, array $base_where, array $raw_where): array
{
$table = $itemtype::getTable();
/** @var DBmysql $DB */
global $DB;

$raw_iterator = $DB->request([
'SELECT' => ['id', 'name'],
'FROM' => $table,
'WHERE' => $raw_where,
'ORDER' => 'name ASC',
]);
$raw_iterator = $DB->request(self::getTranslatedItemsCriteria($itemtype, $raw_where));

/** @var array<int, array{id: int, name: string}> $raw_items */
$raw_items = [];
Expand Down Expand Up @@ -431,6 +432,59 @@ private function getValidItemsForLevel(string $table, array $base_where, array $
return $items;
}

/**
* Request criteria selecting `id` and `name` of items, with `name` translated
* in the session language when dropdown translations exist, sorted by that name.
*
* @param class-string<CommonTreeDropdown> $itemtype
* @param array<mixed> $where
* @return array<string, mixed>
*/
public static function getTranslatedItemsCriteria(string $itemtype, array $where): array
{
$table = $itemtype::getTable();
$name_field = DBmysql::quoteName($table . '.name');

$criteria = [
'SELECT' => [$table . '.id', $table . '.name'],
'FROM' => $table,
'WHERE' => $where,
'ORDER' => [$table . '.name ASC'],
];

if (!Session::haveTranslations($itemtype, 'name')) {
return $criteria;
}

$translated_name = sprintf(
"COALESCE(NULLIF(%s, ''), %s)",
DBmysql::quoteName('namet.value'),
$name_field,
);
$criteria['SELECT'] = [
$table . '.id',
new QueryExpression($translated_name . ' AS ' . DBmysql::quoteName('name')),
];
$criteria['LEFT JOIN'] = [
'glpi_dropdowntranslations AS namet' => [
'ON' => [
'namet' => 'items_id',
$table => 'id',
[
'AND' => [
'namet.itemtype' => $itemtype,
'namet.language' => $_SESSION['glpilanguage'],
'namet.field' => 'name',
],
],
],
],
];
$criteria['ORDER'] = [new QueryExpression($translated_name . ' ASC')];

return $criteria;
}

#[Override]
public function getConditionHandlers(?JsonFieldInterface $question_config): array
{
Expand Down
8 changes: 3 additions & 5 deletions tests/Front/ConfigFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ public function testTabHasContentExistOnConfigPageForAdmin(): void
$this->assertCount(1, $config_header);
}

public function testTabContentIsEmptyForTech(): void
public function testTabContentIsDeniedForTech(): void
{
// Act: go to the advanced form tab on the config
$this->login('tech');
$crawler = $this->get("/ajax/common.tabs.php", $this->getConfigTabUrlParams());

// Assert: no html was rendered as we lack rights to view config
$this->assertEmpty($crawler);
$this->expectException(AccessDeniedHttpException::class);
$this->get("/ajax/common.tabs.php", $this->getConfigTabUrlParams());
}

#[DataProvider('provideQuestionTypes')]
Expand Down
82 changes: 82 additions & 0 deletions tests/Model/QuestionType/TreeCascadeDropdownQuestionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use SoftwareCategory;
use WebhookCategory;
use Dropdown;
use DropdownTranslation;
use Glpi\Form\Form;
use Glpi\Controller\Form\RendererController;
use Glpi\Form\QuestionType\QuestionTypeInterface;
Expand Down Expand Up @@ -815,6 +816,87 @@ public function testHelpdeskRenderingWithCustomDropdownAndSubtreeRoot(): void
$this->assertNotContains('Test 2 Option', $all_option_texts);
}

/**
* Verify that every level (first level, default value ancestors, AJAX children
* and selectable root) shows names translated in the user language, sorted by translated name.
*/
public function testDropdownsShowTranslatedNames(): void
{
$this->login();
$this->enableConfigurableItem(TreeCascadeDropdownQuestion::class);

$entity_id = Session::getActiveEntity();
$hardware = $this->createItem(ITILCategory::class, [
'name' => 'Matériel',
'itilcategories_id' => 0,
'entities_id' => $entity_id,
]);
$screen = $this->createItem(ITILCategory::class, [
'name' => 'Écran',
'itilcategories_id' => $hardware->getID(),
'entities_id' => $entity_id,
]);
$software = $this->createItem(ITILCategory::class, [
'name' => 'Logiciel',
'itilcategories_id' => 0,
'entities_id' => $entity_id,
]);

$language = $_SESSION['glpilanguage'];
foreach ([[$hardware, 'Hardware'], [$screen, 'Screen'], [$software, 'Software']] as [$category, $translation]) {
$this->createItem(DropdownTranslation::class, [
'itemtype' => ITILCategory::class,
'items_id' => $category->getID(),
'language' => $language,
'field' => 'name',
'value' => $translation,
]);
}

// First level and default value ancestor chain
$builder = new FormBuilder("Tree Cascade Translations");
$builder->addQuestion(
"My category",
TreeCascadeDropdownQuestion::class,
$screen->getID(),
json_encode(new QuestionTypeItemDropdownExtraDataConfig(itemtype: ITILCategory::class)),
);
$form = $this->createForm($builder);
$selects = $this->renderHelpdeskForm($form)->filter('.af-tree-cascade-select');
$first_options = array_values(array_filter(
$selects->eq(0)->filter('option')->each(fn(Crawler $node) => $node->text()),
fn(string $text) => in_array($text, ['Hardware', 'Software', 'Matériel', 'Logiciel'], true),
));
$this->assertSame(['Hardware', 'Software'], $first_options);
$this->assertContains('Screen', $selects->eq(1)->filter('option')->each(fn(Crawler $node) => $node->text()));

// Children loaded on selection
$question = array_values($form->getQuestions())[0];
$response = (new TreeDropdownChildrenController())->__invoke(
Request::create('', 'GET', ['questions_id' => $question->getID(), 'parent_id' => $hardware->getID()]),
);
$this->assertStringContainsString('Screen', (string) $response->getContent());
$this->assertStringNotContainsString('Écran', (string) $response->getContent());

// Selectable root
$builder = new FormBuilder("Tree Cascade Translated Root");
$builder->addQuestion(
"My category",
TreeCascadeDropdownQuestion::class,
'',
json_encode(new QuestionTypeItemDropdownExtraDataConfig(
itemtype: ITILCategory::class,
root_items_id: $hardware->getID(),
selectable_tree_root: true,
)),
);
$root_options = $this->renderHelpdeskForm($this->createForm($builder))
->filter('.af-tree-cascade-select')->eq(0)->filter('option')
->each(fn(Crawler $node) => $node->text());
$this->assertContains('Hardware', $root_options);
$this->assertNotContains('Matériel', $root_options);
}

private function renderHelpdeskForm(Form $form): Crawler
{
$this->login();
Expand Down
Loading