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