Merge pull request #15760 from colemanw/iconPicker
[civicrm-core.git] / Civi / Api4 / Generic / BasicSaveAction.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace Civi\Api4\Generic;
23
24 use Civi\API\Exception\NotImplementedException;
25 use Civi\Api4\Utils\ActionUtil;
26
27 /**
28 * Create or update one or more records.
29 *
30 * If creating more than one record with similar values, use the "defaults" param.
31 *
32 * Set "reload" if you need the api to return complete records.
33 */
34 class BasicSaveAction extends AbstractSaveAction {
35
36 /**
37 * @var callable
38 *
39 * Function(array $item, BasicCreateAction $thisAction) => array
40 */
41 private $setter;
42
43 /**
44 * Basic Create constructor.
45 *
46 * @param string $entityName
47 * @param string $actionName
48 * @param string $idField
49 * @param callable $setter
50 * Function(array $item, BasicCreateAction $thisAction) => array
51 */
52 public function __construct($entityName, $actionName, $idField = 'id', $setter = NULL) {
53 parent::__construct($entityName, $actionName, $idField);
54 $this->setter = $setter;
55 }
56
57 /**
58 * We pass the writeRecord function an array representing one item to write.
59 * We expect to get the same format back.
60 *
61 * @param \Civi\Api4\Generic\Result $result
62 */
63 public function _run(Result $result) {
64 $this->validateValues();
65 foreach ($this->records as $record) {
66 $record += $this->defaults;
67 $result[] = $this->writeRecord($record);
68 }
69 if ($this->reload) {
70 /** @var BasicGetAction $get */
71 $get = ActionUtil::getAction($this->getEntityName(), 'get');
72 $get
73 ->setCheckPermissions($this->getCheckPermissions())
74 ->addWhere($this->getIdField(), 'IN', (array) $result->column($this->getIdField()));
75 $result->exchangeArray((array) $get->execute());
76 }
77 }
78
79 /**
80 * This Basic Save class can be used in one of two ways:
81 *
82 * 1. Use this class directly by passing a callable ($setter) to the constructor.
83 * 2. Extend this class and override this function.
84 *
85 * Either way, this function should return an array representing the one new object.
86 *
87 * @param array $item
88 * @return array
89 * @throws \Civi\API\Exception\NotImplementedException
90 */
91 protected function writeRecord($item) {
92 if (is_callable($this->setter)) {
93 $this->addCallbackToDebugOutput($this->setter);
94 return call_user_func($this->setter, $item, $this);
95 }
96 throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
97 }
98
99 }