diff --git a/Classes/Controller/NewContentElementController.php b/Classes/Controller/NewContentElementController.php index 263fa88..b889715 100644 --- a/Classes/Controller/NewContentElementController.php +++ b/Classes/Controller/NewContentElementController.php @@ -5,6 +5,9 @@ use Lemming\PageTreeFilter\Utility\ConfigurationUtility; use Psr\Http\Message\ServerRequestInterface; +use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Database\Query\QueryBuilder; +use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\VersionNumberUtility; use TYPO3\CMS\Fluid\View\StandaloneView; @@ -26,6 +29,7 @@ protected function init(ServerRequestInterface $request) public function getWizards(): array { $wizards = parent::getWizards(); + $wizards = $this->disableWizardsHavingNoResults($wizards); $wizards = $this->appendRecords($wizards); $wizards = $this->appendPageTypes($wizards); @@ -36,6 +40,7 @@ protected function appendPageTypes(array $wizards): array { $wizards['pagetypes']['header'] = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:be_groups.pagetypes_select'); $backendUser = $this->getBackendUser(); + $usedPageTypes = $this->getUsedPageTypes(); foreach ($GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'] as $no => $pageTypeConfiguration) { if ($pageTypeConfiguration[1] === '--div--') { continue; @@ -45,7 +50,8 @@ protected function appendPageTypes(array $wizards): array 'title' => $this->getLanguageService()->sL($pageTypeConfiguration[0]), 'iconIdentifier' => $GLOBALS['TCA']['pages']['ctrl']['typeicon_classes'][$pageTypeConfiguration[1]], 'params' => '', // if missing it leads to an exception - 'filter' => sprintf('table=pages doktype=%d', $pageTypeConfiguration[1]) + 'filter' => sprintf('table=pages doktype=%d', $pageTypeConfiguration[1]), + 'disabled' => !in_array($pageTypeConfiguration[1], $usedPageTypes) ]; } } @@ -77,7 +83,8 @@ protected function appendRecords(array $wizards): array 'title' => $this->getLanguageService()->sL($tableConfiguration['ctrl']['title']), 'iconIdentifier' => $iconIdentifier, 'params' => '', // if missing it leads to an exception - 'filter' => sprintf('table=%s', $tableName) + 'filter' => sprintf('table=%s', $tableName), + 'disabled' => $this->areRecordsInTable($tableName) ? false : true ]; } } @@ -85,12 +92,82 @@ protected function appendRecords(array $wizards): array return $wizards; } + protected function getUsedPageTypes(): array + { + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages'); + $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); + $pageTypes = $queryBuilder + ->select('doktype') + ->from('pages') + ->groupBy('doktype') + ->execute() + ->fetchFirstColumn(); + + return $pageTypes; + } + + protected function disableWizardsHavingNoResults(array $wizards): array + { + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content'); + $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); + $contentTypes = $queryBuilder + ->select('CType', 'list_type') + ->from('tt_content') + ->groupBy('CType', 'list_type') + ->execute() + ->fetchAllAssociative(); + + $generalPluginEnabled = false; + foreach ($contentTypes as $no => $contentTypeCombination) { + if ($contentTypeCombination['CType'] !== 'list') { + unset($contentTypes[$no]['list_type']); + } else { + $generalPluginEnabled = true; + } + } + + foreach ($wizards as $no => $wizard) { + if (!isset($wizard['tt_content_defValues'])) { + continue; + } + + if ($wizard['tt_content_defValues']['CType'] !== 'list') { + unset($wizard['tt_content_defValues']['list_type']); + } + + if ($wizard['tt_content_defValues'] === ['CType' => 'list']) { + $wizard[$no]['disabled'] = $generalPluginEnabled; + } else { + $wizards[$no]['disabled'] = !in_array($wizard['tt_content_defValues'], $contentTypes); + } + } + + return $wizards; + } + + protected function areRecordsInTable($tableName): bool + { + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName); + $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); + $oneRecord = $queryBuilder->select('*') + ->from($tableName) + ->setMaxResults(1) + ->execute() + ->fetchOne(); + + return $oneRecord !== false; + } + protected function getFluidTemplateObject(string $filename = 'Main.html'): StandaloneView { /** @var StandaloneView $view */ $view = GeneralUtility::makeInstance(StandaloneView::class); $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:pagetreefilter/Resources/Private/Templates/Filter/' . $filename)); $view->getRequest()->setControllerExtensionName('Pagetreefilter'); + return $view; } } \ No newline at end of file diff --git a/Resources/Private/Templates/Filter/MenuItem.html b/Resources/Private/Templates/Filter/MenuItem.html index 2f9e529..053fcdf 100644 --- a/Resources/Private/Templates/Filter/MenuItem.html +++ b/Resources/Private/Templates/Filter/MenuItem.html @@ -4,7 +4,7 @@ data-namespace-typo3-fluid="true">
-