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 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Generic
;
15 use Civi\API\Exception\NotImplementedException
;
16 use Civi\API\Exception\UnauthorizedException
;
17 use Civi\Api4\Utils\CoreUtil
;
20 * $ACTION one or more $ENTITIES.
22 * $ENTITIES are selected based on criteria specified in `where` parameter (required).
24 * @package Civi\Api4\Generic
26 class BasicBatchAction
extends AbstractBatchAction
{
30 * Function(array $item, BasicBatchAction $thisAction): array
35 * BasicBatchAction constructor.
38 * $myAction = new BasicBatchAction($entityName, $actionName, function($item) {
39 * // Do something with $item
44 * @param string $entityName
45 * @param string $actionName
46 * @param callable $doer
48 public function __construct($entityName, $actionName, $doer = NULL) {
49 parent
::__construct($entityName, $actionName);
51 // Accept doer as 4th param for now, but emit deprecated warning
52 $this->doer
= func_get_args()[3] ??
NULL;
54 \CRM_Core_Error
::deprecatedWarning(__CLASS__
. ' constructor received $doer as 4th param; it should be the 3rd as the $select param has been removed');
62 * Checks permissions and then delegates to processBatch.
64 * Note: Unconditional logic must go here in the run function, as delegated functions may be overridden.
66 * @param \Civi\Api4\Generic\Result $result
68 public function _run(Result
$result) {
69 $items = $this->getBatchRecords();
70 foreach ($items as $item) {
71 if ($this->checkPermissions
&& !CoreUtil
::checkAccessRecord($this, $item, \CRM_Core_Session
::getLoggedInContactID() ?
: 0)) {
72 throw new UnauthorizedException("ACL check failed");
75 $this->processBatch($result, $items);
79 * Calls doTask once per item and stores the result.
81 * We pass the doTask function an array representing one item to process.
82 * We expect to get the same format back.
84 * Note: This function may be overridden by the end api.
86 * @param Result $result
88 * @throws NotImplementedException
90 protected function processBatch(Result
$result, array $items) {
91 foreach ($items as $item) {
92 $result[] = $this->doTask($item);
97 * This Basic Batch class can be used in one of two ways:
99 * 1. Use this class directly by passing a callable ($doer) to the constructor.
100 * 2. Extend this class and override this function.
102 * Either way, this function should return an array with an output record
107 * @throws \Civi\API\Exception\NotImplementedException
109 protected function doTask($item) {
110 if (is_callable($this->doer
)) {
111 $this->addCallbackToDebugOutput($this->doer
);
112 return call_user_func($this->doer
, $item, $this);
114 throw new NotImplementedException('Doer function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());