From 9c009d2f15f8f9c47c1c5e5fe568085db24f4f57 Mon Sep 17 00:00:00 2001 From: Florent VIOLLEAU Date: Sun, 8 Dec 2024 23:05:28 +0100 Subject: [PATCH 1/3] [Core] add dynamic list with ajax requests --- lib/BridgeCard.php | 98 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/lib/BridgeCard.php b/lib/BridgeCard.php index 855ddb9339a..7b7787c5066 100644 --- a/lib/BridgeCard.php +++ b/lib/BridgeCard.php @@ -142,6 +142,8 @@ private static function renderForm( $form .= self::getNumberInput($inputEntry, $idArg, $id); } elseif ($inputEntry['type'] === 'list') { $form .= self::getListInput($inputEntry, $idArg, $id) . "\n"; + } elseif ($inputEntry['type'] === 'dynamic_list') { + $form .= self::getDynamicListInput($inputEntry, $idArg, $id) . "\n"; } elseif ($inputEntry['type'] === 'checkbox') { $form .= self::getCheckboxInput($inputEntry, $idArg, $id); } else { @@ -234,6 +236,102 @@ public static function getListInput(array $entry, string $id, string $name): str return $list; } + public static function getDynamicListInput(array $entry, string $id, string $name): string + { + $required = $entry['required'] ?? null; + if ($required) { + trigger_error('The required attribute is not supported for lists'); + unset($entry['required']); + } + + if (!isset($entry['ajax_route']) || !isset($entry['fields_name_used_as_value']) || !isset($entry['fields_name_used_for_display'])) { + trigger_error('The ajax_route and fields_name_used_as_value and fields_name_used_for_display attributes are required'); + } + + $attributes = self::getInputAttributes($entry); + + $fieldsDisplayString = ''; + foreach($entry['fields_name_used_for_display'] as $index => $field) { + if ($index === 0) { + $fieldsDisplayString = 'option.' . $field; + } else { + $fieldsDisplayString .= ' + \' - \' + option.' . $field; + } + } + + $fieldsValueString = ''; + foreach($entry['fields_name_used_as_value'] as $index => $field) { + if ($index === 0) { + $fieldsValueString = 'option.' . $field; + } else { + $fieldsValueString .= ' + \'-\' + option.' . $field; + } + } + + $list = sprintf('' . "\n", + $attributes, + $id, + $name, + $id, + $id, + Configuration::getConfig('proxy', 'url') ?: 'https://cors-anywhere.herokuapp.com/', + $entry['ajax_route'], + $fieldsDisplayString, + $fieldsValueString, + ); + $list .= sprintf('' . "\n", $id); + $list .= sprintf('
' . "\n", $id); + + return $list; + } public static function getCheckboxInput(array $entry, string $id, string $name): string { From 60874b093aca7fcb09d6d4f79f86a7ab6e1c3bba Mon Sep 17 00:00:00 2001 From: Florent VIOLLEAU Date: Sun, 8 Dec 2024 23:23:32 +0100 Subject: [PATCH 2/3] [Core] lint --- lib/BridgeCard.php | 113 +++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/lib/BridgeCard.php b/lib/BridgeCard.php index 7b7787c5066..edde6e703c3 100644 --- a/lib/BridgeCard.php +++ b/lib/BridgeCard.php @@ -251,7 +251,7 @@ public static function getDynamicListInput(array $entry, string $id, string $nam $attributes = self::getInputAttributes($entry); $fieldsDisplayString = ''; - foreach($entry['fields_name_used_for_display'] as $index => $field) { + foreach ($entry['fields_name_used_for_display'] as $index => $field) { if ($index === 0) { $fieldsDisplayString = 'option.' . $field; } else { @@ -260,7 +260,7 @@ public static function getDynamicListInput(array $entry, string $id, string $nam } $fieldsValueString = ''; - foreach($entry['fields_name_used_as_value'] as $index => $field) { + foreach ($entry['fields_name_used_as_value'] as $index => $field) { if ($index === 0) { $fieldsValueString = 'option.' . $field; } else { @@ -268,64 +268,65 @@ public static function getDynamicListInput(array $entry, string $id, string $nam } } - $list = sprintf('' . "\n", - $attributes, - $id, - $name, - $id, - $id, - Configuration::getConfig('proxy', 'url') ?: 'https://cors-anywhere.herokuapp.com/', - $entry['ajax_route'], - $fieldsDisplayString, - $fieldsValueString, + xhr.onload = function () { + if (xhr.status === 200) { + // Parse the response and update the datalist + datalist.innerHTML = \'\'; // Clear existing options + const options = JSON.parse(xhr.responseText); // Assuming JSON response + options.forEach(option => { + const opt = document.createElement(\'option\'); + + opt.innerHTML = %s; // the displayed value + opt.value = %s; // the value + datalist.appendChild(opt); + }); + // set himself enabled when the request is done + inputElement.value = \'\'; + inputElement.disabled = false; + } else { + errorElement.innerHTML = \'failed fetching data\'; + inputElement.value = \'\'; + inputElement.disabled = false; + } + }; + xhr.send(); + } + " />' . "\n", + $attributes, + $id, + $name, + $id, + $id, + Configuration::getConfig('proxy', 'url') ?: 'https://cors-anywhere.herokuapp.com/', + $entry['ajax_route'], + $fieldsDisplayString, + $fieldsValueString, ); $list .= sprintf('' . "\n", $id); $list .= sprintf('
' . "\n", $id); From 4ae6f847e65abcbbc83f9d84aac5ca832bfb8f19 Mon Sep 17 00:00:00 2001 From: Florent VIOLLEAU Date: Mon, 9 Dec 2024 01:22:49 +0100 Subject: [PATCH 3/3] [Core] allow more dynamic options --- lib/BridgeCard.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/BridgeCard.php b/lib/BridgeCard.php index edde6e703c3..ad37d16b905 100644 --- a/lib/BridgeCard.php +++ b/lib/BridgeCard.php @@ -260,16 +260,17 @@ public static function getDynamicListInput(array $entry, string $id, string $nam } $fieldsValueString = ''; + $fieldsNameUsedAsValueSeparator = isset($entry['fields_name_used_as_value_separator']) ? $entry['fields_name_used_as_value_separator'] : '-'; foreach ($entry['fields_name_used_as_value'] as $index => $field) { if ($index === 0) { $fieldsValueString = 'option.' . $field; } else { - $fieldsValueString .= ' + \'-\' + option.' . $field; + $fieldsValueString .= ' + \'' . $fieldsNameUsedAsValueSeparator . '\' + option.' . $field; } } $list = sprintf( - '