Merge pull request #17656 from civicrm/5.27
[civicrm-core.git] / api / v3 / UFGroup.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 profile group.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Adjust metadata for create action.
20 *
21 * @param array $params
22 */
23 function _civicrm_api3_uf_group_create_spec(&$params) {
24 $session = CRM_Core_Session::singleton();
25 $params['title']['api.required'] = 1;
26 $params['is_active']['api.default'] = 1;
27 $params['is_update_dupe']['api.default'] = 1;
28 // Default to the logged in user.
29 $params['created_id']['api.default'] = 'user_contact_id';
30 $params['created_date']['api.default'] = 'now';
31 }
32
33 /**
34 * Use this API to create a new group.
35 *
36 * See the CRM Data Model for uf_group property definitions
37 *
38 * @param array $params
39 * Array per getfields metadata.
40 *
41 * @return array
42 * API result array
43 */
44 function civicrm_api3_uf_group_create($params) {
45 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'UFGroup');
46 }
47
48 /**
49 * Returns array of uf groups (profiles) matching a set of one or more group properties.
50 *
51 * @param array $params
52 * Array of properties. If empty, all records will be returned.
53 *
54 * @return array
55 * Array of matching profiles
56 */
57 function civicrm_api3_uf_group_get($params) {
58
59 return _civicrm_api3_basic_get('CRM_Core_BAO_UFGroup', $params);
60 }
61
62 /**
63 * Delete uf group.
64 *
65 * @param array $params
66 *
67 * @return array
68 */
69 function civicrm_api3_uf_group_delete($params) {
70 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
71 }
72
73 /**
74 * Set default getlist parameters.
75 *
76 * @see _civicrm_api3_generic_getlist_defaults
77 *
78 * @param array $request
79 *
80 * @return array
81 */
82 function _civicrm_api3_uf_group_getlist_defaults(&$request) {
83 return [
84 'description_field' => [
85 'description',
86 'group_type',
87 ],
88 'params' => [
89 'is_active' => 1,
90 ],
91 ];
92 }
93
94 /**
95 * Format getlist output
96 *
97 * @see _civicrm_api3_generic_getlist_output
98 *
99 * @param array $result
100 * @param array $request
101 * @param string $entity
102 * @param array $fields
103 *
104 * @return array
105 */
106 function _civicrm_api3_uf_group_getlist_output($result, $request, $entity, $fields) {
107 $output = [];
108 if (!empty($result['values'])) {
109 foreach ($result['values'] as $row) {
110 $data = [
111 'id' => $row[$request['id_field']],
112 'label' => $row[$request['label_field']],
113 ];
114 if (!empty($request['description_field'])) {
115 $data['description'] = [];
116 foreach ((array) $request['description_field'] as $field) {
117 if (!empty($row[$field])) {
118 // Special formatting for group_type field
119 if ($field == 'group_type') {
120 $groupTypes = CRM_UF_Page_Group::extractGroupTypes($row[$field]);
121 $data['description'][] = CRM_UF_Page_Group::formatGroupTypes($groupTypes);
122 continue;
123 }
124 if (!isset($fields[$field]['pseudoconstant'])) {
125 $data['description'][] = $row[$field];
126 }
127 else {
128 $data['description'][] = CRM_Core_PseudoConstant::getLabel(
129 _civicrm_api3_get_BAO($entity),
130 $field,
131 $row[$field]
132 );
133 }
134 }
135 }
136 };
137 if (!empty($request['image_field'])) {
138 $data['image'] = $row[$request['image_field']] ?? '';
139 }
140 $output[] = $data;
141 }
142 }
143 return $output;
144 }