Merge pull request #12114 from jitendrapurohit/membership-2
[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
8f3aca71
CW
104 // Fixme: This code belongs in the BAO
105 if (empty($params['id']) || !empty($params['num_terms'])) {
106 if (empty($params['id'])) {
107 $calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType(
108 $params['membership_type_id'],
109 CRM_Utils_Array::value('join_date', $params),
110 CRM_Utils_Array::value('start_date', $params),
111 CRM_Utils_Array::value('end_date', $params),
112 CRM_Utils_Array::value('num_terms', $params, 1)
113 );
114 }
115 else {
116 $calcDates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType(
117 $params['id'],
118 NULL,
119 CRM_Utils_Array::value('membership_type_id', $params),
120 $params['num_terms']
121 );
122 }
123 foreach (array('join_date', 'start_date', 'end_date') as $date) {
124 if (empty($params[$date]) && isset($calcDates[$date])) {
125 $params[$date] = $calcDates[$date];
126 }
127 }
128 }
129
130 // Fixme: This code belongs in the BAO
6a488035
TO
131 $action = CRM_Core_Action::ADD;
132 // we need user id during add mode
7c550ca0
WA
133 $ids = array();
134 if (!empty($params['contact_id'])) {
135 $ids['userId'] = $params['contact_id'];
136 }
6a488035 137 //for edit membership id should be present
13a16f43 138 // probably not required now.
a7488080 139 if (!empty($params['id'])) {
4d63cfde 140 $ids['membership'] = $params['id'];
6a488035
TO
141 $action = CRM_Core_Action::UPDATE;
142 }
6a488035
TO
143 //need to pass action to handle related memberships.
144 $params['action'] = $action;
145
6a488035
TO
146 $membershipBAO = CRM_Member_BAO_Membership::create($params, $ids, TRUE);
147
148 if (array_key_exists('is_error', $membershipBAO)) {
149 // In case of no valid status for given dates, $membershipBAO
150 // is going to contain 'is_error' => "Error Message"
151 return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates'));
152 }
153
154 $membership = array();
155 _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id]);
156
244bbdd8 157 return civicrm_api3_create_success($membership, $params, 'Membership', 'create', $membershipBAO);
6a488035
TO
158
159}
11e09c59
TO
160
161/**
0aa0303c
EM
162 * Adjust Metadata for Create action.
163 *
164 * The metadata is used for setting defaults, documentation & validation.
6a488035 165 *
cf470720 166 * @param array $params
b081365f 167 * Array of parameters determined by getfields.
6a488035
TO
168 */
169function _civicrm_api3_membership_create_spec(&$params) {
170 $params['contact_id']['api.required'] = 1;
4d63cfde 171 $params['membership_type_id']['api.required'] = 1;
8f3aca71 172 $params['is_test']['api.default'] = 0;
4d63cfde 173 $params['membership_type_id']['api.aliases'] = array('membership_type');
eea6f5de 174 $params['status_id']['api.aliases'] = array('membership_status');
8f3aca71 175 $params['skipStatusCal'] = array(
b2ed8e73
CW
176 'title' => 'Skip status calculation',
177 'description' => 'By default this is 0 if id is not set and 1 if it is set.',
d142432b 178 'type' => CRM_Utils_Type::T_BOOLEAN,
8f3aca71
CW
179 );
180 $params['num_terms'] = array(
b2ed8e73
CW
181 'title' => 'Number of terms',
182 '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 183 'type' => CRM_Utils_Type::T_INT,
8f3aca71 184 );
6a488035 185}
5e4f7f74 186
4d63cfde 187/**
dc64d047 188 * Adjust Metadata for Get action.
4d63cfde 189 *
0aa0303c
EM
190 * The metadata is used for setting defaults, documentation & validation.
191 *
cf470720 192 * @param array $params
b081365f 193 * Array of parameters determined by getfields.
4d63cfde
CW
194 */
195function _civicrm_api3_membership_get_spec(&$params) {
196 $params['membership_type_id']['api.aliases'] = array('membership_type');
5e4f7f74 197 $params['active_only'] = array(
b2ed8e73
CW
198 'title' => 'Active Only',
199 'description' => 'Only retrieve active memberships',
5e4f7f74
EM
200 'type' => CRM_Utils_Type::T_BOOLEAN,
201 );
4d63cfde
CW
202}
203
6a488035 204/**
244bbdd8 205 * Get contact Membership record.
6a488035
TO
206 *
207 * This api will return the membership records for the contacts
208 * having membership based on the relationship with the direct members.
209 *
2241036a 210 * @param array $params
cf470720 211 * Key/value pairs for contact_id and some.
6a488035
TO
212 * options affecting the desired results; has legacy support
213 * for just passing the contact_id itself as the argument
214 *
16b10e64
CW
215 * @return array
216 * Array of all found membership property values.
6a488035
TO
217 */
218function civicrm_api3_membership_get($params) {
4d63cfde 219 $activeOnly = $membershipTypeId = $membershipType = NULL;
6a488035
TO
220
221 $contactID = CRM_Utils_Array::value('contact_id', $params);
a73daeff
E
222 if (!empty($params['filters']) && is_array($params['filters']) && isset($params['filters']['is_current'])) {
223 $activeOnly = $params['filters']['is_current'];
224 unset($params['filters']['is_current']);
6a488035
TO
225 }
226 $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
d031c654 227 if ($activeOnly && empty($params['status_id'])) {
a73daeff 228 $params['status_id'] = array('IN' => CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent());
324116ad
SD
229 }
230
244bbdd8 231 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'Membership', 'get');
d031c654 232 if ($options['is_count']) {
a73daeff 233 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035 234 }
9af2925b 235 $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Membership');
6a488035 236
37eda84b 237 $return = $options['return'];
22e263ad 238 if (empty($membershipValues) ||
37eda84b 239 (!empty($return)
240 && !array_key_exists('related_contact_id', $return)
241 && !array_key_exists('relationship_name', $return)
242 )
243 ) {
244bbdd8 244 return civicrm_api3_create_success($membershipValues, $params, 'Membership', 'get');
6a488035
TO
245 }
246
7c550ca0 247 $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID);
244bbdd8 248 return civicrm_api3_create_success($members, $params, 'Membership', 'get');
37eda84b 249}
250
251/**
dc64d047
EM
252 * Perform api v2 custom behaviour.
253 *
37eda84b 254 * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
255 * membership a complicated return array is constructed. The original
256 * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
257 * is passed in as part of the reasonable expectation developers have that we will keep the api
258 * as stable as possible
259 *
cf470720
TO
260 * @param array $params
261 * Parameters passed into get function.
100fef9d 262 * @param int $membershipTypeId
46332f2b
EM
263 * @param $activeOnly
264 *
a6c01b45 265 * @return array
72b3a70c 266 * result for calling function
37eda84b 267 */
268function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) {
269 // get the membership for the given contact ID
270 $membershipParams = array('contact_id' => $params['contact_id']);
271 if ($membershipTypeId) {
272 $membershipParams['membership_type_id'] = $membershipTypeId;
273 }
274 $membershipValues = array();
275 CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly);
276 return $membershipValues;
277}
278
279
280/**
1747ab99 281 * Non-standard behaviour inherited from v2.
46332f2b 282 *
cf470720
TO
283 * @param array $params
284 * Parameters passed into get function.
46332f2b 285 * @param $membershipValues
100fef9d 286 * @param int $contactID
46332f2b 287 *
a6c01b45 288 * @return array
72b3a70c 289 * result for calling function
46332f2b 290 */
37eda84b 291function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) {
6a488035
TO
292 $relationships = array();
293 foreach ($membershipValues as $membershipId => $values) {
294 // populate the membership type name for the membership type id
295 $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']);
296
297 $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
298
a7488080 299 if (!empty($membershipType['relationship_type_id'])) {
6a488035
TO
300 $relationships[$membershipType['relationship_type_id']] = $membershipId;
301 }
302
303 // populating relationship type name.
304 $relationshipType = new CRM_Contact_BAO_RelationshipType();
305 $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType);
306 if ($relationshipType->find(TRUE)) {
307 $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b;
308 }
309
e9ff5391 310 _civicrm_api3_custom_data_get($membershipValues[$membershipId], CRM_Utils_Array::value('check_permissions', $params), 'Membership', $membershipId, NULL, $values['membership_type_id']);
6a488035
TO
311 }
312
313 $members = $membershipValues;
314
5e4f7f74 315 // Populating contacts in members array based on their relationship with direct members.
6a488035
TO
316 if (!empty($relationships)) {
317 foreach ($relationships as $relTypeId => $membershipId) {
318 // As members are not direct members, there should not be
319 // membership id in the result array.
320 unset($membershipValues[$membershipId]['id']);
321 $relationship = new CRM_Contact_BAO_Relationship();
322 $relationship->contact_id_b = $contactID;
323 $relationship->relationship_type_id = $relTypeId;
324 if ($relationship->find()) {
325 while ($relationship->fetch()) {
326 clone($relationship);
327 $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a;
328 $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a;
329 }
330 }
331
332 }
333 }
37eda84b 334 return $members;
6a488035 335}