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
;
40 use Civi\API\Exception\NotImplementedException
;
41 use Civi\Api4\Utils\ActionUtil
;
44 * Create or update one or more records.
46 * If creating more than one record with similar values, use the "defaults" param.
48 * Set "reload" if you need the api to return complete records.
50 class BasicSaveAction
extends AbstractSaveAction
{
55 * Function(array $item, BasicCreateAction $thisAction) => array
60 * Basic Create constructor.
62 * @param string $entityName
63 * @param string $actionName
64 * @param string $idField
65 * @param callable $setter
66 * Function(array $item, BasicCreateAction $thisAction) => array
68 public function __construct($entityName, $actionName, $idField = 'id', $setter = NULL) {
69 parent
::__construct($entityName, $actionName, $idField);
70 $this->setter
= $setter;
74 * We pass the writeRecord function an array representing one item to write.
75 * We expect to get the same format back.
77 * @param \Civi\Api4\Generic\Result $result
79 public function _run(Result
$result) {
80 $this->validateValues();
81 foreach ($this->records
as $record) {
82 $record +
= $this->defaults
;
83 $result[] = $this->writeRecord($record);
86 /** @var BasicGetAction $get */
87 $get = ActionUtil
::getAction($this->getEntityName(), 'get');
89 ->setCheckPermissions($this->getCheckPermissions())
90 ->addWhere($this->getIdField(), 'IN', (array) $result->column($this->getIdField()));
91 $result->exchangeArray((array) $get->execute());
96 * This Basic Save class can be used in one of two ways:
98 * 1. Use this class directly by passing a callable ($setter) to the constructor.
99 * 2. Extend this class and override this function.
101 * Either way, this function should return an array representing the one new object.
105 * @throws \Civi\API\Exception\NotImplementedException
107 protected function writeRecord($item) {
108 if (is_callable($this->setter
)) {
109 return call_user_func($this->setter
, $item, $this);
111 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());