INFRA-132 - Batch 14 (g)
[civicrm-core.git] / api / v3 / CustomGroup.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 * File for the CiviCRM APIv3 custom group functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_CustomGroup
33 *
731a0992 34 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
35 * @version $Id: CustomGroup.php 30879 2010-11-22 15:45:55Z shot $
36 */
37
38/**
39 * Most API functions take in associative arrays ( name => value pairs
40 * as parameters. Some of the most commonly used parameters are
41 * described below
42 *
cf470720
TO
43 * @param array $params
44 * An associative array used in construction.
16b10e64 45 * retrieval of the object
6a488035
TO
46 * @todo missing get function
47 *
48 *
49 */
50
51/**
52 * Use this API to create a new group. The 'extends' value accepts an array or a comma separated string.
53 * e.g array(
54 'Individual','Contact') or 'Individual,Contact'
55 * See the CRM Data Model for custom_group property definitions
56 * $params['class_name'] is a required field, class being extended.
57 *
16b10e64 58 * @param array $params
cf470720 59 * Array Associative array of property name/value pairs to insert in group.
6a488035
TO
60 * {@getfields CustomGroup_create}
61 *
72b3a70c 62 * @return array
6a488035 63 * @todo $params['extends'] is array format - is that std compatible
6a488035
TO
64 */
65function civicrm_api3_custom_group_create($params) {
882e0216 66 if (isset($params['extends']) && is_string($params['extends'])) {
6a488035
TO
67 $extends = explode(",", $params['extends']);
68 unset($params['extends']);
69 $params['extends'] = $extends;
70 }
71 if (!isset($params['extends'][0]) || !trim($params['extends'][0])) {
72 return civicrm_api3_create_error("First item in params['extends'] must be a class name (e.g. 'Contact').");
73 }
74 if (isset($params['extends_entity_column_value']) && !is_array($params['extends_entity_column_value'])) {
75 // BAO fails if this is a string, but API getFields says this must be a string, so we'll do a double backflip
76 $params['extends_entity_column_value'] = CRM_Utils_Array::explodePadded($params['extends_entity_column_value']);
77 }
78
ea12ce50 79 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035
TO
80}
81
11e09c59 82/**
6a488035
TO
83 * Adjust Metadata for Create action
84 *
cf470720
TO
85 * @param array $params
86 * Array or parameters determined by getfields.
6a488035
TO
87 */
88function _civicrm_api3_custom_group_create_spec(&$params) {
89 $params['extends']['api.required'] = 1;
90 $params['title']['api.required'] = 1;
91 $params['style']['api.default'] = 'Inline';
46b88fe1 92 $params['is_active']['api.default'] = 1;
6a488035
TO
93}
94
95/**
96 * Use this API to delete an existing group.
97 *
decce855 98 * @param array $params
6a488035 99 *
decce855 100 * @return array
c23f45d3 101 */
6a488035 102function civicrm_api3_custom_group_delete($params) {
6a488035
TO
103 $values = new CRM_Core_DAO_CustomGroup();
104 $values->id = $params['id'];
105 $values->find(TRUE);
106
6a488035
TO
107 $result = CRM_Core_BAO_CustomGroup::deleteGroup($values, TRUE);
108 return $result ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting custom group');
109}
110
111/**
112 * Use this API to get existing custom fields.
113 *
cf470720
TO
114 * @param array $params
115 * Array to search on.
6a488035 116 *
77b97be7 117 * @return array
ae5ffbb7 118 */
6a488035
TO
119function civicrm_api3_custom_group_get($params) {
120 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
121}
122
5322ae12
CW
123/**
124 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom group
d0997921 125 * @param array $params
645ee340 126 * @return array
5322ae12
CW
127 */
128function civicrm_api3_custom_group_setvalue($params) {
129 require_once 'api/v3/Generic/Setvalue.php';
130 $result = civicrm_api3_generic_setValue(array("entity" => 'custom_group', 'params' => $params));
131 if (empty($result['is_error'])) {
132 CRM_Utils_System::flushCache();
133 }
134 return $result;
135}