Code cleanup - Batch #9
[civicrm-core.git] / api / v3 / Membership.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * File for the CiviCRM APIv3 membership contact functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_Membership
34 *
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * @version $Id: MembershipContact.php 30171 2010-10-14 09:11:27Z mover $
37 */
38
39 /**
40 * Deletes an existing contact membership
41 *
42 * This API is used for deleting a contact membership
43 *
44 * @param array $params
45 * Array array holding id - Id of the contact membership to be deleted.
46 *
47 * @return array
48 * api result
49 * {@getfields membership_delete}
50 */
51 function civicrm_api3_membership_delete($params) {
52 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
53 }
54
55 /**
56 * Create a Contact Membership
57 *
58 * This API is used for creating a Membership for a contact.
59 * Required parameters : membership_type_id and status_id.
60 *
61 * @param array $params
62 * An associative array of name/value property values of civicrm_membership.
63 *
64 * @return array
65 * Array of newly created membership property values.
66 * {@getfields membership_create}
67 */
68 function civicrm_api3_membership_create($params) {
69 // check params for membership id during update
70 if (!empty($params['id']) && !isset($params['skipStatusCal'])) {
71 //don't calculate status on exisiting membership - expect API use to pass them in
72 // or leave unchanged
73 $params['skipStatusCal'] = 1;
74 }
75 else {
76 // also check for status id if override is set (during add/update)
77 if (!empty($params['is_override']) && empty($params['status_id'])) {
78 return civicrm_api3_create_error('Status ID required');
79 }
80 }
81
82 $values = array();
83 _civicrm_api3_custom_format_params($params, $values, 'Membership');
84 $params = array_merge($params, $values);
85
86 // Fixme: This code belongs in the BAO
87 if (empty($params['id']) || !empty($params['num_terms'])) {
88 if (empty($params['id'])) {
89 $calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType(
90 $params['membership_type_id'],
91 CRM_Utils_Array::value('join_date', $params),
92 CRM_Utils_Array::value('start_date', $params),
93 CRM_Utils_Array::value('end_date', $params),
94 CRM_Utils_Array::value('num_terms', $params, 1)
95 );
96 }
97 else {
98 $calcDates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType(
99 $params['id'],
100 NULL,
101 CRM_Utils_Array::value('membership_type_id', $params),
102 $params['num_terms']
103 );
104 }
105 foreach (array('join_date', 'start_date', 'end_date') as $date) {
106 if (empty($params[$date]) && isset($calcDates[$date])) {
107 $params[$date] = $calcDates[$date];
108 }
109 }
110 }
111
112 // Fixme: This code belongs in the BAO
113 $action = CRM_Core_Action::ADD;
114 // we need user id during add mode
115 $ids = array();
116 if (!empty($params['contact_id'])) {
117 $ids['userId'] = $params['contact_id'];
118 }
119 //for edit membership id should be present
120 if (!empty($params['id'])) {
121 $ids['membership'] = $params['id'];
122 $action = CRM_Core_Action::UPDATE;
123 }
124 //need to pass action to handle related memberships.
125 $params['action'] = $action;
126
127 if (empty($params['line_item']) && !empty($params['membership_type_id'])) {
128 CRM_Price_BAO_LineItem::getLineItemArray($params, NULL, 'membership', $params['membership_type_id']);
129 }
130
131 $membershipBAO = CRM_Member_BAO_Membership::create($params, $ids, TRUE);
132
133 if (array_key_exists('is_error', $membershipBAO)) {
134 // In case of no valid status for given dates, $membershipBAO
135 // is going to contain 'is_error' => "Error Message"
136 return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates'));
137 }
138
139 $membership = array();
140 _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id]);
141
142 return civicrm_api3_create_success($membership, $params, 'membership', 'create', $membershipBAO);
143
144 }
145
146 /**
147 * Adjust Metadata for Create action
148 *
149 * The metadata is used for setting defaults, documentation & validation
150 * @param array $params
151 * Array or parameters determined by getfields.
152 */
153 function _civicrm_api3_membership_create_spec(&$params) {
154 $params['contact_id']['api.required'] = 1;
155 $params['membership_type_id']['api.required'] = 1;
156 $params['is_test']['api.default'] = 0;
157 $params['membership_type_id']['api.aliases'] = array('membership_type');
158 $params['status_id']['api.aliases'] = array('membership_status');
159 $params['skipStatusCal'] = array(
160 'title' => 'Skip status calculation. By default this is 0 if id is not set and 1 if it is set.',
161 );
162 $params['num_terms'] = array(
163 'title' => 'Number of terms to add/renew. If this parameter is passed, dates will be calculated automatically. If no id is passed (new membership) and no dates are given, num_terms will be assumed to be 1.',
164 'type' => CRM_Utils_Type::T_INT,
165 );
166 }
167 /**
168 * Adjust Metadata for Get action
169 *
170 * The metadata is used for setting defaults, documentation & validation
171 * @param array $params
172 * Array or parameters determined by getfields.
173 */
174 function _civicrm_api3_membership_get_spec(&$params) {
175 $params['membership_type_id']['api.aliases'] = array('membership_type');
176 }
177
178 /**
179 * Get contact membership record.
180 *
181 * This api will return the membership records for the contacts
182 * having membership based on the relationship with the direct members.
183 *
184 * @param array $params
185 * Key/value pairs for contact_id and some.
186 * options affecting the desired results; has legacy support
187 * for just passing the contact_id itself as the argument
188 *
189 * @return array
190 * Array of all found membership property values.
191 */
192 function civicrm_api3_membership_get($params) {
193 $activeOnly = $membershipTypeId = $membershipType = NULL;
194
195 $contactID = CRM_Utils_Array::value('contact_id', $params);
196 if (!empty($params['filters']) && is_array($params['filters']) && isset($params['filters']['is_current'])) {
197 $activeOnly = $params['filters']['is_current'];
198 unset($params['filters']['is_current']);
199 }
200 $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
201 if ($activeOnly && empty($params['status_id'])) {
202 $params['status_id'] = array('IN' => CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent());
203 }
204
205 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'membership', 'get');
206 if ($options['is_count']) {
207 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
208 }
209 $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Membership');
210
211 $return = $options['return'];
212 if (empty($membershipValues) ||
213 (!empty($return)
214 && !array_key_exists('related_contact_id', $return)
215 && !array_key_exists('relationship_name', $return)
216 )
217 ) {
218 return civicrm_api3_create_success($membershipValues, $params, 'membership', 'get');
219 }
220
221 $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID);
222 return civicrm_api3_create_success($members, $params, 'membership', 'get');
223 }
224
225 /**
226 * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
227 * membership a complicated return array is constructed. The original
228 * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
229 * is passed in as part of the reasonable expectation developers have that we will keep the api
230 * as stable as possible
231 *
232 * @param array $params
233 * Parameters passed into get function.
234 * @param int $membershipTypeId
235 * @param $activeOnly
236 *
237 * @return array
238 * result for calling function
239 */
240 function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) {
241 // get the membership for the given contact ID
242 $membershipParams = array('contact_id' => $params['contact_id']);
243 if ($membershipTypeId) {
244 $membershipParams['membership_type_id'] = $membershipTypeId;
245 }
246 $membershipValues = array();
247 CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly);
248 return $membershipValues;
249 }
250
251
252 /**
253 * non-standard behaviour inherited from v2
254 *
255 * @param array $params
256 * Parameters passed into get function.
257 * @param $membershipValues
258 * @param int $contactID
259 *
260 * @return array
261 * result for calling function
262 */
263 function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) {
264 $relationships = array();
265 foreach ($membershipValues as $membershipId => $values) {
266 // populate the membership type name for the membership type id
267 $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']);
268
269 $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
270
271 if (!empty($membershipType['relationship_type_id'])) {
272 $relationships[$membershipType['relationship_type_id']] = $membershipId;
273 }
274
275 // populating relationship type name.
276 $relationshipType = new CRM_Contact_BAO_RelationshipType();
277 $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType);
278 if ($relationshipType->find(TRUE)) {
279 $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b;
280 }
281
282 _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']);
283 }
284
285 $members = $membershipValues;
286
287 // populating contacts in members array based on their relationship with direct members.
288 if (!empty($relationships)) {
289 foreach ($relationships as $relTypeId => $membershipId) {
290 // As members are not direct members, there should not be
291 // membership id in the result array.
292 unset($membershipValues[$membershipId]['id']);
293 $relationship = new CRM_Contact_BAO_Relationship();
294 $relationship->contact_id_b = $contactID;
295 $relationship->relationship_type_id = $relTypeId;
296 if ($relationship->find()) {
297 while ($relationship->fetch()) {
298 clone($relationship);
299 $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a;
300 $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a;
301 }
302 }
303
304 }
305 }
306 return $members;
307 }