Skip to content

Commit

Permalink
Feature/configure download template (#77)
Browse files Browse the repository at this point in the history
* fix: changes done by cs-fixer

* feat: make download template configurable

* feat: make content disposition - display in browser vs. download of files - configurable in placeholder element

* fix:  fix ecs issues
  • Loading branch information
cgoIT authored Nov 16, 2023
1 parent 6a724f9 commit 0b55f8d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
19 changes: 18 additions & 1 deletion contao/dca/tl_form_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

declare(strict_types=1);

use Contao\Controller;

$GLOBALS['TL_DCA']['tl_form_field']['palettes']['mp_form_pageswitch'] = '{type_legend},type,label,mp_forms_backButton,slabel;{image_legend:hide},imageSubmit;{expert_legend:hide},mp_forms_backFragment,mp_forms_nextFragment,class,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible';
$GLOBALS['TL_DCA']['tl_form_field']['palettes']['mp_form_placeholder'] = '{type_legend},type;{text_legend},html;{template_legend:hide},customTpl;{invisible_legend:hide},invisible';
$GLOBALS['TL_DCA']['tl_form_field']['palettes']['mp_form_placeholder'] = '{type_legend},type;{text_legend},html;{mp_forms_download_legend},mp_forms_downloadTemplate,mp_forms_downloadInline;{template_legend:hide},customTpl;{invisible_legend:hide},invisible';

$GLOBALS['TL_DCA']['tl_form_field']['fields']['mp_forms_backButton'] = [
'exclude' => true,
Expand All @@ -25,3 +27,18 @@
'eval' => ['tl_class' => 'w50', 'maxlength' => 255],
'sql' => ['type' => 'string', 'length' => 255, 'default' => '', 'notnull' => true],
];

$GLOBALS['TL_DCA']['tl_form_field']['fields']['mp_forms_downloadTemplate'] = [
'exclude' => true,
'inputType' => 'select',
'options_callback' => static fn () => Controller::getTemplateGroup('ce_download_', [], 'ce_download'),
'eval' => ['chosen' => true, 'tl_class' => 'clr w50'],
'sql' => "varchar(64) NOT NULL default ''",
];

$GLOBALS['TL_DCA']['tl_form_field']['fields']['mp_forms_downloadInline'] = [
'exclude' => true,
'inputType' => 'checkbox',
'eval' => ['tl_class' => 'm12 w50'],
'sql' => "char(1) COLLATE ascii_bin NOT NULL default ''",
];
7 changes: 7 additions & 0 deletions contao/languages/de/tl_form_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
$GLOBALS['TL_LANG']['FFL']['mp_form_pageswitch'] = ['Seitenumbruch', 'Trennt die Felder in mehrere Seiten.'];
$GLOBALS['TL_LANG']['FFL']['mp_form_placeholder'] = ['Zusammenfassung', 'Erlaubt die Ausgabe von Daten der vorderen Seite(n).'];

/*
* Legends
*/
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_download_legend'] = 'Download-Einstellungen';

/*
* Fields
*/
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_backButton'] = ['Bezeichnung der Zurück-Schaltfläche', 'Bitte geben Sie die Bezeichnung der Zurück-Schaltfläche ein.'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_backFragment'] = ['URL Fragment für die Zurück-Schaltfläche', 'Hier können Sie ein optionales URL Fragment eingeben, welches der URL hinzugefügt wird (z.B. für Sprunglinks). Lassen Sie "#" weg.'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_nextFragment'] = ['URL Fragment für die Weiter-Schaltfläche', 'Hier können Sie ein optionales URL Fragment eingeben, welches der URL hinzugefügt wird (z.B. für Sprunglinks). Lassen Sie "#" weg.'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_downloadTemplate'] = ['Template für Download-Elemente', 'Template, das für das Rendern des Download-Links auf Zusammenfassungsseiten verwendet wird.'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_downloadInline'] = ['Im Browser anzeigen', 'Die Datei im Browser anzeigen, anstatt den Download-Dialog zu öffnen.'];
7 changes: 7 additions & 0 deletions contao/languages/en/tl_form_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
$GLOBALS['TL_LANG']['FFL']['mp_form_pageswitch'] = ['Page break', 'Separates the form fields into different pages/steps.'];
$GLOBALS['TL_LANG']['FFL']['mp_form_placeholder'] = ['Summary', 'Allows the output of the form data of the previous page(s).'];

/*
* Legends
*/
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_download_legend'] = 'Download settings';

/*
* Fields
*/
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_backButton'] = ['Back button label', 'Please enter the label of the back button.'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_backFragment'] = ['Back button URL fragment', 'You may enter an optional URL fragment here which will be added to the URL when hitting the back button (e.g. for anchor links). Omit the "#" here.'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_nextFragment'] = ['Continue button URL fragment', 'You may enter an optional URL fragment here which will be added to the URL when hitting the continue button (e.g. for anchor links). Omit the "#" here.'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_downloadTemplate'] = ['Template for download elements', 'Template used for rendering the download link on summary pages.'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_downloadInline'] = ['Show in browser', 'Show the file in the browser instead of opening the download dialog.'];
7 changes: 7 additions & 0 deletions contao/languages/ja/tl_form_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
$GLOBALS['TL_LANG']['FFL']['mp_form_pageswitch'] = ['改ページ', 'フォームの項目を異るページ/段階に分割します。'];
$GLOBALS['TL_LANG']['FFL']['mp_form_placeholder'] = ['要約', '以前のページのフォームのデータを表示します。'];

/*
* Legends
*/
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_download_legend'] = 'ダウンロードの設定';

/*
* Fields
*/
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_backButton'] = ['戻るボタンのラベル', '戻るボタンのラベルを入力してください。'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_backFragment'] = ['戻るボタンのURLの断片', '戻るボタンをクリックしたときのURLに付加するURLの断片(例えば、アンカーのリンク)を入力できます。"#"とすると省略できます。'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_nextFragment'] = ['続けるボタンのURLの断片', '続けるボタンをクリックしたときのURLに付加するURLの断片(例えば、アンカーのリンク)を入力できます。"#"とすると省略できます。'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_downloadTemplate'] = ['ダウンロード用テンプレート', 'サマリーページのダウンロードリンクのレンダリングに使用されるテンプレート。'];
$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_downloadInline'] = ['ブラウザーで表示', 'ダウンロードのダイアログを開く代わりにブラウザーでファイルを表示します。'];
11 changes: 9 additions & 2 deletions src/Widget/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Terminal42\MultipageFormsBundle\FormManagerFactoryInterface;
Expand Down Expand Up @@ -115,7 +116,13 @@ private function generateTokens(): array
}

if (isset($_GET['summary_download']) && $k === $_GET['summary_download']) {
throw new ResponseException(new BinaryFileResponse($file));
$binaryFileResponse = new BinaryFileResponse($file);
$binaryFileResponse->setContentDisposition(
$this->mp_forms_downloadInline ? ResponseHeaderBag::DISPOSITION_INLINE : ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$file->getBasename(),
);

throw new ResponseException($binaryFileResponse);
}

$fileTokens['download_url'] = $urlParser->addQueryString('summary_download='.$k);
Expand All @@ -128,7 +135,7 @@ private function generateTokens(): array
}

// Generate a general HTML output using the download template
$tpl = new FrontendTemplate('ce_download'); // TODO: make configurable in form field settings?
$tpl = new FrontendTemplate(empty($this->mp_forms_downloadTemplate) ? 'ce_download' : $this->mp_forms_downloadTemplate);
$tpl->link = $file->getBasename($file->getExtension());
$tpl->title = StringUtil::specialchars(sprintf($GLOBALS['TL_LANG']['MSC']['download'], $file->getBasename($file->getExtension())));
$tpl->href = $fileTokens['download_url'];
Expand Down

0 comments on commit 0b55f8d

Please sign in to comment.