Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 98 additions & 13 deletions src/Backend/Core/Engine/DataGridFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Backend\Core\Engine;

use Backend\Core\Engine\Model as BackendModel;
use Backend\Core\Language\Language;
use Backend\Core\Language\Language as BackendLanguage;
use SpoonDate;
use IntlDateFormatter;
use function Symfony\Component\String\s;

/**
Expand Down Expand Up @@ -70,9 +71,16 @@ public static function getDate(int $timestamp): string
return '';
}

$format = 'j F Y';
$date = new IntlDateFormatter(
BackendLanguage::getInterfaceLanguage(),
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
null,
null,
'd MMMM yyyy'
)->format($timestamp);

return SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage());
return $date;
}

/**
Expand All @@ -89,9 +97,17 @@ public static function getLongDate(int $timestamp): string
return '';
}

$format = 'j F Y H:i';

return SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage());
$date = new IntlDateFormatter(
BackendLanguage::getInterfaceLanguage(),
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
null,
null,
'd MMMM yyyy HH:mm'
)->format($timestamp);

return $date;
}

/**
Expand All @@ -108,9 +124,17 @@ public static function getTime(int $timestamp): string
return '';
}

$format = 'H:i';

return SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage());
$date = new IntlDateFormatter(
BackendLanguage::getInterfaceLanguage(),
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
null,
null,
'HH:mm'
)->format($timestamp);

return $date;
}

/**
Expand All @@ -122,15 +146,76 @@ public static function getTime(int $timestamp): string
*/
public static function getTimeAgo(int $timestamp): string
{
// get user setting for long dates
$format = 'j F Y H:i';
if ($timestamp === 0) {
return '';
}

$locale = BackendLanguage::getInterfaceLanguage();

$dateTimeFormatter = new IntlDateFormatter(
$locale,
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
null,
null,
'yyyy-MM-dd HH:mm:ss'
);

$titleFormatter = new IntlDateFormatter(
$locale,
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
null,
null,
'd MMMM yyyy HH:mm'
);

$diff = abs(time() - $timestamp);

$seconds = (int) $diff;
$minutes = (int) floor($seconds / 60);
$hours = (int) floor($seconds / 3600);
$days = (int) floor($seconds / 86400);
$months = (int) floor($seconds / (30 * 86400));
$years = (int) floor($seconds / (365 * 86400));

if ($years > 0) {
$count = $years;
$keySingular = 'TimeAgoYearSingular';
$keyPlural = 'TimeAgoYearPlural';
} elseif ($months > 0) {
$count = $months;
$keySingular = 'TimeAgoMonthSingular';
$keyPlural = 'TimeAgoMonthPlural';
} elseif ($days > 0) {
$count = $days;
$keySingular = 'TimeAgoDaySingular';
$keyPlural = 'TimeAgoDayPlural';
} elseif ($hours > 0) {
$count = $hours;
$keySingular = 'TimeAgoHourSingular';
$keyPlural = 'TimeAgoHourPlural';
} elseif ($minutes > 0) {
$count = $minutes;
$keySingular = 'TimeAgoMinuteSingular';
$keyPlural = 'TimeAgoMinutePlural';
} else {
$count = $seconds;
$keySingular = 'TimeAgoSecondSingular';
$keyPlural = 'TimeAgoSecondPlural';
}

if ($count <= 0) {
return Language::lbl('TimeAgoEmpty');
}

// get the time ago as a string
$timeAgo = SpoonDate::getTimeAgo($timestamp, BackendLanguage::getInterfaceLanguage(), $format);
$timeAgo = $count === 1
? Language::lbl($keySingular)
: Language::lblWithParameters($keyPlural, [$count]);

return '<time tabindex="0" data-toggle="tooltip" datetime="'
. SpoonDate::getDate('Y-m-d H:i:s', $timestamp)
. '" title="' . SpoonDate::getDate($format, $timestamp, BackendLanguage::getInterfaceLanguage())
. $dateTimeFormatter->format($timestamp)
. '" title="' . $titleFormatter->format($timestamp)
. '">' . $timeAgo . '</time>';
}

Expand Down
60 changes: 60 additions & 0 deletions src/Backend/Core/Language/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,64 @@ public static function isActiveLanguage(string $language): bool
{
return in_array($language, self::getActiveLanguages(), true);
}

/**
* Get a label with parameters
*
* @param string $key
* @param array $parameters
* @param bool $fallback
*
* @return string
*/
public static function lblWithParameters(string $key, array $parameters = [], bool $fallback = true): string
{
$label = self::getLabel($key, $fallback);

if ($parameters === []) {
return $label;
}

return vsprintf($label, $parameters);
}

/**
* Get a message with parameters
*
* @param string $key
* @param array $parameters
* @param bool $fallback
*
* @return string
*/
public static function msgWithParameters(string $key, array $parameters = [], bool $fallback = true): string
{
$message = self::getMessage($key, $fallback);

if ($parameters === []) {
return $message;
}

return vsprintf($message, $parameters);
}

