3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * Deletes an existing contact Membership.
38 * @param array $params
39 * Array array holding id - Id of the contact membership to be deleted.
44 function civicrm_api3_membership_delete($params) {
45 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
49 * Create a Contact Membership.
51 * This API is used for creating a Membership for a contact.
52 * Required parameters : membership_type_id and status_id.
54 * @param array $params
55 * Array of name/value property values of civicrm_membership.
60 function civicrm_api3_membership_create($params) {
61 // check params for membership id during update
62 if (!empty($params['id']) && !isset($params['skipStatusCal'])) {
63 // Don't calculate status on existing membership - expect API use to pass them in
64 // or leave unchanged.
65 $params['skipStatusCal'] = 1;
68 // also check for status id if override is set (during add/update)
69 if (!empty($params['is_override']) && empty($params['status_id'])) {
70 return civicrm_api3_create_error('Status ID required');
75 _civicrm_api3_custom_format_params($params, $values, 'Membership');
76 $params = array_merge($params, $values);
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)
90 $calcDates = CRM_Member_BAO_MembershipType
::getRenewalDatesForMembershipType(
93 CRM_Utils_Array
::value('membership_type_id', $params),
97 foreach (array('join_date', 'start_date', 'end_date') as $date) {
98 if (empty($params[$date]) && isset($calcDates[$date])) {
99 $params[$date] = $calcDates[$date];
104 // Fixme: This code belongs in the BAO
105 $action = CRM_Core_Action
::ADD
;
106 // we need user id during add mode
108 if (!empty($params['contact_id'])) {
109 $ids['userId'] = $params['contact_id'];
111 //for edit membership id should be present
112 if (!empty($params['id'])) {
113 $ids['membership'] = $params['id'];
114 $action = CRM_Core_Action
::UPDATE
;
116 //need to pass action to handle related memberships.
117 $params['action'] = $action;
119 if (empty($params['line_item']) && !empty($params['membership_type_id'])) {
120 CRM_Price_BAO_LineItem
::getLineItemArray($params, NULL, 'membership', $params['membership_type_id']);
123 $membershipBAO = CRM_Member_BAO_Membership
::create($params, $ids, TRUE);
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'));
131 $membership = array();
132 _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id
]);
134 return civicrm_api3_create_success($membership, $params, 'Membership', 'create', $membershipBAO);
139 * Adjust Metadata for Create action.
141 * The metadata is used for setting defaults, documentation & validation.
143 * @param array $params
144 * Array of parameters determined by getfields.
146 function _civicrm_api3_membership_create_spec(&$params) {
147 $params['contact_id']['api.required'] = 1;
148 $params['membership_type_id']['api.required'] = 1;
149 $params['is_test']['api.default'] = 0;
150 $params['membership_type_id']['api.aliases'] = array('membership_type');
151 $params['status_id']['api.aliases'] = array('membership_status');
152 $params['skipStatusCal'] = array(
153 'title' => 'Skip status calculation',
154 'description' => 'By default this is 0 if id is not set and 1 if it is set.',
155 'type' => CRM_Utils_Type
::T_BOOLEAN
,
157 $params['num_terms'] = array(
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.',
160 'type' => CRM_Utils_Type
::T_INT
,
165 * Adjust Metadata for Get action.
167 * The metadata is used for setting defaults, documentation & validation.
169 * @param array $params
170 * Array of parameters determined by getfields.
172 function _civicrm_api3_membership_get_spec(&$params) {
173 $params['membership_type_id']['api.aliases'] = array('membership_type');
174 $params['active_only'] = array(
175 'title' => 'Active Only',
176 'description' => 'Only retrieve active memberships',
177 'type' => CRM_Utils_Type
::T_BOOLEAN
,
182 * Get contact Membership record.
184 * This api will return the membership records for the contacts
185 * having membership based on the relationship with the direct members.
187 * @param array $params
188 * Key/value pairs for contact_id and some.
189 * options affecting the desired results; has legacy support
190 * for just passing the contact_id itself as the argument
193 * Array of all found membership property values.
195 function civicrm_api3_membership_get($params) {
196 $activeOnly = $membershipTypeId = $membershipType = NULL;
198 $contactID = CRM_Utils_Array
::value('contact_id', $params);
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']);
203 $activeOnly = CRM_Utils_Array
::value('active_only', $params, $activeOnly);
204 if ($activeOnly && empty($params['status_id'])) {
205 $params['status_id'] = array('IN' => CRM_Member_BAO_MembershipStatus
::getMembershipStatusCurrent());
208 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'Membership', 'get');
209 if ($options['is_count']) {
210 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
212 $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Membership');
214 $return = $options['return'];
215 if (empty($membershipValues) ||
217 && !array_key_exists('related_contact_id', $return)
218 && !array_key_exists('relationship_name', $return)
221 return civicrm_api3_create_success($membershipValues, $params, 'Membership', 'get');
224 $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID);
225 return civicrm_api3_create_success($members, $params, 'Membership', 'get');
229 * Perform api v2 custom behaviour.
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
237 * @param array $params
238 * Parameters passed into get function.
239 * @param int $membershipTypeId
243 * result for calling function
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;
251 $membershipValues = array();
252 CRM_Member_BAO_Membership
::getValues($membershipParams, $membershipValues, $activeOnly);
253 return $membershipValues;
258 * Non-standard behaviour inherited from v2.
260 * @param array $params
261 * Parameters passed into get function.
262 * @param $membershipValues
263 * @param int $contactID
266 * result for calling function
268 function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) {
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']);
274 $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
276 if (!empty($membershipType['relationship_type_id'])) {
277 $relationships[$membershipType['relationship_type_id']] = $membershipId;
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
;
287 _civicrm_api3_custom_data_get($membershipValues[$membershipId], CRM_Utils_Array
::value('check_permissions', $params), 'Membership', $membershipId, NULL, $values['membership_type_id']);
290 $members = $membershipValues;
292 // Populating contacts in members array based on their relationship with direct members.
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
;