cleanup global context usage in widgets
This commit is contained in:
@@ -30,7 +30,7 @@ abstract class AbstractWidgetType implements WidgetInterface
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
public function setId(string $id): AbstractWidgetType
|
||||
public function setId(string $id): self
|
||||
{
|
||||
$this->id = $id;
|
||||
|
||||
@@ -42,7 +42,7 @@ abstract class AbstractWidgetType implements WidgetInterface
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setData($data): AbstractWidgetType
|
||||
public function setData($data): self
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
@@ -58,7 +58,7 @@ abstract class AbstractWidgetType implements WidgetInterface
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function setTitle(string $title): AbstractWidgetType
|
||||
public function setTitle(string $title): self
|
||||
{
|
||||
$this->title = $title;
|
||||
|
||||
@@ -70,7 +70,7 @@ abstract class AbstractWidgetType implements WidgetInterface
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function setOptions(array $options): AbstractWidgetType
|
||||
public function setOptions(array $options): self
|
||||
{
|
||||
foreach ($options as $key => $value) {
|
||||
$this->options[$key] = $value;
|
||||
|
||||
@@ -9,10 +9,13 @@
|
||||
|
||||
namespace App\Widget\Type;
|
||||
|
||||
class Counter extends SimpleWidget
|
||||
use App\Repository\TimesheetRepository;
|
||||
|
||||
final class Counter extends SimpleStatisticChart
|
||||
{
|
||||
public function __construct()
|
||||
public function __construct(TimesheetRepository $repository)
|
||||
{
|
||||
parent::__construct($repository);
|
||||
$this->setOption('dataType', 'int');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,11 @@ namespace App\Widget\Type;
|
||||
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Project;
|
||||
use App\Entity\User;
|
||||
use App\Repository\TimesheetRepository;
|
||||
use App\Security\CurrentUser;
|
||||
use App\Timesheet\UserDateTimeFactory;
|
||||
use DateTime;
|
||||
|
||||
class DailyWorkingTimeChart extends SimpleWidget
|
||||
class DailyWorkingTimeChart extends SimpleWidget implements UserWidget
|
||||
{
|
||||
public const DEFAULT_CHART = 'bar';
|
||||
|
||||
@@ -24,27 +23,26 @@ class DailyWorkingTimeChart extends SimpleWidget
|
||||
* @var TimesheetRepository
|
||||
*/
|
||||
protected $repository;
|
||||
/**
|
||||
* @var UserDateTimeFactory
|
||||
*/
|
||||
private $dateTimeFactory;
|
||||
|
||||
public function __construct(TimesheetRepository $repository, CurrentUser $user, UserDateTimeFactory $dateTime)
|
||||
public function __construct(TimesheetRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->dateTimeFactory = $dateTime;
|
||||
$this->setId('DailyWorkingTimeChart');
|
||||
$this->setTitle('stats.yourWorkingHours');
|
||||
$this->setOptions([
|
||||
'begin' => 'monday this week 00:00:00',
|
||||
'end' => 'sunday this week 23:59:59',
|
||||
'color' => '',
|
||||
'user' => $user->getUser(),
|
||||
'type' => self::DEFAULT_CHART,
|
||||
'id' => '',
|
||||
]);
|
||||
}
|
||||
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->setOption('user', $user);
|
||||
}
|
||||
|
||||
public function getOptions(array $options = []): array
|
||||
{
|
||||
$options = parent::getOptions($options);
|
||||
@@ -65,16 +63,20 @@ class DailyWorkingTimeChart extends SimpleWidget
|
||||
$options = $this->getOptions($options);
|
||||
|
||||
$user = $options['user'];
|
||||
if (null === $user || !($user instanceof User)) {
|
||||
throw new \InvalidArgumentException('Widget option "user" must be an instance of ' . User::class);
|
||||
}
|
||||
|
||||
if ($options['begin'] instanceof DateTime) {
|
||||
$begin = $options['begin'];
|
||||
} else {
|
||||
$begin = new DateTime($options['begin'], $this->dateTimeFactory->getTimezone());
|
||||
$begin = new DateTime($options['begin'], new \DateTimeZone($user->getTimezone()));
|
||||
}
|
||||
|
||||
if ($options['end'] instanceof DateTime) {
|
||||
$end = $options['end'];
|
||||
} else {
|
||||
$end = new DateTime($options['end'], $this->dateTimeFactory->getTimezone());
|
||||
$end = new DateTime($options['end'], new \DateTimeZone($user->getTimezone()));
|
||||
}
|
||||
|
||||
$activities = [];
|
||||
|
||||
@@ -9,37 +9,40 @@
|
||||
|
||||
namespace App\Widget\Type;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Repository\TimesheetRepository;
|
||||
use App\Security\CurrentUser;
|
||||
use App\Timesheet\UserDateTimeFactory;
|
||||
use DateTime;
|
||||
|
||||
final class PaginatedWorkingTimeChart extends SimpleWidget
|
||||
final class PaginatedWorkingTimeChart extends SimpleWidget implements UserWidget
|
||||
{
|
||||
/**
|
||||
* @var TimesheetRepository
|
||||
*/
|
||||
private $repository;
|
||||
/**
|
||||
* @var UserDateTimeFactory
|
||||
*/
|
||||
private $dateTimeFactory;
|
||||
|
||||
public function __construct(TimesheetRepository $repository, CurrentUser $user, UserDateTimeFactory $dateTime)
|
||||
public function __construct(TimesheetRepository $repository)
|
||||
{
|
||||
$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(),
|
||||
'year' => (new DateTime('now'))->format('Y'),
|
||||
'week' => (new DateTime('now'))->format('W'),
|
||||
'type' => 'bar',
|
||||
]);
|
||||
}
|
||||
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->setOption('user', $user);
|
||||
$now = new DateTime('now', new \DateTimeZone($user->getTimezone()));
|
||||
$this->setOptions([
|
||||
'year' => $now->format('Y'),
|
||||
'week' => $now->format('W'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function getOptions(array $options = []): array
|
||||
{
|
||||
$options = parent::getOptions($options);
|
||||
@@ -51,9 +54,9 @@ final class PaginatedWorkingTimeChart extends SimpleWidget
|
||||
return $options;
|
||||
}
|
||||
|
||||
private function getDate($year, $week, $day, $hour, $minute, $second)
|
||||
private function getDate(\DateTimeZone $timezone, $year, $week, $day, $hour, $minute, $second)
|
||||
{
|
||||
$now = new DateTime('now', $this->dateTimeFactory->getTimezone());
|
||||
$now = new DateTime('now', $timezone);
|
||||
$now->setISODate($year, $week, $day);
|
||||
$now->setTime($hour, $minute, $second);
|
||||
|
||||
@@ -63,10 +66,16 @@ final class PaginatedWorkingTimeChart extends SimpleWidget
|
||||
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);
|
||||
$user = $options['user'];
|
||||
if (null === $user || !($user instanceof User)) {
|
||||
throw new \InvalidArgumentException('Widget option "user" must be an instance of ' . User::class);
|
||||
}
|
||||
|
||||
$timezone = new \DateTimeZone($user->getTimezone());
|
||||
|
||||
$weekBegin = $this->getDate($timezone, $options['year'], $options['week'], 1, 0, 0, 0);
|
||||
$weekEnd = $this->getDate($timezone, $options['year'], $options['week'], 7, 23, 59, 59);
|
||||
|
||||
return [
|
||||
'begin' => clone $weekBegin,
|
||||
@@ -74,8 +83,8 @@ final class PaginatedWorkingTimeChart extends SimpleWidget
|
||||
'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()),
|
||||
new DateTime('00:00:00', $timezone),
|
||||
new DateTime('23:59:59', $timezone),
|
||||
$user
|
||||
),
|
||||
'week' => $this->repository->getStatistic(
|
||||
@@ -92,8 +101,8 @@ final class PaginatedWorkingTimeChart extends SimpleWidget
|
||||
),
|
||||
'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()),
|
||||
new DateTime(sprintf('01 january %s 00:00:00', $options['year']), $timezone),
|
||||
new DateTime(sprintf('31 december %s 23:59:59', $options['year']), $timezone),
|
||||
$user
|
||||
),
|
||||
];
|
||||
|
||||
101
src/Widget/Type/SimpleStatisticChart.php
Normal file
101
src/Widget/Type/SimpleStatisticChart.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\Widget\Type;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Repository\TimesheetRepository;
|
||||
use App\Widget\WidgetException;
|
||||
|
||||
class SimpleStatisticChart extends SimpleWidget
|
||||
{
|
||||
/**
|
||||
* @var TimesheetRepository
|
||||
*/
|
||||
private $repository;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $query;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $begin;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $end;
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
private $user;
|
||||
|
||||
public function __construct(TimesheetRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function setQuery(string $query): SimpleStatisticChart
|
||||
{
|
||||
$this->query = $query;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBegin(?string $begin): SimpleStatisticChart
|
||||
{
|
||||
$this->begin = $begin;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setEnd(?string $end): SimpleStatisticChart
|
||||
{
|
||||
$this->end = $end;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setUser(User $user): SimpleStatisticChart
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setData($data): AbstractWidgetType
|
||||
{
|
||||
throw new \InvalidArgumentException('Cannot set data on instances of SimpleStatisticChart');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
* @return mixed|null
|
||||
* @throws WidgetException
|
||||
*/
|
||||
public function getData(array $options = [])
|
||||
{
|
||||
$timezone = date_default_timezone_get();
|
||||
if (null !== $this->user) {
|
||||
$timezone = $this->user->getTimezone();
|
||||
}
|
||||
$timezone = new \DateTimeZone($timezone);
|
||||
|
||||
$begin = !empty($this->begin) ? new \DateTime($this->begin, $timezone) : null;
|
||||
$end = !empty($this->end) ? new \DateTime($this->end, $timezone) : null;
|
||||
|
||||
try {
|
||||
return $this->repository->getStatistic($this->query, $begin, $end, $this->user);
|
||||
} catch (\Exception $ex) {
|
||||
throw new WidgetException(
|
||||
'Failed loading widget data: ' . $ex->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,23 +13,19 @@ use App\Entity\Project;
|
||||
use App\Entity\Team;
|
||||
use App\Entity\User;
|
||||
use App\Repository\ProjectRepository;
|
||||
use App\Security\CurrentUser;
|
||||
|
||||
class UserTeamProjects extends SimpleWidget implements AuthorizedWidget
|
||||
class UserTeamProjects extends SimpleWidget implements AuthorizedWidget, UserWidget
|
||||
{
|
||||
/**
|
||||
* @var ProjectRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
public function __construct(CurrentUser $user, ProjectRepository $repository)
|
||||
public function __construct(ProjectRepository $repository)
|
||||
{
|
||||
$this->setId('UserTeamProjects');
|
||||
$this->setTitle('label.my_team_projects');
|
||||
$this->setOptions([
|
||||
'user' => $user->getUser(),
|
||||
'id' => '',
|
||||
]);
|
||||
$this->setOption('id', '');
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
@@ -80,4 +76,9 @@ class UserTeamProjects extends SimpleWidget implements AuthorizedWidget
|
||||
{
|
||||
return ['budget_team_project', 'budget_teamlead_project', 'budget_project'];
|
||||
}
|
||||
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->setOption('user', $user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,18 +10,14 @@
|
||||
namespace App\Widget\Type;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Security\CurrentUser;
|
||||
|
||||
class UserTeams extends SimpleWidget implements AuthorizedWidget
|
||||
class UserTeams extends SimpleWidget implements AuthorizedWidget, UserWidget
|
||||
{
|
||||
public function __construct(CurrentUser $user)
|
||||
public function __construct()
|
||||
{
|
||||
$this->setId('UserTeams');
|
||||
$this->setTitle('label.my_teams');
|
||||
$this->setOptions([
|
||||
'user' => $user->getUser(),
|
||||
'id' => '',
|
||||
]);
|
||||
$this->setOption('id', '');
|
||||
}
|
||||
|
||||
public function getOptions(array $options = []): array
|
||||
@@ -51,4 +47,9 @@ class UserTeams extends SimpleWidget implements AuthorizedWidget
|
||||
{
|
||||
return ['view_team_member', 'view_team'];
|
||||
}
|
||||
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->setOption('user', $user);
|
||||
}
|
||||
}
|
||||
|
||||
22
src/Widget/Type/UserWidget.php
Normal file
22
src/Widget/Type/UserWidget.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?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\Entity\User;
|
||||
|
||||
interface UserWidget
|
||||
{
|
||||
/**
|
||||
* Sets the current user.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void;
|
||||
}
|
||||
@@ -9,6 +9,6 @@
|
||||
|
||||
namespace App\Widget\Type;
|
||||
|
||||
class YearChart extends SimpleWidget
|
||||
final class YearChart extends SimpleStatisticChart
|
||||
{
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@ class WidgetService
|
||||
/**
|
||||
* @var WidgetRendererInterface[]
|
||||
*/
|
||||
protected $renderer = [];
|
||||
private $renderer = [];
|
||||
/**
|
||||
* @var WidgetRepository
|
||||
*/
|
||||
protected $repository;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @param WidgetRepository $repository
|
||||
@@ -34,10 +34,6 @@ class WidgetService
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $widget
|
||||
* @return bool
|
||||
*/
|
||||
public function hasWidget(string $widget): bool
|
||||
{
|
||||
return $this->repository->has($widget);
|
||||
|
||||
Reference in New Issue
Block a user