Commit | Line | Data |
---|---|---|
19b53e5b | 1 | <?php |
380f3545 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
41498ac5 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
41498ac5 TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
19b53e5b C |
13 | namespace Civi\Api4\Generic; |
14 | ||
4bf92107 | 15 | use Civi\Api4\Event\ValidateValuesEvent; |
929a9585 CW |
16 | use Civi\API\Exception\UnauthorizedException; |
17 | use Civi\Api4\Utils\CoreUtil; | |
4bf92107 | 18 | |
19b53e5b | 19 | /** |
fc95d9a5 | 20 | * Base class for all `Create` api actions. |
19b53e5b C |
21 | * |
22 | * @method $this setValues(array $values) Set all field values from an array of key => value pairs. | |
19b53e5b C |
23 | * @method array getValues() Get field values. |
24 | * | |
25 | * @package Civi\Api4\Generic | |
26 | */ | |
27 | abstract class AbstractCreateAction extends AbstractAction { | |
28 | ||
29 | /** | |
fc95d9a5 | 30 | * Field values to set for the new $ENTITY. |
19b53e5b C |
31 | * |
32 | * @var array | |
33 | */ | |
34 | protected $values = []; | |
35 | ||
36 | /** | |
121ec912 | 37 | * @param string $fieldName |
19b53e5b C |
38 | * @return mixed|null |
39 | */ | |
121ec912 | 40 | public function getValue(string $fieldName) { |
2e1f50d6 | 41 | return $this->values[$fieldName] ?? NULL; |
121ec912 CW |
42 | } |
43 | ||
44 | /** | |
45 | * Add a field value. | |
46 | * @param string $fieldName | |
47 | * @param mixed $value | |
48 | * @return $this | |
49 | */ | |
50 | public function addValue(string $fieldName, $value) { | |
51 | $this->values[$fieldName] = $value; | |
52 | return $this; | |
19b53e5b C |
53 | } |
54 | ||
55 | /** | |
56 | * @throws \API_Exception | |
929a9585 | 57 | * @throws \Civi\API\Exception\UnauthorizedException |
19b53e5b C |
58 | */ |
59 | protected function validateValues() { | |
4bf92107 | 60 | // FIXME: There should be a protocol to report a full list of errors... Perhaps a subclass of API_Exception? |
19b53e5b C |
61 | $unmatched = $this->checkRequiredFields($this->getValues()); |
62 | if ($unmatched) { | |
63 | throw new \API_Exception("Mandatory values missing from Api4 {$this->getEntityName()}::{$this->getActionName()}: " . implode(", ", $unmatched), "mandatory_missing", ["fields" => $unmatched]); | |
64 | } | |
929a9585 | 65 | |
70da3927 | 66 | if ($this->checkPermissions && !CoreUtil::checkAccessRecord($this, $this->getValues(), \CRM_Core_Session::getLoggedInContactID() ?: 0)) { |
929a9585 CW |
67 | throw new UnauthorizedException("ACL check failed"); |
68 | } | |
69 | ||
9f04ea33 TO |
70 | $e = new ValidateValuesEvent($this, [$this->getValues()], new \CRM_Utils_LazyArray(function () { |
71 | return [['old' => NULL, 'new' => $this->getValues()]]; | |
72 | })); | |
4bf92107 TO |
73 | \Civi::dispatcher()->dispatch('civi.api4.validate', $e); |
74 | if (!empty($e->errors)) { | |
75 | throw $e->toException(); | |
76 | } | |
19b53e5b C |
77 | } |
78 | ||
79 | } |