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