allow to select reports as initial view (#2403)

This commit is contained in:
Kevin Papst
2021-03-06 12:59:13 +01:00
committed by GitHub
parent 5583caa9e4
commit 70f7f35009
7 changed files with 134 additions and 59 deletions

View File

@@ -1,68 +0,0 @@
<?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\Twig;
use App\Entity\User;
use App\Event\ReportingEvent;
use App\Reporting\Report;
use App\Reporting\ReportInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
final class ReportingExtension extends AbstractExtension
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var AuthorizationCheckerInterface
*/
private $security;
public function __construct(EventDispatcherInterface $dispatcher, AuthorizationCheckerInterface $security)
{
$this->dispatcher = $dispatcher;
$this->security = $security;
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new TwigFunction('available_reports', [$this, 'getAvailableReports'], []),
];
}
/**
* @param User $user
* @return ReportInterface[]
*/
public function getAvailableReports(User $user): array
{
$event = new ReportingEvent($user);
if ($this->security->isGranted('view_reporting')) {
$event->addReport(new Report('week_by_user', 'report_user_week', 'report_user_week'));
$event->addReport(new Report('month_by_user', 'report_user_month', 'report_user_month'));
if ($this->security->isGranted('view_other_timesheet')) {
$event->addReport(new Report('monthly_users_list', 'report_monthly_users', 'report_monthly_users'));
}
}
$this->dispatcher->dispatch($event);
return $event->getReports();
}
}

View File

@@ -0,0 +1,34 @@
<?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\Twig\Runtime;
use App\Entity\User;
use App\Reporting\ReportingService;
use App\Reporting\ReportInterface;
use Twig\Extension\RuntimeExtensionInterface;
final class ReportingExtension implements RuntimeExtensionInterface
{
private $service;
public function __construct(ReportingService $reportingService)
{
$this->service = $reportingService;
}
/**
* @param User $user
* @return ReportInterface[]
*/
public function getAvailableReports(User $user): array
{
return $this->service->getAvailableReports($user);
}
}

View File

@@ -12,6 +12,7 @@ namespace App\Twig;
use App\Twig\Runtime\EncoreExtension;
use App\Twig\Runtime\ExporterExtension;
use App\Twig\Runtime\MarkdownExtension;
use App\Twig\Runtime\ReportingExtension;
use App\Twig\Runtime\ThemeExtension;
use App\Twig\Runtime\TimesheetExtension;
use App\Twig\Runtime\WidgetExtension;
@@ -34,6 +35,7 @@ class RuntimeExtensions extends AbstractExtension
new TwigFunction('active_timesheets', [TimesheetExtension::class, 'activeEntries']),
new TwigFunction('encore_entry_css_source', [EncoreExtension::class, 'getEncoreEntryCssSource']),
new TwigFunction('render_widget', [WidgetExtension::class, 'renderWidget'], ['is_safe' => ['html']]),
new TwigFunction('available_reports', [ReportingExtension::class, 'getAvailableReports'], []),
];
}