configurable print export (#2657)

This commit is contained in:
Kevin Papst
2021-07-13 10:11:20 +02:00
committed by GitHub
parent 7661d43327
commit 27cea996c0
21 changed files with 571 additions and 228 deletions

View File

@@ -9,8 +9,9 @@
namespace App\Controller;
use App\Entity\Timesheet;
use App\Export\ExportItemInterface;
use App\Export\ServiceExport;
use App\Export\TooManyItemsExportException;
use App\Form\Toolbar\ExportToolbarForm;
use App\Repository\Query\ExportQuery;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
@@ -45,6 +46,7 @@ class ExportController extends AbstractController
$query = $this->getDefaultQuery();
$showPreview = false;
$tooManyResults = false;
$maxItemsPreview = 500;
$entries = [];
@@ -56,25 +58,33 @@ class ExportController extends AbstractController
$byCustomer = [];
if ($form->isValid() && ($query->hasBookmark() || $request->query->has('performSearch'))) {
$showPreview = true;
$entries = $this->getEntries($query);
foreach ($entries as $entry) {
$cid = $entry->getProject()->getCustomer()->getId();
if (!isset($byCustomer[$cid])) {
$byCustomer[$cid] = [
'customer' => $entry->getProject()->getCustomer(),
'rate' => 0,
'internalRate' => 0,
'duration' => 0,
];
try {
$showPreview = true;
$entries = $this->getEntries($query);
foreach ($entries as $entry) {
$cid = $entry->getProject()->getCustomer()->getId();
if (!isset($byCustomer[$cid])) {
$byCustomer[$cid] = [
'customer' => $entry->getProject()->getCustomer(),
'rate' => 0,
'internalRate' => 0,
'duration' => 0,
];
}
$byCustomer[$cid]['rate'] += $entry->getRate();
$byCustomer[$cid]['internalRate'] += $entry->getInternalRate();
$byCustomer[$cid]['duration'] += $entry->getDuration();
}
$byCustomer[$cid]['rate'] += $entry->getRate();
$byCustomer[$cid]['internalRate'] += $entry->getInternalRate();
$byCustomer[$cid]['duration'] += $entry->getDuration();
} catch (TooManyItemsExportException $ex) {
$tooManyResults = true;
$showPreview = false;
$entries = [];
$this->logException($ex);
}
}
return $this->render('export/index.html.twig', [
'too_many' => $tooManyResults,
'by_customer' => $byCustomer,
'query' => $query,
'entries' => $entries,
@@ -136,7 +146,7 @@ class ExportController extends AbstractController
/**
* @param ExportQuery $query
* @return Timesheet[]
* @return ExportItemInterface[]
*/
protected function getEntries(ExportQuery $query): array
{

View File

@@ -0,0 +1,28 @@
<?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\Event;
use App\Repository\Query\ExportQuery;
use Symfony\Contracts\EventDispatcher\Event;
class ExportItemsQueryEvent extends Event
{
private $query;
public function __construct(ExportQuery $query)
{
$this->query = $query;
}
public function getExportQuery(): ExportQuery
{
return $this->query;
}
}

View File

@@ -9,7 +9,9 @@
namespace App\Export;
use App\Event\ExportItemsQueryEvent;
use App\Repository\Query\ExportQuery;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
final class ServiceExport
{
@@ -20,17 +22,24 @@ final class ServiceExport
/**
* @var TimesheetExportInterface[]
*/
private $exporter = [];
private $timesheetExporter = [];
/**
* @var ExportRepositoryInterface[]
*/
private $repositories = [];
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
public function addRenderer(ExportRendererInterface $renderer): ServiceExport
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
public function addRenderer(ExportRendererInterface $renderer): void
{
$this->renderer[] = $renderer;
return $this;
}
/**
@@ -52,11 +61,9 @@ final class ServiceExport
return null;
}
public function addTimesheetExporter(TimesheetExportInterface $exporter): ServiceExport
public function addTimesheetExporter(TimesheetExportInterface $exporter): void
{
$this->exporter[] = $exporter;
return $this;
$this->timesheetExporter[] = $exporter;
}
/**
@@ -64,12 +71,12 @@ final class ServiceExport
*/
public function getTimesheetExporter(): array
{
return $this->exporter;
return $this->timesheetExporter;
}
public function getTimesheetExporterById(string $id): ?TimesheetExportInterface
{
foreach ($this->exporter as $exporter) {
foreach ($this->timesheetExporter as $exporter) {
if ($exporter->getId() === $id) {
return $exporter;
}
@@ -78,19 +85,31 @@ final class ServiceExport
return null;
}
public function addExportRepository(ExportRepositoryInterface $repository): ServiceExport
public function addExportRepository(ExportRepositoryInterface $repository): void
{
$this->repositories[] = $repository;
return $this;
}
/**
* @param ExportQuery $query
* @return ExportItemInterface[]
* @throws TooManyItemsExportException
*/
public function getExportItems(ExportQuery $query)
{
$items = [];
$event = new ExportItemsQueryEvent($query);
$this->eventDispatcher->dispatch($event);
$max = $event->getExportQuery()->getMaxResults();
foreach ($this->repositories as $repository) {
$items = array_merge($items, $repository->getExportItemsForQuery($query));
$items = array_merge($items, $repository->getExportItemsForQuery($event->getExportQuery()));
if ($max !== null && \count($items) > $max) {
throw new TooManyItemsExportException(
sprintf('Limit reached! Expected max. %s items but got %s', $max, \count($items))
);
}
}
return $items;

View File

@@ -0,0 +1,17 @@
<?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;
/**
* THIS IS MORE A QUICKFIX THAN ANYTHING ELSE!
*/
final class TooManyItemsExportException extends \Exception
{
}

View File

@@ -58,6 +58,10 @@ class TimesheetQuery extends ActivityQuery implements BillableInterface
* @var User[]
*/
private $users = [];
/**
* @var int|null
*/
private $maxResults;
public function __construct(bool $resetTimes = true)
{
@@ -69,6 +73,16 @@ class TimesheetQuery extends ActivityQuery implements BillableInterface
]);
}
public function getMaxResults(): ?int
{
return $this->maxResults;
}
public function setMaxResults(?int $maxResults): void
{
$this->maxResults = $maxResults;
}
public function addUser(User $user): self
{
$this->users[$user->getId()] = $user;

View File

@@ -962,6 +962,10 @@ class TimesheetRepository extends EntityRepository
$qb->leftJoin('t.activity', 'a');
}
if ($query->getMaxResults() !== null) {
$qb->setMaxResults($query->getMaxResults());
}
return $qb;
}