Skip to content

Commit

Permalink
Added custom filename field for exported PDF to project properties
Browse files Browse the repository at this point in the history
  • Loading branch information
arno1979 committed Jun 10, 2023
1 parent ecca9bf commit fb714cb
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

## [2.1]

- added custom field to project properties to be able to provide a custom filename for the generated PDF
- changed the custom PDF template name to `custom-export.pdf.twig`

## [2.0]

- added compatability for Kimai version >= 2.0.22
53 changes: 53 additions & 0 deletions EventSubscriber/MetaFieldSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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 KimaiPlugin\CustomExportBundle\EventSubscriber;

use App\Entity\MetaTableTypeInterface;
use App\Entity\ProjectMeta;
use App\Event\ProjectMetaDefinitionEvent;
use App\Event\ProjectMetaDisplayEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Length;

class MetaFieldSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProjectMetaDefinitionEvent::class => ['loadProjectMeta', 200],
ProjectMetaDisplayEvent::class => ['loadProjectFields', 200],
];
}

private function getMetaField(): MetaTableTypeInterface
{
$definition = new ProjectMeta();
$definition->setName('custom_export_filename');
$definition->setLabel('Leistungsnachweis: Dateiname');
$definition->setOptions(['help' => 'Erlaubt es einen Dateinamen für den Custom PDF Export für diese Projekt anzugeben']);
$definition->setType(TextType::class);
$definition->addConstraint(new Length(['max' => 255]));
$definition->setIsVisible(true);

return $definition;
}

public function loadProjectMeta(ProjectMetaDefinitionEvent $event): void
{
$event->getEntity()->setMetaField($this->getMetaField());
}

public function loadProjectFields(ProjectMetaDisplayEvent $event): void
{
$event->addField($this->getMetaField());
}

}
17 changes: 15 additions & 2 deletions Renderer/CustomPDFRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Export\ExportFilename;
use App\Export\ExportRendererInterface;
use App\Entity\Timesheet;
use App\Entity\ProjectMeta;
use App\Timesheet\DateTimeFactory;
use App\Pdf\HtmlToPdfConverter;
use App\Pdf\PdfContext;
Expand All @@ -21,17 +22,20 @@
use App\Export\RendererInterface;
use App\Export\Base\RendererTrait;
use App\Repository\Query\TimesheetQuery;
use App\Repository\Query\ProjectQuery;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
use Doctrine\DBAL\Connection;
use KimaiPlugin\CustomExportBundle\EventSubscriber\MetaFieldDisplaySubscriber;


final class CustomPDFRenderer implements RendererInterface
{
use RendererTrait;
use PDFRendererTrait;

private string $id = 'ascustompdf';
private string $template = 'arno-leistungsnachweis.pdf.twig';
private string $template = 'custom-export.pdf.twig';
private array $pdfOptions = [];

public function __construct(Environment $twig, DateTimeFactory $dateTime, HtmlToPdfConverter $converter, Connection $conn, private ProjectStatisticService $projectStatisticService)
Expand All @@ -52,10 +56,18 @@ public function __construct(Environment $twig, DateTimeFactory $dateTime, HtmlTo
*/
public function render(array $timesheets, TimesheetQuery $query): Response
{
$filename = new ExportFilename($query);

$context = new PdfContext();
$filename = new ExportFilename($query);
$context->setOption('filename', $filename->getFilename());

$customFilename = $query->getProjects()[0]->getMetaField('custom_export_filename');
if ($query->getProjects()[0]->getMetaField('custom_export_filename') !== null && $query->getProjects()[0]->getMetaField('custom_export_filename')->getValue() !== null)
{
$filename = $query->getProjects()[0]->getMetaField('custom_export_filename')->getValue().$query->getBegin()->format('Y-m');
$context->setOption('filename', $filename);
}

$summary = $this->calculateSummary($timesheets);
$content = $this->twig->render($this->getTemplate(), array_merge([
'entries' => $timesheets,
Expand All @@ -66,6 +78,7 @@ public function render(array $timesheets, TimesheetQuery $query): Response
'decimal' => false,
'pdfContext' => $context,
'projectRate' => $this->getRate($query),
'filename' => $filename,
], $this->getOptions($query)));

$pdfOptions = array_merge($context->getOptions(), $this->getPdfOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
{% for summary in summaries %}
{% set project = summary.project %}
{% endfor %}
{%- set filename = 'leistungsnachweis-as-' ~ (project is not null ? project|replace({' ': '-'}) ~ '-' : '') ~ query.begin|date_format('Y-m') -%}
{%- set option = pdfContext.setOption('filename', filename) -%}

{% block styles %}
{%- set fontData = pdfContext.setOption('fonts', {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "arno1979/kimai-custom-export",
"description": "A Kimai 2 plugin",
"type": "kimai-plugin",
"version": "2.0",
"version": "2.1",
"require": {
"kimai/kimai2-composer": "*"
},
Expand Down

0 comments on commit fb714cb

Please sign in to comment.