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
{
20 * Fetch object based on array of properties.
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.
27 * @return CRM_Member_BAO_MembershipStatus
29 public static function retrieve(&$params, &$defaults) {
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;
40 * Update the is_active flag in the db.
43 * Id of the database record.
44 * @param bool $is_active
45 * Value we want to set the is_active field.
48 * true if we found and updated the object, else false
50 public static function setIsActive($id, $is_active) {
51 return CRM_Core_DAO
::setFieldValue('CRM_Member_DAO_MembershipStatus', $id, 'is_active', $is_active);
55 * Takes an associative array and creates a membership Status object.
57 * @param array $params
58 * Array of name/value pairs.
60 * @throws CRM_Core_Exception
61 * @return CRM_Member_DAO_MembershipStatus
63 public static function create($params) {
64 if (empty($params['id'])) {
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)) {
69 throw new CRM_Core_Exception('A membership status with this name already exists.');
72 return self
::add($params);
76 * Add the membership types.
78 * @param array $params
79 * Reference array contains the values submitted by the form.
81 * Array contains the id - this param is deprecated.
83 * @return CRM_Member_DAO_MembershipStatus
85 public static function add(&$params, $ids = []) {
87 CRM_Core_Error
::deprecatedFunctionWarning('ids is a deprecated parameter');
89 $id = $params['id'] ??
$ids['membershipStatus'] ??
NULL;
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'];
97 if (empty($params['name']) && !empty($params['label'])) {
98 $params['name'] = $params['label'];
102 // set all other defaults to false.
103 if (!empty($params['is_default'])) {
104 $query = "UPDATE civicrm_membership_status SET is_default = 0";
105 CRM_Core_DAO
::executeQuery($query);
108 // action is taken depending upon the mode
109 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
110 $membershipStatus->copyValues($params);
112 $membershipStatus->id
= $id;
114 $membershipStatus->save();
115 CRM_Member_PseudoConstant
::flush('membershipStatus');
116 return $membershipStatus;
120 * Get defaults for new entity.
123 public static function getDefaults() {
125 'is_active' => FALSE,
126 'is_current_member' => FALSE,
128 'is_default' => FALSE,
133 * Get membership status.
135 * @param int $membershipStatusId
139 public static function getMembershipStatus($membershipStatusId) {
141 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
142 $membershipStatus->id
= $membershipStatusId;
143 if ($membershipStatus->find(TRUE)) {
144 CRM_Core_DAO
::storeValues($membershipStatus, $statusDetails);
146 return $statusDetails;
150 * Delete membership Types.
152 * @param int $membershipStatusId
154 * @throws CRM_Core_Exception
156 public static function del($membershipStatusId) {
158 //checking if membership status is present in some other table
161 $dependency = ['Membership', 'MembershipLog'];
162 foreach ($dependency as $name) {
163 $baoString = 'CRM_Member_BAO_' . $name;
164 $dao = new $baoString();
165 $dao->status_id
= $membershipStatusId;
166 if ($dao->find(TRUE)) {
167 throw new CRM_Core_Exception(ts('This membership status cannot be deleted as memberships exist with this status'));
170 CRM_Utils_Weight
::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId);
171 //delete from membership Type table
172 $membershipStatus = new CRM_Member_DAO_MembershipStatus();
173 $membershipStatus->id
= $membershipStatusId;
174 if (!$membershipStatus->find()) {
175 throw new CRM_Core_Exception(ts('Cannot delete membership status ' . $membershipStatusId));
177 $membershipStatus->delete();
178 CRM_Member_PseudoConstant
::flush('membershipStatus');
182 * Find the membership status based on start date, end date, join date & status date.
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.
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.
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.
203 * @param int $membershipTypeID
204 * Not used directly but gets passed to the hook.
205 * @param array $membership
206 * Membership params as available to calling function - not used directly but passed to the hook.
210 public static function getMembershipStatusByDate(
211 $startDate, $endDate, $joinDate,
212 $statusDate = 'now', $excludeIsAdmin = FALSE, $membershipTypeID = NULL, $membership = []
214 $membershipDetails = [];
216 if (!$statusDate ||
$statusDate === 'today') {
218 CRM_Core_Error
::deprecatedFunctionWarning('pass now rather than today in');
221 $statusDate = date('Ymd', strtotime($statusDate));
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";
232 FROM civicrm_membership_status
234 ORDER BY weight ASC";
236 $membershipStatus = CRM_Core_DAO
::executeQuery($query);
239 'start' => ($startDate && $startDate !== 'null') ?
date('Ymd', strtotime($startDate)) : '',
240 'end' => ($endDate && $endDate !== 'null') ?
date('Ymd', strtotime($endDate)) : '',
241 'join' => ($joinDate && $joinDate !== 'null') ?
date('Ymd', strtotime($joinDate)) : '',
244 while ($membershipStatus->fetch()) {
247 foreach (['start', 'end'] as $eve) {
248 foreach ($dates as $dat => $date) {
249 // calculate start-event/date and end-event/date
250 if (($membershipStatus->{$eve . '_event'} === $dat . '_date') &&
253 if ($membershipStatus->{$eve . '_event_adjust_unit'} &&
254 $membershipStatus->{$eve . '_event_adjust_interval'}
256 $month = date('m', strtotime($date));
257 $day = date('d', strtotime($date));
258 $year = date('Y', strtotime($date));
260 if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'month') {
261 $
{$eve . 'Event'} = date('Ymd', mktime(0, 0, 0,
262 $month +
$membershipStatus->{$eve . '_event_adjust_interval'},
268 if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'day') {
269 $
{$eve . 'Event'} = date('Ymd', mktime(0, 0, 0,
271 $day +
$membershipStatus->{$eve . '_event_adjust_interval'},
276 if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'year') {
277 $
{$eve . 'Event'} = date('Ymd', mktime(0, 0, 0,
280 $year +
$membershipStatus->{$eve . '_event_adjust_interval'}
283 // if no interval and unit, present
286 $
{$eve . 'Event'} = $date;
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
;
299 elseif ($startEvent) {
300 if ($statusDate >= $startEvent) {
301 $membershipDetails['id'] = $membershipStatus->id
;
302 $membershipDetails['name'] = $membershipStatus->name
;
306 if ($statusDate <= $endEvent) {
307 $membershipDetails['id'] = $membershipStatus->id
;
308 $membershipDetails['name'] = $membershipStatus->name
;
312 // returns FIRST status record for which status_date is in range.
313 if ($membershipDetails) {
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
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,
331 CRM_Utils_Hook
::alterCalculatedMembershipStatus($membershipDetails, $arguments, $membership);
332 return $membershipDetails;
336 * Function that return the status ids whose is_current_member is set.
340 public static function getMembershipStatusCurrent() {
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
;