135 lines
4.4 KiB
PHP
135 lines
4.4 KiB
PHP
<?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\Export\Base;
|
|
|
|
use App\Activity\ActivityStatisticService;
|
|
use App\Entity\ExportableItem;
|
|
use App\Entity\MetaTableTypeInterface;
|
|
use App\Event\ActivityMetaDisplayEvent;
|
|
use App\Event\CustomerMetaDisplayEvent;
|
|
use App\Event\MetaDisplayEventInterface;
|
|
use App\Event\ProjectMetaDisplayEvent;
|
|
use App\Event\TimesheetMetaDisplayEvent;
|
|
use App\Event\UserPreferenceDisplayEvent;
|
|
use App\Export\ExportRendererInterface;
|
|
use App\Project\ProjectStatisticService;
|
|
use App\Repository\Query\ActivityQuery;
|
|
use App\Repository\Query\CustomerQuery;
|
|
use App\Repository\Query\ProjectQuery;
|
|
use App\Repository\Query\TimesheetQuery;
|
|
use App\Twig\SecurityPolicy\ExportPolicy;
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Twig\Environment;
|
|
use Twig\Extension\SandboxExtension;
|
|
|
|
class HtmlRenderer implements ExportRendererInterface
|
|
{
|
|
use RendererTrait;
|
|
|
|
private string $id = 'html';
|
|
private string $template = 'default.html.twig';
|
|
|
|
public function __construct(
|
|
protected readonly Environment $twig,
|
|
protected readonly EventDispatcherInterface $dispatcher,
|
|
private readonly ProjectStatisticService $projectStatisticService,
|
|
private readonly ActivityStatisticService $activityStatisticService
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return MetaTableTypeInterface[]
|
|
*/
|
|
protected function findMetaColumns(MetaDisplayEventInterface $event): array
|
|
{
|
|
$this->dispatcher->dispatch($event);
|
|
|
|
return $event->getFields();
|
|
}
|
|
|
|
protected function getOptions(TimesheetQuery $query): array
|
|
{
|
|
$decimal = false;
|
|
if (null !== $query->getCurrentUser()) {
|
|
$decimal = $query->getCurrentUser()->isExportDecimal();
|
|
} elseif (null !== $query->getUser()) {
|
|
$decimal = $query->getUser()->isExportDecimal();
|
|
}
|
|
|
|
return ['decimal' => $decimal];
|
|
}
|
|
|
|
/**
|
|
* @param ExportableItem[] $timesheets
|
|
*/
|
|
public function render(array $timesheets, TimesheetQuery $query): Response
|
|
{
|
|
$timesheetMetaFields = $this->findMetaColumns(new TimesheetMetaDisplayEvent($query, TimesheetMetaDisplayEvent::EXPORT));
|
|
$customerMetaFields = $this->findMetaColumns(new CustomerMetaDisplayEvent($query->copyTo(new CustomerQuery()), CustomerMetaDisplayEvent::EXPORT));
|
|
$projectMetaFields = $this->findMetaColumns(new ProjectMetaDisplayEvent($query->copyTo(new ProjectQuery()), ProjectMetaDisplayEvent::EXPORT));
|
|
$activityMetaFields = $this->findMetaColumns(new ActivityMetaDisplayEvent($query->copyTo(new ActivityQuery()), ActivityMetaDisplayEvent::EXPORT));
|
|
|
|
$event = new UserPreferenceDisplayEvent(UserPreferenceDisplayEvent::EXPORT);
|
|
$this->dispatcher->dispatch($event);
|
|
$userPreferences = $event->getPreferences();
|
|
|
|
$summary = $this->calculateSummary($timesheets);
|
|
|
|
// enable basic security measures
|
|
$sandbox = new SandboxExtension(new ExportPolicy());
|
|
$sandbox->enableSandbox();
|
|
$this->twig->addExtension($sandbox);
|
|
|
|
$content = $this->twig->render($this->getTemplate(), array_merge([
|
|
'entries' => $timesheets,
|
|
'query' => $query,
|
|
'summaries' => $summary,
|
|
'budgets' => $this->calculateProjectBudget($timesheets, $query, $this->projectStatisticService),
|
|
'activity_budgets' => $this->calculateActivityBudget($timesheets, $query, $this->activityStatisticService),
|
|
'timesheetMetaFields' => $timesheetMetaFields,
|
|
'customerMetaFields' => $customerMetaFields,
|
|
'projectMetaFields' => $projectMetaFields,
|
|
'activityMetaFields' => $activityMetaFields,
|
|
'userPreferences' => $userPreferences,
|
|
], $this->getOptions($query)));
|
|
|
|
$response = new Response();
|
|
$response->setContent($content);
|
|
|
|
return $response;
|
|
}
|
|
|
|
protected function getTemplate(): string
|
|
{
|
|
return '@export/' . $this->template;
|
|
}
|
|
|
|
public function setTemplate(string $filename): void
|
|
{
|
|
$this->template = $filename;
|
|
}
|
|
|
|
public function setId(string $id): void
|
|
{
|
|
$this->id = $id;
|
|
}
|
|
|
|
public function getId(): string
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getTitle(): string
|
|
{
|
|
return 'print';
|
|
}
|
|
}
|