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