4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2019
38 namespace Civi\Api4\Generic
;
41 * Base class for all "Save" api actions.
43 * @method $this setRecords(array $records) Array of records.
44 * @method $this addRecord($record) Add a record to update.
45 * @method array getRecords()
46 * @method $this setDefaults(array $defaults) Array of defaults.
47 * @method $this addDefault($name, $value) Add a default value.
48 * @method array getDefaults()
49 * @method $this setReload(bool $reload) Specify whether complete objects will be returned after saving.
50 * @method bool getReload()
52 * @package Civi\Api4\Generic
54 abstract class AbstractSaveAction
extends AbstractAction
{
59 * Should be in the same format as returned by Get.
64 protected $records = [];
67 * Array of default values.
69 * These defaults will be applied to all records unless they specify otherwise.
73 protected $defaults = [];
76 * Reload records after saving.
78 * By default this api typically returns partial records containing only the fields
79 * that were updated. Set reload to TRUE to do an additional lookup after saving
80 * to return complete records.
84 protected $reload = FALSE;
92 * BatchAction constructor.
93 * @param string $entityName
94 * @param string $actionName
95 * @param string $idField
97 public function __construct($entityName, $actionName, $idField = 'id') {
98 // $idField should be a string but some apis (e.g. CustomValue) give us an array
99 $this->idField
= array_values((array) $idField)[0];
100 parent
::__construct($entityName, $actionName);
104 * @throws \API_Exception
106 protected function validateValues() {
108 foreach ($this->records
as $record) {
109 if (empty($record[$this->idField
])) {
110 $unmatched = array_unique(array_merge($unmatched, $this->checkRequiredFields($record)));
114 throw new \
API_Exception("Mandatory values missing from Api4 {$this->getEntityName()}::{$this->getActionName()}: " . implode(", ", $unmatched), "mandatory_missing", ["fields" => $unmatched]);
121 protected function getIdField() {
122 return $this->idField
;