Merge pull request #15842 from totten/master-config-backend
[civicrm-core.git] / api / v3 / CustomGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM custom group.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Use this API to create a new group.
20 *
21 * The 'extends' value accepts an array or a comma separated string.
22 * e.g array(
23 * 'Individual','Contact') or 'Individual,Contact'
24 * See the CRM Data Model for custom_group property definitions
25 * $params['class_name'] is a required field, class being extended.
26 *
27 * @param array $params
28 * Array per getfields metadata.
29 *
30 * @return array
31 * @todo $params['extends'] is array format - is that std compatible
32 */
33 function civicrm_api3_custom_group_create($params) {
34 if (isset($params['extends']) && is_string($params['extends'])) {
35 $extends = explode(",", $params['extends']);
36 unset($params['extends']);
37 $params['extends'] = $extends;
38 }
39 if (!isset($params['id']) && (!isset($params['extends'][0]) || !trim($params['extends'][0]))) {
40
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
48 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'CustomGroup');
49 }
50
51 /**
52 * Adjust Metadata for Create action.
53 *
54 * @param array $params
55 * Array of parameters determined by getfields.
56 */
57 function _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';
61 $params['is_active']['api.default'] = 1;
62 }
63
64 /**
65 * Use this API to delete an existing group.
66 *
67 * @param array $params
68 *
69 * @return array
70 */
71 function civicrm_api3_custom_group_delete($params) {
72 $values = new CRM_Core_DAO_CustomGroup();
73 $values->id = $params['id'];
74 $values->find(TRUE);
75
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 /**
81 * API to get existing custom fields.
82 *
83 * @param array $params
84 * Array per getfields metadata.
85 *
86 * @return array
87 */
88 function civicrm_api3_custom_group_get($params) {
89 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
90 }
91
92 /**
93 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom group.
94 *
95 * @param array $params
96 * Array per getfields metadata.
97 *
98 * @return array
99 */
100 function civicrm_api3_custom_group_setvalue($params) {
101 require_once 'api/v3/Generic/Setvalue.php';
102 $result = civicrm_api3_generic_setValue(["entity" => 'CustomGroup', 'params' => $params]);
103 if (empty($result['is_error'])) {
104 CRM_Utils_System::flushCache();
105 }
106 return $result;
107 }