4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace Civi\Api4\Generic
;
24 use Civi\API\Exception\NotImplementedException
;
25 use Civi\Api4\Utils\ActionUtil
;
28 * Replaces an existing set of $ENTITYs with a new one.
30 * This will select a group of existing $ENTITYs based on the `where` parameter.
31 * Each will be compared with the $ENTITYs passed in as `records`:
33 * - $ENTITYs in `records` that don't already exist will be created.
34 * - Existing $ENTITYs that are included in `records` will be updated.
35 * - Existing $ENTITYs that are omitted from `records` will be deleted.
37 * @method $this setRecords(array $records) Set array of records.
38 * @method array getRecords()
39 * @method $this setDefaults(array $defaults) Set array of defaults.
40 * @method array getDefaults()
41 * @method $this setReload(bool $reload) Specify whether complete objects will be returned after saving.
42 * @method bool getReload()
44 class BasicReplaceAction
extends AbstractBatchAction
{
47 * Array of $ENTITY records.
49 * Should be in the same format as returned by `Get`.
54 protected $records = [];
57 * Array of default values.
59 * Will be merged into `records` before saving.
61 * **Note:** Values from the `where` clause that use the `=` operator are _also_ saved into each record;
62 * those do not need to be repeated here.
66 protected $defaults = [];
69 * Reload $ENTITYs after saving.
71 * By default this action typically returns partial records containing only the fields
72 * that were updated. Set `reload` to `true` to do an additional lookup after saving
73 * to return complete values for every $ENTITY.
77 protected $reload = FALSE;
80 * @return \Civi\Api4\Result\ReplaceResult
82 public function execute() {
83 return parent
::execute();
89 public function _run(Result
$result) {
90 $items = $this->getBatchRecords();
92 // Copy defaults from where clause if the operator is =
93 foreach ($this->where
as $clause) {
94 if (is_array($clause) && $clause[1] === '=') {
95 $this->defaults
[$clause[0]] = $clause[2];
99 $idField = $this->getSelect()[0];
100 $toDelete = array_diff_key(array_column($items, NULL, $idField), array_flip(array_filter(\CRM_Utils_Array
::collect($idField, $this->records
))));
102 // Try to delegate to the Save action
104 $saveAction = ActionUtil
::getAction($this->getEntityName(), 'save');
106 ->setCheckPermissions($this->getCheckPermissions())
107 ->setReload($this->reload
)
108 ->setRecords($this->records
)
109 ->setDefaults($this->defaults
);
110 $result->exchangeArray((array) $saveAction->execute());
112 // Fall back on Create/Update if Save doesn't exist
113 catch (NotImplementedException
$e) {
114 foreach ($this->records
as $record) {
115 $record +
= $this->defaults
;
116 if (!empty($record[$idField])) {
117 $result[] = civicrm_api4($this->getEntityName(), 'update', [
118 'reload' => $this->reload
,
119 'where' => [[$idField, '=', $record[$idField]]],
121 'checkPermissions' => $this->getCheckPermissions(),
125 $result[] = civicrm_api4($this->getEntityName(), 'create', [
127 'checkPermissions' => $this->getCheckPermissions(),
134 $result->deleted
= (array) civicrm_api4($this->getEntityName(), 'delete', [
135 'where' => [[$idField, 'IN', array_keys($toDelete)]],
136 'checkPermissions' => $this->getCheckPermissions(),
142 * Set default value for a field.
143 * @param string $fieldName
144 * @param mixed $defaultValue
147 public function addDefault(string $fieldName, $defaultValue) {
148 $this->defaults
[$fieldName] = $defaultValue;
153 * Add one or more records
154 * @param array ...$records
157 public function addRecord(array ...$records) {
158 $this->records
= array_merge($this->records
, $records);