INFRA-132 - s/array (/array(/
[civicrm-core.git] / api / v3 / Membership.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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
46 * Array array holding id - Id of the contact membership to be deleted.
47 *
48 * @return array api result
49 * {@getfields membership_delete}
50 * @access public
51 */
52 function civicrm_api3_membership_delete($params) {
53 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
54 }
55
56 /**
57 * Create a Contact Membership
58 *
59 * This API is used for creating a Membership for a contact.
60 * Required parameters : membership_type_id and status_id.
61 *
62 * @param array $params
63 * An associative array of name/value property values of civicrm_membership.
64 *
65 * @return array of newly created membership property values.
66 * {@getfields membership_create}
67 * @access public
68 */
69 function civicrm_api3_membership_create($params) {
70 // check params for membership id during update
71 if (!empty($params['id']) && !isset($params['skipStatusCal'])) {
72 //don't calculate status on exisiting membership - expect API use to pass them in
73 // or leave unchanged
74 $params['skipStatusCal'] = 1;
75 }
76 else {
77 // also check for status id if override is set (during add/update)
78 if (!empty($params['is_override']) && empty($params['status_id'])) {
79 return civicrm_api3_create_error('Status ID required');
80 }
81 }
82
83 $values = array();
84 _civicrm_api3_custom_format_params($params, $values, 'Membership');
85 $params = array_merge($params, $values);
86
87 // Fixme: This code belongs in the BAO
88 if (empty($params['id']) || !empty($params['num_terms'])) {
89 if (empty($params['id'])) {
90 $calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType(
91 $params['membership_type_id'],
92 CRM_Utils_Array::value('join_date', $params),
93 CRM_Utils_Array::value('start_date', $params),
94 CRM_Utils_Array::value('end_date', $params),
95 CRM_Utils_Array::value('num_terms', $params, 1)
96 );
97 }
98 else {
99 $calcDates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType(
100 $params['id'],
101 NULL,
102 CRM_Utils_Array::value('membership_type_id', $params),
103 $params['num_terms']
104 );
105 }
106 foreach (array('join_date', 'start_date', 'end_date') as $date) {
107 if (empty($params[$date]) && isset($calcDates[$date])) {
108 $params[$date] = $calcDates[$date];
109 }
110 }
111 }
112
113 // Fixme: This code belongs in the BAO
114 $action = CRM_Core_Action::ADD;
115 // we need user id during add mode
116 $ids = array();
117 if (!empty($params['contact_id'])) {
118 $ids['userId'] = $params['contact_id'];
119 }
120 //for edit membership id should be present
121 if (!empty($params['id'])) {
122 $ids['membership'] = $params['id'];
123 $action = CRM_Core_Action::UPDATE;
124 }
125 //need to pass action to handle related memberships.
126 $params['action'] = $action;
127
128 if (empty($params['line_item']) && !empty($params['membership_type_id'])) {
129 CRM_Price_BAO_LineItem::getLineItemArray($params, NULL, 'membership', $params['membership_type_id']);
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
152 * Array or parameters determined by getfields.
153 */
154 function _civicrm_api3_membership_create_spec(&$params) {
155 $params['contact_id']['api.required'] = 1;
156 $params['membership_type_id']['api.required'] = 1;
157 $params['is_test']['api.default'] = 0;
158 $params['membership_type_id']['api.aliases'] = array('membership_type');
159 $params['status_id']['api.aliases'] = array('membership_status');
160 $params['skipStatusCal'] = array(
161 'title' => 'Skip status calculation. By default this is 0 if id is not set and 1 if it is set.'
162 );
163 $params['num_terms'] = array(
164 '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.',
165 'type' => CRM_Utils_Type::T_INT,
166 );
167 }
168 /**
169 * Adjust Metadata for Get action
170 *
171 * The metadata is used for setting defaults, documentation & validation
172 * @param array $params
173 * Array or parameters determined by getfields.
174 */
175 function _civicrm_api3_membership_get_spec(&$params) {
176 $params['membership_type_id']['api.aliases'] = array('membership_type');
177 }
178
179 /**
180 * Get contact membership record.
181 *
182 * This api will return the membership records for the contacts
183 * having membership based on the relationship with the direct members.
184 *
185 * @param Array $params
186 * Key/value pairs for contact_id and some.
187 * options affecting the desired results; has legacy support
188 * for just passing the contact_id itself as the argument
189 *
190 * @return Array of all found membership property values.
191 * @access public
192 * @todo needs some love - basically only a get for a given contact right now
193 * {@getfields membership_get}
194 */
195 function civicrm_api3_membership_get($params) {
196 $activeOnly = $membershipTypeId = $membershipType = NULL;
197
198 $contactID = CRM_Utils_Array::value('contact_id', $params);
199 if (!empty($params['filters']) && is_array($params['filters']) && isset($params['filters']['is_current'])) {
200 $activeOnly = $params['filters']['is_current'];
201 unset($params['filters']['is_current']);
202 }
203 $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
204 if ($activeOnly && empty($params['status_id'])) {
205 $params['status_id'] = array('IN' => CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent());
206 }
207
208 $options = _civicrm_api3_get_options_from_params($params, TRUE,'membership', 'get');
209 if ($options['is_count']) {
210 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
211 }
212 $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Membership');
213
214 $return = $options['return'];
215 if (empty($membershipValues) ||
216 (!empty($return)
217 && !array_key_exists('related_contact_id', $return)
218 && !array_key_exists('relationship_name', $return)
219 )
220 ) {
221 return civicrm_api3_create_success($membershipValues, $params, 'membership', 'get');
222 }
223
224 $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID);
225 return civicrm_api3_create_success($members, $params, 'membership', 'get');
226 }
227
228 /**
229 * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
230 * membership a complicated return array is constructed. The original
231 * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
232 * is passed in as part of the reasonable expectation developers have that we will keep the api
233 * as stable as possible
234 *
235 * @param array $params
236 * Parameters passed into get function.
237 * @param int $membershipTypeId
238 * @param $activeOnly
239 *
240 * @return array result for calling function
241 */
242 function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) {
243 // get the membership for the given contact ID
244 $membershipParams = array('contact_id' => $params['contact_id']);
245 if ($membershipTypeId) {
246 $membershipParams['membership_type_id'] = $membershipTypeId;
247 }
248 $membershipValues = array();
249 CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly);
250 return $membershipValues;
251 }
252
253
254 /**
255 * non-standard behaviour inherited from v2
256 *
257 * @param array $params
258 * Parameters passed into get function.
259 * @param $membershipValues
260 * @param int $contactID
261 *
262 * @return array result for calling function
263 */
264 function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) {
265 $relationships = array();
266 foreach ($membershipValues as $membershipId => $values) {
267 // populate the membership type name for the membership type id
268 $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']);
269
270 $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
271
272 if (!empty($membershipType['relationship_type_id'])) {
273 $relationships[$membershipType['relationship_type_id']] = $membershipId;
274 }
275
276 // populating relationship type name.
277 $relationshipType = new CRM_Contact_BAO_RelationshipType();
278 $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType);
279 if ($relationshipType->find(TRUE)) {
280 $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b;
281 }
282
283 _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']);
284 }
285
286 $members = $membershipValues;
287
288 // populating contacts in members array based on their relationship with direct members.
289 if (!empty($relationships)) {
290 foreach ($relationships as $relTypeId => $membershipId) {
291 // As members are not direct members, there should not be
292 // membership id in the result array.
293 unset($membershipValues[$membershipId]['id']);
294 $relationship = new CRM_Contact_BAO_Relationship();
295 $relationship->contact_id_b = $contactID;
296 $relationship->relationship_type_id = $relTypeId;
297 if ($relationship->find()) {
298 while ($relationship->fetch()) {
299 clone($relationship);
300 $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a;
301 $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a;
302 }
303 }
304
305 }
306 }
307 return $members;
308 }