Skip to content

Commit

Permalink
Implement a custom format option (inspiredminds#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg authored Sep 10, 2024
1 parent 60835e2 commit a2e85e8
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
3 changes: 1 addition & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
EOF;

$finder = (new PhpCsFixer\Finder())
->exclude('Resources/contao/templates')
->exclude('Fixtures')
->in([
__DIR__.'/contao',
__DIR__.'/src',
])
;
Expand Down
23 changes: 15 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
},
"require":{
"php": ">=7.1",
"contao/core-bundle": "^4.9",
"contao/core-bundle": "^4.13",
"isotope/isotope-core": "^2.5",
"mpdf/mpdf": "^8.0",
"menatwork/contao-multicolumnwizard-bundle": "^3.4",
"symfony/dependency-injection": "^4.0 || ^5.0",
"symfony/http-kernel": "^4.0 || ^5.0",
"symfony/event-dispatcher": "^4.0 || ^5.0",
"symfony/dependency-injection": "^5.4",
"symfony/http-kernel": "^5.4",
"symfony/event-dispatcher": "^5.4",
"symfony/event-dispatcher-contracts": "^1.0 || ^2.0 || ^3.0",
"symfony/finder": "^4.0 || ^5.0",
"symfony/config": "^4.0 || ^5.0",
"webmozart/path-util": "^2.3"
"symfony/finder": "^5.4",
"symfony/config": "^5.4",
"symfony/filesystem": "^5.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.0 || ^3.0"
Expand All @@ -41,6 +41,13 @@
}
},
"extra": {
"contao-manager-plugin": "InspiredMinds\\ContaoIsotopePdfTemplatesBundle\\ContaoManagerPlugin"
"contao-manager-plugin": "InspiredMinds\\ContaoIsotopePdfTemplatesBundle\\ContaoManager\\Plugin"
},
"config": {
"allow-plugins": {
"contao-components/installer": true,
"php-http/discovery": false,
"contao-community-alliance/composer-plugin": true
}
}
}
13 changes: 13 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
_defaults:
autowire: true
autoconfigure: true

InspiredMinds\ContaoIsotopePdfTemplatesBundle\:
resource: ../src
exclude: ../src/{ContaoManager,DependencyInjection,Event,Isotope,Resources}

InspiredMinds\ContaoIsotopePdfTemplatesBundle\EventListener\DataContainer\IsoDocumentListener:
public: true
arguments:
- '%kernel.project_dir%'
16 changes: 16 additions & 0 deletions contao/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ContaoIsotopePdfTemplatesBundle bundle.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

use InspiredMinds\ContaoIsotopePdfTemplatesBundle\Isotope\Model\Document\Template;
use Isotope\Model\Document;

Document::registerModelType('template', Template::class);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* @license LGPL-3.0-or-later
*/

use InspiredMinds\ContaoIsotopePdfTemplatesBundle\DataContainer\IsoDocumentListener;
use Contao\CoreBundle\DataContainer\PaletteManipulator;
use InspiredMinds\ContaoIsotopePdfTemplatesBundle\EventListener\DataContainer\IsoDocumentListener;

$GLOBALS['TL_DCA']['tl_iso_document']['config']['onload_callback'][] = [IsoDocumentListener::class, 'onLoadCallback'];

Expand Down Expand Up @@ -104,19 +105,27 @@
'exclude' => true,
'inputType' => 'select',
'options' => ['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10'],
'eval' => ['tl_class' => 'w50', 'mandatory' => true],
'eval' => ['tl_class' => 'w50', 'includeBlankOption' => true, 'blankOptionLabel' => &$GLOBALS['TL_LANG']['tl_iso_document']['pdfFormatBlank']],
'sql' => ['type' => 'string', 'length' => 3, 'default' => 'A4'],
];

$GLOBALS['TL_DCA']['tl_iso_document']['fields']['pdfOrientation'] = [
'label' => &$GLOBALS['TL_LANG']['tl_iso_document']['pdfOrientation'],
'exclude' => true,
'inputType' => 'select',
'options' => ['P' => 'portrait', 'L' => 'landscape'],
'options' => ['P', 'L'],
'reference' => &$GLOBALS['TL_LANG']['tl_iso_document']['pdfOrientationOptions'],
'eval' => ['tl_class' => 'w50', 'mandatory' => true],
'sql' => ['type' => 'string', 'length' => 1, 'default' => 'P'],
];

$GLOBALS['TL_DCA']['tl_iso_document']['fields']['pdfFormatCustom'] = [
'exclude' => true,
'inputType' => 'text',
'eval' => ['tl_class' => 'w50', 'maxlength' => 32, 'multiple' => true, 'size' => 2],
'sql' => ['type' => 'blob', 'notnull' => false],
];

