Merge pull request #21150 from colemanw/afformUpload
[civicrm-core.git] / Civi / Api4 / Generic / BasicSaveAction.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 namespace Civi\Api4\Generic;
14
15 use Civi\API\Exception\NotImplementedException;
16 use Civi\Api4\Utils\CoreUtil;
17
18 /**
19 * @inheritDoc
20 */
21 class BasicSaveAction extends AbstractSaveAction {
22
23 /**
24 * @var callable
25 * Function(array $item, BasicCreateAction $thisAction): array
26 */
27 private $setter;
28
29 /**
30 * Basic Save constructor.
31 *
32 * @param string $entityName
33 * @param string $actionName
34 * @param callable $setter
35 */
36 public function __construct($entityName, $actionName, $setter = NULL) {
37 parent::__construct($entityName, $actionName);
38 // Accept setter as 4th param for now, but emit deprecated warning
39 $this->setter = func_get_args()[3] ?? NULL;
40 if ($this->setter) {
41 \CRM_Core_Error::deprecatedWarning(__CLASS__ . ' constructor received $setter as 4th param; it should be the 3rd as the $select param has been removed');
42 }
43 else {
44 $this->setter = $setter;
45 }
46 }
47
48 /**
49 * We pass the writeRecord function an array representing one item to write.
50 * We expect to get the same format back.
51 *
52 * @param \Civi\Api4\Generic\Result $result
53 */
54 public function _run(Result $result) {
55 $idField = CoreUtil::getIdFieldName($this->getEntityName());
56 foreach ($this->records as &$record) {
57 $record += $this->defaults;
58 $this->formatWriteValues($record);
59 }
60 $this->validateValues();
61 foreach ($this->records as $item) {
62 $result[] = $this->writeRecord($item);
63 }
64 if ($this->reload) {
65 /** @var BasicGetAction $get */
66 $get = \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4]);
67 $get
68 ->setCheckPermissions($this->getCheckPermissions())
69 ->addWhere($idField, 'IN', (array) $result->column($idField));
70 $result->exchangeArray((array) $get->execute());
71 }
72 }
73
74 /**
75 * This Basic Save class can be used in one of two ways:
76 *
77 * 1. Use this class directly by passing a callable ($setter) to the constructor.
78 * 2. Extend this class and override this function.
79 *
80 * Either way, this function should return an array representing the one new object.
81 *
82 * @param array $item
83 * @return array
84 * @throws \Civi\API\Exception\NotImplementedException
85 */
86 protected function writeRecord($item) {
87 if (is_callable($this->setter)) {
88 $this->addCallbackToDebugOutput($this->setter);
89 return call_user_func($this->setter, $item, $this);
90 }
91 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
92 }
93
94 }