4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
31 * File for the CiviCRM APIv3 membership contact functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Membership
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: MembershipContact.php 30171 2010-10-14 09:11:27Z mover $
41 * Deletes an existing contact membership
43 * This API is used for deleting a contact membership
45 * @param $params array array holding id - Id of the contact membership to be deleted
47 * @return array api result
48 * {@getfields membership_delete}
51 function civicrm_api3_membership_delete($params) {
52 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
56 * Create a Contact Membership
58 * This API is used for creating a Membership for a contact.
59 * Required parameters : membership_type_id and status_id.
61 * @param array $params an associative array of name/value property values of civicrm_membership
63 * @return array of newly created membership property values.
64 * {@getfields membership_create}
67 function civicrm_api3_membership_create($params) {
68 // check params for membership id during update
69 if (!empty($params['id']) && !isset($params['skipStatusCal'])) {
70 //don't calculate status on exisiting membership - expect API use to pass them in
72 $params['skipStatusCal'] = 1;
75 // also check for status id if override is set (during add/update)
76 if (!empty($params['is_override']) && empty($params['status_id'])) {
77 return civicrm_api3_create_error('Status ID required');
82 _civicrm_api3_custom_format_params($params, $values, 'Membership');
83 $params = array_merge($params, $values);
85 // Fixme: This code belongs in the BAO
86 if (empty($params['id']) ||
!empty($params['num_terms'])) {
87 if (empty($params['id'])) {
88 $calcDates = CRM_Member_BAO_MembershipType
::getDatesForMembershipType(
89 $params['membership_type_id'],
90 CRM_Utils_Array
::value('join_date', $params),
91 CRM_Utils_Array
::value('start_date', $params),
92 CRM_Utils_Array
::value('end_date', $params),
93 CRM_Utils_Array
::value('num_terms', $params, 1)
97 $calcDates = CRM_Member_BAO_MembershipType
::getRenewalDatesForMembershipType(
100 CRM_Utils_Array
::value('membership_type_id', $params),
104 foreach (array('join_date', 'start_date', 'end_date') as $date) {
105 if (empty($params[$date]) && isset($calcDates[$date])) {
106 $params[$date] = $calcDates[$date];
111 // Fixme: This code belongs in the BAO
112 $action = CRM_Core_Action
::ADD
;
113 // we need user id during add mode
115 if(CRM_Utils_Array
::value('contact_id', $params)) {
116 $ids['userId'] = $params['contact_id'];
118 //for edit membership id should be present
119 if (CRM_Utils_Array
::value('id', $params)) {
120 $ids['membership'] = $params['id'];
121 $action = CRM_Core_Action
::UPDATE
;
123 //need to pass action to handle related memberships.
124 $params['action'] = $action;
126 $membershipBAO = CRM_Member_BAO_Membership
::create($params, $ids, TRUE);
128 if (array_key_exists('is_error', $membershipBAO)) {
129 // In case of no valid status for given dates, $membershipBAO
130 // is going to contain 'is_error' => "Error Message"
131 return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates'));
134 $membership = array();
135 _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id
]);
137 return civicrm_api3_create_success($membership, $params, 'membership', 'create', $membershipBAO);
142 * Adjust Metadata for Create action
144 * The metadata is used for setting defaults, documentation & validation
145 * @param array $params array or parameters determined by getfields
147 function _civicrm_api3_membership_create_spec(&$params) {
148 $params['contact_id']['api.required'] = 1;
149 $params['membership_type_id']['api.required'] = 1;
150 $params['is_test']['api.default'] = 0;
151 $params['membership_type_id']['api.aliases'] = array('membership_type');
152 $params['status_id']['api.aliases'] = array('membership_status');
153 $params['skipStatusCal'] = array(
154 'title' => 'Skip status calculation. By default this is 0 if id is not set and 1 if it is set.'
156 $params['num_terms'] = array(
157 '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.'
161 * Adjust Metadata for Get action
163 * The metadata is used for setting defaults, documentation & validation
164 * @param array $params array or parameters determined by getfields
166 function _civicrm_api3_membership_get_spec(&$params) {
167 $params['membership_type_id']['api.aliases'] = array('membership_type');
171 * Get contact membership record.
173 * This api will return the membership records for the contacts
174 * having membership based on the relationship with the direct members.
176 * @param Array $params key/value pairs for contact_id and some
177 * options affecting the desired results; has legacy support
178 * for just passing the contact_id itself as the argument
180 * @return Array of all found membership property values.
182 * @todo needs some love - basically only a get for a given contact right now
183 * {@getfields membership_get}
185 function civicrm_api3_membership_get($params) {
186 $activeOnly = $membershipTypeId = $membershipType = NULL;
188 $contactID = CRM_Utils_Array
::value('contact_id', $params);
189 if (!empty($params['filters']) && is_array($params['filters'])) {
190 $activeOnly = CRM_Utils_Array
::value('is_current', $params['filters'], FALSE);
192 $activeOnly = CRM_Utils_Array
::value('active_only', $params, $activeOnly);
194 if (CRM_Utils_Array
::value('contact_id', $params)) {
195 $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $contactID, $membershipTypeId, $activeOnly );
198 //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise
199 $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE);
202 if (empty($membershipValues)) {
203 // No results is NOT an error!
204 return civicrm_api3_create_success($membershipValues, $params);
207 $relationships = array();
208 foreach ($membershipValues as $membershipId => $values) {
209 // populate the membership type name for the membership type id
210 $membershipType = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($values['membership_type_id']);
212 $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
214 if (CRM_Utils_Array
::value('relationship_type_id', $membershipType)) {
215 $relationships[$membershipType['relationship_type_id']] = $membershipId;
218 // populating relationship type name.
219 $relationshipType = new CRM_Contact_BAO_RelationshipType();
220 $relationshipType->id
= CRM_Utils_Array
::value('relationship_type_id', $membershipType);
221 if ($relationshipType->find(TRUE)) {
222 $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b
;
225 _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']);
228 $members = $membershipValues;
230 // populating contacts in members array based on their relationship with direct members.
231 if (!empty($relationships)) {
232 foreach ($relationships as $relTypeId => $membershipId) {
233 // As members are not direct members, there should not be
234 // membership id in the result array.
235 unset($membershipValues[$membershipId]['id']);
236 $relationship = new CRM_Contact_BAO_Relationship();
237 $relationship->contact_id_b
= $contactID;
238 $relationship->relationship_type_id
= $relTypeId;
239 if ($relationship->find()) {
240 while ($relationship->fetch()) {
241 clone($relationship);
242 $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a
;
243 $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a
;
250 return civicrm_api3_create_success($members, $params, 'membership', 'get');
254 * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
255 * membership a complicated return array is constructed. The original
256 * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
257 * is passed in as part of the reasonable expectation developers have that we will keep the api
258 * as stable as possible
260 * @param array $params parameters passed into get function
261 * @return array result for calling function
263 function _civicrm_api3_membership_get_customv2behaviour(&$params, $contactID, $membershipTypeId, $activeOnly) {
264 // get the membership for the given contact ID
265 $membershipParams = array('contact_id' => $contactID);
266 if ($membershipTypeId) {
267 $membershipParams['membership_type_id'] = $membershipTypeId;
269 $membershipValues = array();
270 CRM_Member_BAO_Membership
::getValues($membershipParams, $membershipValues, $activeOnly);
271 return $membershipValues;