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 | |
6a488035 | 89 | * |
d9f93da9 EM |
90 | * @param array $params (reference ) an assoc array of name/value pairs |
91 | * | |
92 | * @throws Exception | |
6a488035 TO |
93 | * @return object CRM_Member_BAO_MembershipStatus object |
94 | * @access public | |
95 | * @static | |
96 | */ | |
97 | static function create($params){ | |
98 | $ids = array(); | |
99 | if(!empty($params['id'])){ | |
100 | $ids['membershipStatus'] = $params['id']; | |
101 | } | |
102 | else{ | |
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 | /** | |
114 | * function to add the membership types | |
115 | * | |
116 | * @param array $params reference array contains the values submitted by the form | |
dd12dc87 | 117 | * @param array $ids array contains the id - this param is deprecated |
6a488035 TO |
118 | * |
119 | * @access public | |
120 | * @static | |
121 | * | |
122 | * @return object | |
123 | */ | |
dd12dc87 | 124 | static function add(&$params, $ids = array()) { |
6a488035 TO |
125 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
126 | $params['is_current_member'] = CRM_Utils_Array::value('is_current_member', $params, FALSE); | |
127 | $params['is_admin'] = CRM_Utils_Array::value('is_admin', $params, FALSE); | |
128 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
129 | ||
130 | // set all other defaults to false. | |
131 | if ($params['is_default']) { | |
132 | $query = "UPDATE civicrm_membership_status SET is_default = 0"; | |
133 | CRM_Core_DAO::executeQuery($query, | |
134 | CRM_Core_DAO::$_nullArray | |
135 | ); | |
136 | } | |
137 | ||
138 | //copy name to label when not passed. | |
8cc574cf | 139 | if (empty($params['label']) && !empty($params['name'])) { |
6a488035 TO |
140 | $params['label'] = $params['name']; |
141 | } | |
142 | ||
143 | //for add mode, copy label to name. | |
975ffda1 | 144 | $statusId = !empty($params['id']) ? $params['id'] : CRM_Utils_Array::value('membershipStatus', $ids); |
8cc574cf | 145 | if (!$statusId && !empty($params['label']) && empty($params['name'])) { |
6a488035 TO |
146 | $params['name'] = $params['label']; |
147 | } | |
148 | ||
149 | // action is taken depending upon the mode | |
150 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
151 | $membershipStatus->copyValues($params); | |
152 | ||
153 | $membershipStatus->id = $statusId; | |
154 | ||
155 | $membershipStatus->save(); | |
156 | return $membershipStatus; | |
157 | } | |
158 | ||
159 | /** | |
160 | * Function to get membership status | |
161 | * | |
162 | * @param int $membershipStatusId | |
d9f93da9 EM |
163 | * |
164 | * @return array | |
6a488035 TO |
165 | * @static |
166 | */ | |
167 | public static function getMembershipStatus($membershipStatusId) { | |
168 | $statusDetails = array(); | |
169 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
170 | $membershipStatus->id = $membershipStatusId; | |
171 | if ($membershipStatus->find(TRUE)) { | |
172 | CRM_Core_DAO::storeValues($membershipStatus, $statusDetails); | |
173 | } | |
174 | return $statusDetails; | |
175 | } | |
176 | ||
177 | /** | |
178 | * Function to delete membership Types | |
179 | * | |
180 | * @param int $membershipStatusId | |
77b97be7 EM |
181 | * |
182 | * @throws CRM_Core_Exception | |
183 | * @internal param $ | |
6a488035 TO |
184 | * @static |
185 | */ | |
dcc4f6a7 | 186 | static function del($membershipStatusId) { |
6a488035 TO |
187 | //check dependencies |
188 | //checking if membership status is present in some other table | |
189 | $check = FALSE; | |
190 | ||
191 | $dependancy = array('Membership', 'MembershipLog'); | |
192 | foreach ($dependancy as $name) { | |
4d5c2eb5 | 193 | $baoString = 'CRM_Member_BAO_' . $name; |
194 | $dao = new $baoString(); | |
6a488035 TO |
195 | $dao->status_id = $membershipStatusId; |
196 | if ($dao->find(TRUE)) { | |
dcc4f6a7 | 197 | throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status')); |
6a488035 TO |
198 | } |
199 | } | |
dcc4f6a7 | 200 | CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId); |
6a488035 TO |
201 | //delete from membership Type table |
202 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
203 | $membershipStatus->id = $membershipStatusId; | |
204 | $membershipStatus->delete(); | |
205 | $membershipStatus->free(); | |
206 | } | |
207 | ||
208 | /** | |
209 | * Function to find the membership status based on start date, end date, join date & status date. | |
210 | * | |
f5d25ee9 EM |
211 | * @param string $startDate start date of the member whose membership status is to be calculated. |
212 | * @param string $endDate end date of the member whose membership status is to be calculated. | |
213 | * @param string $joinDate join date of the member whose membership status is to be calculated. | |
da6b46f4 | 214 | * @param \date|string $statusDate status date of the member whose membership status is to be calculated. |
6a488035 | 215 | * @param boolean $excludeIsAdmin exclude the statuses those having is_admin = 1 |
da6b46f4 | 216 | * @param $membershipTypeID |
5f11bbcc | 217 | * @param array $membership membership params as available to calling function - passed to the hook |
6a488035 | 218 | * |
da6b46f4 EM |
219 | * @internal param int $membershipType membership type id - passed to the hook |
220 | * @return array | |
221 | @static | |
6a488035 TO |
222 | */ |
223 | static function getMembershipStatusByDate($startDate, $endDate, $joinDate, | |
5f11bbcc | 224 | $statusDate = 'today', $excludeIsAdmin = FALSE, $membershipTypeID, $membership = array() |
6a488035 TO |
225 | ) { |
226 | $membershipDetails = array(); | |
5f11bbcc | 227 | |
6a488035 TO |
228 | if (!$statusDate || $statusDate == 'today') { |
229 | $statusDate = getDate(); | |
230 | $statusDate = date('Ymd', | |
231 | mktime($statusDate['hours'], | |
232 | $statusDate['minutes'], | |
233 | $statusDate['seconds'], | |
234 | $statusDate['mon'], | |
235 | $statusDate['mday'], | |
236 | $statusDate['year'] | |
237 | ) | |
238 | ); | |
239 | } | |
240 | else { | |
241 | $statusDate = CRM_Utils_Date::customFormat($statusDate, '%Y%m%d'); | |
242 | } | |
243 | ||
6a488035 TO |
244 | $dates = array('start', 'end', 'join'); |
245 | $events = array('start', 'end'); | |
246 | ||
247 | foreach ($dates as $dat) { | |
a0713e70 | 248 | if (${$dat . 'Date'} && ${$dat . 'Date'} != "null") { |
249 | ${$dat . 'Date'} = CRM_Utils_Date::customFormat(${$dat . 'Date'}, '%Y%m%d'); | |
250 | ||
6a488035 TO |
251 | ${$dat . 'Year'} = substr(${$dat . 'Date'}, 0, 4); |
252 | ||
253 | ${$dat . 'Month'} = substr(${$dat . 'Date'}, 4, 2); | |
254 | ||
255 | ${$dat . 'Day'} = substr(${$dat . 'Date'}, 6, 2); | |
256 | } | |
a0713e70 | 257 | else { |
258 | ${$dat . 'Date'} = ''; | |
259 | } | |
6a488035 TO |
260 | } |
261 | ||
262 | //fix for CRM-3570, if we have statuses with is_admin=1, | |
263 | //exclude these statuses from calculatation during import. | |
264 | $where = "is_active = 1"; | |
265 | if ($excludeIsAdmin) { | |
266 | $where .= " AND is_admin != 1"; | |
267 | } | |
268 | ||
269 | $query = " | |
270 | SELECT * | |
271 | FROM civicrm_membership_status | |
272 | WHERE {$where} | |
273 | ORDER BY weight ASC"; | |
274 | ||
275 | $membershipStatus = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
276 | $hour = $minute = $second = 0; | |
277 | ||
278 | while ($membershipStatus->fetch()) { | |
279 | $startEvent = NULL; | |
280 | $endEvent = NULL; | |
281 | foreach ($events as $eve) { | |
282 | foreach ($dates as $dat) { | |
283 | // calculate start-event/date and end-event/date | |
284 | if (($membershipStatus->{$eve . '_event'} == $dat . '_date') && | |
285 | ${$dat . 'Date'} | |
286 | ) { | |
287 | if ($membershipStatus->{$eve . '_event_adjust_unit'} && | |
288 | $membershipStatus->{$eve . '_event_adjust_interval'} | |
289 | ) { | |
290 | // add in months | |
291 | if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') { | |
292 | ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, | |
293 | ${$dat . 'Month'} + $membershipStatus->{$eve . '_event_adjust_interval'}, | |
294 | ${$dat . 'Day'}, | |
295 | ${$dat . 'Year'} | |
296 | )); | |
297 | } | |
298 | // add in days | |
299 | if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') { | |
300 | ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, | |
301 | ${$dat . 'Month'}, | |
302 | ${$dat . 'Day'} + $membershipStatus->{$eve . '_event_adjust_interval'}, | |
303 | ${$dat . 'Year'} | |
304 | )); | |
305 | } | |
306 | // add in years | |
307 | if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') { | |
308 | ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, | |
309 | ${$dat . 'Month'}, | |
310 | ${$dat . 'Day'}, | |
311 | ${$dat . 'Year'} + $membershipStatus->{$eve . '_event_adjust_interval'} | |
312 | )); | |
313 | } | |
314 | // if no interval and unit, present | |
315 | } | |
316 | else { | |
317 | ${$eve . 'Event'} = ${$dat . 'Date'}; | |
318 | } | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
323 | // check if statusDate is in the range of start & end events. | |
324 | if ($startEvent && $endEvent) { | |
325 | if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) { | |
326 | $membershipDetails['id'] = $membershipStatus->id; | |
327 | $membershipDetails['name'] = $membershipStatus->name; | |
328 | } | |
329 | } | |
330 | elseif ($startEvent) { | |
331 | if ($statusDate >= $startEvent) { | |
332 | $membershipDetails['id'] = $membershipStatus->id; | |
333 | $membershipDetails['name'] = $membershipStatus->name; | |
334 | } | |
335 | } | |
336 | elseif ($endEvent) { | |
337 | if ($statusDate <= $endEvent) { | |
338 | $membershipDetails['id'] = $membershipStatus->id; | |
339 | $membershipDetails['name'] = $membershipStatus->name; | |
340 | } | |
341 | } | |
342 | ||
343 | // returns FIRST status record for which status_date is in range. | |
344 | if ($membershipDetails) { | |
5f11bbcc | 345 | break; |
6a488035 TO |
346 | } |
347 | } | |
348 | //end fetch | |
349 | ||
350 | $membershipStatus->free(); | |
5f11bbcc EM |
351 | |
352 | //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise | |
353 | // the membership array might contain the pre-altered settings so we don't want to merge this | |
354 | $arguments = array( | |
355 | 'start_date' => $startDate, | |
356 | 'end_date' => $endDate, | |
357 | 'join_date' => $joinDate, | |
358 | 'status_date' => $statusDate, | |
359 | 'exclude_is_admin' => $endDate, | |
360 | 'membership_type_id' => $membershipTypeID, | |
361 | 'start_event' => $startEvent, | |
362 | 'end_event' => $endEvent, | |
363 | ); | |
364 | CRM_Utils_Hook::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership); | |
6a488035 TO |
365 | return $membershipDetails; |
366 | } | |
367 | ||
368 | /** | |
369 | * Function that return the status ids whose is_current_member is set | |
370 | * | |
77b97be7 EM |
371 | * @return array |
372 | @static | |
6a488035 TO |
373 | */ |
374 | public static function getMembershipStatusCurrent() { | |
375 | $statusIds = array(); | |
376 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
377 | $membershipStatus->is_current_member = 1; | |
378 | $membershipStatus->find(); | |
379 | $membershipStatus->selectAdd(); | |
380 | $membershipStatus->selectAdd('id'); | |
381 | while ($membershipStatus->fetch()) { | |
382 | $statusIds[] = $membershipStatus->id; | |
383 | } | |
384 | $membershipStatus->free(); | |
385 | return $statusIds; | |
386 | } | |
387 | } | |
388 |