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