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 * Update one or more $ENTITY with new values.
29 * Use the `where` clause (required) to select them.
31 class BasicUpdateAction
extends AbstractUpdateAction
{
35 * Function(array $item, BasicUpdateAction $thisAction): array
40 * BasicUpdateAction constructor.
42 * @param string $entityName
43 * @param string $actionName
44 * @param string|array $select
45 * One or more fields to select from each matching item.
46 * @param callable $setter
48 public function __construct($entityName, $actionName, $select = 'id', $setter = NULL) {
49 parent
::__construct($entityName, $actionName, $select);
50 $this->setter
= $setter;
54 * We pass the writeRecord function an array representing one item to update.
55 * We expect to get the same format back.
57 * @param \Civi\Api4\Generic\Result $result
58 * @throws \API_Exception
59 * @throws \Civi\API\Exception\NotImplementedException
61 public function _run(Result
$result) {
62 $this->formatWriteValues($this->values
);
63 $this->validateValues();
64 foreach ($this->getBatchRecords() as $item) {
65 $record = $this->values +
$item;
66 if ($this->checkPermissions
&& !CoreUtil
::checkAccessRecord($this, $record, \CRM_Core_Session
::getLoggedInContactID() ?
: 0)) {
67 throw new UnauthorizedException("ACL check failed");
69 $result[] = $this->writeRecord($record);
74 * This Basic Update class can be used in one of two ways:
76 * 1. Use this class directly by passing a callable ($setter) to the constructor.
77 * 2. Extend this class and override this function.
79 * Either way, this function should return an array representing the one modified object.
83 * @throws \Civi\API\Exception\NotImplementedException
85 protected function writeRecord($item) {
86 if (is_callable($this->setter
)) {
87 $this->addCallbackToDebugOutput($this->setter
);
88 return call_user_func($this->setter
, $item, $this);
90 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());