Merge pull request #19973 from JMAConsulting/core_365_1
[civicrm-core.git] / Civi / Api4 / Generic / BasicUpdateAction.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 namespace Civi\Api4\Generic;
21
22 use Civi\API\Exception\NotImplementedException;
23 use Civi\API\Exception\UnauthorizedException;
24 use Civi\Api4\Utils\CoreUtil;
25
26 /**
27 * Update one or more $ENTITY with new values.
28 *
29 * Use the `where` clause (required) to select them.
30 */
31 class BasicUpdateAction extends AbstractUpdateAction {
32
33 /**
34 * @var callable
35 * Function(array $item, BasicUpdateAction $thisAction): array
36 */
37 private $setter;
38
39 /**
40 * BasicUpdateAction constructor.
41 *
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
47 */
48 public function __construct($entityName, $actionName, $select = 'id', $setter = NULL) {
49 parent::__construct($entityName, $actionName, $select);
50 $this->setter = $setter;
51 }
52
53 /**
54 * We pass the writeRecord function an array representing one item to update.
55 * We expect to get the same format back.
56 *
57 * @param \Civi\Api4\Generic\Result $result
58 * @throws \API_Exception
59 * @throws \Civi\API\Exception\NotImplementedException
60 */
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");
68 }
69 $result[] = $this->writeRecord($record);
70 }
71 }
72
73 /**
74 * This Basic Update class can be used in one of two ways:
75 *
76 * 1. Use this class directly by passing a callable ($setter) to the constructor.
77 * 2. Extend this class and override this function.
78 *
79 * Either way, this function should return an array representing the one modified object.
80 *
81 * @param array $item
82 * @return array
83 * @throws \Civi\API\Exception\NotImplementedException
84 */
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);
89 }
90 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
91 }
92
93 }