3 namespace Civi\Api4\Event\Subscriber
;
5 use Civi\API\Event\PrepareEvent
;
6 use Civi\Api4\Utils\ReflectionUtils
;
9 * Process $current api param for Get actions
11 * @see \Civi\Api4\Generic\Traits\IsCurrentTrait
13 class IsCurrentSubscriber
extends Generic\AbstractPrepareSubscriber
{
15 public function onApiPrepare(PrepareEvent
$event) {
16 /** @var \Civi\Api4\Generic\AbstractQueryAction $action */
17 $action = $event->getApiRequest();
18 if ($action['version'] == 4 && method_exists($action, 'getCurrent')
19 && in_array('Civi\Api4\Generic\Traits\IsCurrentTrait', ReflectionUtils
::getTraits($action))
21 $fields = $action->entityFields();
22 if ($action->getCurrent()) {
23 if (isset($fields['is_active'])) {
24 $action->addWhere('is_active', '=', '1');
26 $action->addClause('OR', ['start_date', 'IS NULL'], ['start_date', '<=', 'now']);
27 $action->addClause('OR', ['end_date', 'IS NULL'], ['end_date', '>=', 'now']);
29 elseif ($action->getCurrent() === FALSE) {
30 $conditions = [['end_date', '<', 'now'], ['start_date', '>', 'now']];
31 if (isset($fields['is_active'])) {
32 $conditions[] = ['is_active', '=', '0'];
34 $action->addClause('OR', $conditions);