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 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
21 | ||
19b53e5b C |
22 | namespace Civi\Api4\Generic; |
23 | ||
24 | use Civi\API\Exception\NotImplementedException; | |
19b53e5b C |
25 | |
26 | /** | |
e3c6d5ff | 27 | * $ACTION one or more $ENTITIES. |
19b53e5b | 28 | * |
fc95d9a5 | 29 | * If saving more than one new $ENTITY with similar values, use the `defaults` parameter. |
19b53e5b | 30 | * |
fc95d9a5 | 31 | * Set `reload` if you need the api to return complete $ENTITY records. |
19b53e5b C |
32 | */ |
33 | class BasicSaveAction extends AbstractSaveAction { | |
34 | ||
35 | /** | |
36 | * @var callable | |
37 | * | |
38 | * Function(array $item, BasicCreateAction $thisAction) => array | |
39 | */ | |
40 | private $setter; | |
41 | ||
42 | /** | |
43 | * Basic Create constructor. | |
44 | * | |
45 | * @param string $entityName | |
46 | * @param string $actionName | |
47 | * @param string $idField | |
48 | * @param callable $setter | |
49 | * Function(array $item, BasicCreateAction $thisAction) => array | |
50 | */ | |
51 | public function __construct($entityName, $actionName, $idField = 'id', $setter = NULL) { | |
52 | parent::__construct($entityName, $actionName, $idField); | |
53 | $this->setter = $setter; | |
54 | } | |
55 | ||
56 | /** | |
57 | * We pass the writeRecord function an array representing one item to write. | |
58 | * We expect to get the same format back. | |
59 | * | |
60 | * @param \Civi\Api4\Generic\Result $result | |
61 | */ | |
62 | public function _run(Result $result) { | |
961e974c | 63 | foreach ($this->records as &$record) { |
19b53e5b | 64 | $record += $this->defaults; |
961e974c CW |
65 | $this->formatWriteValues($record); |
66 | } | |
67 | $this->validateValues(); | |
68 | foreach ($this->records as $item) { | |
69 | $result[] = $this->writeRecord($item); | |
19b53e5b C |
70 | } |
71 | if ($this->reload) { | |
72 | /** @var BasicGetAction $get */ | |
3a8dc228 | 73 | $get = \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4]); |
19b53e5b C |
74 | $get |
75 | ->setCheckPermissions($this->getCheckPermissions()) | |
76 | ->addWhere($this->getIdField(), 'IN', (array) $result->column($this->getIdField())); | |
77 | $result->exchangeArray((array) $get->execute()); | |
78 | } | |
79 | } | |
80 | ||
81 | /** | |
82 | * This Basic Save class can be used in one of two ways: | |
83 | * | |
84 | * 1. Use this class directly by passing a callable ($setter) to the constructor. | |
85 | * 2. Extend this class and override this function. | |
86 | * | |
87 | * Either way, this function should return an array representing the one new object. | |
88 | * | |
89 | * @param array $item | |
90 | * @return array | |
91 | * @throws \Civi\API\Exception\NotImplementedException | |
92 | */ | |
93 | protected function writeRecord($item) { | |
94 | if (is_callable($this->setter)) { | |
32f72d83 | 95 | $this->addCallbackToDebugOutput($this->setter); |
19b53e5b C |
96 | return call_user_func($this->setter, $item, $this); |
97 | } | |
98 | throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); | |
99 | } | |
100 | ||
101 | } |