Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / Civi / Api4 / Generic / AbstractGetAction.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
f299f7db 7 | Copyright CiviCRM LLC (c) 2004-2020 |
380f3545
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 *
31 * @package CRM
f299f7db 32 * @copyright CiviCRM LLC (c) 2004-2020
380f3545
TO
33 * $Id$
34 *
35 */
36
37
19b53e5b
C
38namespace Civi\Api4\Generic;
39
40/**
41 * Base class for all "Get" api actions.
42 *
43 * @package Civi\Api4\Generic
44 *
45 * @method $this addSelect(string $select)
46 * @method $this setSelect(array $selects)
47 * @method array getSelect()
48 */
49abstract class AbstractGetAction extends AbstractQueryAction {
50
51 /**
52 * Fields to return. Defaults to all fields.
53 *
54 * Set to ["row_count"] to return only the number of items found.
55 *
56 * @var array
57 */
58 protected $select = [];
59
60 /**
61 * Only return the number of found items.
62 *
63 * @return $this
64 */
65 public function selectRowCount() {
66 $this->select = ['row_count'];
67 return $this;
68 }
69
70 /**
71 * Adds field defaults to the where clause.
72 *
73 * Note: it will skip adding field defaults when fetching records by id,
74 * or if that field has already been added to the where clause.
75 *
76 * @throws \API_Exception
77 */
78 protected function setDefaultWhereClause() {
79 if (!$this->_itemsToGet('id')) {
80 $fields = $this->entityFields();
81 foreach ($fields as $field) {
82 if (isset($field['default_value']) && !$this->_whereContains($field['name'])) {
83 $this->addWhere($field['name'], '=', $field['default_value']);
84 }
85 }
86 }
87 }
88
89 /**
90 * Helper to parse the WHERE param for getRecords to perform simple pre-filtering.
91 *
92 * This is intended to optimize some common use-cases e.g. calling the api to get
93 * one or more records by name or id.
94 *
95 * Ex: If getRecords fetches a long list of items each with a unique name,
96 * but the user has specified a single record to retrieve, you can optimize the call
97 * by checking $this->_itemsToGet('name') and only fetching the item(s) with that name.
98 *
99 * @param string $field
100 * @return array|null
101 */
102 protected function _itemsToGet($field) {
103 foreach ($this->where as $clause) {
104 // Look for exact-match operators (=, IN, or LIKE with no wildcard)
105 if ($clause[0] == $field && (in_array($clause[1], ['=', 'IN']) || ($clause[1] == 'LIKE' && !(is_string($clause[2]) && strpos($clause[2], '%') !== FALSE)))) {
106 return (array) $clause[2];
107 }
108 }
109 return NULL;
110 }
111
112 /**
113 * Helper to see if a field should be selected by the getRecords function.
114 *
115 * Checks the SELECT, WHERE and ORDER BY params to see what fields are needed.
116 *
117 * Note that if no SELECT clause has been set then all fields should be selected
118 * and this function will always return TRUE.
119 *
120 * @param string $field
121 * @return bool
122 */
123 protected function _isFieldSelected($field) {
124 if (!$this->select || in_array($field, $this->select) || isset($this->orderBy[$field])) {
125 return TRUE;
126 }
127 return $this->_whereContains($field);
128 }
129
130 /**
131 * Walk through the where clause and check if a field is in use.
132 *
133 * @param string $field
134 * @param array $clauses
135 * @return bool
136 */
137 protected function _whereContains($field, $clauses = NULL) {
138 if ($clauses === NULL) {
139 $clauses = $this->where;
140 }
141 foreach ($clauses as $clause) {
142 if (is_array($clause) && is_string($clause[0])) {
143 if ($clause[0] == $field) {
144 return TRUE;
145 }
146 elseif (is_array($clause[1])) {
147 return $this->_whereContains($field, $clause[1]);
148 }
149 }
150 }
151 return FALSE;
152 }
153
154}