Merge pull request #23602 from kainuk/core-3369
[civicrm-core.git] / api / v3 / CustomGroup.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
c28e1768 13 * This api exposes CiviCRM custom group.
6a488035
TO
14 *
15 * @package CiviCRM_APIv3
6a488035
TO
16 */
17
6a488035 18/**
c100cf44 19 * Create or modify a custom field group.
6a488035 20 *
16b10e64 21 * @param array $params
c100cf44 22 * For legacy reasons, 'extends' can be passed as an array (for setting Participant column_value)
6a488035 23 *
72b3a70c 24 * @return array
6a488035 25 * @todo $params['extends'] is array format - is that std compatible
6a488035
TO
26 */
27function civicrm_api3_custom_group_create($params) {
882e0216 28 if (isset($params['extends']) && is_string($params['extends'])) {
6a488035
TO
29 $extends = explode(",", $params['extends']);
30 unset($params['extends']);
31 $params['extends'] = $extends;
32 }
16bdd936
MM
33 if (!isset($params['id']) && (!isset($params['extends'][0]) || !trim($params['extends'][0]))) {
34
6a488035
TO
35 return civicrm_api3_create_error("First item in params['extends'] must be a class name (e.g. 'Contact').");
36 }
9857487f
SL
37 if (!isset($params['extends_entity_column_value']) && isset($params['extends'][1])) {
38 $extendsEntity = $params['extends'][0] ?? NULL;
39 $participantEntities = [
40 'ParticipantRole',
41 'ParticipantEventName',
42 'ParticipantEventType',
43 ];
44 $params['extends_entity_column_id'] = 'null';
45 if (in_array($extendsEntity, $participantEntities)
46 ) {
47 $params['extends_entity_column_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $extendsEntity, 'value', 'name');
48 }
49 $params['extends_entity_column_value'] = $params['extends'][1];
50 if (in_array($extendsEntity, $participantEntities)) {
51 $params['extends'] = 'Participant';
52 }
53 else {
54 $params['extends'] = $extendsEntity;
55 }
56 }
57 elseif (isset($params['extends']) && (!isset($params['extends'][1]) || empty($params['extends'][1]))) {
58 $params['extends'] = $params['extends'][0];
59 }
6a488035
TO
60 if (isset($params['extends_entity_column_value']) && !is_array($params['extends_entity_column_value'])) {
61 // BAO fails if this is a string, but API getFields says this must be a string, so we'll do a double backflip
62 $params['extends_entity_column_value'] = CRM_Utils_Array::explodePadded($params['extends_entity_column_value']);
63 }
64
98fd592b 65 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'CustomGroup');
6a488035
TO
66}
67
11e09c59 68/**
0aa0303c 69 * Adjust Metadata for Create action.
6a488035 70 *
cf470720 71 * @param array $params
b081365f 72 * Array of parameters determined by getfields.
6a488035
TO
73 */
74function _civicrm_api3_custom_group_create_spec(&$params) {
75 $params['extends']['api.required'] = 1;
76 $params['title']['api.required'] = 1;
77 $params['style']['api.default'] = 'Inline';
46b88fe1 78 $params['is_active']['api.default'] = 1;
6a488035
TO
79}
80
81/**
82 * Use this API to delete an existing group.
83 *
decce855 84 * @param array $params
6a488035 85 *
decce855 86 * @return array
c23f45d3 87 */
6a488035 88function civicrm_api3_custom_group_delete($params) {
6a488035
TO
89 $values = new CRM_Core_DAO_CustomGroup();
90 $values->id = $params['id'];
91 $values->find(TRUE);
92
6a488035
TO
93 $result = CRM_Core_BAO_CustomGroup::deleteGroup($values, TRUE);
94 return $result ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting custom group');
95}
96
97/**
2e66abf8 98 * API to get existing custom fields.
6a488035 99 *
cf470720 100 * @param array $params
2e66abf8 101 * Array per getfields metadata.
6a488035 102 *
77b97be7 103 * @return array
ae5ffbb7 104 */
6a488035
TO
105function civicrm_api3_custom_group_get($params) {
106 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
107}
108
5322ae12 109/**
9d32e6f7
EM
110 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom group.
111 *
d0997921 112 * @param array $params
2e66abf8 113 * Array per getfields metadata.
9d32e6f7 114 *
645ee340 115 * @return array
5322ae12
CW
116 */
117function civicrm_api3_custom_group_setvalue($params) {
118 require_once 'api/v3/Generic/Setvalue.php';
cf8f0fff 119 $result = civicrm_api3_generic_setValue(["entity" => 'CustomGroup', 'params' => $params]);
5322ae12
CW
120 if (empty($result['is_error'])) {
121 CRM_Utils_System::flushCache();
122 }
123 return $result;
124}