contactcats/Civi/Api4/Action/ContactCategoryDefinition/Get.php
Rich Lott / Artful Robot c2546732b5 respect withLabels
2025-04-02 10:56:32 +01:00

73 lines
2.1 KiB
PHP

<?php
namespace Civi\Api4\Action\ContactCategoryDefinition;
use Civi\Api4\Generic\DAOGetAction;
use Civi\Api4\Generic\Result;
use Civi\Api4\Group;
use Civi\Api4\SavedSearch;
class Get extends DAOGetAction {
/**
* Whether to look up and return extra data, helpful for presentations.
*
* @var bool
*/
protected $withLabels = FALSE;
/**
*/
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);
if ($this->withLabels) {
$groupIDs = $searchIDs = [];
foreach ($result as $row) {
if ($row['search_type'] === 'group') {
$groupIDs[] = $row['search_data']['group_id'] ?? NULL;
}
elseif ($row['search_type'] === 'search') {
$searchIDs[] = $row['search_data']['saved_search_id'] ?? NULL;
}
}
$groupIDs = array_filter($groupIDs);
$searchIDs = array_filter($searchIDs);
$groupNames = $searchNames = [];
if ($groupIDs) {
$groupNames = Group::get()
->addWhere('id', 'IN', $groupIDs)
->addSelect('title')
->execute()->indexBy('id')->column('title');
}
if ($searchIDs) {
$searchNames = SavedSearch::get()
->addWhere('id', 'IN', $searchIDs)
->addSelect('label')
->execute()->indexBy('id')->column('label');
}
if ($searchNames || $groupNames) {
foreach ($result as $idx => $row) {
if ($row['search_type'] === 'group'
&& !empty($groupNames[$row['search_data']['group_id'] ?? ''])
) {
$row['search_source'] = $groupNames[$row['search_data']['group_id'] ?? ''];
}
elseif (($row['search_type'] === 'search')
&& !empty($searchNames[$row['search_data']['saved_search_id'] ?? ''])
) {
$row['search_source'] = $searchNames[$row['search_data']['saved_search_id'] ?? ''];
}
$result[$idx] = $row;
}
}
}
}
}