Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus { | |
18 | ||
6a488035 | 19 | /** |
fe482240 | 20 | * Fetch object based on array of properties. |
6a488035 | 21 | * |
b2363ea8 TO |
22 | * @param array $params |
23 | * (reference ) an assoc array of name/value pairs. | |
24 | * @param array $defaults | |
25 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 26 | * |
16b10e64 | 27 | * @return CRM_Member_BAO_MembershipStatus |
6a488035 | 28 | */ |
00be9182 | 29 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
30 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); |
31 | $membershipStatus->copyValues($params); | |
32 | if ($membershipStatus->find(TRUE)) { | |
33 | CRM_Core_DAO::storeValues($membershipStatus, $defaults); | |
34 | return $membershipStatus; | |
35 | } | |
36 | return NULL; | |
37 | } | |
38 | ||
39 | /** | |
fe482240 | 40 | * Update the is_active flag in the db. |
6a488035 | 41 | * |
b2363ea8 TO |
42 | * @param int $id |
43 | * Id of the database record. | |
44 | * @param bool $is_active | |
45 | * Value we want to set the is_active field. | |
6a488035 | 46 | * |
8a4fede3 | 47 | * @return bool |
48 | * true if we found and updated the object, else false | |
6a488035 | 49 | */ |
00be9182 | 50 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
51 | return CRM_Core_DAO::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active); |
52 | } | |
53 | ||
54 | /** | |
fe482240 | 55 | * Takes an associative array and creates a membership Status object. |
6a488035 | 56 | * |
b2363ea8 | 57 | * @param array $params |
e021e985 | 58 | * Array of name/value pairs. |
d9f93da9 | 59 | * |
e021e985 | 60 | * @throws CRM_Core_Exception |
61 | * @return CRM_Member_DAO_MembershipStatus | |
6a488035 | 62 | */ |
9b873358 | 63 | public static function create($params) { |
e021e985 | 64 | if (empty($params['id'])) { |
6a488035 TO |
65 | //don't allow duplicate names - if id not set |
66 | $status = new CRM_Member_DAO_MembershipStatus(); | |
67 | $status->name = $params['name']; | |
68 | if ($status->find(TRUE)) { | |
e021e985 | 69 | throw new CRM_Core_Exception('A membership status with this name already exists.'); |
6a488035 TO |
70 | } |
71 | } | |
e021e985 | 72 | return self::add($params); |
6a488035 | 73 | } |
353ffa53 | 74 | |
6a488035 | 75 | /** |
fe482240 | 76 | * Add the membership types. |
6a488035 | 77 | * |
b2363ea8 TO |
78 | * @param array $params |
79 | * Reference array contains the values submitted by the form. | |
80 | * @param array $ids | |
81 | * Array contains the id - this param is deprecated. | |
6a488035 | 82 | * |
e021e985 | 83 | * @return CRM_Member_DAO_MembershipStatus |
6a488035 | 84 | */ |
be2fb01f | 85 | public static function add(&$params, $ids = []) { |
e021e985 | 86 | if (!empty($ids)) { |
87 | CRM_Core_Error::deprecatedFunctionWarning('ids is a deprecated parameter'); | |
88 | } | |
8df1a020 | 89 | $id = $params['id'] ?? $ids['membershipStatus'] ?? NULL; |
64d24a64 EM |
90 | if (!$id) { |
91 | CRM_Core_DAO::setCreateDefaults($params, self::getDefaults()); | |
92 | //copy name to label when not passed. | |
93 | if (empty($params['label']) && !empty($params['name'])) { | |
94 | $params['label'] = $params['name']; | |
95 | } | |
96 | ||
97 | if (empty($params['name']) && !empty($params['label'])) { | |
98 | $params['name'] = $params['label']; | |
99 | } | |
100 | } | |
6a488035 TO |
101 | |
102 | // set all other defaults to false. | |
64d24a64 | 103 | if (!empty($params['is_default'])) { |
6a488035 | 104 | $query = "UPDATE civicrm_membership_status SET is_default = 0"; |
e03e1641 | 105 | CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
106 | } |
107 | ||
6a488035 TO |
108 | // action is taken depending upon the mode |
109 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
110 | $membershipStatus->copyValues($params); | |
111 | ||
64d24a64 | 112 | $membershipStatus->id = $id; |
6a488035 TO |
113 | |
114 | $membershipStatus->save(); | |
9d4d481e | 115 | CRM_Member_PseudoConstant::flush('membershipStatus'); |
6a488035 TO |
116 | return $membershipStatus; |
117 | } | |
118 | ||
64d24a64 | 119 | /** |
fe482240 | 120 | * Get defaults for new entity. |
64d24a64 EM |
121 | * @return array |
122 | */ | |
00be9182 | 123 | public static function getDefaults() { |
be2fb01f | 124 | return [ |
64d24a64 EM |
125 | 'is_active' => FALSE, |
126 | 'is_current_member' => FALSE, | |
127 | 'is_admin' => FALSE, | |
128 | 'is_default' => FALSE, | |
be2fb01f | 129 | ]; |
64d24a64 EM |
130 | } |
131 | ||
6a488035 | 132 | /** |
fe482240 | 133 | * Get membership status. |
6a488035 TO |
134 | * |
135 | * @param int $membershipStatusId | |
d9f93da9 EM |
136 | * |
137 | * @return array | |
6a488035 TO |
138 | */ |
139 | public static function getMembershipStatus($membershipStatusId) { | |
be2fb01f | 140 | $statusDetails = []; |
6a488035 TO |
141 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); |
142 | $membershipStatus->id = $membershipStatusId; | |
143 | if ($membershipStatus->find(TRUE)) { | |
144 | CRM_Core_DAO::storeValues($membershipStatus, $statusDetails); | |
145 | } | |
146 | return $statusDetails; | |
147 | } | |
148 | ||
149 | /** | |
fe482240 | 150 | * Delete membership Types. |
6a488035 TO |
151 | * |
152 | * @param int $membershipStatusId | |
77b97be7 EM |
153 | * |
154 | * @throws CRM_Core_Exception | |
6a488035 | 155 | */ |
00be9182 | 156 | public static function del($membershipStatusId) { |
6a488035 TO |
157 | //check dependencies |
158 | //checking if membership status is present in some other table | |
159 | $check = FALSE; | |
160 | ||
be2fb01f | 161 | $dependency = ['Membership', 'MembershipLog']; |
b44e3f84 | 162 | foreach ($dependency as $name) { |
4d5c2eb5 | 163 | $baoString = 'CRM_Member_BAO_' . $name; |
164 | $dao = new $baoString(); | |
6a488035 TO |
165 | $dao->status_id = $membershipStatusId; |
166 | if ($dao->find(TRUE)) { | |
dcc4f6a7 | 167 | throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status')); |
6a488035 TO |
168 | } |
169 | } | |
dcc4f6a7 | 170 | CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId); |
6a488035 TO |
171 | //delete from membership Type table |
172 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
173 | $membershipStatus->id = $membershipStatusId; | |
a60c0bc8 SL |
174 | if (!$membershipStatus->find()) { |
175 | throw new CRM_Core_Exception(ts('Cannot delete membership status ' . $membershipStatusId)); | |
176 | } | |
6a488035 | 177 | $membershipStatus->delete(); |
9d4d481e | 178 | CRM_Member_PseudoConstant::flush('membershipStatus'); |
6a488035 TO |
179 | } |
180 | ||
181 | /** | |
100fef9d | 182 | * Find the membership status based on start date, end date, join date & status date. |
6a488035 | 183 | * |
04bc93fa | 184 | * Loop through all the membership status definitions, ordered by their |
185 | * weight. For each, we loop through all possible variations of the given | |
186 | * start, end, and join dates and adjust the starts and ends based on that | |
187 | * membership status's rules, where the last computed set of adjusted start | |
188 | * and end becomes a candidate. Then we compare that candidate to either | |
189 | * "today" or some other given date, and if it falls between the adjusted | |
190 | * start and end we have a match and we stop looping through status | |
191 | * definitions. Then we call a hook in case that wasn't enough loops. | |
192 | * | |
b2363ea8 TO |
193 | * @param string $startDate |
194 | * Start date of the member whose membership status is to be calculated. | |
195 | * @param string $endDate | |
196 | * End date of the member whose membership status is to be calculated. | |
197 | * @param string $joinDate | |
198 | * Join date of the member whose membership status is to be calculated. | |
04bc93fa | 199 | * @param string $statusDate |
200 | * Either the string "today" or a date against which we compare the adjusted start and end based on the status rules. | |
201 | * @param bool $excludeIsAdmin | |
202 | * Exclude the statuses having is_admin = 1. | |
c490a46a | 203 | * @param int $membershipTypeID |
04bc93fa | 204 | * Not used directly but gets passed to the hook. |
b2363ea8 | 205 | * @param array $membership |
04bc93fa | 206 | * Membership params as available to calling function - not used directly but passed to the hook. |
6a488035 | 207 | * |
da6b46f4 | 208 | * @return array |
6a488035 | 209 | */ |
acb1052e | 210 | public static function getMembershipStatusByDate( |
500cfe81 | 211 | $startDate, $endDate, $joinDate, |
2cb64970 | 212 | $statusDate = 'now', $excludeIsAdmin = FALSE, $membershipTypeID = NULL, $membership = [] |
6a488035 | 213 | ) { |
be2fb01f | 214 | $membershipDetails = []; |
5f11bbcc | 215 | |
2cb64970 | 216 | if (!$statusDate || $statusDate === 'today') { |
217 | $statusDate = 'now'; | |
218 | CRM_Core_Error::deprecatedFunctionWarning('pass now rather than today in'); | |
6a488035 TO |
219 | } |
220 | ||
ccd459b8 | 221 | $statusDate = date('Ymd', CRM_Utils_Time::strtotime($statusDate)); |
2cb64970 | 222 | |
6a488035 TO |
223 | //fix for CRM-3570, if we have statuses with is_admin=1, |
224 | //exclude these statuses from calculatation during import. | |
225 | $where = "is_active = 1"; | |
226 | if ($excludeIsAdmin) { | |
227 | $where .= " AND is_admin != 1"; | |
228 | } | |
229 | ||
230 | $query = " | |
231 | SELECT * | |
232 | FROM civicrm_membership_status | |
233 | WHERE {$where} | |
234 | ORDER BY weight ASC"; | |
235 | ||
33621c4f | 236 | $membershipStatus = CRM_Core_DAO::executeQuery($query); |
6a488035 | 237 | |
7438fb00 | 238 | $dates = [ |
ccd459b8 TO |
239 | 'start' => ($startDate && $startDate !== 'null') ? date('Ymd', CRM_Utils_Time::strtotime($startDate)) : '', |
240 | 'end' => ($endDate && $endDate !== 'null') ? date('Ymd', CRM_Utils_Time::strtotime($endDate)) : '', | |
241 | 'join' => ($joinDate && $joinDate !== 'null') ? date('Ymd', CRM_Utils_Time::strtotime($joinDate)) : '', | |
7438fb00 | 242 | ]; |
243 | ||
6a488035 TO |
244 | while ($membershipStatus->fetch()) { |
245 | $startEvent = NULL; | |
246 | $endEvent = NULL; | |
7438fb00 | 247 | foreach (['start', 'end'] as $eve) { |
248 | foreach ($dates as $dat => $date) { | |
6a488035 | 249 | // calculate start-event/date and end-event/date |
7438fb00 | 250 | if (($membershipStatus->{$eve . '_event'} === $dat . '_date') && |
251 | $date | |
6a488035 TO |
252 | ) { |
253 | if ($membershipStatus->{$eve . '_event_adjust_unit'} && | |
254 | $membershipStatus->{$eve . '_event_adjust_interval'} | |
255 | ) { | |
ccd459b8 TO |
256 | $month = date('m', CRM_Utils_Time::strtotime($date)); |
257 | $day = date('d', CRM_Utils_Time::strtotime($date)); | |
258 | $year = date('Y', CRM_Utils_Time::strtotime($date)); | |
6a488035 | 259 | // add in months |
5872a4b7 | 260 | if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'month') { |
261 | ${$eve . 'Event'} = date('Ymd', mktime(0, 0, 0, | |
262 | $month + $membershipStatus->{$eve . '_event_adjust_interval'}, | |
263 | $day, | |
264 | $year | |
b09fe5ed | 265 | )); |
6a488035 TO |
266 | } |
267 | // add in days | |
5872a4b7 | 268 | if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'day') { |
269 | ${$eve . 'Event'} = date('Ymd', mktime(0, 0, 0, | |
270 | $month, | |
271 | $day + $membershipStatus->{$eve . '_event_adjust_interval'}, | |
272 | $year | |
b09fe5ed | 273 | )); |
6a488035 TO |
274 | } |
275 | // add in years | |
5872a4b7 | 276 | if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'year') { |
277 | ${$eve . 'Event'} = date('Ymd', mktime(0, 0, 0, | |
278 | $month, | |
279 | $day, | |
280 | $year + $membershipStatus->{$eve . '_event_adjust_interval'} | |
b09fe5ed | 281 | )); |
6a488035 TO |
282 | } |
283 | // if no interval and unit, present | |
284 | } | |
285 | else { | |
7438fb00 | 286 | ${$eve . 'Event'} = $date; |
6a488035 TO |
287 | } |
288 | } | |
289 | } | |
290 | } | |
291 | ||
292 | // check if statusDate is in the range of start & end events. | |
293 | if ($startEvent && $endEvent) { | |
294 | if (($statusDate >= $startEvent) && ($statusDate <= $endEvent)) { | |
295 | $membershipDetails['id'] = $membershipStatus->id; | |
296 | $membershipDetails['name'] = $membershipStatus->name; | |
297 | } | |
298 | } | |
299 | elseif ($startEvent) { | |
300 | if ($statusDate >= $startEvent) { | |
301 | $membershipDetails['id'] = $membershipStatus->id; | |
302 | $membershipDetails['name'] = $membershipStatus->name; | |
303 | } | |
304 | } | |
305 | elseif ($endEvent) { | |
306 | if ($statusDate <= $endEvent) { | |
307 | $membershipDetails['id'] = $membershipStatus->id; | |
308 | $membershipDetails['name'] = $membershipStatus->name; | |
309 | } | |
310 | } | |
311 | ||
312 | // returns FIRST status record for which status_date is in range. | |
313 | if ($membershipDetails) { | |
5f11bbcc | 314 | break; |
6a488035 TO |
315 | } |
316 | } | |
317 | //end fetch | |
318 | ||
5f11bbcc EM |
319 | //we bundle the arguments into an array as we can't pass 8 variables to the hook otherwise |
320 | // the membership array might contain the pre-altered settings so we don't want to merge this | |
be2fb01f | 321 | $arguments = [ |
5f11bbcc EM |
322 | 'start_date' => $startDate, |
323 | 'end_date' => $endDate, | |
324 | 'join_date' => $joinDate, | |
325 | 'status_date' => $statusDate, | |
326 | 'exclude_is_admin' => $endDate, | |
327 | 'membership_type_id' => $membershipTypeID, | |
328 | 'start_event' => $startEvent, | |
329 | 'end_event' => $endEvent, | |
be2fb01f | 330 | ]; |
5f11bbcc | 331 | CRM_Utils_Hook::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership); |
6a488035 TO |
332 | return $membershipDetails; |
333 | } | |
334 | ||
335 | /** | |
fe482240 | 336 | * Function that return the status ids whose is_current_member is set. |
6a488035 | 337 | * |
77b97be7 | 338 | * @return array |
6a488035 TO |
339 | */ |
340 | public static function getMembershipStatusCurrent() { | |
be2fb01f | 341 | $statusIds = []; |
6a488035 TO |
342 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); |
343 | $membershipStatus->is_current_member = 1; | |
344 | $membershipStatus->find(); | |
345 | $membershipStatus->selectAdd(); | |
346 | $membershipStatus->selectAdd('id'); | |
347 | while ($membershipStatus->fetch()) { | |
348 | $statusIds[] = $membershipStatus->id; | |
349 | } | |
6a488035 TO |
350 | return $statusIds; |
351 | } | |
96025800 | 352 | |
6a488035 | 353 | } |