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
;
25 * Replaces an existing set of $ENTITIES with a new one.
27 * This will select a group of existing $ENTITIES based on the `where` parameter.
28 * Each will be compared with the $ENTITIES passed in as `records`:
30 * - $ENTITIES in `records` that don't already exist will be created.
31 * - Existing $ENTITIES that are included in `records` will be updated.
32 * - Existing $ENTITIES that are omitted from `records` will be deleted.
34 * @method $this setRecords(array $records) Set array of records.
35 * @method array getRecords()
36 * @method $this setDefaults(array $defaults) Set array of defaults.
37 * @method array getDefaults()
38 * @method $this setReload(bool $reload) Specify whether complete objects will be returned after saving.
39 * @method bool getReload()
41 class BasicReplaceAction
extends AbstractBatchAction
{
44 * Array of $ENTITY records.
46 * Should be in the same format as returned by `Get`.
51 protected $records = [];
54 * Array of default values.
56 * These defaults will be merged into every $ENTITY in `records` before saving.
57 * Values set in `records` will override these defaults if set in both places,
58 * but updating existing $ENTITIES will overwrite current values with these defaults.
60 * **Note:** Values from the `where` clause that use the `=` operator are _also_ treated as default values;
61 * those do not need to be repeated here.
65 protected $defaults = [];
68 * Reload $ENTITIES after saving.
70 * By default this action typically returns partial records containing only the fields
71 * that were updated. Set `reload` to `true` to do an additional lookup after saving
72 * to return complete values for every $ENTITY.
76 protected $reload = FALSE;
79 * @return \Civi\Api4\Result\ReplaceResult
81 public function execute() {
82 return parent
::execute();
88 public function _run(Result
$result) {
89 $items = $this->getBatchRecords();
91 // Copy defaults from where clause if the operator is =
92 foreach ($this->where
as $clause) {
93 if (is_array($clause) && $clause[1] === '=') {
94 $this->defaults
[$clause[0]] = $clause[2];
98 $idField = $this->getSelect()[0];
99 $toDelete = array_diff_key(array_column($items, NULL, $idField), array_flip(array_filter(\CRM_Utils_Array
::collect($idField, $this->records
))));
101 $saveAction = \Civi\API\Request
::create($this->getEntityName(), 'save', ['version' => 4]);
103 ->setCheckPermissions($this->getCheckPermissions())
104 ->setReload($this->reload
)
105 ->setRecords($this->records
)
106 ->setDefaults($this->defaults
);
107 $result->exchangeArray((array) $saveAction->execute());
110 $result->deleted
= (array) civicrm_api4($this->getEntityName(), 'delete', [
111 'where' => [[$idField, 'IN', array_keys($toDelete)]],
112 'checkPermissions' => $this->getCheckPermissions(),
118 * Set default value for a field.
119 * @param string $fieldName
120 * @param mixed $defaultValue
123 public function addDefault(string $fieldName, $defaultValue) {
124 $this->defaults
[$fieldName] = $defaultValue;
129 * Add one or more records
130 * @param array ...$records
133 public function addRecord(array ...$records) {
134 $this->records
= array_merge($this->records
, $records);