diff --git a/lib/BridgeCard.php b/lib/BridgeCard.php index 855ddb9339a..ad37d16b905 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,109 @@ 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 = ''; + $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 .= ' + \'' . $fieldsNameUsedAsValueSeparator . '\' + option.' . $field; + } + } + + $list = sprintf( + '' . "\n", + $attributes, + $id, + $name, + $id, + $id, + Configuration::getConfig('proxy', 'url') ?: 'https://cors-anywhere.herokuapp.com/', + $entry['ajax_route'], + isset($entry['field_for_options']) ? 'true' : 'false', + isset($entry['field_for_options']) ? $entry['field_for_options'] : null, + $fieldsDisplayString, + $fieldsValueString, + ); + $list .= sprintf('' . "\n", $id); + $list .= sprintf('
' . "\n", $id); + + return $list; + } public static function getCheckboxInput(array $entry, string $id, string $name): string {