3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * This api exposes CiviCRM membership contact records.
32 * @package CiviCRM_APIv3
36 * Adjust Metadata for Delete action.
38 * The metadata is used for setting defaults, documentation & validation.
40 * @param array $params
41 * Array of parameters determined by getfields.
43 function _civicrm_api3_membership_delete_spec(&$params) {
44 $params['preserve_contribution'] = array(
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
,
53 * Deletes an existing contact Membership.
55 * @param array $params
56 * Array array holding id - Id of the contact membership to be deleted.
61 function civicrm_api3_membership_delete($params) {
62 if (isset($params['preserve_contribution'])) {
63 if (CRM_Member_BAO_Membership
::del($params['id'], $params['preserve_contribution'])) {
64 return civicrm_api3_create_success(TRUE, $params);
67 throw new API_Exception(ts('Could not delete membership'));
71 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
76 * Create a Contact Membership.
78 * This API is used for creating a Membership for a contact.
79 * Required parameters : membership_type_id and status_id.
81 * @param array $params
82 * Array of name/value property values of civicrm_membership.
87 function civicrm_api3_membership_create($params) {
88 // check params for membership id during update
89 if (!empty($params['id']) && !isset($params['skipStatusCal'])) {
90 // Don't calculate status on existing membership - expect API use to pass them in
91 // or leave unchanged.
92 $params['skipStatusCal'] = 1;
95 // also check for status id if override is set (during add/update)
96 if (!empty($params['is_override']) && empty($params['status_id'])) {
97 return civicrm_api3_create_error('Status ID required');
102 _civicrm_api3_custom_format_params($params, $values, 'Membership');
103 $params = array_merge($params, $values);
105 // Fixme: This code belongs in the BAO
106 if (empty($params['id']) ||
!empty($params['num_terms'])) {
107 if (empty($params['id'])) {
108 $calcDates = CRM_Member_BAO_MembershipType
::getDatesForMembershipType(
109 $params['membership_type_id'],
110 CRM_Utils_Array
::value('join_date', $params),
111 CRM_Utils_Array
::value('start_date', $params),
112 CRM_Utils_Array
::value('end_date', $params),
113 CRM_Utils_Array
::value('num_terms', $params, 1)
117 $calcDates = CRM_Member_BAO_MembershipType
::getRenewalDatesForMembershipType(
120 CRM_Utils_Array
::value('membership_type_id', $params),
124 foreach (array('join_date', 'start_date', 'end_date') as $date) {
125 if (empty($params[$date]) && isset($calcDates[$date])) {
126 $params[$date] = $calcDates[$date];
131 // Fixme: This code belongs in the BAO
132 $action = CRM_Core_Action
::ADD
;
133 // we need user id during add mode
135 if (!empty($params['contact_id'])) {
136 $ids['userId'] = $params['contact_id'];
138 //for edit membership id should be present
139 if (!empty($params['id'])) {
140 $ids['membership'] = $params['id'];
141 $action = CRM_Core_Action
::UPDATE
;
143 //need to pass action to handle related memberships.
144 $params['action'] = $action;
146 if (empty($params['line_item']) && !empty($params['membership_type_id']) && empty($params['skipLineItem'])) {
147 CRM_Price_BAO_LineItem
::getLineItemArray($params, NULL, 'membership', $params['membership_type_id']);
150 $membershipBAO = CRM_Member_BAO_Membership
::create($params, $ids, TRUE);
152 if (array_key_exists('is_error', $membershipBAO)) {
153 // In case of no valid status for given dates, $membershipBAO
154 // is going to contain 'is_error' => "Error Message"
155 return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates'));
158 $membership = array();
159 _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id
]);
161 return civicrm_api3_create_success($membership, $params, 'Membership', 'create', $membershipBAO);
166 * Adjust Metadata for Create action.
168 * The metadata is used for setting defaults, documentation & validation.
170 * @param array $params
171 * Array of parameters determined by getfields.
173 function _civicrm_api3_membership_create_spec(&$params) {
174 $params['contact_id']['api.required'] = 1;
175 $params['membership_type_id']['api.required'] = 1;
176 $params['is_test']['api.default'] = 0;
177 $params['membership_type_id']['api.aliases'] = array('membership_type');
178 $params['status_id']['api.aliases'] = array('membership_status');
179 $params['skipStatusCal'] = array(
180 'title' => 'Skip status calculation',
181 'description' => 'By default this is 0 if id is not set and 1 if it is set.',
182 'type' => CRM_Utils_Type
::T_BOOLEAN
,
184 $params['num_terms'] = array(
185 'title' => 'Number of terms',
186 '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.',
187 'type' => CRM_Utils_Type
::T_INT
,
192 * Adjust Metadata for Get action.
194 * The metadata is used for setting defaults, documentation & validation.
196 * @param array $params
197 * Array of parameters determined by getfields.
199 function _civicrm_api3_membership_get_spec(&$params) {
200 $params['membership_type_id']['api.aliases'] = array('membership_type');
201 $params['active_only'] = array(
202 'title' => 'Active Only',
203 'description' => 'Only retrieve active memberships',
204 'type' => CRM_Utils_Type
::T_BOOLEAN
,
209 * Get contact Membership record.
211 * This api will return the membership records for the contacts
212 * having membership based on the relationship with the direct members.
214 * @param array $params
215 * Key/value pairs for contact_id and some.
216 * options affecting the desired results; has legacy support
217 * for just passing the contact_id itself as the argument
220 * Array of all found membership property values.
222 function civicrm_api3_membership_get($params) {
223 $activeOnly = $membershipTypeId = $membershipType = NULL;
225 $contactID = CRM_Utils_Array
::value('contact_id', $params);
226 if (!empty($params['filters']) && is_array($params['filters']) && isset($params['filters']['is_current'])) {
227 $activeOnly = $params['filters']['is_current'];
228 unset($params['filters']['is_current']);
230 $activeOnly = CRM_Utils_Array
::value('active_only', $params, $activeOnly);
231 if ($activeOnly && empty($params['status_id'])) {
232 $params['status_id'] = array('IN' => CRM_Member_BAO_MembershipStatus
::getMembershipStatusCurrent());
235 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'Membership', 'get');
236 if ($options['is_count']) {
237 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
239 $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Membership');
241 $return = $options['return'];
242 if (empty($membershipValues) ||
244 && !array_key_exists('related_contact_id', $return)
245 && !array_key_exists('relationship_name', $return)
248 return civicrm_api3_create_success($membershipValues, $params, 'Membership', 'get');
251 $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID);
252 return civicrm_api3_create_success($members, $params, 'Membership', 'get');
256 * Perform api v2 custom behaviour.
258 * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
259 * membership a complicated return array is constructed. The original
260 * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
261 * is passed in as part of the reasonable expectation developers have that we will keep the api
262 * as stable as possible
264 * @param array $params
265 * Parameters passed into get function.
266 * @param int $membershipTypeId
270 * result for calling function
272 function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) {
273 // get the membership for the given contact ID
274 $membershipParams = array('contact_id' => $params['contact_id']);
275 if ($membershipTypeId) {
276 $membershipParams['membership_type_id'] = $membershipTypeId;
278 $membershipValues = array();
279 CRM_Member_BAO_Membership
::getValues($membershipParams, $membershipValues, $activeOnly);
280 return $membershipValues;
285 * Non-standard behaviour inherited from v2.
287 * @param array $params
288 * Parameters passed into get function.
289 * @param $membershipValues
290 * @param int $contactID
293 * result for calling function
295 function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) {
296 $relationships = array();
297 foreach ($membershipValues as $membershipId => $values) {
298 // populate the membership type name for the membership type id
299 $membershipType = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($values['membership_type_id']);
301 $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
303 if (!empty($membershipType['relationship_type_id'])) {
304 $relationships[$membershipType['relationship_type_id']] = $membershipId;
307 // populating relationship type name.
308 $relationshipType = new CRM_Contact_BAO_RelationshipType();
309 $relationshipType->id
= CRM_Utils_Array
::value('relationship_type_id', $membershipType);
310 if ($relationshipType->find(TRUE)) {
311 $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b
;
314 _civicrm_api3_custom_data_get($membershipValues[$membershipId], CRM_Utils_Array
::value('check_permissions', $params), 'Membership', $membershipId, NULL, $values['membership_type_id']);
317 $members = $membershipValues;
319 // Populating contacts in members array based on their relationship with direct members.
320 if (!empty($relationships)) {
321 foreach ($relationships as $relTypeId => $membershipId) {
322 // As members are not direct members, there should not be
323 // membership id in the result array.
324 unset($membershipValues[$membershipId]['id']);
325 $relationship = new CRM_Contact_BAO_Relationship();
326 $relationship->contact_id_b
= $contactID;
327 $relationship->relationship_type_id
= $relTypeId;
328 if ($relationship->find()) {
329 while ($relationship->fetch()) {
330 clone($relationship);
331 $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a
;
332 $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a
;