1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
<?php
namespace Civi\Api4\Generic;
/**
* Base class for all actions that need to fetch records (Get, Update, Delete, etc)
*
* @package Civi\Api4\Generic
*
* @method $this setWhere(array $wheres)
* @method array getWhere()
* @method $this setOrderBy(array $order)
* @method array getOrderBy()
* @method $this setLimit(int $limit)
* @method int getLimit()
* @method $this setOffset(int $offset)
* @method int getOffset()
*/
abstract class AbstractQueryAction extends AbstractAction {
/**
* Criteria for selecting items.
*
* $example->addWhere('contact_type', 'IN', array('Individual', 'Household'))
*
* @var array
*/
protected $where = [];
/**
* Array of field(s) to use in ordering the results
*
* Defaults to id ASC
*
* $example->addOrderBy('sort_name', 'ASC')
*
* @var array
*/
protected $orderBy = [];
/**
* Maximum number of results to return.
*
* Defaults to unlimited.
*
* Note: the Api Explorer sets this to 25 by default to avoid timeouts.
* Change or remove this default for your application code.
*
* @var int
*/
protected $limit = 0;
/**
* Zero-based index of first result to return.
*
* Defaults to "0" - first record.
*
* @var int
*/
protected $offset = 0;
/**
* @param string $field
* @param string $op
* @param mixed $value
* @return $this
* @throws \API_Exception
*/
public function addWhere($field, $op, $value = NULL) {
if (!in_array($op, \CRM_Core_DAO::acceptedSQLOperators())) {
throw new \API_Exception('Unsupported operator');
}
$this->where[] = [$field, $op, $value];
return $this;
}
/**
* Adds one or more AND/OR/NOT clause groups
*
* @param string $operator
* @param mixed $condition1 ... $conditionN
* Either a nested array of arguments, or a variable number of arguments passed to this function.
*
* @return $this
* @throws \API_Exception
*/
public function addClause($operator, $condition1) {
if (!is_array($condition1[0])) {
$condition1 = array_slice(func_get_args(), 1);
}
$this->where[] = [$operator, $condition1];
return $this;
}
/**
* @param string $field
* @param string $direction
* @return $this
*/
public function addOrderBy($field, $direction = 'ASC') {
$this->orderBy[$field] = $direction;
return $this;
}
}
|