/**
* Get an error with parameters
*
* @param string $key
* @param array $parameters
* @param bool $fallback
*
* @return string
*/
public static function errWithParameters(string $key, array $parameters = [], bool $fallback = true): string
{
$error = self::getError($key, $fallback);

if ($parameters === []) {
return $error;
}

return vsprintf($error, $parameters);
}
}
2 changes: 1 addition & 1 deletion src/Backend/Modules/Faq/Engine/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function deleteFeedback(int $itemId): void
{
BackendModel::getContainer()->get('database')->update(
'faq_feedback',
['processed' => true, 'edited_on' => \SpoonDate::getDate('Y-m-d H:i:s')],
['processed' => true, 'edited_on' => date('Y-m-d H:i:s')],
'id = ?',
$itemId
);
Expand Down
16 changes: 11 additions & 5 deletions src/Backend/Modules/FormBuilder/Actions/ExportData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Backend\Modules\FormBuilder\Engine\Model as BackendFormBuilderModel;
use Common\Exception\RedirectException;
use ForkCMS\Utility\Csv\Writer;
use IntlDateFormatter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use function Symfony\Component\String\s;

Expand Down Expand Up @@ -186,12 +187,17 @@ private function setItems(): void
foreach ($records as $row) {
// first row of a submission
if (!isset($data[$row['data_id']])) {
$date = new IntlDateFormatter(
BL::getWorkingLanguage(),
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
null,
null,
'yyyy-MM-dd HH:mm:ss'
)->format($row['sent_on']);

$data[$row['data_id']][$lblSessionId] = $row['session_id'];
$data[$row['data_id']][$lblSentOn] = \SpoonDate::getDate(
'Y-m-d H:i:s',
$row['sent_on'],
BL::getWorkingLanguage()
);
$data[$row['data_id']][$lblSentOn] = $date;
}

// value is serialized
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Core/Engine/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(string $title, string $link, string $description, ar

// set feed properties
$this->setLanguage(LANGUAGE);
$this->setCopyright(\SpoonDate::getDate('Y') . ' ' . $siteTitle);
$this->setCopyright(date('Y') . ' ' . $siteTitle);
$this->setGenerator($siteTitle);
$this->setImage(SITE_URL . FRONTEND_CORE_URL . '/Layout/images/rss_image.png', $title, $link);

Expand Down
25 changes: 19 additions & 6 deletions src/Frontend/Modules/Blog/Actions/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Frontend\Core\Language\Language as FL;
use Frontend\Core\Engine\Navigation as FrontendNavigation;
use Frontend\Modules\Blog\Engine\Model as FrontendBlogModel;
use IntlDateFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use function Symfony\Component\String\s;
Expand Down Expand Up @@ -170,8 +171,16 @@ private function setPageTitle(): void
$this->header->setPageTitle(s(FL::lbl('Archive'))->title()->toString());
$this->header->setPageTitle($this->startDate->format('Y'));
if ($this->hasMonth) {
$date = new IntlDateFormatter(
LANGUAGE,
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
null,
null,
'MMMM'
Comment on lines +174 to +180

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The IntlDateFormatter instantiation is duplicated and could be shared between methods.

Both setPageTitle() and addPageToBreadcrumb() construct the same IntlDateFormatter for the month label. Please extract this into a private helper (e.g. formatMonthName(DateTimeInterface $date): string) and call it from both methods to remove duplication and keep future changes (pattern/locale) in one place.

Suggested implementation:

        if ($this->hasMonth) {
            $this->header->setPageTitle(
                $this->formatMonthName($this->startDate)
            );
        }

`.

Here are the edits:

<file_operations>
<file_operation operation="edit" file_path="src/Frontend/Modules/Blog/Actions/Archive.php">
<<<<<<< SEARCH
if ($this->hasMonth) {
$date = new IntlDateFormatter(
LANGUAGE,
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
null,
null,
'MMMM'
)->format($this->startDate->getTimestamp());
$this->header->setPageTitle(
$date
);
}

    if ($this->hasMonth) {
        $this->header->setPageTitle(
            $this->formatMonthName($this->startDate)
        );
    }

REPLACE
</file_operation>
</file_operations>

<additional_changes>
To fully implement the refactor you described, you should also:

  1. Add a private helper method on the Archive class to encapsulate the IntlDateFormatter logic, for example:

    private function formatMonthName(\DateTimeInterface $date): string
    {
        return (new IntlDateFormatter(
            LANGUAGE,
            IntlDateFormatter::NONE,
            IntlDateFormatter::NONE,
            null,
            null,
            'MMMM'
        ))->format($date->getTimestamp());
    }
    • Place this method alongside the other private helper methods in the Archive class.
    • If the file does not already import DateTimeInterface, either fully qualify it as above or add use DateTimeInterface; at the top and then use DateTimeInterface in the signature.
  2. Find the duplicated IntlDateFormatter instantiation in addPageToBreadcrumb() (it should look very similar or identical to the block that was just replaced) and refactor it to use the helper as well, e.g.:

    $monthLabel = $this->formatMonthName($this->startDate);
    // use $monthLabel where the formatted month was previously used
  3. Ensure both setPageTitle() usage (the one shown in your snippet) and the breadcrumb code now rely solely on formatMonthName(), so future changes to locale/pattern only need to be applied in one place.

)->format($this->startDate->getTimestamp());
$this->header->setPageTitle(
\SpoonDate::getDate('F', $this->startDate->getTimestamp(), LANGUAGE)
$date
);
}
}
Expand All @@ -181,12 +190,16 @@ private function addPageToBreadcrumb(): void
$this->breadcrumb->addElement(s(FL::lbl('Archive'))->title()->toString());
$this->breadcrumb->addElement($this->startDate->format('Y'));
if ($this->hasMonth) {
$date = new IntlDateFormatter(
LANGUAGE,
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
null,
null,
'MMMM'
)->format($this->startDate->getTimestamp());
$this->breadcrumb->addElement(
\SpoonDate::getDate(
'F',
$this->startDate->getTimestamp(),
LANGUAGE
)
$date
);
}
}
Expand Down
Loading