Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus { | |
36 | ||
37 | /** | |
38 | * static holder for the default LT | |
39 | */ | |
40 | static $_defaultMembershipStatus = NULL; | |
41 | ||
42 | /** | |
43 | * class constructor | |
44 | */ | |
45 | function __construct() { | |
46 | parent::__construct(); | |
47 | } | |
48 | ||
49 | /** | |
50 | * Takes a bunch of params that are needed to match certain criteria and | |
51 | * retrieves the relevant objects. Typically the valid params are only | |
52 | * contact_id. We'll tweak this function to be more full featured over a period | |
53 | * of time. This is the inverse function of create. It also stores all the retrieved | |
54 | * values in the default array | |
55 | * | |
56 | * @param array $params (reference ) an assoc array of name/value pairs | |
57 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
58 | * | |
59 | * @return object CRM_Member_BAO_MembershipStatus object | |
60 | * @access public | |
61 | * @static | |
62 | */ | |
63 | static function retrieve(&$params, &$defaults) { | |
64 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
65 | $membershipStatus->copyValues($params); | |
66 | if ($membershipStatus->find(TRUE)) { | |
67 | CRM_Core_DAO::storeValues($membershipStatus, $defaults); | |
68 | return $membershipStatus; | |
69 | } | |
70 | return NULL; | |
71 | } | |
72 | ||
73 | /** | |
74 | * update the is_active flag in the db | |
75 | * | |
76 | * @param int $id id of the database record | |
77 | * @param boolean $is_active value we want to set the is_active field | |
78 | * | |
79 | * @return Object DAO object on sucess, null otherwise | |
80 | * @static | |
81 | */ | |
82 | static function setIsActive($id, $is_active) { | |
83 | return CRM_Core_DAO::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Takes an associative array and creates a membership Status object | |
88 | * See http://wiki.civicrm.org/confluence/display/CRM/Database+layer | |
89 | * @param array $params (reference ) an assoc array of name/value pairs | |
90 | * | |
91 | * @return object CRM_Member_BAO_MembershipStatus object | |
92 | * @access public | |
93 | * @static | |
94 | */ | |
95 | static function create($params){ | |
96 | $ids = array(); | |
97 | if(!empty($params['id'])){ | |
98 | $ids['membershipStatus'] = $params['id']; | |
99 | } | |
100 | else{ | |
101 | //don't allow duplicate names - if id not set | |
102 | $status = new CRM_Member_DAO_MembershipStatus(); | |
103 | $status->name = $params['name']; | |
104 | if ($status->find(TRUE)) { | |
105 | throw new Exception('A membership status with this name already exists.'); | |
106 | } | |
107 | } | |
108 | $membershipStatusBAO = CRM_Member_BAO_MembershipStatus::add($params, $ids); | |
109 | return $membershipStatusBAO; | |
110 | } | |
111 | /** | |
112 | * function to add the membership types | |
113 | * | |
114 | * @param array $params reference array contains the values submitted by the form | |
115 | * @param array $ids reference array contains the id | |
116 | * | |
117 | * @access public | |
118 | * @static | |
119 | * | |
120 | * @return object | |
121 | */ | |
122 | static function add(&$params, &$ids) { | |
123 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
124 | $params['is_current_member'] = CRM_Utils_Array::value('is_current_member', $params, FALSE); | |
125 | $params['is_admin'] = CRM_Utils_Array::value('is_admin', $params, FALSE); | |
126 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
127 | ||
128 | // set all other defaults to false. | |
129 | if ($params['is_default']) { | |
130 | $query = "UPDATE civicrm_membership_status SET is_default = 0"; | |
131 | CRM_Core_DAO::executeQuery($query, | |
132 | CRM_Core_DAO::$_nullArray | |
133 | ); | |
134 | } | |
135 | ||
136 | //copy name to label when not passed. | |
8cc574cf | 137 | if (empty($params['label']) && !empty($params['name'])) { |
6a488035 TO |
138 | $params['label'] = $params['name']; |
139 | } | |
140 | ||
141 | //for add mode, copy label to name. | |
142 | $statusId = CRM_Utils_Array::value('membershipStatus', $ids); | |
8cc574cf | 143 | if (!$statusId && !empty($params['label']) && empty($params['name'])) { |
6a488035 TO |
144 | $params['name'] = $params['label']; |
145 | } | |
146 | ||
147 | // action is taken depending upon the mode | |
148 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
149 | $membershipStatus->copyValues($params); | |
150 | ||
151 | $membershipStatus->id = $statusId; | |
152 | ||
153 | $membershipStatus->save(); | |
154 | return $membershipStatus; | |
155 | } | |
156 | ||
157 | /** | |
158 | * Function to get membership status | |
159 | * | |
160 | * @param int $membershipStatusId | |
161 | * @static | |
162 | */ | |
163 | public static function getMembershipStatus($membershipStatusId) { | |
164 | $statusDetails = array(); | |
165 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
166 | $membershipStatus->id = $membershipStatusId; | |
167 | if ($membershipStatus->find(TRUE)) { | |
168 | CRM_Core_DAO::storeValues($membershipStatus, $statusDetails); | |
169 | } | |
170 | return $statusDetails; | |
171 | } | |
172 | ||
173 | /** | |
174 | * Function to delete membership Types | |
175 | * | |
176 | * @param int $membershipStatusId | |
177 | * @param | |
178 | * @static | |
179 | */ | |
dcc4f6a7 | 180 | static function del($membershipStatusId) { |
6a488035 TO |
181 | //check dependencies |
182 | //checking if membership status is present in some other table | |
183 | $check = FALSE; | |
184 | ||
185 | $dependancy = array('Membership', 'MembershipLog'); | |
186 | foreach ($dependancy as $name) { | |
4d5c2eb5 | 187 | $baoString = 'CRM_Member_BAO_' . $name; |
188 | $dao = new $baoString(); | |
6a488035 TO |
189 | $dao->status_id = $membershipStatusId; |
190 | if ($dao->find(TRUE)) { | |
dcc4f6a7 | 191 | throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status')); |
6a488035 TO |
192 | } |
193 | } | |
dcc4f6a7 | 194 | CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId); |
6a488035 TO |
195 | //delete from membership Type table |
196 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
197 | $membershipStatus->id = $membershipStatusId; | |
198 | $membershipStatus->delete(); | |
199 | $membershipStatus->free(); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Function to find the membership status based on start date, end date, join date & status date. | |
204 | * | |
205 | * @param date $startDate start date of the member whose membership status is to be calculated. | |
206 | * @param date $endDate end date of the member whose membership status is to be calculated. | |
207 | * @param date $joinDate join date of the member whose membership status is to be calculated. | |
208 | * @param date $statusDate status date of the member whose membership status is to be calculated. | |
209 | * @param boolean $excludeIsAdmin exclude the statuses those having is_admin = 1 | |
5f11bbcc EM |
210 | * @param integer $membershipType membership type id - passed to the hook |
211 | * @param array $membership membership params as available to calling function - passed to the hook | |
6a488035 TO |
212 | * |
213 | * @return | |
214 | * @static | |
215 | */ | |
216 | static function getMembershipStatusByDate($startDate, $endDate, $joinDate, | |
5f11bbcc | 217 | $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array() |
6a488035 TO |
218 | ) { |
219 | $membershipDetails = array(); | |
5f11bbcc | 220 | |
6a488035 TO |
221 | if (!$statusDate || $statusDate == 'today') { |
222 | $statusDate = getDate(); | |
223 | $statusDate = date('Ymd', | |
224 | mktime($statusDate['hours'], | |
225 | $statusDate['minutes'], | |
226 | $statusDate['seconds'], | |
227 | $statusDate['mon'], | |
228 | $statusDate['mday'], | |
229 | $statusDate['year'] | |
230 | ) | |
231 | ); | |
232 | } | |
233 | else { | |
234 | $statusDate = CRM_Utils_Date::customFormat($statusDate, '%Y%m%d'); | |
235 | } | |
236 | ||
6a488035 TO |
237 | $dates = array('start', 'end', 'join'); |
238 | $events = array('start', 'end'); | |
239 | ||
240 | foreach ($dates as $dat) { | |
a0713e70 | 241 | if (${$dat . 'Date'} && ${$dat . 'Date'} != "null") { |
242 | ${$dat . 'Date'} = CRM_Utils_Date::customFormat(${$dat . 'Date'}, '%Y%m%d'); | |
243 | ||
6a488035 TO |
244 | ${$dat . 'Year'} = substr(${$dat . 'Date'}, 0, 4); |
245 | ||
246 | ${$dat . 'Month'} = substr(${$dat . 'Date'}, 4, 2); | |
247 | ||
248 | ${$dat . 'Day'} = substr(${$dat . 'Date'}, 6, 2); | |
249 | } | |
a0713e70 | 250 | else { |
251 | ${$dat . 'Date'} = ''; | |
252 | } | |
6a488035 TO |
253 | } |
254 | ||
255 | //fix for CRM-3570, if we have statuses with is_admin=1, | |
256 | //exclude these statuses from calculatation during import. | |
257 | $where = "is_active = 1"; | |
258 | if ($excludeIsAdmin) { | |
259 | $where .= " AND is_admin != 1"; | |
260 | } | |
261 | ||
262 | $query = " | |
263 | SELECT * | |
264 | FROM civicrm_membership_status | |
265 | WHERE {$where} | |
266 | ORDER BY weight ASC"; | |
267 | ||
268 | $membershipStatus = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
269 | $hour = $minute = $second = 0; | |
270 | ||
271 | while ($membershipStatus->fetch()) { | |
272 | $startEvent = NULL; | |
273 | $endEvent = NULL; | |
274 | foreach ($events as $eve) { | |
275 | foreach ($dates as $dat) { | |
276 | // calculate start-event/date and end-event/date | |
277 | if (($membershipStatus->{$eve . '_event'} == $dat . '_date') && | |
278 | ${$dat . 'Date'} | |
279 | ) { | |
280 | if ($membershipStatus->{$eve . '_event_adjust_unit'} && | |
281 | $membershipStatus->{$eve . '_event_adjust_interval'} | |
282 | ) { | |
283 | // add in months | |
284 | if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') { | |
285 | ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, | |
286 | ${$dat . 'Month'} + $membershipStatus->{$eve . '_event_adjust_interval'}, | |
287 | ${$dat . 'Day'}, | |
288 | ${$dat . 'Year'} | |
289 | )); | |
290 | } | |
291 | // add in days | |
292 | if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') { | |
293 | ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, | |
294 | ${$dat . 'Month'}, | |
295 | ${$dat . 'Day'} + $membershipStatus->{$eve . '_event_adjust_interval'}, | |
296 | ${$dat . 'Year'} | |
297 | )); | |
298 | } | |
299 | // add in years | |
300 | if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') { | |
301 | ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, | |
302 | ${$dat . 'Month'}, | |
303 | ${$dat . 'Day'}, | |
304 | ${$dat . 'Year'} + $membershipStatus->{$eve . '_event_adjust_interval'} | |
305 | )); | |
306 | } | |
307 | // if no interval and unit, present | |
308 | } | |
309 | else { | |
310 | ${$eve . 'Event'} = ${$dat . 'Date'}; | |
311 | } | |
312 | } | |
313 | } | |
314 | } | |
315 | ||
316 | // check if statusDate is in the range of start & end events. | |
317 | if ($startEvent && $endEvent) { | |
318 | if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) { | |
319 | $membershipDetails['id'] = $membershipStatus->id; | |
320 | $membershipDetails['name'] = $membershipStatus->name; | |
321 | } | |
322 | } | |
323 | elseif ($startEvent) { | |
324 | if ($statusDate >= $startEvent) { | |
325 | $membershipDetails['id'] = $membershipStatus->id; | |
326 | $membershipDetails['name'] = $membershipStatus->name; | |
327 | } | |
328 | } | |
329 | elseif ($endEvent) { | |
330 | if ($statusDate <= $endEvent) { | |
331 | $membershipDetails['id'] = $membershipStatus->id; | |
332 | $membershipDetails['name'] = $membershipStatus->name; | |
333 | } | |
334 | } | |
335 | ||
336 | // returns FIRST status record for which status_date is in range. | |
337 | if ($membershipDetails) { | |
5f11bbcc | 338 | break; |
6a488035 TO |
339 | } |
340 | } | |
341 | //end fetch | |
342 | ||
343 | $membershipStatus->free(); | |
5f11bbcc EM |
344 | |
345 | //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise | |
346 | // the membership array might contain the pre-altered settings so we don't want to merge this | |
347 | $arguments = array( | |
348 | 'start_date' => $startDate, | |
349 | 'end_date' => $endDate, | |
350 | 'join_date' => $joinDate, | |
351 | 'status_date' => $statusDate, | |
352 | 'exclude_is_admin' => $endDate, | |
353 | 'membership_type_id' => $membershipTypeID, | |
354 | 'start_event' => $startEvent, | |
355 | 'end_event' => $endEvent, | |
356 | ); | |
357 | CRM_Utils_Hook::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership); | |
6a488035 TO |
358 | return $membershipDetails; |
359 | } | |
360 | ||
361 | /** | |
362 | * Function that return the status ids whose is_current_member is set | |
363 | * | |
364 | * @return | |
365 | * @static | |
366 | */ | |
367 | public static function getMembershipStatusCurrent() { | |
368 | $statusIds = array(); | |
369 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
370 | $membershipStatus->is_current_member = 1; | |
371 | $membershipStatus->find(); | |
372 | $membershipStatus->selectAdd(); | |
373 | $membershipStatus->selectAdd('id'); | |
374 | while ($membershipStatus->fetch()) { | |
375 | $statusIds[] = $membershipStatus->id; | |
376 | } | |
377 | $membershipStatus->free(); | |
378 | return $statusIds; | |
379 | } | |
380 | } | |
381 |