Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 5 | | |
6 | +--------------------------------------------------------------------+ | |
f299f7db | 7 | | Copyright CiviCRM LLC (c) 2004-2020 | |
380f3545 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
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. | | |
14 | | | | |
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. | | |
19 | | | | |
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 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
ca5cec67 | 32 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | ||
19b53e5b C |
38 | namespace Civi\Api4\Generic; |
39 | ||
40 | /** | |
41 | * Base class for all "Save" api actions. | |
42 | * | |
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() | |
51 | * | |
52 | * @package Civi\Api4\Generic | |
53 | */ | |
54 | abstract class AbstractSaveAction extends AbstractAction { | |
55 | ||
56 | /** | |
57 | * Array of records. | |
58 | * | |
59 | * Should be in the same format as returned by Get. | |
60 | * | |
61 | * @var array | |
62 | * @required | |
63 | */ | |
64 | protected $records = []; | |
65 | ||
66 | /** | |
67 | * Array of default values. | |
68 | * | |
69 | * These defaults will be applied to all records unless they specify otherwise. | |
70 | * | |
71 | * @var array | |
72 | */ | |
73 | protected $defaults = []; | |
74 | ||
75 | /** | |
76 | * Reload records after saving. | |
77 | * | |
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. | |
81 | * | |
82 | * @var bool | |
83 | */ | |
84 | protected $reload = FALSE; | |
85 | ||
86 | /** | |
87 | * @var string | |
88 | */ | |
89 | private $idField; | |
90 | ||
91 | /** | |
92 | * BatchAction constructor. | |
93 | * @param string $entityName | |
94 | * @param string $actionName | |
95 | * @param string $idField | |
96 | */ | |
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); | |
101 | } | |
102 | ||
103 | /** | |
104 | * @throws \API_Exception | |
105 | */ | |
106 | protected function validateValues() { | |
107 | $unmatched = []; | |
108 | foreach ($this->records as $record) { | |
109 | if (empty($record[$this->idField])) { | |
110 | $unmatched = array_unique(array_merge($unmatched, $this->checkRequiredFields($record))); | |
111 | } | |
112 | } | |
113 | if ($unmatched) { | |
114 | throw new \API_Exception("Mandatory values missing from Api4 {$this->getEntityName()}::{$this->getActionName()}: " . implode(", ", $unmatched), "mandatory_missing", ["fields" => $unmatched]); | |
115 | } | |
116 | } | |
117 | ||
118 | /** | |
119 | * @return string | |
120 | */ | |
121 | protected function getIdField() { | |
122 | return $this->idField; | |
123 | } | |
124 | ||
125 | } |