Merge pull request #4463 from jmcclelland/authorize-recur-unlimited
[civicrm-core.git] / api / v3 / CustomGroup.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
731a0992 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
731a0992 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 * File for the CiviCRM APIv3 custom group functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_CustomGroup
34 *
731a0992 35 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
36 * @version $Id: CustomGroup.php 30879 2010-11-22 15:45:55Z shot $
37 */
38
39/**
40 * Most API functions take in associative arrays ( name => value pairs
41 * as parameters. Some of the most commonly used parameters are
42 * described below
43 *
44 * @param array $params an associative array used in construction
45 * retrieval of the object
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 *
58 * @param $params array Associative array of property name/value pairs to insert in group.
59 * {@getfields CustomGroup_create}
60 *
61 * @return Newly create custom_group object
62 * @todo $params['extends'] is array format - is that std compatible
63 * @access public
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
79
80 $customGroup = CRM_Core_BAO_CustomGroup::create($params);
81
82 _civicrm_api3_object_to_array($customGroup, $values[$customGroup->id]);
83 return civicrm_api3_create_success($values, $params, 'custom_group', $customGroup);
84}
85
11e09c59 86/**
6a488035
TO
87 * Adjust Metadata for Create action
88 *
89 * @param array $params array or parameters determined by getfields
90 */
91function _civicrm_api3_custom_group_create_spec(&$params) {
92 $params['extends']['api.required'] = 1;
93 $params['title']['api.required'] = 1;
94 $params['style']['api.default'] = 'Inline';
46b88fe1 95 $params['is_active']['api.default'] = 1;
6a488035
TO
96}
97
98/**
99 * Use this API to delete an existing group.
100 *
101 * @param array id of the group to be deleted
102 *
103 * @return Null if success
104 * @access public
105 * {@getfields CustomGroup_delete}
106 * @example CustomGroupDelete.php
107 **/
108function civicrm_api3_custom_group_delete($params) {
109
110 $values = new CRM_Core_DAO_CustomGroup();
111 $values->id = $params['id'];
112 $values->find(TRUE);
113
6a488035
TO
114 $result = CRM_Core_BAO_CustomGroup::deleteGroup($values, TRUE);
115 return $result ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting custom group');
116}
117
118/**
119 * Use this API to get existing custom fields.
120 *
121 * @param array $params Array to search on
122 *
77b97be7 123 * @return array
6a488035
TO
124 * @access public
125 * {@getfields CustomGroup_get}
126 * @example CustomGroupGet.php
127 **/
128function civicrm_api3_custom_group_get($params) {
129 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
130}
131
5322ae12
CW
132/**
133 * CRM-15191 - Hack to ensure the cache gets cleared after updating a custom group
134 */
135function civicrm_api3_custom_group_setvalue($params) {
136 require_once 'api/v3/Generic/Setvalue.php';
137 $result = civicrm_api3_generic_setValue(array("entity" => 'custom_group', 'params' => $params));
138 if (empty($result['is_error'])) {
139 CRM_Utils_System::flushCache();
140 }
141 return $result;
142}