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\API\Exception\UnauthorizedException
;
24 use Civi\Api4\Utils\CoreUtil
;
27 * $ACTION one or more $ENTITIES.
29 * $ENTITIES are selected based on criteria specified in `where` parameter (required).
31 * @package Civi\Api4\Generic
33 class BasicBatchAction
extends AbstractBatchAction
{
37 * Function(array $item, BasicBatchAction $thisAction): array
42 * BasicBatchAction constructor.
45 * $myAction = new BasicBatchAction($entityName, $actionName, function($item) {
46 * // Do something with $item
51 * @param string $entityName
52 * @param string $actionName
53 * @param string|array $select
54 * One or more fields to select from each matching item.
55 * @param callable $doer
57 public function __construct($entityName, $actionName, $select = 'id', $doer = NULL) {
58 parent
::__construct($entityName, $actionName, $select);
63 * We pass the doTask function an array representing one item to update.
64 * We expect to get the same format back.
66 * @param \Civi\Api4\Generic\Result $result
68 public function _run(Result
$result) {
69 foreach ($this->getBatchRecords() as $item) {
70 if ($this->checkPermissions
&& !CoreUtil
::checkAccess($this->getEntityName(), $this->getActionName(), $item)) {
71 throw new UnauthorizedException("ACL check failed");
73 $result[] = $this->doTask($item);
78 * This Basic Batch class can be used in one of two ways:
80 * 1. Use this class directly by passing a callable ($doer) to the constructor.
81 * 2. Extend this class and override this function.
83 * Either way, this function should return an array with an output record
88 * @throws \Civi\API\Exception\NotImplementedException
90 protected function doTask($item) {
91 if (is_callable($this->doer
)) {
92 $this->addCallbackToDebugOutput($this->doer
);
93 return call_user_func($this->doer
, $item, $this);
95 throw new NotImplementedException('Doer function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());