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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace Civi\Api4\Generic\Traits
;
24 use Civi\Api4\Utils\FormattingUtil
;
25 use Civi\Api4\Utils\CoreUtil
;
28 * Helper functions for working with custom values
30 * @package Civi\Api4\Generic
32 trait CustomValueActionTrait
{
34 public function __construct($customGroup, $actionName) {
35 $this->customGroup
= $customGroup;
36 parent
::__construct('CustomValue', $actionName, ['id', 'entity_id']);
40 * Custom Group name if this is a CustomValue pseudo-entity.
49 public function getEntityName() {
50 return 'Custom_' . $this->getCustomGroup();
56 protected function writeObjects($items) {
58 $fields = $this->entityFields();
59 foreach ($items as $item) {
60 FormattingUtil
::formatWriteParams($item, $fields);
62 // Convert field names to custom_xx format
63 foreach ($fields as $name => $field) {
64 if (!empty($field['custom_field_id']) && isset($item[$name])) {
65 $item['custom_' . $field['custom_field_id']] = $item[$name];
70 $result[] = \CRM_Core_BAO_CustomValueTable
::setValues($item);
78 protected function deleteObjects($items) {
79 $customTable = CoreUtil
::getCustomTableByName($this->getCustomGroup());
81 foreach ($items as $item) {
82 \CRM_Utils_Hook
::pre('delete', $this->getEntityName(), $item['id'], \CRM_Core_DAO
::$_nullArray);
83 \CRM_Utils_SQL_Delete
::from($customTable)
84 ->where('id = #value')
85 ->param('#value', $item['id'])
87 \CRM_Utils_Hook
::post('delete', $this->getEntityName(), $item['id'], \CRM_Core_DAO
::$_nullArray);
96 protected function fillDefaults(&$params) {
97 foreach ($this->entityFields() as $name => $field) {
98 if (!isset($params[$name]) && isset($field['default_value'])) {
99 $params[$name] = $field['default_value'];
107 public function getCustomGroup() {
108 return $this->customGroup
;