Devices: Add vendor-based filtering for device key types (#8004)
This commit is contained in:
+102
-7
@@ -1637,13 +1637,16 @@
|
||||
echo " <td class='vtable'>".$text['label-device_key_id']."</td>\n";
|
||||
}
|
||||
if ($vendor_count > 1 && !empty($row['device_key_vendor'])) {
|
||||
echo " <td class='vtable'><i>".ucwords($row['device_key_vendor'])."</i></td>\n";
|
||||
// echo " <td class='vtable'><i>".ucwords($row['device_key_vendor'])."</i></td>\n";
|
||||
echo " <td class='vtable'>".$text['label-device_vendor']."</td>\n";
|
||||
echo " <td class='vtable'>".$text['label-device_key_type']."</td>\n";
|
||||
if ($show_key_subtype) {
|
||||
echo " <td class='vtable'>".$text['label-device_key_subtype']."</td>\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
$device_keys_generic_header_displayed = true;
|
||||
echo " <td class='vtable'>".$text['label-device_vendor']."</td>\n";
|
||||
echo " <td class='vtable'>".$text['label-device_key_type']."</td>\n";
|
||||
if ($show_key_subtype) {
|
||||
echo " <td class='vtable'>".$text['label-device_key_subtype']."</td>\n";
|
||||
@@ -1724,16 +1727,27 @@
|
||||
}
|
||||
|
||||
echo "<td align='left' nowrap='nowrap'>\n";
|
||||
//echo " <input class='formfld' type='text' name='device_keys[".$x."][device_key_type]' style='width: 120px;' maxlength='255' value=\"$row['device_key_type']\">\n";
|
||||
if (!empty($row['device_key_vendor'])) {
|
||||
$device_key_vendor = $row['device_key_vendor'];
|
||||
echo " <select class='formfld' name='device_keys[".$x."][device_key_vendor]' id='key_vendor_".$x."'>\n";
|
||||
echo " <option value=''></option>\n";
|
||||
$device_key_vendor = $row['device_key_vendor'] ?? '';
|
||||
foreach ($device_vendors as $vendor) {
|
||||
$selected = '';
|
||||
if ($device_key_vendor == $vendor['name']) {
|
||||
$selected = "selected='selected'";
|
||||
}
|
||||
if (!empty($vendor['name'])) {
|
||||
echo " <option value='".escape($vendor['name'])."' $selected >".escape(ucwords($vendor['name']))."</option>\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
echo " </select>\n";
|
||||
echo "</td>\n";
|
||||
|
||||
echo "<td align='left' nowrap='nowrap'>\n";
|
||||
//echo " <input class='formfld' type='text' name='device_keys[".$x."][device_key_type]' style='width: 120px;' maxlength='255' value=\"$row['device_key_type']\">\n";
|
||||
if (empty($row['device_key_vendor'])) {
|
||||
$device_key_vendor = $device_vendor;
|
||||
}
|
||||
|
||||
echo "<input type='hidden' id='key_vendor_".$x."' name='device_keys[".$x."][device_key_vendor]' value=\"".$device_key_vendor."\" />\n";
|
||||
|
||||
echo "<select class='formfld' name='device_keys[".$x."][device_key_type]' id='key_type_".$x."' onchange=\"document.getElementById('key_vendor_".$x."').value = this.options[this.selectedIndex].getAttribute('vendor');\">\n";
|
||||
echo " <option value=''></option>\n";
|
||||
$previous_vendor = '';
|
||||
@@ -1831,6 +1845,87 @@
|
||||
echo " </tr>";
|
||||
}
|
||||
|
||||
// Filter device key types when a vendor is selected
|
||||
echo "<select id='hidden_device_key_types' style='display: none;'>\n";
|
||||
$prev_vendor = '';
|
||||
foreach ($vendor_functions as $function) {
|
||||
if ($function['vendor_name'] != $prev_vendor) {
|
||||
if ($prev_vendor !== '') {
|
||||
echo " </optgroup>\n";
|
||||
}
|
||||
echo " <optgroup label='".escape(ucwords($function['vendor_name']))."'>\n";
|
||||
$prev_vendor = $function['vendor_name'];
|
||||
}
|
||||
echo " <option value='".escape($function['value'])."' vendor='".escape($function['vendor_name'])."'>".($text['label-'.$function['type'] ?? ''] ?? $function['value'])."</option>\n";
|
||||
}
|
||||
if ($prev_vendor !== '') {
|
||||
echo " </optgroup>\n";
|
||||
}
|
||||
echo "</select>\n";
|
||||
?>
|
||||
<script>
|
||||
const hidden_select = document.getElementById('hidden_device_key_types');
|
||||
|
||||
if (hidden_select) {
|
||||
function populate_type_select(type_select, vendor_value) {
|
||||
// Clear current options
|
||||
type_select.innerHTML = '<option value=""></option>';
|
||||
|
||||
const options = hidden_select.querySelectorAll('option');
|
||||
const selected_vendor = (vendor_value || '').trim().toLowerCase();
|
||||
|
||||
let last_label = null;
|
||||
let current_group = null;
|
||||
|
||||
for (const option of options) {
|
||||
const option_vendor = (option.getAttribute('vendor') || '').trim().toLowerCase();
|
||||
|
||||
// Filter by vendor if one is selected
|
||||
if (selected_vendor !== '' && option_vendor !== selected_vendor) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (selected_vendor === '') {
|
||||
// When no vendor selected, append all options with optgroups
|
||||
const option_group = option.closest('optgroup');
|
||||
const option_label = option_group ? option_group.getAttribute('label') : null;
|
||||
|
||||
if (option_label && option_label !== last_label) {
|
||||
if (current_group) type_select.appendChild(current_group);
|
||||
current_group = document.createElement('optgroup');
|
||||
current_group.setAttribute('label', option_label);
|
||||
last_label = option_label;
|
||||
}
|
||||
|
||||
if (current_group) {
|
||||
current_group.appendChild(option.cloneNode(true));
|
||||
} else {
|
||||
type_select.appendChild(option.cloneNode(true));
|
||||
}
|
||||
} else {
|
||||
// When a vendor is selected, append options without optgroups
|
||||
type_select.appendChild(option.cloneNode(true));
|
||||
}
|
||||
}
|
||||
|
||||
// Append the final group if it exists
|
||||
if (current_group) type_select.appendChild(current_group);
|
||||
}
|
||||
|
||||
// Attach listener to every vendor select in the table
|
||||
document.querySelectorAll('select[name*="[device_key_vendor]"]').forEach(vendor_select => {
|
||||
vendor_select.addEventListener('change', function() {
|
||||
const type_select = this.closest('tr').querySelector('select[name*="[device_key_type]"]');
|
||||
if (!type_select) return;
|
||||
|
||||
populate_type_select(type_select, this.value);
|
||||
type_select.value = ''; // Reset selection when vendor changes
|
||||
});
|
||||
});
|
||||
}
|
||||
</script>
|
||||
<?php
|
||||
|
||||
//device settings
|
||||
if (permission_exists('device_setting_edit')) {
|
||||
$device_setting_exists = false;
|
||||
|
||||
Reference in New Issue
Block a user