5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
32 * File for the CiviCRM APIv3 membership contact functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Membership
37 * @copyright CiviCRM LLC (c) 2004-2013
38 * @version $Id: MembershipContact.php 30171 2010-10-14 09:11:27Z mover $
42 * Files required for this package
45 require_once 'CRM/Utils/Rule.php';
46 require_once 'CRM/Utils/Array.php';
49 * Deletes an existing contact membership
51 * This API is used for deleting a contact membership
53 * @param $params array array holding id - Id of the contact membership to be deleted
55 * @return array api result
56 * {@getfields membership_delete}
59 function civicrm_api3_membership_delete($params) {
61 // membershipID should be numeric
62 // this check should be done @ wrapper level
63 if (!is_numeric($params['id'])) {
64 return civicrm_api3_create_error('Input parameter should be numeric');
67 CRM_Member_BAO_Membership
::deleteRelatedMemberships($params['id']);
69 $membership = new CRM_Member_BAO_Membership();
70 $result = $membership->deleteMembership($params['id']);
72 return $result ?
civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting Membership');
79 function _civicrm_api3_membership_delete_spec(&$params) {
80 // set as not required as membership_id also acceptable & no either/or std yet
81 $params['id']['api.required'] = 1;
82 $params['id']['api.aliases'] = array('membership_id');
86 * Create a Contact Membership
88 * This API is used for creating a Membership for a contact.
89 * Required parameters : membership_type_id and status_id.
91 * @param array $params an associative array of name/value property values of civicrm_membership
93 * @return array of newly created membership property values.
94 * {@getfields membership_create}
97 function civicrm_api3_membership_create($params) {
98 // @todo shouldn't be required - should be handling by api.aliases & api.required in _spec
99 civicrm_api3_verify_one_mandatory($params, NULL, array('membership_type_id', 'membership_type'));
100 // check params for membership id during update
101 if (CRM_Utils_Array
::value('id', $params) && !isset($params['skipStatusCal'])) {
102 //don't calculate dates on exisiting membership - expect API use to pass them in
103 // or leave unchanged
104 $params['skipStatusCal'] = 1;
107 // also check for status id if override is set (during add/update)
108 if (isset($params['is_override']) &&
109 !CRM_Utils_Array
::value('status_id', $params)
111 return civicrm_api3_create_error('Status ID required');
117 $error = _civicrm_api3_membership_format_params($params, $values);
119 if (civicrm_error($error)) {
122 _civicrm_api3_custom_format_params($params, $values, 'Membership');
123 $params = array_merge($params, $values);
126 $action = CRM_Core_Action
::ADD
;
127 // we need user id during add mode
129 if(CRM_Utils_Array
::value('contact_id',$params)){
130 $ids['userId'] = $params['contact_id'];
132 //for edit membership id should be present
133 if (CRM_Utils_Array
::value('id', $params)) {
134 $ids['membership'] = $params['id'];
135 $action = CRM_Core_Action
::UPDATE
;
138 //need to pass action to handle related memberships.
139 $params['action'] = $action;
142 $membershipBAO = CRM_Member_BAO_Membership
::create($params, $ids, TRUE);
144 if (array_key_exists('is_error', $membershipBAO)) {
145 // In case of no valid status for given dates, $membershipBAO
146 // is going to contain 'is_error' => "Error Message"
147 return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates'));
150 $membership = array();
151 _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id
]);
153 return civicrm_api3_create_success($membership, $params, 'membership', 'create', $membershipBAO);
157 * Adjust Metadata for Create action
159 * The metadata is used for setting defaults, documentation & validation
160 * @param array $params array or parameters determined by getfields
162 function _civicrm_api3_membership_create_spec(&$params) {
163 $params['contact_id']['api.required'] = 1;
164 $params['skipStatusCal'] = array('title' => 'skip status calculation. By default this is 0 if id is not set and 1 if it is set');
167 * Get contact membership record.
169 * This api will return the membership records for the contacts
170 * having membership based on the relationship with the direct members.
172 * @param Array $params key/value pairs for contact_id and some
173 * options affecting the desired results; has legacy support
174 * for just passing the contact_id itself as the argument
176 * @return Array of all found membership property values.
178 * @todo needs some love - basically only a get for a given contact right now
179 * {@getfields membership_get}
181 function civicrm_api3_membership_get($params) {
182 $contactID = $activeOnly = $membershipTypeId = $membershipType = NULL;
184 $contactID = CRM_Utils_Array
::value('contact_id', $params);
185 if (is_array(CRM_Utils_Array
::value('filters', $params)) && !empty($params['filters'])) {
186 $activeOnly = CRM_Utils_Array
::value('is_current', $params['filters'], FALSE);
188 $activeOnly = CRM_Utils_Array
::value('active_only', $params, $activeOnly);
189 //@todo replace this by handling in API layer - we should have enough info to do this
190 // between pseudoconstant & fk - see comments in format_params
191 $membershipTypeId = CRM_Utils_Array
::value('membership_type_id', $params);
192 if (!$membershipTypeId) {
193 $membershipType = CRM_Utils_Array
::value('membership_type', $params);
194 if ($membershipType) {
195 $membershipTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
196 $membershipType, 'id', 'name'
200 if(CRM_Utils_Array
::value('contact_id',$params)){
201 $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $contactID, $membershipTypeId, $activeOnly );
204 //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise
205 $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE);
209 if (empty($membershipValues)) {
210 # No results is NOT an error!
211 return civicrm_api3_create_success($membershipValues, $params);
214 $relationships = array();
215 foreach ($membershipValues as $membershipId => $values) {
216 // populate the membership type name for the membership type id
217 $membershipType = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($values['membership_type_id']);
219 $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
221 if (CRM_Utils_Array
::value('relationship_type_id', $membershipType)) {
222 $relationships[$membershipType['relationship_type_id']] = $membershipId;
225 // populating relationship type name.
226 $relationshipType = new CRM_Contact_BAO_RelationshipType();
227 $relationshipType->id
= CRM_Utils_Array
::value('relationship_type_id', $membershipType);
228 if ($relationshipType->find(TRUE)) {
229 $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b
;
232 _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']);
235 $members = $membershipValues;
237 // populating contacts in members array based on their relationship with direct members.
238 if (!empty($relationships)) {
239 foreach ($relationships as $relTypeId => $membershipId) {
240 // As members are not direct members, there should not be
241 // membership id in the result array.
242 unset($membershipValues[$membershipId]['id']);
243 $relationship = new CRM_Contact_BAO_Relationship();
244 $relationship->contact_id_b
= $contactID;
245 $relationship->relationship_type_id
= $relTypeId;
246 if ($relationship->find()) {
247 while ($relationship->fetch()) {
248 clone($relationship);
249 $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a
;
250 $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a
;
257 return civicrm_api3_create_success($members, $params, 'membership', 'get');
264 * Deprecated function to support membership create. Do not call this. It will be removed in favour of
265 * wrapper layer formatting
266 * take the input parameter list as specified in the data model and
267 * convert it into the same format that we use in QF and BAO object
269 * @param array $params Associative array of property name/value
270 * pairs to insert in new contact.
271 * @param array $values The reformatted properties that we can use internally
273 * @param array $create Is the formatted Values array going to
274 * be used for CRM_Member_BAO_Membership:create()
276 * @return array|error
279 function _civicrm_api3_membership_format_params($params, &$values, $create = FALSE) {
281 $fields = CRM_Member_DAO_Membership
::fields();
282 _civicrm_api3_store_values($fields, $params, $values);
284 foreach ($params as $key => $value) {
285 // ignore empty values or empty arrays etc
286 if (CRM_Utils_System
::isNull($value)) {
292 case 'membership_type':
293 // @todo we still need to adequately figure out how to handle this @ the API layer.
294 // it is an FK & a pseudoconstant - we should probably alias it onto membership_type_id &
295 // then in the validate_integer function do an if(!is_integer && $fieldInfo['pseudoconstant) look
296 // up pseudoconstant & flip it over. By the time it hits api it will be a valid membership_type & handling @
297 // api layer not required
298 $membershipTypeId = CRM_Utils_Array
::key(ucfirst($value),
299 CRM_Member_PseudoConstant
::membershipType()
301 if ($membershipTypeId) {
302 if (CRM_Utils_Array
::value('membership_type_id', $values) &&
303 $membershipTypeId != $values['membership_type_id']
305 return civicrm_api3_create_error('Mismatched membership Type and Membership Type Id');
309 return civicrm_api3_create_error('Invalid Membership Type');
311 $values['membership_type_id'] = $membershipTypeId;
321 * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
322 * membership a complicated return array is constructed. The original
323 * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
324 * is passed in as part of the reasonable expectation developers have that we will keep the api
325 * as stable as possible
327 * @param array $params parameters passed into get function
328 * @return array result for calling function
330 function _civicrm_api3_membership_get_customv2behaviour(&$params, $contactID, $membershipTypeId, $activeOnly ){
331 // get the membership for the given contact ID
332 require_once 'CRM/Member/BAO/Membership.php';
333 $membershipParams = array( 'contact_id' => $contactID );
334 if ( $membershipTypeId ) {
335 $membershipParams['membership_type_id'] = $membershipTypeId;
337 $membershipValues = array();
338 CRM_Member_BAO_Membership
::getValues( $membershipParams, $membershipValues, $activeOnly );
339 return $membershipValues;