added last months and last quarters to daterange-picker selections (#5317)
This commit is contained in:
@@ -14,6 +14,7 @@ use App\Entity\User;
|
||||
use App\Form\Model\DateRange;
|
||||
use App\Timesheet\DateTimeFactory;
|
||||
use App\Utils\FormFormatConverter;
|
||||
use App\Utils\LocaleFormatter;
|
||||
use IntlDateFormatter;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
@@ -49,6 +50,7 @@ final class DateRangeType extends AbstractType
|
||||
'max_day' => null,
|
||||
'locale' => \Locale::getDefault(),
|
||||
]);
|
||||
$resolver->setAllowedTypes('locale', 'string');
|
||||
$resolver->setAllowedTypes('separator', 'string');
|
||||
$resolver->addAllowedTypes('timezone', 'string');
|
||||
$resolver->addAllowedTypes('min_day', ['null', 'string', \DateTimeInterface::class]);
|
||||
@@ -74,9 +76,15 @@ final class DateRangeType extends AbstractType
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options): void
|
||||
{
|
||||
if (!\is_string($options['locale'])) {
|
||||
throw new \InvalidArgumentException('Locale needs to be a string');
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = $options['user'];
|
||||
$factory = DateTimeFactory::createByUser($user);
|
||||
$formatter = new LocaleFormatter($this->localeService, $options['locale']);
|
||||
$thisMonth = \DateTimeImmutable::createFromInterface($factory->getStartOfMonth());
|
||||
|
||||
if ($options['with_presets']) {
|
||||
$ranges = [
|
||||
@@ -85,11 +93,32 @@ final class DateRangeType extends AbstractType
|
||||
'daterangepicker.yesterday' => [$factory->createDateTime('-1 day 00:00:00'), $factory->createDateTime('-1 day 23:59:59')],
|
||||
'daterangepicker.thisWeek' => [$factory->getStartOfWeek(), $factory->getEndOfWeek()],
|
||||
'daterangepicker.lastWeek' => [$factory->getStartOfWeek('-1 week'), $factory->getEndOfWeek('-1 week')],
|
||||
'daterangepicker.thisMonth' => [$factory->getStartOfMonth(), $factory->getEndOfMonth()],
|
||||
'daterangepicker.lastMonth' => [$factory->getStartOfLastMonth(), $factory->getEndOfLastMonth()],
|
||||
'daterangepicker.thisYearUntilNow' => [$factory->createStartOfYear(), $factory->createDateTime('23:59:59')],
|
||||
'divider1' => [],
|
||||
];
|
||||
|
||||
// last months
|
||||
$month = clone $thisMonth;
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$end = $factory->getEndOfMonth($month);
|
||||
$ranges[$formatter->monthName($month, true)] = [$month, $end];
|
||||
$month = $month->sub(new \DateInterval('P1M'));
|
||||
}
|
||||
$ranges['divider2'] = [];
|
||||
|
||||
// last quarters
|
||||
$month = clone $thisMonth;
|
||||
$quarterStartMonth = (int) (floor(($month->format('n') - 1) / 3) * 3) + 1;
|
||||
$month = \DateTimeImmutable::createFromInterface($factory->getStartOfMonth($month->setDate((int) $month->format('Y'), $quarterStartMonth, 1)));
|
||||
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$end = $factory->getEndOfMonth($month->add(new \DateInterval('P2M')));
|
||||
$ranges[$formatter->quarterName($month, true)] = [$month, $end];
|
||||
$month = $month->sub(new \DateInterval('P3M'));
|
||||
}
|
||||
$ranges['divider3'] = [];
|
||||
|
||||
// last years
|
||||
$thisYear = (int) $factory->createStartOfYear()->format('Y');
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$year = $thisYear - $i;
|
||||
|
||||
@@ -308,6 +308,11 @@ final class LocaleFormatter
|
||||
return $this->formatIntl($dateTime, ($withYear ? 'LLLL yyyy' : 'LLLL'));
|
||||
}
|
||||
|
||||
public function quarterName(\DateTimeInterface $dateTime, bool $withYear = false): string
|
||||
{
|
||||
return $this->formatIntl($dateTime, ($withYear ? 'QQQ yyyy' : 'QQQ'));
|
||||
}
|
||||
|
||||
public function dayName(\DateTimeInterface $dateTime, bool $short = false): string
|
||||
{
|
||||
return $this->formatIntl($dateTime, ($short ? 'EE' : 'EEEE'));
|
||||
|
||||
Reference in New Issue
Block a user