4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace Civi\Api4\Generic
;
25 * Base class for all "Get" api actions.
27 * @package Civi\Api4\Generic
29 * @method $this addSelect(string $select)
30 * @method $this setSelect(array $selects)
31 * @method array getSelect()
33 abstract class AbstractGetAction
extends AbstractQueryAction
{
36 * Fields to return. Defaults to all fields.
38 * Set to ["row_count"] to return only the number of items found.
42 protected $select = [];
45 * Only return the number of found items.
49 public function selectRowCount() {
50 $this->select
= ['row_count'];
55 * Adds field defaults to the where clause.
57 * Note: it will skip adding field defaults when fetching records by id,
58 * or if that field has already been added to the where clause.
60 * @throws \API_Exception
62 protected function setDefaultWhereClause() {
63 if (!$this->_itemsToGet('id')) {
64 $fields = $this->entityFields();
65 foreach ($fields as $field) {
66 if (isset($field['default_value']) && !$this->_whereContains($field['name'])) {
67 $this->addWhere($field['name'], '=', $field['default_value']);
74 * Helper to parse the WHERE param for getRecords to perform simple pre-filtering.
76 * This is intended to optimize some common use-cases e.g. calling the api to get
77 * one or more records by name or id.
79 * Ex: If getRecords fetches a long list of items each with a unique name,
80 * but the user has specified a single record to retrieve, you can optimize the call
81 * by checking $this->_itemsToGet('name') and only fetching the item(s) with that name.
83 * @param string $field
86 protected function _itemsToGet($field) {
87 foreach ($this->where
as $clause) {
88 // Look for exact-match operators (=, IN, or LIKE with no wildcard)
89 if ($clause[0] == $field && (in_array($clause[1], ['=', 'IN']) ||
($clause[1] == 'LIKE' && !(is_string($clause[2]) && strpos($clause[2], '%') !== FALSE)))) {
90 return (array) $clause[2];
97 * Helper to see if a field should be selected by the getRecords function.
99 * Checks the SELECT, WHERE and ORDER BY params to see what fields are needed.
101 * Note that if no SELECT clause has been set then all fields should be selected
102 * and this function will always return TRUE.
104 * @param string $field
107 protected function _isFieldSelected($field) {
108 if (!$this->select ||
in_array($field, $this->select
) ||
isset($this->orderBy
[$field])) {
111 return $this->_whereContains($field);
115 * Walk through the where clause and check if a field is in use.
117 * @param string $field
118 * @param array $clauses
121 protected function _whereContains($field, $clauses = NULL) {
122 if ($clauses === NULL) {
123 $clauses = $this->where
;
125 foreach ($clauses as $clause) {
126 if (is_array($clause) && is_string($clause[0])) {
127 if ($clause[0] == $field) {
130 elseif (is_array($clause[1])) {
131 return $this->_whereContains($field, $clause[1]);