Commit | Line | Data |
---|---|---|
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 | /** |
9d32e6f7 EM |
19 | * Use this API to create a new group. |
20 | * | |
21 | * The 'extends' value accepts an array or a comma separated string. | |
6a488035 | 22 | * e.g array( |
d1b0d05e | 23 | * 'Individual','Contact') or 'Individual,Contact' |
6a488035 TO |
24 | * See the CRM Data Model for custom_group property definitions |
25 | * $params['class_name'] is a required field, class being extended. | |
26 | * | |
16b10e64 | 27 | * @param array $params |
2e66abf8 | 28 | * Array per getfields metadata. |
6a488035 | 29 | * |
72b3a70c | 30 | * @return array |
6a488035 | 31 | * @todo $params['extends'] is array format - is that std compatible |
6a488035 TO |
32 | */ |
33 | function civicrm_api3_custom_group_create($params) { | |
882e0216 | 34 | if (isset($params['extends']) && is_string($params['extends'])) { |
6a488035 TO |
35 | $extends = explode(",", $params['extends']); |
36 | unset($params['extends']); | |
37 | $params['extends'] = $extends; | |
38 | } | |
16bdd936 MM |
39 | if (!isset($params['id']) && (!isset($params['extends'][0]) || !trim($params['extends'][0]))) { |
40 | ||
6a488035 TO |
41 | return civicrm_api3_create_error("First item in params['extends'] must be a class name (e.g. 'Contact')."); |
42 | } | |
9857487f SL |
43 | if (!isset($params['extends_entity_column_value']) && isset($params['extends'][1])) { |
44 | $extendsEntity = $params['extends'][0] ?? NULL; | |
45 | $participantEntities = [ | |
46 | 'ParticipantRole', | |
47 | 'ParticipantEventName', | |
48 | 'ParticipantEventType', | |
49 | ]; | |
50 | $params['extends_entity_column_id'] = 'null'; | |
51 | if (in_array($extendsEntity, $participantEntities) | |
52 | ) { | |
53 | $params['extends_entity_column_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $extendsEntity, 'value', 'name'); | |
54 | } | |
55 | $params['extends_entity_column_value'] = $params['extends'][1]; | |
56 | if (in_array($extendsEntity, $participantEntities)) { | |
57 | $params['extends'] = 'Participant'; | |
58 | } | |
59 | else { | |
60 | $params['extends'] = $extendsEntity; | |
61 | } | |
62 | } | |
63 | elseif (isset($params['extends']) && (!isset($params['extends'][1]) || empty($params['extends'][1]))) { | |
64 | $params['extends'] = $params['extends'][0]; | |
65 | } | |
6a488035 TO |
66 | if (isset($params['extends_entity_column_value']) && !is_array($params['extends_entity_column_value'])) { |
67 | // BAO fails if this is a string, but API getFields says this must be a string, so we'll do a double backflip | |
68 | $params['extends_entity_column_value'] = CRM_Utils_Array::explodePadded($params['extends_entity_column_value']); | |
69 | } | |
70 | ||
98fd592b | 71 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'CustomGroup'); |
6a488035 TO |
72 | } |
73 | ||
11e09c59 | 74 | /** |
0aa0303c | 75 | * Adjust Metadata for Create action. |
6a488035 | 76 | * |
cf470720 | 77 | * @param array $params |
b081365f | 78 | * Array of parameters determined by getfields. |
6a488035 TO |
79 | */ |
80 | function _civicrm_api3_custom_group_create_spec(&$params) { | |
81 | $params['extends']['api.required'] = 1; | |
82 | $params['title']['api.required'] = 1; | |
83 | $params['style']['api.default'] = 'Inline'; | |
46b88fe1 | 84 | $params['is_active']['api.default'] = 1; |
6a488035 TO |
85 | } |
86 | ||
87 | /** | |
88 | * Use this API to delete an existing group. | |
89 | * | |
decce855 | 90 | * @param array $params |
6a488035 | 91 | * |
decce855 | 92 | * @return array |
c23f45d3 | 93 | */ |
6a488035 | 94 | function civicrm_api3_custom_group_delete($params) { |
6a488035 TO |
95 | $values = new CRM_Core_DAO_CustomGroup(); |
96 | $values->id = $params['id']; | |
97 | $values->find(TRUE); | |
98 | ||
6a488035 TO |
99 | $result = CRM_Core_BAO_CustomGroup::deleteGroup($values, TRUE); |
100 | return $result ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting custom group'); | |
101 | } | |
102 | ||
103 | /** | |
2e66abf8 | 104 | * API to get existing custom fields. |
6a488035 | 105 | * |
cf470720 | 106 | * @param array $params |
2e66abf8 | 107 | * Array per getfields metadata. |
6a488035 | 108 | * |
77b97be7 | 109 | * @return array |
ae5ffbb7 | 110 | */ |
6a488035 TO |
111 | function civicrm_api3_custom_group_get($params) { |
112 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
113 | } | |
114 | ||
5322ae12 | 115 | /** |
9d32e6f7 EM |
116 | * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom group. |
117 | * | |
d0997921 | 118 | * @param array $params |
2e66abf8 | 119 | * Array per getfields metadata. |
9d32e6f7 | 120 | * |
645ee340 | 121 | * @return array |
5322ae12 CW |
122 | */ |
123 | function civicrm_api3_custom_group_setvalue($params) { | |
124 | require_once 'api/v3/Generic/Setvalue.php'; | |
cf8f0fff | 125 | $result = civicrm_api3_generic_setValue(["entity" => 'CustomGroup', 'params' => $params]); |
5322ae12 CW |
126 | if (empty($result['is_error'])) { |
127 | CRM_Utils_System::flushCache(); | |
128 | } | |
129 | return $result; | |
130 | } |