respect withLabels

This commit is contained in:
Rich Lott / Artful Robot 2025-04-02 10:56:32 +01:00
parent 0b625bcba0
commit c2546732b5

View file

@ -18,10 +18,18 @@ class Get extends DAOGetAction {
/** /**
*/ */
public function _run(Result $result) { public function _run(Result $result) {
if ($this->withLabels) {
// Ensure we have search_type.
if ($this->select && !in_array('search_type', $this->select)) {
$this->addSelect('search_type');
}
}
parent::_run($result); parent::_run($result);
if ($this->withLabels) {
$groupIDs = $searchIDs = []; $groupIDs = $searchIDs = [];
foreach ($result as $idx => $row) { foreach ($result as $row) {
if ($row['search_type'] === 'group') { if ($row['search_type'] === 'group') {
$groupIDs[] = $row['search_data']['group_id'] ?? NULL; $groupIDs[] = $row['search_data']['group_id'] ?? NULL;
} }
@ -31,6 +39,7 @@ class Get extends DAOGetAction {
} }
$groupIDs = array_filter($groupIDs); $groupIDs = array_filter($groupIDs);
$searchIDs = array_filter($searchIDs); $searchIDs = array_filter($searchIDs);
$groupNames = $searchNames = [];
if ($groupIDs) { if ($groupIDs) {
$groupNames = Group::get() $groupNames = Group::get()
->addWhere('id', 'IN', $groupIDs) ->addWhere('id', 'IN', $groupIDs)
@ -44,7 +53,7 @@ class Get extends DAOGetAction {
->execute()->indexBy('id')->column('label'); ->execute()->indexBy('id')->column('label');
} }
if ($searchNames || $groupNames) { if ($searchNames || $groupNames) {
foreach ($result as $row) { foreach ($result as $idx => $row) {
if ($row['search_type'] === 'group' if ($row['search_type'] === 'group'
&& !empty($groupNames[$row['search_data']['group_id'] ?? '']) && !empty($groupNames[$row['search_data']['group_id'] ?? ''])
) { ) {
@ -55,9 +64,10 @@ class Get extends DAOGetAction {
) { ) {
$row['search_source'] = $searchNames[$row['search_data']['saved_search_id'] ?? '']; $row['search_source'] = $searchNames[$row['search_data']['saved_search_id'] ?? ''];
} }
}
}
$result[$idx] = $row; $result[$idx] = $row;
} }
}
}
}
} }