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 | ||
19b53e5b C |
13 | namespace Civi\Api4\Generic; |
14 | ||
15 | /** | |
fc95d9a5 | 16 | * Base class for all `Get` api actions. |
19b53e5b C |
17 | * |
18 | * @package Civi\Api4\Generic | |
19b53e5b C |
19 | */ |
20 | abstract class AbstractGetAction extends AbstractQueryAction { | |
21 | ||
5c952e51 | 22 | use Traits\SelectParamTrait; |
19b53e5b C |
23 | |
24 | /** | |
25 | * Only return the number of found items. | |
26 | * | |
27 | * @return $this | |
28 | */ | |
29 | public function selectRowCount() { | |
30 | $this->select = ['row_count']; | |
31 | return $this; | |
32 | } | |
33 | ||
34 | /** | |
35 | * Adds field defaults to the where clause. | |
36 | * | |
37 | * Note: it will skip adding field defaults when fetching records by id, | |
38 | * or if that field has already been added to the where clause. | |
39 | * | |
40 | * @throws \API_Exception | |
41 | */ | |
42 | protected function setDefaultWhereClause() { | |
43 | if (!$this->_itemsToGet('id')) { | |
44 | $fields = $this->entityFields(); | |
45 | foreach ($fields as $field) { | |
46 | if (isset($field['default_value']) && !$this->_whereContains($field['name'])) { | |
47 | $this->addWhere($field['name'], '=', $field['default_value']); | |
48 | } | |
49 | } | |
50 | } | |
51 | } | |
52 | ||
53 | /** | |
54 | * Helper to parse the WHERE param for getRecords to perform simple pre-filtering. | |
55 | * | |
56 | * This is intended to optimize some common use-cases e.g. calling the api to get | |
57 | * one or more records by name or id. | |
58 | * | |
59 | * Ex: If getRecords fetches a long list of items each with a unique name, | |
60 | * but the user has specified a single record to retrieve, you can optimize the call | |
136ca5bb | 61 | * by checking `$this->_itemsToGet('name')` and only fetching the item(s) with that name. |
19b53e5b C |
62 | * |
63 | * @param string $field | |
64 | * @return array|null | |
65 | */ | |
66 | protected function _itemsToGet($field) { | |
67 | foreach ($this->where as $clause) { | |
68 | // Look for exact-match operators (=, IN, or LIKE with no wildcard) | |
675e2573 | 69 | if ($clause[0] == $field && (in_array($clause[1], ['=', 'IN'], TRUE) || ($clause[1] == 'LIKE' && !(is_string($clause[2]) && strpos($clause[2], '%') !== FALSE)))) { |
19b53e5b C |
70 | return (array) $clause[2]; |
71 | } | |
72 | } | |
73 | return NULL; | |
74 | } | |
75 | ||
76 | /** | |
07d6d25b | 77 | * Helper to see if field(s) should be selected by the getRecords function. |
19b53e5b C |
78 | * |
79 | * Checks the SELECT, WHERE and ORDER BY params to see what fields are needed. | |
80 | * | |
81 | * Note that if no SELECT clause has been set then all fields should be selected | |
961e974c | 82 | * and this function will return TRUE for field expressions that don't contain a :pseudoconstant suffix. |
19b53e5b | 83 | * |
07d6d25b CW |
84 | * @param string ...$fieldNames |
85 | * One or more field names to check (uses OR if multiple) | |
19b53e5b | 86 | * @return bool |
07d6d25b | 87 | * Returns true if any given fields are in use. |
19b53e5b | 88 | */ |
07d6d25b | 89 | protected function _isFieldSelected(string ...$fieldNames) { |
961e974c | 90 | if ((!$this->select && strpos($fieldNames[0], ':') === FALSE) || array_intersect($fieldNames, array_merge($this->select, array_keys($this->orderBy)))) { |
19b53e5b C |
91 | return TRUE; |
92 | } | |
07d6d25b | 93 | return $this->_whereContains($fieldNames); |
19b53e5b C |
94 | } |
95 | ||
96 | /** | |
07d6d25b | 97 | * Walk through the where clause and check if field(s) are in use. |
19b53e5b | 98 | * |
07d6d25b CW |
99 | * @param string|array $fieldName |
100 | * A single fieldName or an array of names (uses OR if multiple) | |
19b53e5b C |
101 | * @param array $clauses |
102 | * @return bool | |
07d6d25b | 103 | * Returns true if any given fields are found in the where clause. |
19b53e5b | 104 | */ |
07d6d25b | 105 | protected function _whereContains($fieldName, $clauses = NULL) { |
19b53e5b C |
106 | if ($clauses === NULL) { |
107 | $clauses = $this->where; | |
108 | } | |
07d6d25b | 109 | $fieldName = (array) $fieldName; |
19b53e5b C |
110 | foreach ($clauses as $clause) { |
111 | if (is_array($clause) && is_string($clause[0])) { | |
07d6d25b | 112 | if (in_array($clause[0], $fieldName)) { |
19b53e5b C |
113 | return TRUE; |
114 | } | |
115 | elseif (is_array($clause[1])) { | |
07d6d25b | 116 | return $this->_whereContains($fieldName, $clause[1]); |
19b53e5b C |
117 | } |
118 | } | |
119 | } | |
120 | return FALSE; | |
121 | } | |
122 | ||
123 | } |