Weekly reporting view (#1892)
This commit is contained in:
101
src/Timesheet/DateTimeFactory.php
Normal file
101
src/Timesheet/DateTimeFactory.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Timesheet;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
|
||||
class DateTimeFactory
|
||||
{
|
||||
/**
|
||||
* @var DateTimeZone
|
||||
*/
|
||||
private $timezone;
|
||||
|
||||
public function __construct(?DateTimeZone $timezone = null)
|
||||
{
|
||||
if (null === $timezone) {
|
||||
$timezone = new \DateTimeZone(date_default_timezone_get());
|
||||
}
|
||||
$this->setTimezone($timezone);
|
||||
}
|
||||
|
||||
public function setTimezone(DateTimeZone $timezone)
|
||||
{
|
||||
$this->timezone = $timezone;
|
||||
}
|
||||
|
||||
public function getTimezone(): DateTimeZone
|
||||
{
|
||||
return $this->timezone;
|
||||
}
|
||||
|
||||
public function getStartOfMonth(): DateTime
|
||||
{
|
||||
$date = $this->createDateTime('first day of this month');
|
||||
$date->setTime(0, 0, 0);
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
public function getStartOfWeek(?DateTime $date = null): DateTime
|
||||
{
|
||||
if (null === $date) {
|
||||
$date = $this->createDateTime('now');
|
||||
}
|
||||
|
||||
return $this->createWeekDateTime($date->format('Y'), $date->format('W'), 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
public function getEndOfWeek(?DateTime $date = null): DateTime
|
||||
{
|
||||
if (null === $date) {
|
||||
$date = $this->createDateTime('now');
|
||||
}
|
||||
|
||||
return $this->createWeekDateTime($date->format('Y'), $date->format('W'), 7, 23, 59, 59);
|
||||
}
|
||||
|
||||
public function getEndOfMonth(): DateTime
|
||||
{
|
||||
$date = $this->createDateTime('last day of this month');
|
||||
$date->setTime(23, 59, 59);
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
private function createWeekDateTime($year, $week, $day, $hour, $minute, $second)
|
||||
{
|
||||
$date = new DateTime('now', $this->getTimezone());
|
||||
$date->setISODate($year, $week, $day);
|
||||
$date->setTime($hour, $minute, $second);
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
public function createDateTime(string $datetime = 'now'): DateTime
|
||||
{
|
||||
$date = new DateTime($datetime, $this->getTimezone());
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $format
|
||||
* @param null|string $datetime
|
||||
* @return bool|DateTime
|
||||
*/
|
||||
public function createDateTimeFromFormat(string $format, ?string $datetime = 'now')
|
||||
{
|
||||
$date = DateTime::createFromFormat($format, $datetime, $this->getTimezone());
|
||||
|
||||
return $date;
|
||||
}
|
||||
}
|
||||
@@ -11,26 +11,31 @@ namespace App\Timesheet;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Security\CurrentUser;
|
||||
use DateTimeZone;
|
||||
|
||||
class UserDateTimeFactory
|
||||
/**
|
||||
* @internal use DateTimeFactory instead: this one relies on the global context and will be deprecated in the future
|
||||
*/
|
||||
class UserDateTimeFactory extends DateTimeFactory
|
||||
{
|
||||
/**
|
||||
* @var \DateTimeZone
|
||||
*/
|
||||
private $timezone;
|
||||
/**
|
||||
* @var CurrentUser
|
||||
*/
|
||||
private $user;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $initializedFromUser = false;
|
||||
|
||||
public function __construct(CurrentUser $user)
|
||||
{
|
||||
parent::__construct(null);
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function getTimezone(): \DateTimeZone
|
||||
public function getTimezone(): DateTimeZone
|
||||
{
|
||||
if (null === $this->timezone) {
|
||||
if ($this->initializedFromUser === false) {
|
||||
$timezone = date_default_timezone_get();
|
||||
|
||||
$user = $this->user->getUser();
|
||||
@@ -38,44 +43,12 @@ class UserDateTimeFactory
|
||||
$timezone = $user->getTimezone();
|
||||
}
|
||||
|
||||
$this->timezone = new \DateTimeZone($timezone);
|
||||
$timezone = new DateTimeZone($timezone);
|
||||
|
||||
parent::setTimezone($timezone);
|
||||
$this->initializedFromUser = true;
|
||||
}
|
||||
|
||||
return $this->timezone;
|
||||
}
|
||||
|
||||
public function getStartOfMonth(): \DateTime
|
||||
{
|
||||
$date = $this->createDateTime('first day of this month');
|
||||
$date->setTime(0, 0, 0);
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
public function getEndOfMonth(): \DateTime
|
||||
{
|
||||
$date = $this->createDateTime('last day of this month');
|
||||
$date->setTime(23, 59, 59);
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
public function createDateTime(string $datetime = 'now'): \DateTime
|
||||
{
|
||||
$date = new \DateTime($datetime, $this->getTimezone());
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $format
|
||||
* @param null|string $datetime
|
||||
* @return bool|\DateTime
|
||||
*/
|
||||
public function createDateTimeFromFormat(string $format, ?string $datetime = 'now')
|
||||
{
|
||||
$date = \DateTime::createFromFormat($format, $datetime, $this->getTimezone());
|
||||
|
||||
return $date;
|
||||
return parent::getTimezone();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user