Merge pull request #14981 from eileenmcnaughton/load_extract
[civicrm-core.git] / api / v3 / CustomGroup.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
c28e1768 29 * This api exposes CiviCRM custom group.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
6a488035
TO
32 */
33
6a488035 34/**
9d32e6f7
EM
35 * Use this API to create a new group.
36 *
37 * The 'extends' value accepts an array or a comma separated string.
6a488035 38 * e.g array(
d1b0d05e 39 * 'Individual','Contact') or 'Individual,Contact'
6a488035
TO
40 * See the CRM Data Model for custom_group property definitions
41 * $params['class_name'] is a required field, class being extended.
42 *
16b10e64 43 * @param array $params
2e66abf8 44 * Array per getfields metadata.
6a488035 45 *
72b3a70c 46 * @return array
6a488035 47 * @todo $params['extends'] is array format - is that std compatible
6a488035
TO
48 */
49function civicrm_api3_custom_group_create($params) {
882e0216 50 if (isset($params['extends']) && is_string($params['extends'])) {
6a488035
TO
51 $extends = explode(",", $params['extends']);
52 unset($params['extends']);
53 $params['extends'] = $extends;
54 }
16bdd936
MM
55 if (!isset($params['id']) && (!isset($params['extends'][0]) || !trim($params['extends'][0]))) {
56
6a488035
TO
57 return civicrm_api3_create_error("First item in params['extends'] must be a class name (e.g. 'Contact').");
58 }
59 if (isset($params['extends_entity_column_value']) && !is_array($params['extends_entity_column_value'])) {
60 // BAO fails if this is a string, but API getFields says this must be a string, so we'll do a double backflip
61 $params['extends_entity_column_value'] = CRM_Utils_Array::explodePadded($params['extends_entity_column_value']);
62 }
63
98fd592b 64 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'CustomGroup');
6a488035
TO
65}
66
11e09c59 67/**
0aa0303c 68 * Adjust Metadata for Create action.
6a488035 69 *
cf470720 70 * @param array $params
b081365f 71 * Array of parameters determined by getfields.
6a488035
TO
72 */
73function _civicrm_api3_custom_group_create_spec(&$params) {
74 $params['extends']['api.required'] = 1;
75 $params['title']['api.required'] = 1;
76 $params['style']['api.default'] = 'Inline';
46b88fe1 77 $params['is_active']['api.default'] = 1;
6a488035
TO
78}
79
80/**
81 * Use this API to delete an existing group.
82 *
decce855 83 * @param array $params
6a488035 84 *
decce855 85 * @return array
c23f45d3 86 */
6a488035 87function civicrm_api3_custom_group_delete($params) {
6a488035
TO
88 $values = new CRM_Core_DAO_CustomGroup();
89 $values->id = $params['id'];
90 $values->find(TRUE);
91
6a488035
TO
92 $result = CRM_Core_BAO_CustomGroup::deleteGroup($values, TRUE);
93 return $result ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting custom group');
94}
95
96/**
2e66abf8 97 * API to get existing custom fields.
6a488035 98 *
cf470720 99 * @param array $params
2e66abf8 100 * Array per getfields metadata.
6a488035 101 *
77b97be7 102 * @return array
ae5ffbb7 103 */
6a488035
TO
104function civicrm_api3_custom_group_get($params) {
105 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
106}
107
5322ae12 108/**
9d32e6f7
EM
109 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom group.
110 *
d0997921 111 * @param array $params
2e66abf8 112 * Array per getfields metadata.
9d32e6f7 113 *
645ee340 114 * @return array
5322ae12
CW
115 */
116function civicrm_api3_custom_group_setvalue($params) {
117 require_once 'api/v3/Generic/Setvalue.php';
cf8f0fff 118 $result = civicrm_api3_generic_setValue(["entity" => 'CustomGroup', 'params' => $params]);
5322ae12
CW
119 if (empty($result['is_error'])) {
120 CRM_Utils_System::flushCache();
121 }
122 return $result;
123}