Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / Civi / Api4 / Generic / AbstractQueryAction.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 actions that need to fetch records (Get, Update, Delete, etc)
42 *
43 * @package Civi\Api4\Generic
44 *
45 * @method $this setWhere(array $wheres)
46 * @method array getWhere()
47 * @method $this setOrderBy(array $order)
48 * @method array getOrderBy()
49 * @method $this setLimit(int $limit)
50 * @method int getLimit()
51 * @method $this setOffset(int $offset)
52 * @method int getOffset()
53 */
54abstract class AbstractQueryAction extends AbstractAction {
55
56 /**
57 * Criteria for selecting items.
58 *
59 * $example->addWhere('contact_type', 'IN', array('Individual', 'Household'))
60 *
61 * @var array
62 */
63 protected $where = [];
64
65 /**
66 * Array of field(s) to use in ordering the results
67 *
68 * Defaults to id ASC
69 *
70 * $example->addOrderBy('sort_name', 'ASC')
71 *
72 * @var array
73 */
74 protected $orderBy = [];
75
76 /**
77 * Maximum number of results to return.
78 *
79 * Defaults to unlimited.
80 *
81 * Note: the Api Explorer sets this to 25 by default to avoid timeouts.
82 * Change or remove this default for your application code.
83 *
84 * @var int
85 */
86 protected $limit = 0;
87
88 /**
89 * Zero-based index of first result to return.
90 *
91 * Defaults to "0" - first record.
92 *
93 * @var int
94 */
95 protected $offset = 0;
96
97 /**
98 * @param string $field
99 * @param string $op
100 * @param mixed $value
101 * @return $this
102 * @throws \API_Exception
103 */
104 public function addWhere($field, $op, $value = NULL) {
105 if (!in_array($op, \CRM_Core_DAO::acceptedSQLOperators())) {
106 throw new \API_Exception('Unsupported operator');
107 }
108 $this->where[] = [$field, $op, $value];
109 return $this;
110 }
111
112 /**
113 * Adds one or more AND/OR/NOT clause groups
114 *
115 * @param string $operator
116 * @param mixed $condition1 ... $conditionN
117 * Either a nested array of arguments, or a variable number of arguments passed to this function.
118 *
119 * @return $this
120 * @throws \API_Exception
121 */
122 public function addClause($operator, $condition1) {
123 if (!is_array($condition1[0])) {
124 $condition1 = array_slice(func_get_args(), 1);
125 }
126 $this->where[] = [$operator, $condition1];
127 return $this;
128 }
129
130 /**
131 * @param string $field
132 * @param string $direction
133 * @return $this
134 */
135 public function addOrderBy($field, $direction = 'ASC') {
136 $this->orderBy[$field] = $direction;
137 return $this;
138 }
139
140 /**
141 * A human-readable where clause, for the reading enjoyment of you humans.
142 *
143 * @param array $whereClause
144 * @param string $op
145 * @return string
146 */
147 protected function whereClauseToString($whereClause = NULL, $op = 'AND') {
148 if ($whereClause === NULL) {
149 $whereClause = $this->where;
150 }
151 $output = '';
152 if (!is_array($whereClause) || !$whereClause) {
153 return $output;
154 }
155 if (in_array($whereClause[0], ['AND', 'OR', 'NOT'])) {
156 $op = array_shift($whereClause);
157 if ($op == 'NOT') {
158 $output = 'NOT ';
159 $op = 'AND';
160 }
161 return $output . '(' . $this->whereClauseToString($whereClause, $op) . ')';
162 }
163 elseif (isset($whereClause[1]) && in_array($whereClause[1], \CRM_Core_DAO::acceptedSQLOperators())) {
164 $output = $whereClause[0] . ' ' . $whereClause[1] . ' ';
165 if (isset($whereClause[2])) {
166 $output .= is_array($whereClause[2]) ? '[' . implode(', ', $whereClause[2]) . ']' : $whereClause[2];
167 }
168 }
169 else {
170 $clauses = [];
171 foreach (array_filter($whereClause) as $clause) {
172 $clauses[] = $this->whereClauseToString($clause, $op);
173 }
174 $output = implode(" $op ", $clauses);
175 }
176 return $output;
177 }
178
179}