blob: d7a0e86930e980d5f02b7771e657f02f9ca7f1ef (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?php
namespace Civi\Api4\Generic;
use Civi\Api4\Generic\Result;
/**
* Create a new object from supplied values.
*
* This function will create 1 new object. It cannot be used to update existing objects. Use the Update or Replace actions for that.
*/
class DAOCreateAction extends AbstractCreateAction {
use Traits\DAOActionTrait;
/**
* @inheritDoc
*/
public function _run(Result $result) {
$this->validateValues();
$params = $this->values;
$this->fillDefaults($params);
$resultArray = $this->writeObjects([$params]);
$result->exchangeArray($resultArray);
}
/**
* @throws \API_Exception
*/
protected function validateValues() {
if (!empty($this->values['id'])) {
throw new \API_Exception('Cannot pass id to Create action. Use Update action instead.');
}
parent::validateValues();
}
/**
* Fill field defaults which were declared by the api.
*
* Note: default values from core are ignored because the BAO or database layer will supply them.
*
* @param array $params
*/
protected function fillDefaults(&$params) {
$fields = $this->getEntityFields();
$bao = $this->getBaoName();
$coreFields = array_column($bao::fields(), NULL, 'name');
foreach ($fields as $name => $field) {
// If a default value is set in the api but not in core, the api should supply it.
if (!isset($params[$name]) && !empty($field['default_value']) && empty($coreFields[$name]['default'])) {
$params[$name] = $field['default_value'];
}
}
}
}
|