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