Skip to content

Commit

Permalink
Merge pull request #6 from christophlehmann/feature-5-disable-wizards
Browse files Browse the repository at this point in the history
Disable wizards having no results
  • Loading branch information
christophlehmann authored Sep 13, 2021
2 parents de938d1 + 6d60286 commit ba70bc1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
81 changes: 79 additions & 2 deletions Classes/Controller/NewContentElementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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)
];
}
}
Expand Down Expand Up @@ -77,20 +83,91 @@ 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
];
}
}

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;
}
}
2 changes: 1 addition & 1 deletion Resources/Private/Templates/Filter/MenuItem.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
data-namespace-typo3-fluid="true">

<div class="{f:if(condition: '{isVersion10}', then: 'media')} t3js-media-new-content-element-wizard media-new-content-element-wizard">
<a href="#" class="{f:if(condition: '{isVersion10}', else: 'media')} pagetreefilter"
<a href="#" class="pagetreefilter{f:if(condition: '{isVersion10}', else: ' media')}{f:if(condition: '{wizardInformation.disabled}', then: ' disabled')}"
data-pagetreefilter="{f:if(condition: '{wizardInformation.filter}', then: '{wizardInformation.filter}', else: '{pagetreefilter:buildFilter(wizardInformation: \'{wizardInformation}\')}')}"
data-bs-slide="next">

Expand Down

0 comments on commit ba70bc1

Please sign in to comment.