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; | |
25 | use Civi\Api4\Utils\ActionUtil; | |
26 | ||
27 | /** | |
28 | * Given a set of records, will appropriately update the database. | |
29 | * | |
30 | * @method $this setRecords(array $records) Array of records. | |
19b53e5b C |
31 | * @method array getRecords() |
32 | * @method $this setDefaults(array $defaults) Array of defaults. | |
19b53e5b C |
33 | * @method array getDefaults() |
34 | * @method $this setReload(bool $reload) Specify whether complete objects will be returned after saving. | |
35 | * @method bool getReload() | |
36 | */ | |
37 | class BasicReplaceAction extends AbstractBatchAction { | |
38 | ||
39 | /** | |
40 | * Array of records. | |
41 | * | |
42 | * Should be in the same format as returned by Get. | |
43 | * | |
44 | * @var array | |
45 | * @required | |
46 | */ | |
47 | protected $records = []; | |
48 | ||
49 | /** | |
50 | * Array of default values. | |
51 | * | |
52 | * Will be merged into $records before saving. | |
53 | * | |
54 | * @var array | |
55 | */ | |
56 | protected $defaults = []; | |
57 | ||
58 | /** | |
59 | * Reload records after saving. | |
60 | * | |
61 | * By default this api typically returns partial records containing only the fields | |
62 | * that were updated. Set reload to TRUE to do an additional lookup after saving | |
63 | * to return complete records. | |
64 | * | |
65 | * @var bool | |
66 | */ | |
67 | protected $reload = FALSE; | |
68 | ||
69 | /** | |
70 | * @return \Civi\Api4\Result\ReplaceResult | |
71 | */ | |
72 | public function execute() { | |
73 | return parent::execute(); | |
74 | } | |
75 | ||
76 | /** | |
77 | * @inheritDoc | |
78 | */ | |
79 | public function _run(Result $result) { | |
80 | $items = $this->getBatchRecords(); | |
81 | ||
82 | // Copy defaults from where clause if the operator is = | |
83 | foreach ($this->where as $clause) { | |
84 | if (is_array($clause) && $clause[1] === '=') { | |
85 | $this->defaults[$clause[0]] = $clause[2]; | |
86 | } | |
87 | } | |
88 | ||
89 | $idField = $this->getSelect()[0]; | |
90 | $toDelete = array_diff_key(array_column($items, NULL, $idField), array_flip(array_filter(\CRM_Utils_Array::collect($idField, $this->records)))); | |
91 | ||
92 | // Try to delegate to the Save action | |
93 | try { | |
94 | $saveAction = ActionUtil::getAction($this->getEntityName(), 'save'); | |
95 | $saveAction | |
96 | ->setCheckPermissions($this->getCheckPermissions()) | |
97 | ->setReload($this->reload) | |
98 | ->setRecords($this->records) | |
99 | ->setDefaults($this->defaults); | |
100 | $result->exchangeArray((array) $saveAction->execute()); | |
101 | } | |
102 | // Fall back on Create/Update if Save doesn't exist | |
103 | catch (NotImplementedException $e) { | |
104 | foreach ($this->records as $record) { | |
105 | $record += $this->defaults; | |
106 | if (!empty($record[$idField])) { | |
107 | $result[] = civicrm_api4($this->getEntityName(), 'update', [ | |
108 | 'reload' => $this->reload, | |
109 | 'where' => [[$idField, '=', $record[$idField]]], | |
110 | 'values' => $record, | |
111 | 'checkPermissions' => $this->getCheckPermissions(), | |
112 | ])->first(); | |
113 | } | |
114 | else { | |
115 | $result[] = civicrm_api4($this->getEntityName(), 'create', [ | |
116 | 'values' => $record, | |
117 | 'checkPermissions' => $this->getCheckPermissions(), | |
118 | ])->first(); | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | if ($toDelete) { | |
124 | $result->deleted = (array) civicrm_api4($this->getEntityName(), 'delete', [ | |
125 | 'where' => [[$idField, 'IN', array_keys($toDelete)]], | |
126 | 'checkPermissions' => $this->getCheckPermissions(), | |
127 | ]); | |
128 | } | |
129 | } | |
130 | ||
121ec912 CW |
131 | /** |
132 | * Set default value for a field. | |
133 | * @param string $fieldName | |
134 | * @param mixed $defaultValue | |
135 | * @return $this | |
136 | */ | |
137 | public function addDefault(string $fieldName, $defaultValue) { | |
138 | $this->defaults[$fieldName] = $defaultValue; | |
139 | return $this; | |
140 | } | |
141 | ||
142 | /** | |
143 | * Add one or more records | |
144 | * @param array ...$records | |
145 | * @return $this | |
146 | */ | |
147 | public function addRecord(array ...$records) { | |
148 | $this->records = array_merge($this->records, $records); | |
149 | return $this; | |
150 | } | |
151 | ||
19b53e5b | 152 | } |