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