Merge pull request #19450 from totten/master-rest-escape
[civicrm-core.git] / api / v3 / UFGroup.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
c28e1768 13 * This api exposes CiviCRM profile group.
6a488035
TO
14 *
15 * @package CiviCRM_APIv3
6a488035
TO
16 */
17
18/**
9d32e6f7
EM
19 * Adjust metadata for create action.
20 *
16b10e64 21 * @param array $params
6a488035 22 */
6a488035
TO
23function _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;
dc64d047
EM
28 // Default to the logged in user.
29 $params['created_id']['api.default'] = 'user_contact_id';
6a488035
TO
30 $params['created_date']['api.default'] = 'now';
31}
dc64d047 32
6a488035 33/**
9d32e6f7
EM
34 * Use this API to create a new group.
35 *
36 * See the CRM Data Model for uf_group property definitions
6a488035 37 *
16b10e64 38 * @param array $params
2e66abf8 39 * Array per getfields metadata.
6a488035 40 *
a6c01b45 41 * @return array
72b3a70c 42 * API result array
6a488035
TO
43 */
44function civicrm_api3_uf_group_create($params) {
98fd592b 45 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'UFGroup');
6a488035
TO
46}
47
48/**
9d32e6f7 49 * Returns array of uf groups (profiles) matching a set of one or more group properties.
6a488035 50 *
cf470720 51 * @param array $params
b081365f 52 * Array of properties. If empty, all records will be returned.
6a488035 53 *
a6c01b45 54 * @return array
72b3a70c 55 * Array of matching profiles
6a488035
TO
56 */
57function civicrm_api3_uf_group_get($params) {
58
59 return _civicrm_api3_basic_get('CRM_Core_BAO_UFGroup', $params);
60}
61
62/**
9d32e6f7 63 * Delete uf group.
6a488035 64 *
c490a46a 65 * @param array $params
7f6921be 66 *
72b3a70c 67 * @return array
6a488035
TO
68 */
69function civicrm_api3_uf_group_delete($params) {
6a488035
TO
70 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
71}
b233e1b4
CW
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 */
82function _civicrm_api3_uf_group_getlist_defaults(&$request) {
cf8f0fff
CW
83 return [
84 'description_field' => [
b233e1b4
CW
85 'description',
86 'group_type',
cf8f0fff
CW
87 ],
88 'params' => [
b233e1b4 89 'is_active' => 1,
cf8f0fff
CW
90 ],
91 ];
b233e1b4
CW
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 */
106function _civicrm_api3_uf_group_getlist_output($result, $request, $entity, $fields) {
cf8f0fff 107 $output = [];
b233e1b4
CW
108 if (!empty($result['values'])) {
109 foreach ($result['values'] as $row) {
cf8f0fff 110 $data = [
b233e1b4
CW
111 'id' => $row[$request['id_field']],
112 'label' => $row[$request['label_field']],
cf8f0fff 113 ];
b233e1b4 114 if (!empty($request['description_field'])) {
cf8f0fff 115 $data['description'] = [];
b233e1b4
CW
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'])) {
2e1f50d6 138 $data['image'] = $row[$request['image_field']] ?? '';
b233e1b4
CW
139 }
140 $output[] = $data;
141 }
142 }
143 return $output;
144}