Skip to content

Commit

Permalink
Rationalise date formatting
Browse files Browse the repository at this point in the history
This came out of discovering a place in the code was 'accidentally formatted correctly'
and finding it far too hard to figure out the right way to specify the date formatting -
something that seems confirmed by finding various places with hard-coded date formattin
rather than using the setting
  • Loading branch information
eileenmcnaughton committed Oct 20, 2020
1 parent 42dada1 commit 56f54f0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CRM/Contribute/Form/Contribution/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1590,8 +1590,8 @@ protected function postProcessMembership(
foreach ($form->_lineItem[$form->_priceSetId] as & $priceFieldOp) {
if (!empty($priceFieldOp['membership_type_id']) && $membership->membership_type_id == $priceFieldOp['membership_type_id']) {
$membershipOb = $membership;
$priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date::customFormat($membershipOb->start_date, '%B %E%f, %Y') : '-';
$priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date::customFormat($membershipOb->end_date, '%B %E%f, %Y') : '-';
$priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date::formatDateOnlyLong($membershipOb->start_date) : '-';
$priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date::formatDateOnlyLong($membershipOb->end_date) : '-';
}
else {
$priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A';
Expand Down
10 changes: 5 additions & 5 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,9 @@ public static function emailReceipt(&$form, &$formValues, &$membership, $customV
$form->assign('formValues', $formValues);

if (empty($lineItem)) {
$form->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date, '%B %E%f, %Y'));
$form->assign('mem_start_date', CRM_Utils_Date::formatDateOnlyLong($membership->start_date));
if (!CRM_Utils_System::isNull($membership->end_date)) {
$form->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date, '%B %E%f, %Y'));
$form->assign('mem_end_date', CRM_Utils_Date::formatDateOnlyLong($membership->end_date));
}
$form->assign('membership_name', CRM_Member_PseudoConstant::membershipType($membership->membership_type_id));
}
Expand Down Expand Up @@ -1610,8 +1610,8 @@ public function submit() {
$totalTaxAmount = 0;
foreach ($lineItem[$this->_priceSetId] as & $priceFieldOp) {
if (!empty($priceFieldOp['membership_type_id'])) {
$priceFieldOp['start_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'], '%B %E%f, %Y') : '-';
$priceFieldOp['end_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'], '%B %E%f, %Y') : '-';
$priceFieldOp['start_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'] ? CRM_Utils_Date::formatDateOnlyLong($membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date']) : '-';
$priceFieldOp['end_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'] ? CRM_Utils_Date::formatDateOnlyLong($membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date']) : '-';
}
else {
$priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A';
Expand Down Expand Up @@ -1845,7 +1845,7 @@ protected function getStatusMessageForCreate($endDate, $membershipTypes, $create
}

if ($memEndDate && $memEndDate !== 'null') {
$memEndDate = CRM_Utils_Date::customFormat($memEndDate);
$memEndDate = CRM_Utils_Date::formatDateOnlyLong($memEndDate);
$statusMsg[$memType] .= ' ' . ts('The new membership End Date is %1.', [1 => $memEndDate]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Member/Form/MembershipRenewal.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,8 @@ protected function sendReceipt($membership) {
$membership->membership_type_id
));
$this->assign('customValues', $customValues);
$this->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date));
$this->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date));
$this->assign('mem_start_date', CRM_Utils_Date::formatDateOnlyLong($membership->start_date));
$this->assign('mem_end_date', CRM_Utils_Date::formatDateOnlyLong($membership->end_date));
if ($this->_mode) {
$this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters(
$this->_params,
Expand Down
13 changes: 13 additions & 0 deletions CRM/Utils/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,19 @@ public static function customFormat($dateString, $format = NULL, $dateParts = NU
}
}

/**
* Format the field according to the site's preferred date format.
*
* This is likely to look something like December 31st, 2020.
*
* @param string $date
*
* @return string
*/
public static function formatDateOnlyLong(string $date):string {
return CRM_Utils_Date::customFormat($date, Civi::settings()->get('dateformatFull'));
}

/**
* Wrapper for customFormat that takes a timestamp
*
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/CRM/Member/Form/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public function testSubmit($thousandSeparator) {
$this->createLoggedInUser();
$params = [
'cid' => $this->_individualId,
'join_date' => date('2/d/Y'),
'join_date' => date('Y-m-d'),
'start_date' => '',
'end_date' => '',
// This format reflects the organisation & then the type.
Expand Down Expand Up @@ -802,7 +802,7 @@ public function testSubmitRecur() {
], 1);
$this->assertEquals([
[
'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been added. The new membership End Date is ' . date('F jS, Y', strtotime('last day of this month')) . ' 12:00 AM.',
'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been added. The new membership End Date is ' . date('F jS, Y', strtotime('last day of this month')) . '.',
'title' => 'Complete',
'type' => 'success',
'options' => NULL,
Expand Down

0 comments on commit 56f54f0

Please sign in to comment.