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