4f1d3494619f00d5d20ee3275b336efa30027a30
[civicrm-core.git] / Civi / Api4 / Event / Subscriber / IsCurrentSubscriber.php
1 <?php
2
3 namespace Civi\Api4\Event\Subscriber;
4
5 use Civi\API\Event\PrepareEvent;
6 use Civi\Api4\Utils\ReflectionUtils;
7
8 /**
9 * Process $current api param for Get actions
10 *
11 * @see \Civi\Api4\Generic\Traits\IsCurrentTrait
12 */
13 class IsCurrentSubscriber extends Generic\AbstractPrepareSubscriber {
14
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))
20 ) {
21 $fields = $action->entityFields();
22 if ($action->getCurrent()) {
23 if (isset($fields['is_active'])) {
24 $action->addWhere('is_active', '=', '1');
25 }
26 $action->addClause('OR', ['start_date', 'IS NULL'], ['start_date', '<=', 'now']);
27 $action->addClause('OR', ['end_date', 'IS NULL'], ['end_date', '>=', 'now']);
28 }
29 elseif ($action->getCurrent() === FALSE) {
30 $conditions = [['end_date', '<', 'now'], ['start_date', '>', 'now']];
31 if (isset($fields['is_active'])) {
32 $conditions[] = ['is_active', '=', '0'];
33 }
34 $action->addClause('OR', $conditions);
35 }
36 }
37 }
38
39 }