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 * Base class for all "Save" api actions.
27 * @method $this setRecords(array $records) Array of records.
28 * @method $this addRecord($record) Add a record to update.
29 * @method array getRecords()
30 * @method $this setDefaults(array $defaults) Array of defaults.
31 * @method $this addDefault($name, $value) Add a default value.
32 * @method array getDefaults()
33 * @method $this setReload(bool $reload) Specify whether complete objects will be returned after saving.
34 * @method bool getReload()
36 * @package Civi\Api4\Generic
38 abstract class AbstractSaveAction
extends AbstractAction
{
43 * Should be in the same format as returned by Get.
48 protected $records = [];
51 * Array of default values.
53 * These defaults will be applied to all records unless they specify otherwise.
57 protected $defaults = [];
60 * Reload records after saving.
62 * By default this api typically returns partial records containing only the fields
63 * that were updated. Set reload to TRUE to do an additional lookup after saving
64 * to return complete records.
68 protected $reload = FALSE;
76 * BatchAction constructor.
77 * @param string $entityName
78 * @param string $actionName
79 * @param string $idField
81 public function __construct($entityName, $actionName, $idField = 'id') {
82 // $idField should be a string but some apis (e.g. CustomValue) give us an array
83 $this->idField
= array_values((array) $idField)[0];
84 parent
::__construct($entityName, $actionName);
88 * @throws \API_Exception
90 protected function validateValues() {
92 foreach ($this->records
as $record) {
93 if (empty($record[$this->idField
])) {
94 $unmatched = array_unique(array_merge($unmatched, $this->checkRequiredFields($record)));
98 throw new \
API_Exception("Mandatory values missing from Api4 {$this->getEntityName()}::{$this->getActionName()}: " . implode(", ", $unmatched), "mandatory_missing", ["fields" => $unmatched]);
105 protected function getIdField() {
106 return $this->idField
;