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 array getRecords()
32 * @method $this setDefaults(array $defaults) Array of defaults.
33 * @method array getDefaults()
34 * @method $this setReload(bool $reload) Specify whether complete objects will be returned after saving.
35 * @method bool getReload()
37 class BasicReplaceAction
extends AbstractBatchAction
{
42 * Should be in the same format as returned by Get.
47 protected $records = [];
50 * Array of default values.
52 * Will be merged into $records before saving.
56 protected $defaults = [];
59 * Reload records after saving.
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.
67 protected $reload = FALSE;
70 * @return \Civi\Api4\Result\ReplaceResult
72 public function execute() {
73 return parent
::execute();
79 public function _run(Result
$result) {
80 $items = $this->getBatchRecords();
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];
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
))));
92 // Try to delegate to the Save action
94 $saveAction = ActionUtil
::getAction($this->getEntityName(), 'save');
96 ->setCheckPermissions($this->getCheckPermissions())
97 ->setReload($this->reload
)
98 ->setRecords($this->records
)
99 ->setDefaults($this->defaults
);
100 $result->exchangeArray((array) $saveAction->execute());
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]]],
111 'checkPermissions' => $this->getCheckPermissions(),
115 $result[] = civicrm_api4($this->getEntityName(), 'create', [
117 'checkPermissions' => $this->getCheckPermissions(),
124 $result->deleted
= (array) civicrm_api4($this->getEntityName(), 'delete', [
125 'where' => [[$idField, 'IN', array_keys($toDelete)]],
126 'checkPermissions' => $this->getCheckPermissions(),
132 * Set default value for a field.
133 * @param string $fieldName
134 * @param mixed $defaultValue
137 public function addDefault(string $fieldName, $defaultValue) {
138 $this->defaults
[$fieldName] = $defaultValue;
143 * Add one or more records
144 * @param array ...$records
147 public function addRecord(array ...$records) {
148 $this->records
= array_merge($this->records
, $records);