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