Merge pull request #20757 from eileenmcnaughton/phone_strip
[civicrm-core.git] / api / v3 / Membership.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 *
14 * This api exposes CiviCRM membership contact records.
15 *
16 * @package CiviCRM_APIv3
17 */
18
19 /**
20 * Adjust Metadata for Delete action.
21 *
22 * The metadata is used for setting defaults, documentation & validation.
23 *
24 * @param array $params
25 * Array of parameters determined by getfields.
26 */
27 function _civicrm_api3_membership_delete_spec(&$params) {
28 $params['preserve_contribution'] = [
29 'api.required' => 0,
30 'title' => 'Preserve Contribution',
31 'description' => 'By default this is 0, or 0 if not set. Set to 1 to preserve the associated contribution record when membership is deleted.',
32 'type' => CRM_Utils_Type::T_BOOLEAN,
33 ];
34 }
35
36 /**
37 * Deletes an existing contact Membership.
38 *
39 * @param array $params
40 * Array array holding id - Id of the contact membership to be deleted.
41 * @return array API result array.
42 * @throws API_Exception
43 */
44 function civicrm_api3_membership_delete($params) {
45 if (isset($params['preserve_contribution'])) {
46 if (CRM_Member_BAO_Membership::del($params['id'], $params['preserve_contribution'])) {
47 return civicrm_api3_create_success(TRUE, $params);
48 }
49 else {
50 throw new API_Exception(ts('Could not delete membership'));
51 }
52 }
53 else {
54 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
55 }
56 }
57
58 /**
59 * Create a Contact Membership.
60 *
61 * This API is used for creating a Membership for a contact.
62 * Required parameters : membership_type_id and status_id.
63 *
64 * @param array $params
65 * Array of name/value property values of civicrm_membership.
66 *
67 * @return array
68 * API result array.
69 *
70 * @throws \CRM_Core_Exception
71 * @throws \CiviCRM_API3_Exception
72 */
73 function civicrm_api3_membership_create($params) {
74 // check params for membership id during update
75 if (!empty($params['id']) && !isset($params['skipStatusCal'])) {
76 // Don't calculate status on existing membership - expect API use to pass them in
77 // or leave unchanged.
78 $params['skipStatusCal'] = 1;
79 }
80 else {
81 // also check for status id if override is set (during add/update)
82 if (!empty($params['is_override']) && empty($params['status_id'])) {
83 return civicrm_api3_create_error('Status ID required');
84 }
85 }
86
87 $values = [];
88 _civicrm_api3_custom_format_params($params, $values, 'Membership');
89 $params = array_merge($params, $values);
90
91 // Calculate membership dates
92 // Fixme: This code belongs in the BAO
93 if (!empty($params['num_terms'])) {
94 // If this is a new membership or we have a specified number of terms calculate membership dates.
95 if (!empty($params['id'])) {
96 // This is an existing membership, calculate the membership dates after renewal
97 // num_terms is treated as a 'special sauce' for is_renewal but this
98 // isn't really helpful for completing pendings.
99 $calcDates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType(
100 $params['id'],
101 NULL,
102 CRM_Utils_Array::value('membership_type_id', $params),
103 $params['num_terms']
104 );
105 foreach (['join_date', 'start_date', 'end_date'] as $date) {
106 if (empty($params[$date]) && isset($calcDates[$date])) {
107 $params[$date] = $calcDates[$date];
108 }
109 }
110 }
111 }
112
113 // Fixme: This code belongs in the BAO
114 $ids = [];
115 if (empty($params['id'])) {
116 $params['action'] = CRM_Core_Action::ADD;
117 }
118 else {
119 // edit mode
120 $params['action'] = CRM_Core_Action::UPDATE;
121 // @todo remove $ids['membership'] is required in CRM_Price_BAO_LineItem::processPriceSet
122 $ids['membership'] = $params['id'];
123 }
124
125 // @todo stop passing $ids (membership and userId may be set above)
126 $membershipBAO = CRM_Member_BAO_Membership::create($params, $ids);
127
128 if (property_exists($membershipBAO, 'is_error')) {
129 // In case of no valid status for given dates, $membershipBAO
130 // is going to contain 'is_error' => "Error Message"
131 return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates'));
132 }
133
134 $membership = [];
135 _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id]);
136
137 return civicrm_api3_create_success($membership, $params, 'Membership', 'create', $membershipBAO);
138
139 }
140
141 /**
142 * Adjust Metadata for Create action.
143 *
144 * The metadata is used for setting defaults, documentation & validation.
145 *
146 * @param array $params
147 * Array of parameters determined by getfields.
148 */
149 function _civicrm_api3_membership_create_spec(&$params) {
150 $params['contact_id']['api.required'] = 1;
151 $params['membership_type_id']['api.required'] = 1;
152 $params['is_test']['api.default'] = 0;
153 $params['membership_type_id']['api.aliases'] = ['membership_type'];
154 $params['status_id']['api.aliases'] = ['membership_status'];
155 $params['skipStatusCal'] = [
156 'title' => 'Skip status calculation',
157 'description' => 'By default this is 0 if id is not set and 1 if it is set.',
158 'type' => CRM_Utils_Type::T_BOOLEAN,
159 ];
160 $params['num_terms'] = [
161 'title' => 'Number of terms',
162 'description' => 'Terms to add/renew. If this parameter is passed, dates will be calculated automatically. If no id is passed (new membership) and no dates are given, num_terms will be assumed to be 1.',
163 'type' => CRM_Utils_Type::T_INT,
164 ];
165 }
166
167 /**
168 * Adjust Metadata for Get action.
169 *
170 * The metadata is used for setting defaults, documentation & validation.
171 *
172 * @param array $params
173 * Array of parameters determined by getfields.
174 */
175 function _civicrm_api3_membership_get_spec(&$params) {
176 $params['membership_type_id']['api.aliases'] = ['membership_type'];
177 $params['active_only'] = [
178 'title' => 'Active Only',
179 'description' => 'Only retrieve active memberships',
180 'type' => CRM_Utils_Type::T_BOOLEAN,
181 ];
182 }
183
184 /**
185 * Get contact Membership record.
186 *
187 * This api will return the membership records for the contacts
188 * having membership based on the relationship with the direct members.
189 *
190 * @param array $params
191 * Key/value pairs for contact_id and some.
192 * options affecting the desired results; has legacy support
193 * for just passing the contact_id itself as the argument
194 *
195 * @return array
196 * Array of all found membership property values.
197 */
198 function civicrm_api3_membership_get($params) {
199 $activeOnly = $membershipTypeId = $membershipType = NULL;
200
201 $contactID = $params['contact_id'] ?? NULL;
202 if (!empty($params['filters']) && is_array($params['filters']) && isset($params['filters']['is_current'])) {
203 $activeOnly = $params['filters']['is_current'];
204 unset($params['filters']['is_current']);
205 }
206 $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
207 if ($activeOnly && empty($params['status_id'])) {
208 $params['status_id'] = ['IN' => CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent()];
209 }
210
211 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'Membership', 'get');
212 if ($options['is_count']) {
213 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
214 }
215 $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Membership');
216
217 $return = $options['return'];
218 if (empty($membershipValues) ||
219 (!empty($return)
220 && !array_key_exists('related_contact_id', $return)
221 && !array_key_exists('relationship_name', $return)
222 )
223 ) {
224 return civicrm_api3_create_success($membershipValues, $params, 'Membership', 'get');
225 }
226
227 $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID);
228 return civicrm_api3_create_success($members, $params, 'Membership', 'get');
229 }
230
231 /**
232 * Perform api v2 custom behaviour.
233 *
234 * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
235 * membership a complicated return array is constructed. The original
236 * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
237 * is passed in as part of the reasonable expectation developers have that we will keep the api
238 * as stable as possible
239 *
240 * @param array $params
241 * Parameters passed into get function.
242 * @param int $membershipTypeId
243 * @param $activeOnly
244 *
245 * @return array
246 * result for calling function
247 */
248 function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) {
249 // get the membership for the given contact ID
250 $membershipParams = ['contact_id' => $params['contact_id']];
251 if ($membershipTypeId) {
252 $membershipParams['membership_type_id'] = $membershipTypeId;
253 }
254 $membershipValues = [];
255 CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly);
256 return $membershipValues;
257 }
258
259 /**
260 * Non-standard behaviour inherited from v2.
261 *
262 * @param array $params
263 * Parameters passed into get function.
264 * @param $membershipValues
265 * @param int $contactID
266 *
267 * @return array
268 * result for calling function
269 */
270 function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) {
271 $relationships = [];
272 foreach ($membershipValues as $membershipId => $values) {
273 // populate the membership type name for the membership type id
274 $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']) ?? [];
275
276 if (!empty($membershipType)) {
277 $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
278
279 if (!empty($membershipType['relationship_type_id'])) {
280 $relationships[$membershipType['relationship_type_id']] = $membershipId;
281 }
282 }
283
284 // populating relationship type name.
285 $relationshipType = new CRM_Contact_BAO_RelationshipType();
286 $relationshipType->id = $membershipType['relationship_type_id'] ?? NULL;
287 if ($relationshipType->find(TRUE)) {
288 $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b;
289 }
290
291 _civicrm_api3_custom_data_get($membershipValues[$membershipId], CRM_Utils_Array::value('check_permissions', $params), 'Membership', $membershipId, NULL, $values['membership_type_id']);
292 }
293
294 $members = $membershipValues;
295
296 // Populating contacts in members array based on their relationship with direct members.
297 if (!empty($relationships)) {
298 foreach ($relationships as $relTypeId => $membershipId) {
299 // As members are not direct members, there should not be
300 // membership id in the result array.
301 unset($membershipValues[$membershipId]['id']);
302 $relationship = new CRM_Contact_BAO_Relationship();
303 $relationship->contact_id_b = $contactID;
304 $relationship->relationship_type_id = $relTypeId;
305 if ($relationship->find()) {
306 while ($relationship->fetch()) {
307 clone($relationship);
308 $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a;
309 $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a;
310 }
311 }
312
313 }
314 }
315 return $members;
316 }