From 387239a3fc9be5f852996cbfb371fc3e520d80f6 Mon Sep 17 00:00:00 2001 From: Georg Ringer Date: Tue, 5 Sep 2023 07:52:22 +0200 Subject: [PATCH] [FEATURE] Search for forms --- Classes/Provider/FormDataProvider.php | 169 ++++++++++++++++++++++++++ Configuration/Services.php | 29 +++++ Configuration/Services.yaml | 1 + README.md | 2 +- ext_emconf.php | 4 +- 5 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 Classes/Provider/FormDataProvider.php create mode 100644 Configuration/Services.php diff --git a/Classes/Provider/FormDataProvider.php b/Classes/Provider/FormDataProvider.php new file mode 100644 index 0000000..b2f104e --- /dev/null +++ b/Classes/Provider/FormDataProvider.php @@ -0,0 +1,169 @@ +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']; + } +} diff --git a/Configuration/Services.php b/Configuration/Services.php new file mode 100644 index 0000000..dd6a2b8 --- /dev/null +++ b/Configuration/Services.php @@ -0,0 +1,29 @@ +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) + ; + } + +}; \ No newline at end of file diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index a72fd29..94379a5 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -6,6 +6,7 @@ services: StudioMitte\LiveSearchExtended\: resource: '../Classes/*' + exclude: '../Classes/Provider/FormDataProvider.php' StudioMitte\LiveSearchExtended\EventListener\ModifyResultEventListener: tags: diff --git a/README.md b/README.md index e056d36..fbf5856 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/ext_emconf.php b/ext_emconf.php index 78ab39a..57bb447 100755 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -5,10 +5,10 @@ 'description' => '', 'category' => 'be', 'author' => 'Georg Ringer', - 'author_email' => '', + 'author_email' => 'gr@studiomitte.com', 'state' => 'beta', 'clearCacheOnLoad' => true, - 'version' => '0.0.1', + 'version' => '1.0.0', 'constraints' => [ 'depends' => [ 'typo3' => '12.4.5-12.99.99',