Merge pull request #5187 from mallezie/CRM-15982
[civicrm-core.git] / api / v3 / CustomGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This api exposes CiviCRM custom group.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34 /**
35 * Use this API to create a new group.
36 *
37 * The 'extends' value accepts an array or a comma separated string.
38 * e.g array(
39 * 'Individual','Contact') or 'Individual,Contact'
40 * See the CRM Data Model for custom_group property definitions
41 * $params['class_name'] is a required field, class being extended.
42 *
43 * @param array $params
44 * Array per getfields metadata.
45 *
46 * @return array
47 * @todo $params['extends'] is array format - is that std compatible
48 */
49 function civicrm_api3_custom_group_create($params) {
50 if (isset($params['extends']) && is_string($params['extends'])) {
51 $extends = explode(",", $params['extends']);
52 unset($params['extends']);
53 $params['extends'] = $extends;
54 }
55 if (!isset($params['extends'][0]) || !trim($params['extends'][0])) {
56 return civicrm_api3_create_error("First item in params['extends'] must be a class name (e.g. 'Contact').");
57 }
58 if (isset($params['extends_entity_column_value']) && !is_array($params['extends_entity_column_value'])) {
59 // BAO fails if this is a string, but API getFields says this must be a string, so we'll do a double backflip
60 $params['extends_entity_column_value'] = CRM_Utils_Array::explodePadded($params['extends_entity_column_value']);
61 }
62
63 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
64 }
65
66 /**
67 * Adjust Metadata for Create action.
68 *
69 * @param array $params
70 * Array of parameters determined by getfields.
71 */
72 function _civicrm_api3_custom_group_create_spec(&$params) {
73 $params['extends']['api.required'] = 1;
74 $params['title']['api.required'] = 1;
75 $params['style']['api.default'] = 'Inline';
76 $params['is_active']['api.default'] = 1;
77 }
78
79 /**
80 * Use this API to delete an existing group.
81 *
82 * @param array $params
83 *
84 * @return array
85 */
86 function civicrm_api3_custom_group_delete($params) {
87 $values = new CRM_Core_DAO_CustomGroup();
88 $values->id = $params['id'];
89 $values->find(TRUE);
90
91 $result = CRM_Core_BAO_CustomGroup::deleteGroup($values, TRUE);
92 return $result ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting custom group');
93 }
94
95 /**
96 * API to get existing custom fields.
97 *
98 * @param array $params
99 * Array per getfields metadata.
100 *
101 * @return array
102 */
103 function civicrm_api3_custom_group_get($params) {
104 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
105 }
106
107 /**
108 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom group.
109 *
110 * @param array $params
111 * Array per getfields metadata.
112 *
113 * @return array
114 */
115 function civicrm_api3_custom_group_setvalue($params) {
116 require_once 'api/v3/Generic/Setvalue.php';
117 $result = civicrm_api3_generic_setValue(array("entity" => 'custom_group', 'params' => $params));
118 if (empty($result['is_error'])) {
119 CRM_Utils_System::flushCache();
120 }
121 return $result;
122 }