mvc: further improve 10c81a4eea to allow sorting on descriptive values and unify usage in ModelRelationField, closes https://github.com/opnsense/core/issues/7383

This commit is contained in:
Ad Schellevis 2024-04-16 12:27:37 +02:00
parent fad8cfdf84
commit c4b64a417d
3 changed files with 9 additions and 16 deletions

View File

@ -192,12 +192,13 @@ class ArrayField extends BaseField
$sortKey = '';
foreach ($fieldNames as $fieldName) {
if (isset($node->internalChildnodes[$fieldName])) {
if (is_numeric((string)$node->$fieldName)) {
$payload = $node->$fieldName->getDescription();
if (is_numeric($payload)) {
// align numeric values right for sorting, not perfect but works for integer type values
$sortKey .= sprintf("%" . $MAX_KEY_LENGTH . "s,", $node->$fieldName);
$sortKey .= sprintf("%" . $MAX_KEY_LENGTH . "s,", $payload);
} else {
// normal text sorting, align left
$sortKey .= sprintf("%-" . $MAX_KEY_LENGTH . "s,", $node->$fieldName);
$sortKey .= sprintf("%-" . $MAX_KEY_LENGTH . "s,", $payload);
}
}
}

View File

@ -135,7 +135,7 @@ abstract class BaseListField extends BaseField
$items[] = $fieldValue['value'];
}
}
return implode(',', $items);
return implode(', ', $items);
} else {
return $data;
}

View File

@ -104,15 +104,7 @@ class ModelRelationField extends BaseListField
foreach ($modelObj->getNodeByReference($modelData['items'])->iterateItems() as $node) {
$descriptions = [];
foreach ($displayKeys as $displayKey) {
if ($node->$displayKey != null) {
if ($node->$displayKey->getObjectType() == 'ModelRelationField') {
$descriptions[] = $node->$displayKey->display_value();
} else {
$descriptions[] = (string)$node->$displayKey;
}
} else {
$descriptions[] = "";
}
$descriptions[] = $node->$displayKey != null ? $node->$displayKey->getDescription() : '';
}
if (!isset($node->getAttributes()['uuid'])) {
continue;
@ -211,15 +203,15 @@ class ModelRelationField extends BaseListField
}
/**
* @return string string display value of this field
* {@inheritdoc}
*/
public function display_value()
public function getDescription()
{
$tmp = [];
foreach (explode(',', $this->internalValue) as $key) {
$tmp[] = $this->internalOptionList[$key] ?? '';
}
return implode(',', $tmp);
return implode(', ', $tmp);
}