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