Merge pull request #20770 from demeritcowboy/smartydash
[civicrm-core.git] / Civi / Api4 / Generic / BasicUpdateAction.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
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 |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
19b53e5b
C
13namespace Civi\Api4\Generic;
14
15use Civi\API\Exception\NotImplementedException;
929a9585
CW
16use Civi\API\Exception\UnauthorizedException;
17use Civi\Api4\Utils\CoreUtil;
19b53e5b
C
18
19/**
fc95d9a5 20 * Update one or more $ENTITY with new values.
19b53e5b 21 *
fc95d9a5 22 * Use the `where` clause (required) to select them.
19b53e5b
C
23 */
24class BasicUpdateAction extends AbstractUpdateAction {
25
26 /**
27 * @var callable
9bafff7c 28 * Function(array $item, BasicUpdateAction $thisAction): array
19b53e5b
C
29 */
30 private $setter;
31
32 /**
33 * BasicUpdateAction constructor.
34 *
35 * @param string $entityName
36 * @param string $actionName
37 * @param string|array $select
38 * One or more fields to select from each matching item.
39 * @param callable $setter
19b53e5b
C
40 */
41 public function __construct($entityName, $actionName, $select = 'id', $setter = NULL) {
42 parent::__construct($entityName, $actionName, $select);
43 $this->setter = $setter;
44 }
45
46 /**
47 * We pass the writeRecord function an array representing one item to update.
48 * We expect to get the same format back.
49 *
50 * @param \Civi\Api4\Generic\Result $result
51 * @throws \API_Exception
52 * @throws \Civi\API\Exception\NotImplementedException
53 */
54 public function _run(Result $result) {
961e974c 55 $this->formatWriteValues($this->values);
a9aac3bf 56 $this->validateValues();
19b53e5b 57 foreach ($this->getBatchRecords() as $item) {
929a9585 58 $record = $this->values + $item;
70da3927 59 if ($this->checkPermissions && !CoreUtil::checkAccessRecord($this, $record, \CRM_Core_Session::getLoggedInContactID() ?: 0)) {
929a9585
CW
60 throw new UnauthorizedException("ACL check failed");
61 }
62 $result[] = $this->writeRecord($record);
19b53e5b 63 }
19b53e5b
C
64 }
65
66 /**
67 * This Basic Update class can be used in one of two ways:
68 *
69 * 1. Use this class directly by passing a callable ($setter) to the constructor.
70 * 2. Extend this class and override this function.
71 *
72 * Either way, this function should return an array representing the one modified object.
73 *
74 * @param array $item
75 * @return array
76 * @throws \Civi\API\Exception\NotImplementedException
77 */
78 protected function writeRecord($item) {
79 if (is_callable($this->setter)) {
32f72d83 80 $this->addCallbackToDebugOutput($this->setter);
19b53e5b
C
81 return call_user_func($this->setter, $item, $this);
82 }
83 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
84 }
85
86}