Add APIv4 and pseudoconstants for RelationshipCache
[civicrm-core.git] / Civi / Api4 / Generic / BasicGetAction.php
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 */
18
19
20 namespace Civi\Api4\Generic;
21
22 use Civi\API\Exception\NotImplementedException;
23 use Civi\Api4\Utils\FormattingUtil;
24
25 /**
26 * Retrieve $ENTITIES based on criteria specified in the `where` parameter.
27 *
28 * Use the `select` param to determine which fields are returned, defaults to `[*]`.
29 */
30 class BasicGetAction extends AbstractGetAction {
31 use Traits\ArrayQueryActionTrait;
32
33 /**
34 * @var callable
35 *
36 * Function(BasicGetAction $thisAction) => array<array>
37 */
38 private $getter;
39
40 /**
41 * Basic Get constructor.
42 *
43 * @param string $entityName
44 * @param string $actionName
45 * @param callable $getter
46 */
47 public function __construct($entityName, $actionName, $getter = NULL) {
48 parent::__construct($entityName, $actionName);
49 $this->getter = $getter;
50 }
51
52 /**
53 * Fetch results from the getter then apply filter/sort/select/limit.
54 *
55 * @param \Civi\Api4\Generic\Result $result
56 */
57 public function _run(Result $result) {
58 $this->setDefaultWhereClause();
59 $this->expandSelectClauseWildcards();
60 $values = $this->getRecords();
61 $this->formatRawValues($values);
62 $this->queryArray($values, $result);
63 }
64
65 /**
66 * This Basic Get class is a general-purpose api for non-DAO-based entities.
67 *
68 * Useful for fetching records from files or other places.
69 * You can specify any php function to retrieve the records, and this class will
70 * automatically filter, sort, select & limit the raw data from your callback.
71 *
72 * You can implement this action in one of two ways:
73 * 1. Use this class directly by passing a callable ($getter) to the constructor.
74 * 2. Extend this class and override this function.
75 *
76 * Either way, this function should return an array of arrays, each representing one retrieved object.
77 *
78 * The simplest thing for your getter function to do is return every full record
79 * and allow this class to automatically do the sorting and filtering.
80 *
81 * Sometimes however that may not be practical for performance reasons.
82 * To optimize your getter, it can use the following helpers from $this:
83 *
84 * Use this->_itemsToGet() to match records to field values in the WHERE clause.
85 * Note the WHERE clause can potentially be very complex and it is not recommended
86 * to parse $this->where yourself.
87 *
88 * Use $this->_isFieldSelected() to check if a field value is called for - useful
89 * if loading the field involves expensive calculations.
90 *
91 * Be careful not to make assumptions, e.g. if LIMIT 100 is specified and your getter "helpfully" truncates the list
92 * at 100 without accounting for WHERE, ORDER BY and LIMIT clauses, the final filtered result may be very incorrect.
93 *
94 * @return array
95 * @throws \Civi\API\Exception\NotImplementedException
96 */
97 protected function getRecords() {
98 if (is_callable($this->getter)) {
99 $this->addCallbackToDebugOutput($this->getter);
100 return call_user_func($this->getter, $this);
101 }
102 throw new NotImplementedException('Getter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
103 }
104
105 /**
106 * Evaluate :pseudoconstant suffix expressions and replace raw values with option values
107 *
108 * @param $records
109 * @throws \API_Exception
110 * @throws \CRM_Core_Exception
111 */
112 protected function formatRawValues(&$records) {
113 // Pad $records and $fields with pseudofields
114 $fields = $this->entityFields();
115 foreach ($records as &$values) {
116 foreach ($this->entityFields() as $field) {
117 if (!empty($field['options'])) {
118 foreach (FormattingUtil::$pseudoConstantSuffixes as $suffix) {
119 $pseudofield = $field['name'] . ':' . $suffix;
120 if (!isset($values[$pseudofield]) && isset($values[$field['name']]) && $this->_isFieldSelected($pseudofield)) {
121 $values[$pseudofield] = $values[$field['name']];
122 $fields[$pseudofield] = $field;
123 }
124 }
125 }
126 }
127 }
128 // Swap raw values with pseudoconstants
129 FormattingUtil::formatOutputValues($records, $fields, $this->getEntityName(), $this->getActionName());
130 }
131
132 }