customize html and pdf exports with twig templates (#1582)
This commit is contained in:
@@ -49,6 +49,9 @@ class AppExtension extends Extension
|
||||
$config['invoice']['documents'] = array_merge($config['invoice']['documents'], $config['invoice']['defaults']);
|
||||
unset($config['invoice']['defaults']);
|
||||
|
||||
$config['export']['documents'] = array_merge($config['export']['documents'], $config['export']['defaults']);
|
||||
unset($config['export']['defaults']);
|
||||
|
||||
// safe alternatives to %kernel.project_dir%
|
||||
$container->setParameter('kimai.data_dir', $config['data_dir']);
|
||||
$container->setParameter('kimai.plugin_dir', $config['plugin_dir']);
|
||||
@@ -60,6 +63,7 @@ class AppExtension extends Extension
|
||||
$container->setParameter('kimai.dashboard', $config['dashboard']);
|
||||
$container->setParameter('kimai.widgets', $config['widgets']);
|
||||
$container->setParameter('kimai.invoice.documents', $config['invoice']['documents']);
|
||||
$container->setParameter('kimai.export.documents', $config['export']['documents']);
|
||||
$container->setParameter('kimai.defaults', $config['defaults']);
|
||||
|
||||
$this->createPermissionParameter($config['permissions'], $container);
|
||||
|
||||
@@ -9,10 +9,14 @@
|
||||
|
||||
namespace App\DependencyInjection\Compiler;
|
||||
|
||||
use App\Export\Renderer\HtmlRenderer;
|
||||
use App\Export\Renderer\HtmlRendererFactory;
|
||||
use App\Export\Renderer\PdfRendererFactory;
|
||||
use App\Export\ServiceExport;
|
||||
use App\Kernel;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
@@ -37,5 +41,40 @@ class ExportServiceCompilerPass implements CompilerPassInterface
|
||||
foreach ($taggedExporter as $id => $tags) {
|
||||
$definition->addMethodCall('addTimesheetExporter', [new Reference($id)]);
|
||||
}
|
||||
|
||||
$path = dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR;
|
||||
foreach ($container->getParameter('kimai.export.documents') as $exportPath) {
|
||||
if (!is_dir($path . $exportPath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (glob($path . $exportPath . '/*.html.twig') as $htmlTpl) {
|
||||
$tplName = basename($htmlTpl);
|
||||
|
||||
$serviceId = 'exporter_renderer.' . str_replace('.', '_', $tplName);
|
||||
|
||||
$factoryDefinition = new Definition(HtmlRenderer::class);
|
||||
$factoryDefinition->addArgument($tplName);
|
||||
$factoryDefinition->addArgument($tplName);
|
||||
$factoryDefinition->setFactory([new Reference(HtmlRendererFactory::class), 'create']);
|
||||
|
||||
$container->setDefinition($serviceId, $factoryDefinition);
|
||||
$definition->addMethodCall('addRenderer', [new Reference($serviceId)]);
|
||||
}
|
||||
|
||||
foreach (glob($path . $exportPath . '/*.pdf.twig') as $pdfHtml) {
|
||||
$tplName = basename($pdfHtml);
|
||||
|
||||
$serviceId = 'exporter_renderer.' . str_replace('.', '_', $tplName);
|
||||
|
||||
$factoryDefinition = new Definition(HtmlRenderer::class);
|
||||
$factoryDefinition->addArgument($tplName);
|
||||
$factoryDefinition->addArgument($tplName);
|
||||
$factoryDefinition->setFactory([new Reference(PdfRendererFactory::class), 'create']);
|
||||
|
||||
$container->setDefinition($serviceId, $factoryDefinition);
|
||||
$definition->addMethodCall('addRenderer', [new Reference($serviceId)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,16 +32,21 @@ class TwigContextCompilerPass implements CompilerPassInterface
|
||||
$saml = $container->getParameter('kimai.saml');
|
||||
$twig->addMethodCall('addGlobal', ['saml', $saml]);
|
||||
|
||||
if ($container->hasDefinition('twig.loader.native_filesystem')) {
|
||||
$definition = $container->getDefinition('twig.loader.native_filesystem');
|
||||
$definition = $container->getDefinition('twig.loader.native_filesystem');
|
||||
|
||||
$path = dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR;
|
||||
foreach ($container->getParameter('kimai.invoice.documents') as $invoicePath) {
|
||||
if (!is_dir($path . $invoicePath)) {
|
||||
continue;
|
||||
}
|
||||
$definition->addMethodCall('addPath', [$path . $invoicePath, 'invoice']);
|
||||
$path = dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR;
|
||||
foreach ($container->getParameter('kimai.invoice.documents') as $invoicePath) {
|
||||
if (!is_dir($path . $invoicePath)) {
|
||||
continue;
|
||||
}
|
||||
$definition->addMethodCall('addPath', [$path . $invoicePath, 'invoice']);
|
||||
}
|
||||
|
||||
foreach ($container->getParameter('kimai.export.documents') as $exportPath) {
|
||||
if (!is_dir($path . $exportPath)) {
|
||||
continue;
|
||||
}
|
||||
$definition->addMethodCall('addPath', [$path . $exportPath, 'export']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,7 @@ class Configuration implements ConfigurationInterface
|
||||
->append($this->getUserNode())
|
||||
->append($this->getTimesheetNode())
|
||||
->append($this->getInvoiceNode())
|
||||
->append($this->getExportNode())
|
||||
->append($this->getLanguagesNode())
|
||||
->append($this->getCalendarNode())
|
||||
->append($this->getThemeNode())
|
||||
@@ -236,6 +237,33 @@ class Configuration implements ConfigurationInterface
|
||||
return $node;
|
||||
}
|
||||
|
||||
protected function getExportNode()
|
||||
{
|
||||
$builder = new TreeBuilder('export');
|
||||
/** @var ArrayNodeDefinition $node */
|
||||
$node = $builder->getRootNode();
|
||||
|
||||
$node
|
||||
->addDefaultsIfNotSet()
|
||||
->children()
|
||||
->arrayNode('defaults')
|
||||
->scalarPrototype()->end()
|
||||
->defaultValue([
|
||||
'var/export/',
|
||||
'templates/export/renderer/'
|
||||
])
|
||||
->end()
|
||||
->arrayNode('documents')
|
||||
->requiresAtLeastOneElement()
|
||||
->scalarPrototype()->end()
|
||||
->defaultValue([])
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
protected function getLanguagesNode()
|
||||
{
|
||||
$builder = new TreeBuilder('languages');
|
||||
|
||||
Reference in New Issue
Block a user