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