Merge pull request #22992 from eileenmcnaughton/billingnot
[civicrm-core.git] / Civi / Api4 / Generic / Traits / CustomValueActionTrait.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 namespace Civi\Api4\Generic\Traits;
14
15 use Civi\Api4\Utils\FormattingUtil;
16 use Civi\Api4\Utils\CoreUtil;
17
18 /**
19 * Helper functions for working with custom values
20 *
21 * @package Civi\Api4\Generic
22 */
23 trait CustomValueActionTrait {
24
25 public function __construct($customGroup, $actionName) {
26 $this->customGroup = $customGroup;
27 parent::__construct('CustomValue', $actionName);
28 }
29
30 /**
31 * Custom Group name if this is a CustomValue pseudo-entity.
32 *
33 * @var string
34 */
35 private $customGroup;
36
37 /**
38 * @inheritDoc
39 */
40 public function getEntityName() {
41 return 'Custom_' . $this->getCustomGroup();
42 }
43
44 /**
45 * Is this api call permitted?
46 *
47 * This function is called if checkPermissions is set to true.
48 *
49 * @return bool
50 */
51 public function isAuthorized(): bool {
52 if ($this->getActionName() !== 'getFields') {
53 // Check access to custom group
54 $permissionToCheck = $this->getActionName() == 'get' ? \CRM_Core_Permission::VIEW : \CRM_Core_Permission::EDIT;
55 $groupId = \CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->getCustomGroup(), 'id', 'name');
56 if (!\CRM_Core_BAO_CustomGroup::checkGroupAccess($groupId, $permissionToCheck)) {
57 return FALSE;
58 }
59 }
60 return parent::isAuthorized();
61 }
62
63 /**
64 * @inheritDoc
65 */
66 protected function writeObjects($items) {
67 $fields = $this->entityFields();
68 foreach ($items as $idx => $item) {
69 FormattingUtil::formatWriteParams($item, $fields);
70
71 // Convert field names to custom_xx format
72 foreach ($fields as $name => $field) {
73 if (!empty($field['custom_field_id']) && isset($item[$name])) {
74 $item['custom_' . $field['custom_field_id']] = $item[$name];
75 unset($item[$name]);
76 }
77 }
78
79 \CRM_Core_BAO_CustomValueTable::setValues($item);
80
81 // Darn setValues function doesn't return an id.
82 if (empty($item['id'])) {
83 $tableName = CoreUtil::getTableName($this->getEntityName());
84 $items[$idx]['id'] = (int) \CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM ' . $tableName);
85 }
86 }
87 FormattingUtil::formatOutputValues($items, $this->entityFields(), 'create');
88 return $items;
89 }
90
91 /**
92 * @inheritDoc
93 */
94 protected function deleteObjects($items) {
95 $customTable = CoreUtil::getTableName($this->getEntityName());
96 $ids = [];
97 foreach ($items as $item) {
98 \CRM_Utils_Hook::pre('delete', $this->getEntityName(), $item['id']);
99 \CRM_Utils_SQL_Delete::from($customTable)
100 ->where('id = #value')
101 ->param('#value', $item['id'])
102 ->execute();
103 \CRM_Utils_Hook::post('delete', $this->getEntityName(), $item['id'], \CRM_Core_DAO::$_nullArray);
104 $ids[] = $item['id'];
105 }
106 return $ids;
107 }
108
109 /**
110 * @inheritDoc
111 */
112 protected function fillDefaults(&$params) {
113 foreach ($this->entityFields() as $name => $field) {
114 if (!isset($params[$name]) && isset($field['default_value'])) {
115 $params[$name] = $field['default_value'];
116 }
117 }
118 }
119
120 /**
121 * @return string
122 */
123 public function getCustomGroup() {
124 return $this->customGroup;
125 }
126
127 /**
128 * @return \CRM_Core_DAO|string
129 */
130 protected function getBaoName() {
131 return \CRM_Core_BAO_CustomValue::class;
132 }
133
134 }