3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Member_BAO_MembershipStatus
extends CRM_Member_DAO_MembershipStatus
implements \Civi\Test\HookInterface
{
20 * Retrieve DB object and copy to defaults array.
22 * @param array $params
23 * Array of criteria values.
24 * @param array $defaults
25 * Array to be populated with found values.
28 * The DAO object, if found.
32 public static function retrieve($params, &$defaults) {
33 return self
::commonRetrieve(self
::class, $params, $defaults);
37 * Update the is_active flag in the db.
40 * Id of the database record.
41 * @param bool $is_active
42 * Value we want to set the is_active field.
45 * true if we found and updated the object, else false
47 public static function setIsActive($id, $is_active) {
48 return CRM_Core_DAO
::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active);
52 * Takes an associative array and creates a membership status object.
54 * @param array $params
55 * Array of name/value pairs.
57 * @throws CRM_Core_Exception
58 * @return CRM_Member_DAO_MembershipStatus
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.');
69 return self
::add($params);
73 * Add the membership status.
75 * @param array $params
76 * Reference array contains the values submitted by the form.
78 * Array contains the id - this param is deprecated.
80 * @return CRM_Member_DAO_MembershipStatus
82 public static function add(&$params, $ids = []) {
84 CRM_Core_Error
::deprecatedFunctionWarning('ids is a deprecated parameter');
86 $id = $params['id'] ??
$ids['membershipStatus'] ??
NULL;
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'];
94 if (empty($params['name']) && !empty($params['label'])) {
95 $params['name'] = $params['label'];
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);
105 // action is taken depending upon the mode
106 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
107 $membershipStatus->copyValues($params);
109 $membershipStatus->id
= $id;
111 $membershipStatus->save();
112 CRM_Member_PseudoConstant
::flush('membershipStatus');
113 return $membershipStatus;
117 * Get defaults for new entity.
120 public static function getDefaults() {
122 'is_active' => FALSE,
123 'is_current_member' => FALSE,
125 'is_default' => FALSE,
130 * Get membership status.
132 * @param int $membershipStatusId
136 public static function getMembershipStatus($membershipStatusId) {
138 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
139 $membershipStatus->id
= $membershipStatusId;
140 if ($membershipStatus->find(TRUE)) {
141 CRM_Core_DAO
::storeValues($membershipStatus, $statusDetails);
143 return $statusDetails;
147 * Delete membership status.
149 * @param int $membershipStatusId
151 * @throws CRM_Core_Exception
153 public static function del($membershipStatusId) {
154 static::deleteRecord(['id' => $membershipStatusId]);
158 * Callback for hook_civicrm_pre().
159 * @param \Civi\Core\Event\PreEvent $event
160 * @throws CRM_Core_Exception
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.'));
174 CRM_Utils_Weight
::delWeight('CRM_Member_DAO_MembershipStatus', $event->id
);
175 CRM_Member_PseudoConstant
::flush('membershipStatus');
180 * Find the membership status based on start date, end date, join date & status date.
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.
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.
208 public static function getMembershipStatusByDate(
209 $startDate, $endDate, $joinDate,
210 $statusDate = 'now', $excludeIsAdmin = FALSE, $membershipTypeID = NULL, $membership = []
212 $membershipDetails = [];
214 if (!$statusDate ||
$statusDate === 'today') {
216 CRM_Core_Error
::deprecatedFunctionWarning('pass now rather than today in');
219 $statusDate = date('Ymd', CRM_Utils_Time
::strtotime($statusDate));
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";
230 FROM civicrm_membership_status
232 ORDER BY weight ASC";
234 $membershipStatus = CRM_Core_DAO
::executeQuery($query);
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)) : '',
242 while ($membershipStatus->fetch()) {
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') &&
251 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
252 $membershipStatus->{$eve . '_event_adjust_interval'}
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));
258 if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'month') {
259 $
{$eve . 'Event'} = date('Ymd', mktime(0, 0, 0,
260 $month +
$membershipStatus->{$eve . '_event_adjust_interval'},
266 if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'day') {
267 $
{$eve . 'Event'} = date('Ymd', mktime(0, 0, 0,
269 $day +
$membershipStatus->{$eve . '_event_adjust_interval'},
274 if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'year') {
275 $
{$eve . 'Event'} = date('Ymd', mktime(0, 0, 0,
278 $year +
$membershipStatus->{$eve . '_event_adjust_interval'}
281 // if no interval and unit, present
284 $
{$eve . 'Event'} = $date;
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
;
297 elseif ($startEvent) {
298 if ($statusDate >= $startEvent) {
299 $membershipDetails['id'] = $membershipStatus->id
;
300 $membershipDetails['name'] = $membershipStatus->name
;
304 if ($statusDate <= $endEvent) {
305 $membershipDetails['id'] = $membershipStatus->id
;
306 $membershipDetails['name'] = $membershipStatus->name
;
310 // returns FIRST status record for which status_date is in range.
311 if ($membershipDetails) {
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
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,
329 CRM_Utils_Hook
::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
330 return $membershipDetails;
334 * Function that return the status ids whose is_current_member is set.
338 public static function getMembershipStatusCurrent() {
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
;