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