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