Files
kimai2/src/Export/ServiceExport.php
Kevin Papst 38e37f1c2e Release 2.1.0 (#4321)
* fix deprecations
* remove unused config
* replace invalid annotation type with attribute
* use AsDoctrineListener to fix deprecation
* new ModifiedSubscriber to support custom logic and fix deprecation
* removed inheritdoc comment
* new ModifiedSubscriber to support custom logic and fix deprecation
* cleanup event dispatcher interface
* re-order annotation params
* one more doctrine based deprecation
* fix query to count active timesheets
* link to "all times" to identify active timesheets
* link icon instead of text
* fix "skin" translation in wizard
* use duration filter to show duration
* added login link command and controller
* bump tabler theme to 1.0
* added wizard to force password reset by user
* allow to configure that new accounts need to reset their password
* prevent uploading twig templates by default
* bump composer packages
* enable sandbox and basic security measures for custom twig templates for invoice and export
* bump to symfony 6.3.5
* allow to export single user reports to excel
* removed broken method to reload twig cache
* added api parameter to fetch user collection fully serialized
* allow to replace or append description via timesheet batch update
* show api username above form
2023-10-19 11:21:50 +02:00

187 lines
4.9 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;
use App\Entity\ExportableItem;
use App\Event\ExportItemsQueryEvent;
use App\Export\Renderer\HtmlRendererFactory;
use App\Export\Renderer\PdfRendererFactory;
use App\Repository\Query\ExportQuery;
use Psr\EventDispatcher\EventDispatcherInterface;
final class ServiceExport
{
/**
* @var array<int, string>
*/
private array $documentDirs = [];
/**
* @var ExportRendererInterface[]
*/
private array $renderer = [];
/**
* @var TimesheetExportInterface[]
*/
private array $timesheetExporter = [];
/**
* @var ExportRepositoryInterface[]
*/
private array $repositories = [];
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private HtmlRendererFactory $htmlRendererFactory,
private PdfRendererFactory $pdfRendererFactory
)
{
}
/**
* @CloudRequired
*/
public function addDirectory(string $directory): void
{
$this->documentDirs[] = $directory;
}
/**
* @CloudRequired
*/
public function removeDirectory(string $directory): void
{
if (($key = array_search($directory, $this->documentDirs, true)) !== false) {
unset($this->documentDirs[$key]);
}
}
public function addRenderer(ExportRendererInterface $renderer): void
{
$this->renderer[] = $renderer;
}
/**
* @return ExportRendererInterface[]
*/
public function getRenderer(): array
{
$renderer = [];
foreach ($this->documentDirs as $exportPath) {
if (!is_dir($exportPath)) {
continue;
}
$htmlTemplates = glob($exportPath . '/*.html.twig');
if (\is_array($htmlTemplates)) {
foreach ($htmlTemplates as $htmlTpl) {
$tplName = basename($htmlTpl);
if (stripos($tplName, '-bundle') !== false) {
continue;
}
$renderer[] = $this->htmlRendererFactory->create($tplName, $tplName);
}
}
$pdfTemplates = glob($exportPath . '/*.pdf.twig');
if (\is_array($pdfTemplates)) {
foreach ($pdfTemplates as $pdfTpl) {
$tplName = basename($pdfTpl);
if (stripos($tplName, '-bundle') !== false) {
continue;
}
$renderer[] = $this->pdfRendererFactory->create($tplName, $tplName);
}
}
}
return array_merge($this->renderer, $renderer);
}
public function getRendererById(string $id): ?ExportRendererInterface
{
foreach ($this->getRenderer() as $renderer) {
if ($renderer->getId() === $id) {
return $renderer;
}
}
return null;
}
public function addTimesheetExporter(TimesheetExportInterface $exporter): void
{
$this->timesheetExporter[] = $exporter;
}
/**
* @return TimesheetExportInterface[]
*/
public function getTimesheetExporter(): array
{
return $this->timesheetExporter;
}
public function getTimesheetExporterById(string $id): ?TimesheetExportInterface
{
foreach ($this->timesheetExporter as $exporter) {
if ($exporter->getId() === $id) {
return $exporter;
}
}
return null;
}
public function addExportRepository(ExportRepositoryInterface $repository): void
{
$this->repositories[] = $repository;
}
/**
* @param ExportQuery $query
* @return ExportableItem[]
* @throws TooManyItemsExportException
*/
public function getExportItems(ExportQuery $query): array
{
$items = [];
$max = $this->getMaximumResults($query);
foreach ($this->repositories as $repository) {
$items = array_merge($items, $repository->getExportItemsForQuery($query));
if ($max !== null && \count($items) > $max) {
throw new TooManyItemsExportException(
sprintf('Limit reached! Expected max. %s items but got %s', $max, \count($items))
);
}
}
return $items;
}
public function setExported(array $items): void
{
foreach ($this->repositories as $repository) {
$repository->setExported($items);
}
}
public function getMaximumResults(ExportQuery $query): ?int
{
$event = new ExportItemsQueryEvent($query);
$this->eventDispatcher->dispatch($event);
return $event->getExportQuery()->getMaxResults();
}
}