added pagination to working time widget (#1418)

This commit is contained in:
Kevin Papst
2020-01-30 21:51:27 +01:00
committed by GitHub
parent f2c6d8aee1
commit 9e2fccb105
9 changed files with 302 additions and 72 deletions

View File

@@ -0,0 +1,35 @@
<?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\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(path="/widgets")
* @Security("is_granted('IS_AUTHENTICATED_REMEMBERED')")
*/
final class WidgetController extends AbstractController
{
/**
* @Route(path="/working-time/{year}/{week}", requirements={"year": "[1-9]\d*", "week": "[0-9]\d*"}, name="widgets_working_time_chart", methods={"GET"})
* @Security("is_granted('view_own_timesheet')")
*/
public function workingtimechartAction($year, $week, Request $request): Response
{
return $this->render('widget/paginatedworkingtimechart.html.twig', [
'user' => $this->getUser(),
'year' => $year,
'week' => $week,
]);
}
}

View File

@@ -431,20 +431,20 @@ class TimesheetRepository extends EntityRepository
*/
public function getDailyStats(User $user, DateTime $begin, DateTime $end): array
{
$results = $this->getDailyData($begin, $end, $user);
/** @var Day[] $days */
$days = [];
// prefill the array
$tmp = clone $end;
$until = (int) $begin->format('Ymd');
while ((int) $tmp->format('Ymd') > $until) {
$tmp->modify('-1 day');
while ((int) $tmp->format('Ymd') >= $until) {
$last = clone $tmp;
$days[$last->format('Ymd')] = new Day($last, 0, 0.00);
$tmp->modify('-1 day');
}
$results = $this->getDailyData($begin, $end, $user);
foreach ($results as $statRow) {
$dateTime = new DateTime();
$dateTime->setDate($statRow['year'], $statRow['month'], $statRow['day']);

View File

@@ -63,8 +63,17 @@ class DailyWorkingTimeChart extends SimpleWidget
$options = $this->getOptions($options);
$user = $options['user'];
$begin = new DateTime($options['begin'], $this->dateTimeFactory->getTimezone());
$end = new DateTime($options['end'], $this->dateTimeFactory->getTimezone());
if ($options['begin'] instanceof DateTime) {
$begin = $options['begin'];
} else {
$begin = new DateTime($options['begin'], $this->dateTimeFactory->getTimezone());
}
if ($options['end'] instanceof DateTime) {
$end = $options['end'];
} else {
$end = new DateTime($options['end'], $this->dateTimeFactory->getTimezone());
}
return $this->repository->getDailyStats($user, $begin, $end);
}

View 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\Widget\Type;
use App\Repository\TimesheetRepository;
use App\Security\CurrentUser;
use App\Timesheet\UserDateTimeFactory;
use DateTime;
final class PaginatedWorkingTimeChart extends SimpleWidget
{
/**
* @var TimesheetRepository
*/
private $repository;
/**
* @var UserDateTimeFactory
*/
private $dateTimeFactory;
public function __construct(TimesheetRepository $repository, CurrentUser $user, UserDateTimeFactory $dateTime)
{
$this->repository = $repository;
$this->dateTimeFactory = $dateTime;
$this->setId('PaginatedWorkingTimeChart');
$this->setTitle('stats.yourWorkingHours');
$this->setOptions([
'year' => (new DateTime('now', $this->dateTimeFactory->getTimezone()))->format('Y'),
'week' => (new DateTime('now', $this->dateTimeFactory->getTimezone()))->format('W'),
'user' => $user->getUser(),
'type' => 'bar',
]);
}
public function getOptions(array $options = []): array
{
$options = parent::getOptions($options);
if (!in_array($options['type'], ['bar', 'line'])) {
$options['type'] = 'bar';
}
return $options;
}
private function getDate($year, $week, $day, $hour, $minute, $second)
{
$now = new DateTime('now', $this->dateTimeFactory->getTimezone());
$now->setISODate($year, $week, $day);
$now->setTime($hour, $minute, $second);
return $now;
}
public function getData(array $options = [])
{
$options = $this->getOptions($options);
$user = $options['user'];
$weekBegin = $this->getDate($options['year'], $options['week'], 1, 0, 0, 0);
$weekEnd = $this->getDate($options['year'], $options['week'], 7, 23, 59, 59);
return [
'begin' => clone $weekBegin,
'end' => clone $weekEnd,
'stats' => $this->repository->getDailyStats($user, $weekBegin, $weekEnd),
'day' => $this->repository->getStatistic(
'duration',
new DateTime('00:00:00', $this->dateTimeFactory->getTimezone()),
new DateTime('23:59:59', $this->dateTimeFactory->getTimezone()),
$user
),
'week' => $this->repository->getStatistic(
'duration',
$weekBegin,
$weekEnd,
$user
),
'month' => $this->repository->getStatistic(
'duration',
(clone $weekBegin)->setDate($weekBegin->format('Y'), $weekBegin->format('n'), 1)->setTime(0, 0, 0),
(clone $weekBegin)->setDate($weekBegin->format('Y'), $weekBegin->format('n'), $weekBegin->format('t'))->setTime(23, 59, 59),
$user
),
'year' => $this->repository->getStatistic(
'duration',
new DateTime(sprintf('01 january %s 00:00:00', $options['year']), $this->dateTimeFactory->getTimezone()),
new DateTime(sprintf('31 december %s 23:59:59', $options['year']), $this->dateTimeFactory->getTimezone()),
$user
),
];
}
}