-
Notifications
You must be signed in to change notification settings - Fork 3
Feat split tree dropdown question #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lainow
wants to merge
20
commits into
main
Choose a base branch
from
feat-split-tree-dropdown-question
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
75b4204
.
Lainow b9d1b42
.
Lainow de2cad4
.
Lainow 390fa10
Implement Tree Cascade Question Type for Location and ITILCategory
Lainow aab66d6
Fix phpstan
Lainow 609014f
FIx phpcs and rector
Lainow b28091a
Use controller
Lainow 8b3cfde
Fix lint
Lainow 624961c
Use module
Lainow 083da9b
Fix options make tree root selectable
Lainow 80a4b64
Fix lints
Lainow 4685f9d
Add units tests
Lainow de9a4fb
Fix lint
Lainow 179c1fb
Fix units tests
Lainow bca1b02
Update src/Model/QuestionType/TreeCascadeDropdownQuestion.php
Lainow 16074cb
Fix phpstan; update and comment unit tests
Lainow 621c9d0
Fix phpcs
Lainow 3b9a921
Update src/Model/QuestionType/TreeCascadeDropdownQuestion.php
Lainow 3ee58af
Fix sql condition
Lainow f8435c0
Add suggestions
Lainow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /** | ||
| * ------------------------------------------------------------------------- | ||
| * advancedforms plugin for GLPI | ||
| * ------------------------------------------------------------------------- | ||
| * | ||
| * MIT License | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| * ------------------------------------------------------------------------- | ||
| * @copyright Copyright (C) 2025 by the advancedforms plugin team. | ||
| * @license MIT https://opensource.org/licenses/mit-license.php | ||
| * @link https://github.com/pluginsGLPI/advancedforms | ||
| * ------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| export class AfTreeCascadeDropdown { | ||
| /** | ||
| * @param {Object} options | ||
| * @param {string} options.selector_id - The ID of the select element to bind | ||
| * @param {number} options.questions_id - The ID of the question | ||
| * @param {number} options.ajax_limit_count - Limit for Select2 adaptation | ||
| * @param {string} [options.next_container_id] - Optional container ID for auto-loading children | ||
| * @param {number} [options.auto_load_parent_id] - Optional parent ID to auto-load children on init | ||
| * @param {number} [options.level] - Current depth level (1 = root) | ||
| */ | ||
| constructor(options) { | ||
| this.selector_id = options.selector_id; | ||
| this.questions_id = options.questions_id; | ||
| this.ajax_limit_count = options.ajax_limit_count || 10; | ||
| this.next_container_id = options.next_container_id || null; | ||
| this.auto_load_parent_id = options.auto_load_parent_id || 0; | ||
| this.level = options.level || 1; | ||
| this.endpoint_url = `${CFG_GLPI.root_doc}/plugins/advancedforms/TreeDropdownChildren`; | ||
|
|
||
| this.#init(); | ||
| } | ||
|
|
||
| #init() { | ||
| const $select = $(`#${this.selector_id}`); | ||
| if ($select.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| this.#setupAdapt($select); | ||
| this.#bindChangeEvent($select); | ||
|
|
||
| if (this.auto_load_parent_id > 0 && this.next_container_id) { | ||
| this.#loadChildren(this.auto_load_parent_id, $(`#${this.next_container_id}`)); | ||
| } | ||
| } | ||
|
|
||
| #setupAdapt($select) { | ||
| if ($select.hasClass('af-tree-cascade-select')) { | ||
| setupAdaptDropdown({ | ||
| field_id: this.selector_id, | ||
| width: '100%', | ||
| dropdown_css_class: '', | ||
| placeholder: '', | ||
| ajax_limit_count: this.ajax_limit_count, | ||
| templateresult: templateResult, | ||
| templateselection: templateSelection, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| #bindChangeEvent($select) { | ||
| $select.on('change', () => { | ||
| const value = $select.val(); | ||
| const fieldName = $select.data('af-tree-field-name'); | ||
| if (fieldName) { | ||
| $(`input[name="${fieldName}"]`).val(value); | ||
| } | ||
|
|
||
| const $wrapper = $select.closest('.af-tree-level-wrapper'); | ||
| $wrapper.nextAll('.af-tree-level-wrapper, .af-tree-next-container').remove(); | ||
|
|
||
| if (value && value > 0) { | ||
| const $parentRow = $wrapper.parent(); | ||
| const $container = $(`<div class="af-tree-next-container"></div>`); | ||
| $parentRow.append($container); | ||
| this.#loadChildren(value, $container); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| #loadChildren(parent_id, $container) { | ||
| $.ajax({ | ||
| url: this.endpoint_url, | ||
| data: { | ||
| questions_id: this.questions_id, | ||
| parent_id: parent_id, | ||
| }, | ||
| success: (html) => { | ||
| if (html.trim().length > 0) { | ||
| $container.html(html); | ||
| this.#initDynamicChild($container); | ||
| } else { | ||
| $container.remove(); | ||
| } | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| #initDynamicChild($container) { | ||
| const $select = $container.find('.af-tree-cascade-select'); | ||
| if ($select.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const child_id = $select.attr('id'); | ||
| const child_options = { | ||
| selector_id: child_id, | ||
| questions_id: $select.data('af-tree-questions-id') || this.questions_id, | ||
| ajax_limit_count: $select.data('af-tree-ajax-limit') || this.ajax_limit_count, | ||
| level: this.level + 1, | ||
| }; | ||
|
|
||
| new AfTreeCascadeDropdown(child_options); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * ------------------------------------------------------------------------- | ||
| * advancedforms plugin for GLPI | ||
| * ------------------------------------------------------------------------- | ||
| * | ||
| * MIT License | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| * ------------------------------------------------------------------------- | ||
| * @copyright Copyright (C) 2025 by the advancedforms plugin team. | ||
| * @license MIT https://opensource.org/licenses/mit-license.php | ||
| * @link https://github.com/pluginsGLPI/advancedforms | ||
| * ------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| namespace GlpiPlugin\Advancedforms\Controller; | ||
|
|
||
| use Glpi\Form\Question; | ||
| use Glpi\Form\QuestionType\QuestionTypeItemDropdown; | ||
| use DBmysql; | ||
| use CommonTreeDropdown; | ||
| use Glpi\Application\View\TemplateRenderer; | ||
| use Glpi\Controller\AbstractController; | ||
| use Glpi\Http\Firewall; | ||
| use Glpi\Security\Attribute\SecurityStrategy; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\Routing\Attribute\Route; | ||
|
|
||
| final class TreeDropdownChildrenController extends AbstractController | ||
| { | ||
| #[SecurityStrategy(Firewall::STRATEGY_AUTHENTICATED)] | ||
| #[Route( | ||
| path: 'TreeDropdownChildren', | ||
| name: 'tree_dropdown_children', | ||
| )] | ||
| public function __invoke(Request $request): Response | ||
| { | ||
| $questions_id = $request->query->getInt('questions_id', 0); | ||
| $parent_id = $request->query->getInt('parent_id', 0); | ||
|
|
||
| if ($parent_id <= 0 || $questions_id <= 0) { | ||
| return new Response('', Response::HTTP_OK); | ||
| } | ||
|
|
||
| $question = new Question(); | ||
| if (!$question->getFromDB($questions_id)) { | ||
| return new Response('', Response::HTTP_OK); | ||
| } | ||
|
|
||
| /** @var QuestionTypeItemDropdown $question_type */ | ||
| $question_type = $question->getQuestionType(); | ||
|
|
||
| $itemtype = $question_type->getDefaultValueItemtype($question) ?? ''; | ||
| $field_name = $question->getEndUserInputName() . '[items_id]'; | ||
| $aria_label = $question_type->items_id_aria_label ?? __('Select a dropdown item'); | ||
|
|
||
| $dropdown_restriction_params = $question_type->getDropdownRestrictionParams($question); | ||
| /** @var array<string, mixed> $condition_param */ | ||
| $condition_param = $dropdown_restriction_params['WHERE'] ?? []; | ||
|
|
||
| if (!class_exists($itemtype) || !is_subclass_of($itemtype, CommonTreeDropdown::class)) { | ||
| return new Response('', Response::HTTP_OK); | ||
| } | ||
|
|
||
| /** @var DBmysql $DB */ | ||
| global $DB; | ||
|
|
||
| $foreign_key = $itemtype::getForeignKeyField(); | ||
| $table = $itemtype::getTable(); | ||
|
|
||
| $level_key = $table . '.level'; | ||
|
|
||
| $where = []; | ||
|
|
||
| $entity_restrict = getEntitiesRestrictCriteria($table); | ||
| if (!empty($entity_restrict)) { | ||
| $where = array_merge($where, $entity_restrict); | ||
| } | ||
|
|
||
| if (!empty($condition_param) && is_array($condition_param)) { | ||
| unset($condition_param[$level_key]); | ||
| $where = array_merge($where, $condition_param); | ||
| } | ||
|
|
||
| $where[$foreign_key] = $parent_id; | ||
|
|
||
| $item_check = getItemForItemtype($itemtype); | ||
| if ($item_check instanceof CommonTreeDropdown && $item_check->isField('is_deleted')) { | ||
| $where['is_deleted'] = 0; | ||
| } | ||
|
|
||
| $children = []; | ||
| $iterator = $DB->request([ | ||
| 'SELECT' => ['id', 'name'], | ||
| 'FROM' => $table, | ||
| 'WHERE' => $where, | ||
| 'ORDER' => 'name ASC', | ||
| ]); | ||
|
|
||
| foreach ($iterator as $row) { | ||
| if (!is_array($row)) { | ||
| continue; | ||
| } | ||
|
|
||
| $children[] = [ | ||
| 'id' => $row['id'], | ||
| 'name' => $row['name'], | ||
| ]; | ||
| } | ||
|
|
||
| if ($children === []) { | ||
| return new Response('', Response::HTTP_OK); | ||
| } | ||
|
|
||
| global $CFG_GLPI; | ||
|
|
||
| $rand_value = random_int(1000000, 9999999); | ||
| $select_id = 'tree_cascade_child_' . $rand_value; | ||
|
|
||
| $twig = TemplateRenderer::getInstance(); | ||
| $html = $twig->render( | ||
| '@advancedforms/tree_cascade_dropdown_children.html.twig', | ||
| [ | ||
| 'select_id' => $select_id, | ||
| 'children' => $children, | ||
| 'questions_id' => $questions_id, | ||
| 'final_field_name' => $field_name, | ||
| 'aria_label' => $aria_label, | ||
| 'ajax_limit_count' => is_numeric($CFG_GLPI['ajax_limit_count'] ?? 10) ? (int) ($CFG_GLPI['ajax_limit_count'] ?? 10) : 10, | ||
| ], | ||
| ); | ||
|
|
||
| return new Response($html, Response::HTTP_OK, ['Content-Type' => 'text/html; charset=UTF-8']); | ||
|
Comment on lines
+140
to
+152
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.