Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
41498ac5 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
41498ac5 TO |
7 | | This work is published under the GNU AGPLv3 license with some | |
8 | | permitted exceptions and without any warranty. For full license | | |
9 | | and copyright information, see https://civicrm.org/licensing | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
21 | ||
19b53e5b C |
22 | namespace Civi\Api4\Generic; |
23 | ||
3c7c8fa6 CW |
24 | use Civi\Api4\Query\Api4SelectQuery; |
25 | ||
19b53e5b | 26 | /** |
e3c6d5ff | 27 | * Retrieve $ENTITIES based on criteria specified in the `where` parameter. |
19b53e5b | 28 | * |
fc95d9a5 | 29 | * Use the `select` param to determine which fields are returned, defaults to `[*]`. |
19b53e5b C |
30 | * |
31 | * Perform joins on other related entities using a dot notation. | |
c9e3ae2e CW |
32 | * |
33 | * @method $this setHaving(array $clauses) | |
34 | * @method array getHaving() | |
19b53e5b C |
35 | */ |
36 | class DAOGetAction extends AbstractGetAction { | |
37 | use Traits\DAOActionTrait; | |
38 | ||
39e0f675 | 39 | /** |
fc95d9a5 | 40 | * Fields to return. Defaults to all non-custom fields `[*]`. |
39e0f675 | 41 | * |
fc95d9a5 | 42 | * Use the dot notation to perform joins in the select clause, e.g. selecting `['*', 'contact.*']` from `Email::get()` |
39e0f675 CW |
43 | * will select all fields for the email + all fields for the related contact. |
44 | * | |
45 | * @var array | |
46 | * @inheritDoc | |
47 | */ | |
48 | protected $select = []; | |
49 | ||
fba513f6 CW |
50 | /** |
51 | * Field(s) by which to group the results. | |
52 | * | |
53 | * @var array | |
54 | */ | |
55 | protected $groupBy = []; | |
56 | ||
c9e3ae2e CW |
57 | /** |
58 | * Clause for filtering results after grouping and filters are applied. | |
59 | * | |
60 | * Each expression should correspond to an item from the SELECT array. | |
61 | * | |
62 | * @var array | |
63 | */ | |
64 | protected $having = []; | |
65 | ||
19b53e5b C |
66 | public function _run(Result $result) { |
67 | $this->setDefaultWhereClause(); | |
39e0f675 | 68 | $this->expandSelectClauseWildcards(); |
19b53e5b C |
69 | $result->exchangeArray($this->getObjects()); |
70 | } | |
71 | ||
3c7c8fa6 CW |
72 | /** |
73 | * @return array|int | |
74 | */ | |
75 | protected function getObjects() { | |
76 | $query = new Api4SelectQuery($this); | |
77 | ||
78 | $result = $query->run(); | |
79 | if (is_array($result)) { | |
80 | \CRM_Utils_API_HTMLInputCoder::singleton()->decodeRows($result); | |
81 | } | |
82 | return $result; | |
83 | } | |
84 | ||
fba513f6 CW |
85 | /** |
86 | * @return array | |
87 | */ | |
88 | public function getGroupBy(): array { | |
89 | return $this->groupBy; | |
90 | } | |
91 | ||
92 | /** | |
93 | * @param array $groupBy | |
94 | * @return $this | |
95 | */ | |
96 | public function setGroupBy(array $groupBy) { | |
97 | $this->groupBy = $groupBy; | |
98 | return $this; | |
99 | } | |
100 | ||
101 | /** | |
102 | * @param string $field | |
103 | * @return $this | |
104 | */ | |
105 | public function addGroupBy(string $field) { | |
106 | $this->groupBy[] = $field; | |
107 | return $this; | |
108 | } | |
109 | ||
c9e3ae2e CW |
110 | /** |
111 | * @param string $expr | |
112 | * @param string $op | |
113 | * @param mixed $value | |
114 | * @return $this | |
115 | * @throws \API_Exception | |
116 | */ | |
117 | public function addHaving(string $expr, string $op, $value = NULL) { | |
118 | if (!in_array($op, \CRM_Core_DAO::acceptedSQLOperators())) { | |
119 | throw new \API_Exception('Unsupported operator'); | |
120 | } | |
121 | $this->having[] = [$expr, $op, $value]; | |
122 | return $this; | |
123 | } | |
124 | ||
19b53e5b | 125 | } |