| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | Copyright CiviCRM LLC. All rights reserved. | |
| 6 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * |
| 15 | * @package CRM |
| 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 17 | * $Id$ |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | namespace Civi\Api4\Generic; |
| 23 | |
| 24 | use Civi\Api4\Query\Api4SelectQuery; |
| 25 | |
| 26 | /** |
| 27 | * Retrieve $ENTITIES based on criteria specified in the `where` parameter. |
| 28 | * |
| 29 | * Use the `select` param to determine which fields are returned, defaults to `[*]`. |
| 30 | * |
| 31 | * Perform joins on other related entities using a dot notation. |
| 32 | * |
| 33 | * @method $this setHaving(array $clauses) |
| 34 | * @method array getHaving() |
| 35 | */ |
| 36 | class DAOGetAction extends AbstractGetAction { |
| 37 | use Traits\DAOActionTrait; |
| 38 | |
| 39 | /** |
| 40 | * Fields to return. Defaults to all non-custom fields `[*]`. |
| 41 | * |
| 42 | * Use the dot notation to perform joins in the select clause, e.g. selecting `['*', 'contact.*']` from `Email::get()` |
| 43 | * will select all fields for the email + all fields for the related contact. |
| 44 | * |
| 45 | * @var array |
| 46 | * @inheritDoc |
| 47 | */ |
| 48 | protected $select = []; |
| 49 | |
| 50 | /** |
| 51 | * Field(s) by which to group the results. |
| 52 | * |
| 53 | * @var array |
| 54 | */ |
| 55 | protected $groupBy = []; |
| 56 | |
| 57 | /** |
| 58 | * Clause for filtering results after grouping and filters are applied. |
| 59 | * |
| 60 | * Each expression should correspond to an item from the SELECT array. |
| 61 | * |
| 62 | * @var array |
| 63 | */ |
| 64 | protected $having = []; |
| 65 | |
| 66 | public function _run(Result $result) { |
| 67 | $this->setDefaultWhereClause(); |
| 68 | $this->expandSelectClauseWildcards(); |
| 69 | $result->exchangeArray($this->getObjects()); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return array|int |
| 74 | */ |
| 75 | protected function getObjects() { |
| 76 | $query = new Api4SelectQuery($this); |
| 77 | |
| 78 | $result = $query->run(); |
| 79 | if (is_array($result)) { |
| 80 | \CRM_Utils_API_HTMLInputCoder::singleton()->decodeRows($result); |
| 81 | } |
| 82 | return $result; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @return array |
| 87 | */ |
| 88 | public function getGroupBy(): array { |
| 89 | return $this->groupBy; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param array $groupBy |
| 94 | * @return $this |
| 95 | */ |
| 96 | public function setGroupBy(array $groupBy) { |
| 97 | $this->groupBy = $groupBy; |
| 98 | return $this; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param string $field |
| 103 | * @return $this |
| 104 | */ |
| 105 | public function addGroupBy(string $field) { |
| 106 | $this->groupBy[] = $field; |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param string $expr |
| 112 | * @param string $op |
| 113 | * @param mixed $value |
| 114 | * @return $this |
| 115 | * @throws \API_Exception |
| 116 | */ |
| 117 | public function addHaving(string $expr, string $op, $value = NULL) { |
| 118 | if (!in_array($op, \CRM_Core_DAO::acceptedSQLOperators())) { |
| 119 | throw new \API_Exception('Unsupported operator'); |
| 120 | } |
| 121 | $this->having[] = [$expr, $op, $value]; |
| 122 | return $this; |
| 123 | } |
| 124 | |
| 125 | } |