From 5872a4b7195f919669bbed63adf220f725245d69 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 1 Aug 2020 18:52:46 +1200 Subject: [PATCH] [REF] Simplify membership status date handling We have a test error coming out of this code. I find it unreadable - I think this would help.... There are some obvious next steps if this is all good --- CRM/Member/BAO/MembershipStatus.php | 40 +++++++++++++---------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/CRM/Member/BAO/MembershipStatus.php b/CRM/Member/BAO/MembershipStatus.php index c7ddc7636c..fdd17ff082 100644 --- a/CRM/Member/BAO/MembershipStatus.php +++ b/CRM/Member/BAO/MembershipStatus.php @@ -240,12 +240,6 @@ class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus { foreach ($dates as $dat) { if (${$dat . 'Date'} && ${$dat . 'Date'} != "null") { ${$dat . 'Date'} = CRM_Utils_Date::customFormat(${$dat . 'Date'}, '%Y%m%d'); - - ${$dat . 'Year'} = substr(${$dat . 'Date'}, 0, 4); - - ${$dat . 'Month'} = substr(${$dat . 'Date'}, 4, 2); - - ${$dat . 'Day'} = substr(${$dat . 'Date'}, 6, 2); } else { ${$dat . 'Date'} = ''; @@ -266,13 +260,15 @@ class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus { ORDER BY weight ASC"; $membershipStatus = CRM_Core_DAO::executeQuery($query); - $hour = $minute = $second = 0; while ($membershipStatus->fetch()) { $startEvent = NULL; $endEvent = NULL; foreach ($events as $eve) { foreach ($dates as $dat) { + $month = date('m', strtotime(${$dat . 'Date'})); + $day = date('d', strtotime(${$dat . 'Date'})); + $year = date('Y', strtotime(${$dat . 'Date'})); // calculate start-event/date and end-event/date if (($membershipStatus->{$eve . '_event'} == $dat . '_date') && ${$dat . 'Date'} @@ -281,27 +277,27 @@ class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus { $membershipStatus->{$eve . '_event_adjust_interval'} ) { // add in months - if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'month') { - ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, - ${$dat . 'Month'} + $membershipStatus->{$eve . '_event_adjust_interval'}, - ${$dat . 'Day'}, - ${$dat . 'Year'} + if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'month') { + ${$eve . 'Event'} = date('Ymd', mktime(0, 0, 0, + $month + $membershipStatus->{$eve . '_event_adjust_interval'}, + $day, + $year )); } // add in days - if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'day') { - ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, - ${$dat . 'Month'}, - ${$dat . 'Day'} + $membershipStatus->{$eve . '_event_adjust_interval'}, - ${$dat . 'Year'} + if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'day') { + ${$eve . 'Event'} = date('Ymd', mktime(0, 0, 0, + $month, + $day + $membershipStatus->{$eve . '_event_adjust_interval'}, + $year )); } // add in years - if ($membershipStatus->{$eve . '_event_adjust_unit'} == 'year') { - ${$eve . 'Event'} = date('Ymd', mktime($hour, $minute, $second, - ${$dat . 'Month'}, - ${$dat . 'Day'}, - ${$dat . 'Year'} + $membershipStatus->{$eve . '_event_adjust_interval'} + if ($membershipStatus->{$eve . '_event_adjust_unit'} === 'year') { + ${$eve . 'Event'} = date('Ymd', mktime(0, 0, 0, + $month, + $day, + $year + $membershipStatus->{$eve . '_event_adjust_interval'} )); } // if no interval and unit, present -- 2.25.1