4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace Civi\Api4\Generic
;
22 use Civi\API\Exception\NotImplementedException
;
23 use Civi\Api4\Utils\FormattingUtil
;
26 * Retrieve $ENTITIES based on criteria specified in the `where` parameter.
28 * Use the `select` param to determine which fields are returned, defaults to `[*]`.
30 class BasicGetAction
extends AbstractGetAction
{
31 use Traits\ArrayQueryActionTrait
;
36 * Function(BasicGetAction $thisAction) => array<array>
41 * Basic Get constructor.
43 * @param string $entityName
44 * @param string $actionName
45 * @param callable $getter
47 public function __construct($entityName, $actionName, $getter = NULL) {
48 parent
::__construct($entityName, $actionName);
49 $this->getter
= $getter;
53 * Fetch results from the getter then apply filter/sort/select/limit.
55 * @param \Civi\Api4\Generic\Result $result
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);
66 * This Basic Get class is a general-purpose api for non-DAO-based entities.
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.
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.
76 * Either way, this function should return an array of arrays, each representing one retrieved object.
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.
81 * Sometimes however that may not be practical for performance reasons.
82 * To optimize your getter, it can use the following helpers from $this:
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.
88 * Use $this->_isFieldSelected() to check if a field value is called for - useful
89 * if loading the field involves expensive calculations.
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.
95 * @throws \Civi\API\Exception\NotImplementedException
97 protected function getRecords() {
98 if (is_callable($this->getter
)) {
99 $this->addCallbackToDebugOutput($this->getter
);
100 return call_user_func($this->getter
, $this);
102 throw new NotImplementedException('Getter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
106 * Evaluate :pseudoconstant suffix expressions and replace raw values with option values
109 * @throws \API_Exception
110 * @throws \CRM_Core_Exception
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;
128 // Swap raw values with pseudoconstants
129 FormattingUtil
::formatOutputValues($records, $fields, $this->getEntityName(), $this->getActionName());