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