Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-07-14-22-39-05
[civicrm-core.git] / api / v3 / Membership.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * File for the CiviCRM APIv3 membership contact functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Membership
35 *
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: MembershipContact.php 30171 2010-10-14 09:11:27Z mover $
38 */
39
40 /**
41 * Deletes an existing contact membership
42 *
43 * This API is used for deleting a contact membership
44 *
45 * @param $params array array holding id - Id of the contact membership to be deleted
46 *
47 * @return array api result
48 * {@getfields membership_delete}
49 * @access public
50 */
51 function civicrm_api3_membership_delete($params) {
52
53 // membershipID should be numeric
54 // this check should be done @ wrapper level
55 if (!is_numeric($params['id'])) {
56 return civicrm_api3_create_error('Input parameter should be numeric');
57 }
58
59 CRM_Member_BAO_Membership::deleteRelatedMemberships($params['id']);
60
61 $membership = new CRM_Member_BAO_Membership();
62 $result = $membership->deleteMembership($params['id']);
63
64 return $result ? civicrm_api3_create_success() : civicrm_api3_create_error('Error while deleting Membership');
65
66 }
67
68 /**
69 * modify metadata
70 */
71 function _civicrm_api3_membership_delete_spec(&$params) {
72 // set as not required as membership_id also acceptable & no either/or std yet
73 $params['id']['api.required'] = 1;
74 $params['id']['api.aliases'] = array('membership_id');
75 }
76
77 /**
78 * Create a Contact Membership
79 *
80 * This API is used for creating a Membership for a contact.
81 * Required parameters : membership_type_id and status_id.
82 *
83 * @param array $params an associative array of name/value property values of civicrm_membership
84 *
85 * @return array of newly created membership property values.
86 * {@getfields membership_create}
87 * @access public
88 */
89 function civicrm_api3_membership_create($params) {
90 // @todo shouldn't be required - should be handling by api.aliases & api.required in _spec
91 civicrm_api3_verify_one_mandatory($params, NULL, array('membership_type_id', 'membership_type'));
92 // check params for membership id during update
93 if (CRM_Utils_Array::value('id', $params) && !isset($params['skipStatusCal'])) {
94 //don't calculate dates on exisiting membership - expect API use to pass them in
95 // or leave unchanged
96 $params['skipStatusCal'] = 1;
97 }
98 else {
99 // also check for status id if override is set (during add/update)
100 if (isset($params['is_override']) &&
101 !CRM_Utils_Array::value('status_id', $params)
102 ) {
103 return civicrm_api3_create_error('Status ID required');
104 }
105 }
106
107
108 $values = array();
109 $error = _civicrm_api3_membership_format_params($params, $values);
110
111 if (civicrm_error($error)) {
112 return $error;
113 }
114 _civicrm_api3_custom_format_params($params, $values, 'Membership');
115 $params = array_merge($params, $values);
116
117
118 $action = CRM_Core_Action::ADD;
119 // we need user id during add mode
120 $ids = array ();
121 if(CRM_Utils_Array::value('contact_id',$params)){
122 $ids['userId'] = $params['contact_id'];
123 }
124 //for edit membership id should be present
125 if (CRM_Utils_Array::value('id', $params)) {
126 $ids['membership'] = $params['id'];
127 $action = CRM_Core_Action::UPDATE;
128 }
129
130 //need to pass action to handle related memberships.
131 $params['action'] = $action;
132
133
134 $membershipBAO = CRM_Member_BAO_Membership::create($params, $ids, TRUE);
135
136 if (array_key_exists('is_error', $membershipBAO)) {
137 // In case of no valid status for given dates, $membershipBAO
138 // is going to contain 'is_error' => "Error Message"
139 return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates'));
140 }
141
142 $membership = array();
143 _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id]);
144
145 return civicrm_api3_create_success($membership, $params, 'membership', 'create', $membershipBAO);
146
147 }
148
149 /**
150 * Adjust Metadata for Create action
151 *
152 * The metadata is used for setting defaults, documentation & validation
153 * @param array $params array or parameters determined by getfields
154 */
155 function _civicrm_api3_membership_create_spec(&$params) {
156 $params['contact_id']['api.required'] = 1;
157 $params['skipStatusCal'] = array('title' => 'skip status calculation. By default this is 0 if id is not set and 1 if it is set');
158 }
159 /**
160 * Get contact membership record.
161 *
162 * This api will return the membership records for the contacts
163 * having membership based on the relationship with the direct members.
164 *
165 * @param Array $params key/value pairs for contact_id and some
166 * options affecting the desired results; has legacy support
167 * for just passing the contact_id itself as the argument
168 *
169 * @return Array of all found membership property values.
170 * @access public
171 * @todo needs some love - basically only a get for a given contact right now
172 * {@getfields membership_get}
173 */
174 function civicrm_api3_membership_get($params) {
175 $contactID = $activeOnly = $membershipTypeId = $membershipType = NULL;
176
177 $contactID = CRM_Utils_Array::value('contact_id', $params);
178 if (is_array(CRM_Utils_Array::value('filters', $params)) && !empty($params['filters'])) {
179 $activeOnly = CRM_Utils_Array::value('is_current', $params['filters'], FALSE);
180 }
181 $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
182 //@todo replace this by handling in API layer - we should have enough info to do this
183 // between pseudoconstant & fk - see comments in format_params
184 $membershipTypeId = CRM_Utils_Array::value('membership_type_id', $params);
185 if (!$membershipTypeId) {
186 $membershipType = CRM_Utils_Array::value('membership_type', $params);
187 if ($membershipType) {
188 $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType',
189 $membershipType, 'id', 'name'
190 );
191 }
192 }
193 if(CRM_Utils_Array::value('contact_id',$params)){
194 $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $contactID, $membershipTypeId, $activeOnly );
195 }
196 else{
197 //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise
198 $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
199 }
200
201
202 if (empty($membershipValues)) {
203 # No results is NOT an error!
204 return civicrm_api3_create_success($membershipValues, $params);
205 }
206
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']);
211
212 $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
213
214 if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) {
215 $relationships[$membershipType['relationship_type_id']] = $membershipId;
216 }
217
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;
223 }
224
225 _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']);
226 }
227
228 $members = $membershipValues;
229
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;
244 }
245 }
246
247 }
248 }
249
250 return civicrm_api3_create_success($members, $params, 'membership', 'get');
251
252 }
253
254
255 /**
256 * @deprecated
257 * Deprecated function to support membership create. Do not call this. It will be removed in favour of
258 * wrapper layer formatting
259 * take the input parameter list as specified in the data model and
260 * convert it into the same format that we use in QF and BAO object
261 *
262 * @param array $params Associative array of property name/value
263 * pairs to insert in new contact.
264 * @param array $values The reformatted properties that we can use internally
265 *
266 * @param array $create Is the formatted Values array going to
267 * be used for CRM_Member_BAO_Membership:create()
268 *
269 * @return array|error
270 * @access public
271 */
272 function _civicrm_api3_membership_format_params($params, &$values, $create = FALSE) {
273
274 $fields = CRM_Member_DAO_Membership::fields();
275 _civicrm_api3_store_values($fields, $params, $values);
276
277 foreach ($params as $key => $value) {
278 // ignore empty values or empty arrays etc
279 if (CRM_Utils_System::isNull($value)) {
280 continue;
281 }
282
283 switch ($key) {
284
285 case 'membership_type':
286 // @todo we still need to adequately figure out how to handle this @ the API layer.
287 // it is an FK & a pseudoconstant - we should probably alias it onto membership_type_id &
288 // then in the validate_integer function do an if(!is_integer && $fieldInfo['pseudoconstant) look
289 // up pseudoconstant & flip it over. By the time it hits api it will be a valid membership_type & handling @
290 // api layer not required
291 $membershipTypeId = CRM_Utils_Array::key(ucfirst($value),
292 CRM_Member_PseudoConstant::membershipType()
293 );
294 if ($membershipTypeId) {
295 if (CRM_Utils_Array::value('membership_type_id', $values) &&
296 $membershipTypeId != $values['membership_type_id']
297 ) {
298 return civicrm_api3_create_error('Mismatched membership Type and Membership Type Id');
299 }
300 }
301 else {
302 return civicrm_api3_create_error('Invalid Membership Type');
303 }
304 $values['membership_type_id'] = $membershipTypeId;
305 break;
306 default:
307 break;
308 }
309 }
310
311 return NULL;
312 }
313
314 /**
315 * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
316 * membership a complicated return array is constructed. The original
317 * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
318 * is passed in as part of the reasonable expectation developers have that we will keep the api
319 * as stable as possible
320 *
321 * @param array $params parameters passed into get function
322 * @return array result for calling function
323 */
324 function _civicrm_api3_membership_get_customv2behaviour(&$params, $contactID, $membershipTypeId, $activeOnly ){
325 // get the membership for the given contact ID
326 $membershipParams = array( 'contact_id' => $contactID );
327 if ( $membershipTypeId ) {
328 $membershipParams['membership_type_id'] = $membershipTypeId;
329 }
330 $membershipValues = array();
331 CRM_Member_BAO_Membership::getValues( $membershipParams, $membershipValues, $activeOnly );
332 return $membershipValues;
333 }