Merge pull request #17656 from civicrm/5.27
[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/**
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 */
33function 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 }
43 if (isset($params['extends_entity_column_value']) && !is_array($params['extends_entity_column_value'])) {
44 // BAO fails if this is a string, but API getFields says this must be a string, so we'll do a double backflip
45 $params['extends_entity_column_value'] = CRM_Utils_Array::explodePadded($params['extends_entity_column_value']);
46 }
47
98fd592b 48 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'CustomGroup');
6a488035
TO
49}
50
11e09c59 51/**
0aa0303c 52 * Adjust Metadata for Create action.
6a488035 53 *
cf470720 54 * @param array $params
b081365f 55 * Array of parameters determined by getfields.
6a488035
TO
56 */
57function _civicrm_api3_custom_group_create_spec(&$params) {
58 $params['extends']['api.required'] = 1;
59 $params['title']['api.required'] = 1;
60 $params['style']['api.default'] = 'Inline';
46b88fe1 61 $params['is_active']['api.default'] = 1;
6a488035
TO
62}
63
64/**
65 * Use this API to delete an existing group.
66 *
decce855 67 * @param array $params
6a488035 68 *
decce855 69 * @return array
c23f45d3 70 */
6a488035 71function civicrm_api3_custom_group_delete($params) {
6a488035
TO
72 $values = new CRM_Core_DAO_CustomGroup();
73 $values->id = $params['id'];
74 $values->find(TRUE);
75
6a488035
TO
76 $result = CRM_Core_BAO_CustomGroup::deleteGroup($values, TRUE);
77 return $result ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting custom group');
78}
79
80/**
2e66abf8 81 * API to get existing custom fields.
6a488035 82 *
cf470720 83 * @param array $params
2e66abf8 84 * Array per getfields metadata.
6a488035 85 *
77b97be7 86 * @return array
ae5ffbb7 87 */
6a488035
TO
88function civicrm_api3_custom_group_get($params) {
89 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
90}
91
5322ae12 92/**
9d32e6f7
EM
93 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom group.
94 *
d0997921 95 * @param array $params
2e66abf8 96 * Array per getfields metadata.
9d32e6f7 97 *
645ee340 98 * @return array
5322ae12
CW
99 */
100function civicrm_api3_custom_group_setvalue($params) {
101 require_once 'api/v3/Generic/Setvalue.php';
cf8f0fff 102 $result = civicrm_api3_generic_setValue(["entity" => 'CustomGroup', 'params' => $params]);
5322ae12
CW
103 if (empty($result['is_error'])) {
104 CRM_Utils_System::flushCache();
105 }
106 return $result;
107}