Merge pull request #19737 from sunilpawar/show_inactive_active_case_role
[civicrm-core.git] / Civi / Api4 / Generic / AbstractGetAction.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 |
10 +--------------------------------------------------------------------+
11 */
12
13 namespace Civi\Api4\Generic;
14
15 use Civi\Api4\Utils\SelectUtil;
16
17 /**
18 * Base class for all `Get` api actions.
19 *
20 * @package Civi\Api4\Generic
21 *
22 * @method $this setSelect(array $selects) Set array of fields to be selected (wildcard * allowed)
23 * @method array getSelect()
24 */
25 abstract class AbstractGetAction extends AbstractQueryAction {
26
27 /**
28 * Fields to return for each $ENTITY. Defaults to all fields `[*]`.
29 *
30 * Use the * wildcard by itself to select all available fields, or use it to match similarly-named fields.
31 * E.g. `is_*` will match fields named is_primary, is_active, etc.
32 *
33 * Set to `["row_count"]` to return only the number of $ENTITIES found.
34 *
35 * @var array
36 */
37 protected $select = [];
38
39 /**
40 * Only return the number of found items.
41 *
42 * @return $this
43 */
44 public function selectRowCount() {
45 $this->select = ['row_count'];
46 return $this;
47 }
48
49 /**
50 * Adds field defaults to the where clause.
51 *
52 * Note: it will skip adding field defaults when fetching records by id,
53 * or if that field has already been added to the where clause.
54 *
55 * @throws \API_Exception
56 */
57 protected function setDefaultWhereClause() {
58 if (!$this->_itemsToGet('id')) {
59 $fields = $this->entityFields();
60 foreach ($fields as $field) {
61 if (isset($field['default_value']) && !$this->_whereContains($field['name'])) {
62 $this->addWhere($field['name'], '=', $field['default_value']);
63 }
64 }
65 }
66 }
67
68 /**
69 * Adds all fields matched by the * wildcard
70 *
71 * @throws \API_Exception
72 */
73 protected function expandSelectClauseWildcards() {
74 $wildFields = array_filter($this->select, function($item) {
75 return strpos($item, '*') !== FALSE && strpos($item, '.') === FALSE && strpos($item, '(') === FALSE && strpos($item, ' ') === FALSE;
76 });
77 foreach ($wildFields as $item) {
78 $pos = array_search($item, array_values($this->select));
79 $matches = SelectUtil::getMatchingFields($item, array_column($this->entityFields(), 'name'));
80 array_splice($this->select, $pos, 1, $matches);
81 }
82 $this->select = array_unique($this->select);
83 }
84
85 /**
86 * Helper to parse the WHERE param for getRecords to perform simple pre-filtering.
87 *
88 * This is intended to optimize some common use-cases e.g. calling the api to get
89 * one or more records by name or id.
90 *
91 * Ex: If getRecords fetches a long list of items each with a unique name,
92 * but the user has specified a single record to retrieve, you can optimize the call
93 * by checking `$this->_itemsToGet('name')` and only fetching the item(s) with that name.
94 *
95 * @param string $field
96 * @return array|null
97 */
98 protected function _itemsToGet($field) {
99 foreach ($this->where as $clause) {
100 // Look for exact-match operators (=, IN, or LIKE with no wildcard)
101 if ($clause[0] == $field && (in_array($clause[1], ['=', 'IN'], TRUE) || ($clause[1] == 'LIKE' && !(is_string($clause[2]) && strpos($clause[2], '%') !== FALSE)))) {
102 return (array) $clause[2];
103 }
104 }
105 return NULL;
106 }
107
108 /**
109 * Helper to see if field(s) should be selected by the getRecords function.
110 *
111 * Checks the SELECT, WHERE and ORDER BY params to see what fields are needed.
112 *
113 * Note that if no SELECT clause has been set then all fields should be selected
114 * and this function will return TRUE for field expressions that don't contain a :pseudoconstant suffix.
115 *
116 * @param string ...$fieldNames
117 * One or more field names to check (uses OR if multiple)
118 * @return bool
119 * Returns true if any given fields are in use.
120 */
121 protected function _isFieldSelected(string ...$fieldNames) {
122 if ((!$this->select && strpos($fieldNames[0], ':') === FALSE) || array_intersect($fieldNames, array_merge($this->select, array_keys($this->orderBy)))) {
123 return TRUE;
124 }
125 return $this->_whereContains($fieldNames);
126 }
127
128 /**
129 * Walk through the where clause and check if field(s) are in use.
130 *
131 * @param string|array $fieldName
132 * A single fieldName or an array of names (uses OR if multiple)
133 * @param array $clauses
134 * @return bool
135 * Returns true if any given fields are found in the where clause.
136 */
137 protected function _whereContains($fieldName, $clauses = NULL) {
138 if ($clauses === NULL) {
139 $clauses = $this->where;
140 }
141 $fieldName = (array) $fieldName;
142 foreach ($clauses as $clause) {
143 if (is_array($clause) && is_string($clause[0])) {
144 if (in_array($clause[0], $fieldName)) {
145 return TRUE;
146 }
147 elseif (is_array($clause[1])) {
148 return $this->_whereContains($fieldName, $clause[1]);
149 }
150 }
151 }
152 return FALSE;
153 }
154
155 /**
156 * Add one or more fields to be selected (wildcard * allowed)
157 * @param string ...$fieldNames
158 * @return $this
159 */
160 public function addSelect(string ...$fieldNames) {
161 $this->select = array_merge($this->select, $fieldNames);
162 return $this;
163 }
164
165 }