Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 06a70b8611 | |||
| 6170b255a3 |
@@ -8,13 +8,10 @@ use Webfarben\DummyCopier\Service\DummyCopier;
|
|||||||
use Webfarben\DummyCopier\Service\DummyCopyOptions;
|
use Webfarben\DummyCopier\Service\DummyCopyOptions;
|
||||||
use Contao\BackendModule;
|
use Contao\BackendModule;
|
||||||
use Contao\Environment;
|
use Contao\Environment;
|
||||||
use Contao\FileTree;
|
|
||||||
use Contao\Input;
|
use Contao\Input;
|
||||||
use Contao\Message;
|
use Contao\Message;
|
||||||
use Contao\PageTree;
|
|
||||||
use Contao\StringUtil;
|
use Contao\StringUtil;
|
||||||
use Contao\System;
|
use Contao\System;
|
||||||
use Contao\Widget;
|
|
||||||
use Doctrine\DBAL\Connection;
|
use Doctrine\DBAL\Connection;
|
||||||
use Symfony\Component\Filesystem\Filesystem;
|
use Symfony\Component\Filesystem\Filesystem;
|
||||||
|
|
||||||
@@ -28,7 +25,7 @@ class DummyCopierModule extends BackendModule
|
|||||||
$connection = System::getContainer()->get('database_connection');
|
$connection = System::getContainer()->get('database_connection');
|
||||||
|
|
||||||
$this->Template->action = Environment::get('request');
|
$this->Template->action = Environment::get('request');
|
||||||
$this->Template->requestToken = \defined('REQUEST_TOKEN') ? REQUEST_TOKEN : '';
|
$this->Template->requestToken = $this->getCsrfToken();
|
||||||
$this->Template->pageChoices = $this->getPageChoices($connection);
|
$this->Template->pageChoices = $this->getPageChoices($connection);
|
||||||
$this->Template->moduleChoices = $this->getModuleChoices($connection);
|
$this->Template->moduleChoices = $this->getModuleChoices($connection);
|
||||||
$this->Template->contentChoices = $this->getContentChoices($connection);
|
$this->Template->contentChoices = $this->getContentChoices($connection);
|
||||||
@@ -36,6 +33,7 @@ class DummyCopierModule extends BackendModule
|
|||||||
$this->Template->sourcePagesWidget = '';
|
$this->Template->sourcePagesWidget = '';
|
||||||
$this->Template->targetParentPageWidget = '';
|
$this->Template->targetParentPageWidget = '';
|
||||||
$this->Template->sourceDirectoriesWidget = '';
|
$this->Template->sourceDirectoriesWidget = '';
|
||||||
|
|
||||||
$targetParentPageId = $this->parseSingleIdInput(Input::postRaw('targetParentPage'));
|
$targetParentPageId = $this->parseSingleIdInput(Input::postRaw('targetParentPage'));
|
||||||
|
|
||||||
$this->Template->selected = [
|
$this->Template->selected = [
|
||||||
@@ -49,8 +47,6 @@ class DummyCopierModule extends BackendModule
|
|||||||
'namePrefix' => trim((string) Input::post('namePrefix')),
|
'namePrefix' => trim((string) Input::post('namePrefix')),
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->prepareTreeWidgets();
|
|
||||||
|
|
||||||
if (Input::post('FORM_SUBMIT') !== 'tl_dummy_copier') {
|
if (Input::post('FORM_SUBMIT') !== 'tl_dummy_copier') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -143,85 +139,24 @@ class DummyCopierModule extends BackendModule
|
|||||||
return $ids[0] ?? 0;
|
return $ids[0] ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function prepareTreeWidgets(): void
|
private function getCsrfToken(): string
|
||||||
{
|
{
|
||||||
if (!class_exists(PageTree::class) || !class_exists(FileTree::class) || !class_exists(Widget::class)) {
|
$container = System::getContainer();
|
||||||
return;
|
|
||||||
|
// Contao 5: use Symfony CSRF token manager
|
||||||
|
if ($container->has('contao.csrf.token_manager')) {
|
||||||
|
return $container
|
||||||
|
->get('contao.csrf.token_manager')
|
||||||
|
->getToken((string) $container->getParameter('contao.csrf_token_name'))
|
||||||
|
->getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// Contao 4 fallback
|
||||||
$selectedPages = $this->parseIdInput(Input::postRaw('sourcePages'));
|
if (\defined('REQUEST_TOKEN')) {
|
||||||
$selectedParent = (int) Input::post('targetParentPage');
|
return REQUEST_TOKEN;
|
||||||
$selectedDirectories = $this->parsePathInput(Input::postRaw('sourceDirectories'));
|
|
||||||
|
|
||||||
$this->Template->sourcePagesWidget = $this->renderPageTreeWidget(
|
|
||||||
'sourcePages',
|
|
||||||
'Quell-Seiten (pageTree)',
|
|
||||||
$selectedPages,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->Template->targetParentPageWidget = $this->renderPageTreeWidget(
|
|
||||||
'targetParentPage',
|
|
||||||
'Ziel-Elternseite (pageTree)',
|
|
||||||
$selectedParent > 0 ? [$selectedParent] : [],
|
|
||||||
false
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->Template->sourceDirectoriesWidget = $this->renderFileTreeWidget(
|
|
||||||
'sourceDirectories',
|
|
||||||
'Quell-Verzeichnisse (fileTree)',
|
|
||||||
$selectedDirectories
|
|
||||||
);
|
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
// If widget rendering differs by Contao version, the module falls back to select boxes.
|
|
||||||
$this->Template->sourcePagesWidget = '';
|
|
||||||
$this->Template->targetParentPageWidget = '';
|
|
||||||
$this->Template->sourceDirectoriesWidget = '';
|
|
||||||
Message::addInfo('Tree-Widgets konnten nicht initialisiert werden, Fallback-Auswahl wird verwendet.');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function renderPageTreeWidget(string $name, string $label, array $value, bool $multiple): string
|
return '';
|
||||||
{
|
|
||||||
$attributes = Widget::getAttributesFromDca([
|
|
||||||
'inputType' => 'pageTree',
|
|
||||||
'label' => [$label, ''],
|
|
||||||
'eval' => [
|
|
||||||
'fieldType' => $multiple ? 'checkbox' : 'radio',
|
|
||||||
'multiple' => $multiple,
|
|
||||||
'tl_class' => 'clr',
|
|
||||||
],
|
|
||||||
], $name, $value, $name, 'tl_dummy_copier');
|
|
||||||
|
|
||||||
$attributes['id'] = $name;
|
|
||||||
$attributes['name'] = $name;
|
|
||||||
|
|
||||||
$widget = new PageTree($attributes);
|
|
||||||
|
|
||||||
return $widget->generate();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function renderFileTreeWidget(string $name, string $label, array $value): string
|
|
||||||
{
|
|
||||||
$attributes = Widget::getAttributesFromDca([
|
|
||||||
'inputType' => 'fileTree',
|
|
||||||
'label' => [$label, ''],
|
|
||||||
'eval' => [
|
|
||||||
'fieldType' => 'checkbox',
|
|
||||||
'filesOnly' => false,
|
|
||||||
'files' => false,
|
|
||||||
'multiple' => true,
|
|
||||||
'tl_class' => 'clr',
|
|
||||||
],
|
|
||||||
], $name, $value, $name, 'tl_dummy_copier');
|
|
||||||
|
|
||||||
$attributes['id'] = $name;
|
|
||||||
$attributes['name'] = $name;
|
|
||||||
|
|
||||||
$widget = new FileTree($attributes);
|
|
||||||
|
|
||||||
return $widget->generate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user