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 | |
f299f7db | 32 | * @copyright CiviCRM LLC (c) 2004-2020 |
380f3545 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | ||
19b53e5b C |
38 | namespace Civi\Api4\Generic; |
39 | ||
40 | use Civi\API\Exception\NotImplementedException; | |
41 | use Civi\Api4\Utils\ActionUtil; | |
42 | ||
43 | /** | |
44 | * Create or update one or more records. | |
45 | * | |
46 | * If creating more than one record with similar values, use the "defaults" param. | |
47 | * | |
48 | * Set "reload" if you need the api to return complete records. | |
49 | */ | |
50 | class BasicSaveAction extends AbstractSaveAction { | |
51 | ||
52 | /** | |
53 | * @var callable | |
54 | * | |
55 | * Function(array $item, BasicCreateAction $thisAction) => array | |
56 | */ | |
57 | private $setter; | |
58 | ||
59 | /** | |
60 | * Basic Create constructor. | |
61 | * | |
62 | * @param string $entityName | |
63 | * @param string $actionName | |
64 | * @param string $idField | |
65 | * @param callable $setter | |
66 | * Function(array $item, BasicCreateAction $thisAction) => array | |
67 | */ | |
68 | public function __construct($entityName, $actionName, $idField = 'id', $setter = NULL) { | |
69 | parent::__construct($entityName, $actionName, $idField); | |
70 | $this->setter = $setter; | |
71 | } | |
72 | ||
73 | /** | |
74 | * We pass the writeRecord function an array representing one item to write. | |
75 | * We expect to get the same format back. | |
76 | * | |
77 | * @param \Civi\Api4\Generic\Result $result | |
78 | */ | |
79 | public function _run(Result $result) { | |
80 | $this->validateValues(); | |
81 | foreach ($this->records as $record) { | |
82 | $record += $this->defaults; | |
83 | $result[] = $this->writeRecord($record); | |
84 | } | |
85 | if ($this->reload) { | |
86 | /** @var BasicGetAction $get */ | |
87 | $get = ActionUtil::getAction($this->getEntityName(), 'get'); | |
88 | $get | |
89 | ->setCheckPermissions($this->getCheckPermissions()) | |
90 | ->addWhere($this->getIdField(), 'IN', (array) $result->column($this->getIdField())); | |
91 | $result->exchangeArray((array) $get->execute()); | |
92 | } | |
93 | } | |
94 | ||
95 | /** | |
96 | * This Basic Save class can be used in one of two ways: | |
97 | * | |
98 | * 1. Use this class directly by passing a callable ($setter) to the constructor. | |
99 | * 2. Extend this class and override this function. | |
100 | * | |
101 | * Either way, this function should return an array representing the one new object. | |
102 | * | |
103 | * @param array $item | |
104 | * @return array | |
105 | * @throws \Civi\API\Exception\NotImplementedException | |
106 | */ | |
107 | protected function writeRecord($item) { | |
108 | if (is_callable($this->setter)) { | |
109 | return call_user_func($this->setter, $item, $this); | |
110 | } | |
111 | throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); | |
112 | } | |
113 | ||
114 | } |