Skip to content

Commit

Permalink
[FEATURE] Search for forms
Browse files Browse the repository at this point in the history
  • Loading branch information
georgringer committed Sep 5, 2023
1 parent dcc468d commit 387239a
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 3 deletions.
169 changes: 169 additions & 0 deletions Classes/Provider/FormDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

declare(strict_types=1);


namespace StudioMitte\LiveSearchExtended\Provider;

use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Search\LiveSearch\ResultItem;
use TYPO3\CMS\Backend\Search\LiveSearch\ResultItemAction;
use TYPO3\CMS\Backend\Search\LiveSearch\SearchDemand\SearchDemand;
use TYPO3\CMS\Backend\Search\LiveSearch\SearchProviderInterface;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager;
use TYPO3\CMS\Form\Service\DatabaseService;

final class FormDataProvider implements SearchProviderInterface
{

protected LanguageService $languageService;
protected string $userPermissions;
protected bool $formsAllowed = false;

// protected FormPersistenceManagerInterface $formPersistenceManager;

public function __construct(
protected readonly LanguageServiceFactory $languageServiceFactory,
protected readonly IconFactory $iconFactory,
protected readonly UriBuilder $uriBuilder,
protected readonly FormPersistenceManager $formPersistenceManager,
protected DatabaseService $databaseService,
)
{
$this->languageService = $this->languageServiceFactory->createFromUserPreferences($this->getBackendUser());
$this->formsAllowed = ExtensionManagementUtility::isLoaded('form') && $this->getBackendUser()->check('modules', 'web_FormFormbuilder');
}

public function getFilterLabel(): string
{
return $this->languageService->sL('LLL:EXT:form/Resources/Private/Language/locallang_module.xlf:mlang_tabs_tab');
}

public function count(SearchDemand $searchDemand): int
{
if (!$this->formsAllowed) {
return 0;
}
return count($this->get($searchDemand));
}

/**
* @return ResultItem[]
*/
public function find(SearchDemand $searchDemand): array
{
if (!$this->formsAllowed) {
return [];
}
$result = [];
$remainingItems = $searchDemand->getLimit();
$offset = $searchDemand->getOffset();
if ($remainingItems < 1) {
return [];
}


$result = $this->get($searchDemand);

return $result;
}

protected function get(SearchDemand $searchDemand)
{
$forms = [];
$allForms = $this->formPersistenceManager->listForms();
foreach ($allForms as $form) {
if (stripos($form['name'], $searchDemand->getQuery()) !== false) {
$forms[] = $form;
}
}

$items = [];
foreach ($forms as $form) {
$actions = [];
$editLink = $this->getEditLink($form['persistenceIdentifier']);
if ($editLink !== '') {
$actions[] = (new ResultItemAction('edit_record'))
->setLabel($this->languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:edit'))
->setIcon($this->iconFactory->getIcon('actions-open', Icon::SIZE_SMALL))
->setUrl($editLink);

}

if ($count = $this->getReferenceCount($form)) {
$text = sprintf('%s: %s', $this->languageService->sL('LLL:EXT:form/Resources/Private/Language/Database.xlf:formManager.references'), $count);
$actions[] = (new ResultItemAction('form-usage'))
->setLabel($text)
->setIcon($this->iconFactory->getIcon('form-number', Icon::SIZE_SMALL))// ->setUrl($editLink);
;
}

$extraData = [
'table' => 'form',
'uid' => $form['identifier'],
'breadcrumb' => $form['persistenceIdentifier'],
];

$items[] = (new ResultItem(self::class))
->setItemTitle($form['name'])
->setTypeLabel('Form')
->setIcon($this->iconFactory->getIcon('content-form', Icon::SIZE_SMALL))
->setActions(...$actions)
->setExtraData($extraData)
->setInternalData([
'row' => $form,
]);
}

return $items;
}

protected function canAccessTable(string $tableName): bool
{
return true;
}


/**
* Build a backend edit link based on given record.
*/
protected function getEditLink(string $formIdentifier): string
{
$backendUser = $this->getBackendUser();
if (true) {
$editLink = (string)$this->uriBuilder->buildUriFromRoute('web_FormFormbuilder.FormEditor_index', [
'formPersistenceIdentifier' => $formIdentifier,
]);
}
return $editLink;
}

protected function getReferenceCount(array $formDefinition): int
{
$allReferencesForFileUid = $this->databaseService->getAllReferencesForFileUid();
$allReferencesForPersistenceIdentifier = $this->databaseService->getAllReferencesForPersistenceIdentifier();

$referenceCount = 0;
if (
isset($formDefinition['fileUid'])
&& array_key_exists($formDefinition['fileUid'], $allReferencesForFileUid)
) {
$referenceCount = $allReferencesForFileUid[$formDefinition['fileUid']];
} elseif (array_key_exists($formDefinition['persistenceIdentifier'], $allReferencesForPersistenceIdentifier)) {
$referenceCount = $allReferencesForPersistenceIdentifier[$formDefinition['persistenceIdentifier']];
}

return $referenceCount;
}

protected function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
}
29 changes: 29 additions & 0 deletions Configuration/Services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace GeorgRinger\Doc;

use StudioMitte\LiveSearchExtended\Provider\FormDataProvider;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use TYPO3\CMS\Form\Domain\Model\FormDefinition;

return static function (ContainerConfigurator $configurator, ContainerBuilder $containerBuilder) {
$services = $configurator->services();
if (class_exists(FormDefinition::class)) {
$services->set(FormDataProvider::class)
->public()
->autowire()
->autoconfigure()
->tag('livesearch.provider', [
'priority' => 5
])
;
} else {
$services->set(FormDataProvider::class)
->autowire(false)
->autoconfigure(false)
;
}

};
1 change: 1 addition & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:

StudioMitte\LiveSearchExtended\:
resource: '../Classes/*'
exclude: '../Classes/Provider/FormDataProvider.php'

StudioMitte\LiveSearchExtended\EventListener\ModifyResultEventListener:
tags:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This extension improves the output of TYPO3's backend search of the top right co

- Appending the UID of the record to the title
- Provide more information about the record in the result
- Search for EXT:form records (upcoming version)
- Search for EXT:form records

Supported TYPO3 versions:

Expand Down
4 changes: 2 additions & 2 deletions ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
'description' => '',
'category' => 'be',
'author' => 'Georg Ringer',
'author_email' => '',
'author_email' => '[email protected]',
'state' => 'beta',
'clearCacheOnLoad' => true,
'version' => '0.0.1',
'version' => '1.0.0',
'constraints' => [
'depends' => [
'typo3' => '12.4.5-12.99.99',
Expand Down

0 comments on commit 387239a

Please sign in to comment.