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