$GLOBALS['TL_DCA']['tl_iso_document']['fields']['pdfMargin'] = [
'label' => &$GLOBALS['TL_LANG']['tl_iso_document']['pdfMargin'],
'exclude' => true,
Expand Down Expand Up @@ -158,7 +167,6 @@
'sql' => ['type' => 'string', 'length' => 128, 'default' => ''],
];


$GLOBALS['TL_DCA']['tl_iso_document']['palettes']['__selector__'][] = 'usePdfTemplate';
$GLOBALS['TL_DCA']['tl_iso_document']['palettes']['__selector__'][] = 'appendPdfTemplate';
$GLOBALS['TL_DCA']['tl_iso_document']['palettes']['__selector__'][] = 'useCustomFonts';
Expand All @@ -167,19 +175,20 @@
$GLOBALS['TL_DCA']['tl_iso_document']['subpalettes']['appendPdfTemplate'] = 'appendPdfTemplateSRC';
$GLOBALS['TL_DCA']['tl_iso_document']['subpalettes']['useCustomFonts'] = 'customFontsDirectory,customFontsConfig';

\Contao\CoreBundle\DataContainer\PaletteManipulator::create()
PaletteManipulator::create()
->addLegend('pdftemplate_legend', 'template_legend')
->addField('usePdfTemplate', 'pdftemplate_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('appendPdfTemplate', 'pdftemplate_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('usePdfTemplate', 'pdftemplate_legend', PaletteManipulator::POSITION_APPEND)
->addField('appendPdfTemplate', 'pdftemplate_legend', PaletteManipulator::POSITION_APPEND)
->addLegend('font_legend', 'pdftemplate_legend')
->addField('useCustomFonts', 'font_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addLegend('pdfconfig_legend', 'pdftemplate_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_AFTER, true)
->addField('pdfFormat', 'pdfconfig_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('pdfOrientation', 'pdfconfig_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('pdfMargin', 'pdfconfig_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('pdfDefaultFont', 'pdfconfig_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('pdfDefaultFontSize', 'pdfconfig_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('pdfCreator', 'pdfconfig_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('pdfAuthor', 'pdfconfig_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)
->addField('useCustomFonts', 'font_legend', PaletteManipulator::POSITION_APPEND)
->addLegend('pdfconfig_legend', 'pdftemplate_legend', PaletteManipulator::POSITION_AFTER, true)
->addField('pdfFormat', 'pdfconfig_legend', PaletteManipulator::POSITION_APPEND)
->addField('pdfOrientation', 'pdfconfig_legend', PaletteManipulator::POSITION_APPEND)
->addField('pdfFormatCustom', 'pdfconfig_legend', PaletteManipulator::POSITION_APPEND)
->addField('pdfMargin', 'pdfconfig_legend', PaletteManipulator::POSITION_APPEND)
->addField('pdfDefaultFont', 'pdfconfig_legend', PaletteManipulator::POSITION_APPEND)
->addField('pdfDefaultFontSize', 'pdfconfig_legend', PaletteManipulator::POSITION_APPEND)
->addField('pdfCreator', 'pdfconfig_legend', PaletteManipulator::POSITION_APPEND)
->addField('pdfAuthor', 'pdfconfig_legend', PaletteManipulator::POSITION_APPEND)
->applyToPalette('template', 'tl_iso_document')
;
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
*/

$GLOBALS['TL_LANG']['MODEL']['tl_iso_document']['template'] = ['Vorlage', 'Vorlage'];
$GLOBALS['TL_LANG']['ERR']['missingCustomPdfFormat'] = 'Kein eigenes Format angegeben.';
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
$GLOBALS['TL_LANG']['tl_iso_document']['customFontsConfigFilename'] = 'Dateiname';
$GLOBALS['TL_LANG']['tl_iso_document']['customFontsConfigFontname'] = 'Schriftarten Name';
$GLOBALS['TL_LANG']['tl_iso_document']['pdfFormat'] = ['Format', 'Hier wird das Format für das PDF ausgewählt.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfFormatBlank'] = 'Eigenes Format';
$GLOBALS['TL_LANG']['tl_iso_document']['pdfFormatCustom'] = ['Eigenes Format', 'Das Format in Millimeter angeben.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfOrientation'] = ['Ausrichtung', 'Hier wird die Ausrichtung für das PDF ausgewählt.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfOrientationOptions'] = ['P' => 'Hochformat', 'L' => 'Querformat'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfMargin'] = ['Dokumentränder', 'Hier werden die Dokumentränder für das PDF eingestellt.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfDefaultFont'] = ['Hauptschriftart', 'Die Hauptschriftart die im PDF verwendet werden soll.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfDefaultFontSize'] = ['Schriftgröße', 'Die Schriftgröße die als Basis im PDF verwendet werden soll.'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
*/

$GLOBALS['TL_LANG']['MODEL']['tl_iso_document']['template'] = ['Template', 'Template'];
$GLOBALS['TL_LANG']['ERR']['missingCustomPdfFormat'] = 'No custom format specified.';
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
$GLOBALS['TL_LANG']['tl_iso_document']['customFontsConfigFilename'] = 'File name';
$GLOBALS['TL_LANG']['tl_iso_document']['customFontsConfigFontname'] = 'Font family name';
$GLOBALS['TL_LANG']['tl_iso_document']['pdfFormat'] = ['Format', 'Here can you select the format for the PDF.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfFormatBlank'] = 'Custom format';
$GLOBALS['TL_LANG']['tl_iso_document']['pdfFormatCustom'] = ['Custom format', 'Enter the width and height in millimeters.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfOrientation'] = ['Orientation', 'Here can you select the orientantion for the PDF.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfOrientationOptions'] = ['P' => 'Portrait', 'L' => 'Landscape'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfMargin'] = ['Document margins', 'Here the document margins for the PDF are set.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfDefaultFont'] = ['Main font', 'The main font to be used in the PDF.'];
$GLOBALS['TL_LANG']['tl_iso_document']['pdfDefaultFontSize'] = ['Font size', 'The font size to be used as the base in the PDF.'];
Expand Down
4 changes: 4 additions & 0 deletions src/ContaoIsotopePdfTemplatesBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@

class ContaoIsotopePdfTemplatesBundle extends Bundle
{
public function getPath()
{
return \dirname(__DIR__);
}
}
8 changes: 3 additions & 5 deletions src/ContaoManagerPlugin.php → src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@
* @license LGPL-3.0-or-later
*/

namespace InspiredMinds\ContaoIsotopePdfTemplatesBundle;
namespace InspiredMinds\ContaoIsotopePdfTemplatesBundle\ContaoManager;

use Contao\CoreBundle\ContaoCoreBundle;
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
use InspiredMinds\ContaoIsotopePdfTemplatesBundle\ContaoIsotopePdfTemplatesBundle;

/**
* Plugin for the Contao Manager.
*/
class ContaoManagerPlugin implements BundlePluginInterface
class Plugin implements BundlePluginInterface
{
/**
* {@inheritdoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class ContaoIsotopePdfTemplatesExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
(new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config')))
->load('services.yaml')
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @license LGPL-3.0-or-later
*/

namespace InspiredMinds\ContaoIsotopePdfTemplatesBundle\DataContainer;
namespace InspiredMinds\ContaoIsotopePdfTemplatesBundle\EventListener\DataContainer;

use Contao\DataContainer;
use Contao\FilesModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ContaoIsotopePdfTemplatesBundle bundle.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

namespace InspiredMinds\ContaoIsotopePdfTemplatesBundle\EventListener\DataContainer;

use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\StringUtil;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @Callback(table="tl_iso_document", target="fields.pdfFormatCustom.save")
*/
class TemplateCustomFormatSaveListener
{
private RequestStack $requestStack;
private TranslatorInterface $translator;

public function __construct(RequestStack $requestStack, TranslatorInterface $translator)
{
$this->requestStack = $requestStack;
$this->translator = $translator;
}

public function __invoke($value): string
{
$request = $this->requestStack->getCurrentRequest();
$values = StringUtil::deserialize($value, true) + ['', ''];

if (!array_filter($values) && !$request->request->get('pdfFormat')) {
throw new \Exception($this->translator->trans('ERR.missingCustomPdfFormat', [], 'contao_default'));
}

return serialize(array_map('floatval', $values));
}
}
8 changes: 6 additions & 2 deletions src/Isotope/Model/Document/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
use Contao\System;
use InspiredMinds\ContaoIsotopePdfTemplatesBundle\Event\ModifyPdfEvent;
use Isotope\Interfaces\IsotopeProductCollection;
use Webmozart\PathUtil\Path;
use Symfony\Component\Filesystem\Path;

class Template extends \Isotope\Model\Document\Standard
{
public function outputToBrowser(IsotopeProductCollection $objCollection)
public function outputToBrowser(IsotopeProductCollection $objCollection): void
{
parent::outputToBrowser($objCollection);

Expand Down Expand Up @@ -68,6 +68,10 @@ protected function generatePDF(IsotopeProductCollection $objCollection, array $a

$margin = StringUtil::deserialize($this->pdfMargin, true);

if (!$this->pdfFormat) {
$this->pdfFormat = array_map('floatval', StringUtil::deserialize($this->pdfFormatCustom, true)) + [210.0, 297.0];
}

// Create new PDF document
$pdf = new \Mpdf\Mpdf([
'fontDir' => $fontDirs,
Expand Down
5 changes: 0 additions & 5 deletions src/Resources/config/services.yml

This file was deleted.

13 changes: 0 additions & 13 deletions src/Resources/contao/config/config.php

This file was deleted.

0 comments on commit a2e85e8

Please sign in to comment.