Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
232624b1 | 5 | | CiviCRM version 4.4 | |
6a488035 TO |
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 | ||
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 (); | |
4d63cfde | 115 | if(CRM_Utils_Array::value('contact_id', $params)) { |
6a488035 TO |
116 | $ids['userId'] = $params['contact_id']; |
117 | } | |
118 | //for edit membership id should be present | |
119 | if (CRM_Utils_Array::value('id', $params)) { | |
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 | ||
6a488035 TO |
126 | $membershipBAO = CRM_Member_BAO_Membership::create($params, $ids, TRUE); |
127 | ||
128 | if (array_key_exists('is_error', $membershipBAO)) { | |
129 | // In case of no valid status for given dates, $membershipBAO | |
130 | // is going to contain 'is_error' => "Error Message" | |
131 | return civicrm_api3_create_error(ts('The membership can not be saved, no valid membership status for given dates')); | |
132 | } | |
133 | ||
134 | $membership = array(); | |
135 | _civicrm_api3_object_to_array($membershipBAO, $membership[$membershipBAO->id]); | |
136 | ||
137 | return civicrm_api3_create_success($membership, $params, 'membership', 'create', $membershipBAO); | |
138 | ||
139 | } | |
11e09c59 TO |
140 | |
141 | /** | |
6a488035 TO |
142 | * Adjust Metadata for Create action |
143 | * | |
144 | * The metadata is used for setting defaults, documentation & validation | |
145 | * @param array $params array or parameters determined by getfields | |
146 | */ | |
147 | function _civicrm_api3_membership_create_spec(&$params) { | |
148 | $params['contact_id']['api.required'] = 1; | |
4d63cfde | 149 | $params['membership_type_id']['api.required'] = 1; |
8f3aca71 | 150 | $params['is_test']['api.default'] = 0; |
4d63cfde | 151 | $params['membership_type_id']['api.aliases'] = array('membership_type'); |
eea6f5de | 152 | $params['status_id']['api.aliases'] = array('membership_status'); |
8f3aca71 CW |
153 | $params['skipStatusCal'] = array( |
154 | 'title' => 'Skip status calculation. 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 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.' | |
158 | ); | |
6a488035 | 159 | } |
4d63cfde CW |
160 | /** |
161 | * Adjust Metadata for Get action | |
162 | * | |
163 | * The metadata is used for setting defaults, documentation & validation | |
164 | * @param array $params array or parameters determined by getfields | |
165 | */ | |
166 | function _civicrm_api3_membership_get_spec(&$params) { | |
167 | $params['membership_type_id']['api.aliases'] = array('membership_type'); | |
168 | } | |
169 | ||
6a488035 TO |
170 | /** |
171 | * Get contact membership record. | |
172 | * | |
173 | * This api will return the membership records for the contacts | |
174 | * having membership based on the relationship with the direct members. | |
175 | * | |
176 | * @param Array $params key/value pairs for contact_id and some | |
177 | * options affecting the desired results; has legacy support | |
178 | * for just passing the contact_id itself as the argument | |
179 | * | |
180 | * @return Array of all found membership property values. | |
181 | * @access public | |
182 | * @todo needs some love - basically only a get for a given contact right now | |
183 | * {@getfields membership_get} | |
184 | */ | |
185 | function civicrm_api3_membership_get($params) { | |
4d63cfde | 186 | $activeOnly = $membershipTypeId = $membershipType = NULL; |
6a488035 TO |
187 | |
188 | $contactID = CRM_Utils_Array::value('contact_id', $params); | |
4d63cfde | 189 | if (!empty($params['filters']) && is_array($params['filters'])) { |
6a488035 TO |
190 | $activeOnly = CRM_Utils_Array::value('is_current', $params['filters'], FALSE); |
191 | } | |
192 | $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly); | |
4d63cfde CW |
193 | |
194 | if (CRM_Utils_Array::value('contact_id', $params)) { | |
195 | $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $contactID, $membershipTypeId, $activeOnly ); | |
6a488035 | 196 | } |
4d63cfde CW |
197 | else { |
198 | //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise | |
6a488035 TO |
199 | $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE); |
200 | } | |
201 | ||
6a488035 | 202 | if (empty($membershipValues)) { |
4d63cfde | 203 | // No results is NOT an error! |
6a488035 TO |
204 | return civicrm_api3_create_success($membershipValues, $params); |
205 | } | |
206 | ||
207 | $relationships = array(); | |
208 | foreach ($membershipValues as $membershipId => $values) { | |
209 | // populate the membership type name for the membership type id | |
210 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); | |
211 | ||
212 | $membershipValues[$membershipId]['membership_name'] = $membershipType['name']; | |
213 | ||
214 | if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) { | |
215 | $relationships[$membershipType['relationship_type_id']] = $membershipId; | |
216 | } | |
217 | ||
218 | // populating relationship type name. | |
219 | $relationshipType = new CRM_Contact_BAO_RelationshipType(); | |
220 | $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType); | |
221 | if ($relationshipType->find(TRUE)) { | |
222 | $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b; | |
223 | } | |
224 | ||
225 | _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']); | |
226 | } | |
227 | ||
228 | $members = $membershipValues; | |
229 | ||
230 | // populating contacts in members array based on their relationship with direct members. | |
231 | if (!empty($relationships)) { | |
232 | foreach ($relationships as $relTypeId => $membershipId) { | |
233 | // As members are not direct members, there should not be | |
234 | // membership id in the result array. | |
235 | unset($membershipValues[$membershipId]['id']); | |
236 | $relationship = new CRM_Contact_BAO_Relationship(); | |
237 | $relationship->contact_id_b = $contactID; | |
238 | $relationship->relationship_type_id = $relTypeId; | |
239 | if ($relationship->find()) { | |
240 | while ($relationship->fetch()) { | |
241 | clone($relationship); | |
242 | $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a; | |
243 | $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a; | |
244 | } | |
245 | } | |
246 | ||
247 | } | |
248 | } | |
249 | ||
250 | return civicrm_api3_create_success($members, $params, 'membership', 'get'); | |
6a488035 | 251 | } |
11e09c59 TO |
252 | |
253 | /** | |
6a488035 TO |
254 | * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of |
255 | * membership a complicated return array is constructed. The original | |
256 | * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id | |
257 | * is passed in as part of the reasonable expectation developers have that we will keep the api | |
258 | * as stable as possible | |
259 | * | |
260 | * @param array $params parameters passed into get function | |
261 | * @return array result for calling function | |
262 | */ | |
4d63cfde CW |
263 | function _civicrm_api3_membership_get_customv2behaviour(&$params, $contactID, $membershipTypeId, $activeOnly) { |
264 | // get the membership for the given contact ID | |
265 | $membershipParams = array('contact_id' => $contactID); | |
266 | if ($membershipTypeId) { | |
267 | $membershipParams['membership_type_id'] = $membershipTypeId; | |
268 | } | |
269 | $membershipValues = array(); | |
270 | CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly); | |
271 | return $membershipValues; | |
6a488035 | 272 | } |