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