4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Generic\Traits
;
15 use Civi\Api4\Utils\FormattingUtil
;
16 use Civi\Api4\Utils\CoreUtil
;
19 * Helper functions for working with custom values
21 * @package Civi\Api4\Generic
23 trait CustomValueActionTrait
{
25 public function __construct($customGroup, $actionName) {
26 $this->customGroup
= $customGroup;
27 parent
::__construct('CustomValue', $actionName);
31 * Custom Group name if this is a CustomValue pseudo-entity.
40 public function getEntityName() {
41 return 'Custom_' . $this->getCustomGroup();
45 * Is this api call permitted?
47 * This function is called if checkPermissions is set to true.
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)) {
60 return parent
::isAuthorized();
66 protected function writeObjects($items) {
67 $fields = $this->entityFields();
68 foreach ($items as $idx => $item) {
69 FormattingUtil
::formatWriteParams($item, $fields);
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];
79 \CRM_Core_BAO_CustomValueTable
::setValues($item);
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);
87 FormattingUtil
::formatOutputValues($items, $this->entityFields(), 'create');
94 protected function deleteObjects($items) {
95 $customTable = CoreUtil
::getTableName($this->getEntityName());
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'])
103 \CRM_Utils_Hook
::post('delete', $this->getEntityName(), $item['id'], \CRM_Core_DAO
::$_nullArray);
104 $ids[] = $item['id'];
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'];
123 public function getCustomGroup() {
124 return $this->customGroup
;
128 * @return \CRM_Core_DAO|string
130 protected function getBaoName() {
131 return \CRM_Core_BAO_CustomValue
::class;