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 * Given a set of records, will appropriately update the database.
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()
39 class BasicReplaceAction
extends AbstractBatchAction
{
44 * Should be in the same format as returned by Get.
49 protected $records = [];
52 * Array of default values.
54 * Will be merged into $records before saving.
58 protected $defaults = [];
61 * Reload records after saving.
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.
69 protected $reload = FALSE;
72 * @return \Civi\Api4\Result\ReplaceResult
74 public function execute() {
75 return parent
::execute();
81 public function _run(Result
$result) {
82 $items = $this->getBatchRecords();
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];
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
))));
94 // Try to delegate to the Save action
96 $saveAction = ActionUtil
::getAction($this->getEntityName(), 'save');
98 ->setCheckPermissions($this->getCheckPermissions())
99 ->setReload($this->reload
)
100 ->setRecords($this->records
)
101 ->setDefaults($this->defaults
);
102 $result->exchangeArray((array) $saveAction->execute());
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]]],
113 'checkPermissions' => $this->getCheckPermissions(),
117 $result[] = civicrm_api4($this->getEntityName(), 'create', [
119 'checkPermissions' => $this->getCheckPermissions(),
126 $result->deleted
= (array) civicrm_api4($this->getEntityName(), 'delete', [
127 'where' => [[$idField, 'IN', array_keys($toDelete)]],
128 'checkPermissions' => $this->getCheckPermissions(),