4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2020 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2020
38 namespace Civi\Api4\Generic
;
41 * Base class for all "Get" api actions.
43 * @package Civi\Api4\Generic
45 * @method $this addSelect(string $select)
46 * @method $this setSelect(array $selects)
47 * @method array getSelect()
49 abstract class AbstractGetAction
extends AbstractQueryAction
{
52 * Fields to return. Defaults to all fields.
54 * Set to ["row_count"] to return only the number of items found.
58 protected $select = [];
61 * Only return the number of found items.
65 public function selectRowCount() {
66 $this->select
= ['row_count'];
71 * Adds field defaults to the where clause.
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.
76 * @throws \API_Exception
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']);
90 * Helper to parse the WHERE param for getRecords to perform simple pre-filtering.
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.
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.
99 * @param string $field
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];
113 * Helper to see if a field should be selected by the getRecords function.
115 * Checks the SELECT, WHERE and ORDER BY params to see what fields are needed.
117 * Note that if no SELECT clause has been set then all fields should be selected
118 * and this function will always return TRUE.
120 * @param string $field
123 protected function _isFieldSelected($field) {
124 if (!$this->select ||
in_array($field, $this->select
) ||
isset($this->orderBy
[$field])) {
127 return $this->_whereContains($field);
131 * Walk through the where clause and check if a field is in use.
133 * @param string $field
134 * @param array $clauses
137 protected function _whereContains($field, $clauses = NULL) {
138 if ($clauses === NULL) {
139 $clauses = $this->where
;
141 foreach ($clauses as $clause) {
142 if (is_array($clause) && is_string($clause[0])) {
143 if ($clause[0] == $field) {
146 elseif (is_array($clause[1])) {
147 return $this->_whereContains($field, $clause[1]);