‪TYPO3CMS  ‪main
LiveSearchController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
29 
34 #[AsController]
36 {
37  public function ‪__construct(
38  protected readonly ‪BackendViewFactory $backendViewFactory,
39  protected readonly ‪SearchRepository $searchService,
40  ) {}
41 
45  public function ‪searchAction(ServerRequestInterface $request): ResponseInterface
46  {
47  $mutableSearchDemand = ‪SearchDemand::fromRequest($request);
48  if ($mutableSearchDemand->getQuery() === '') {
49  return new ‪Response('', 400, [], 'Argument "query" is missing or empty.');
50  }
51 
52  $results = $this->searchService->find($mutableSearchDemand);
53  $pagination = new ‪SlidingWindowPagination($results, 15);
54  $response = [
55  'pagination' => [
56  'itemsPerPage' => ‪SearchDemand::DEFAULT_LIMIT,
57  'currentPage' => $pagination->getPaginator()->getCurrentPageNumber(),
58  'firstPage' => $pagination->getFirstPageNumber(),
59  'lastPage' => $pagination->getLastPageNumber(),
60  'allPageNumbers' => $pagination->getAllPageNumbers(),
61  'previousPageNumber' => $pagination->getPreviousPageNumber(),
62  'nextPageNumber' => $pagination->getNextPageNumber(),
63  'hasMorePages' => $pagination->getHasMorePages(),
64  'hasLessPages' => $pagination->getHasLessPages(),
65  ],
66  'results' => $results->getPaginatedItems(),
67  ];
68 
69  return new ‪JsonResponse($response);
70  }
71 
72  public function ‪formAction(ServerRequestInterface $request): ResponseInterface
73  {
74  $hints = [
75  'LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:liveSearch_helpDescriptionPages',
76  'LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:liveSearch_helpDescriptionContent',
77  'LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:liveSearch_help.shortcutOpen',
78  ];
79  $randomHintKey = array_rand($hints);
80 
81  $searchDemand = ‪SearchDemand::fromRequest($request);
82  $searchProviders = $this->searchService->getSearchProviderState($searchDemand);
83 
84  $activeOptions = 0;
85  // `isActive` is the result of `in_array()`, which returns a `bool`.
86  $activeOptions += count(array_filter($searchProviders, fn(array $searchProviderOption): bool => $searchProviderOption['isActive']));
87 
88  $view = $this->backendViewFactory->create($request, ['typo3/cms-backend']);
89  $view->assignMultiple([
90  'searchDemand' => $searchDemand,
91  'hint' => $hints[$randomHintKey],
92  'searchProviders' => $searchProviders,
93  'activeOptions' => $activeOptions,
94  ]);
95 
96  $response = new ‪Response();
97  $response->getBody()->write($view->render('LiveSearch/Form'));
98 
99  return $response;
100  }
101 }
‪TYPO3\CMS\Backend\Controller\LiveSearchController\searchAction
‪searchAction(ServerRequestInterface $request)
Definition: LiveSearchController.php:45
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Backend\Controller\LiveSearchController\formAction
‪formAction(ServerRequestInterface $request)
Definition: LiveSearchController.php:72
‪TYPO3\CMS\Backend\Controller\LiveSearchController\__construct
‪__construct(protected readonly BackendViewFactory $backendViewFactory, protected readonly SearchRepository $searchService,)
Definition: LiveSearchController.php:37
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:32
‪TYPO3\CMS\Backend\Search\LiveSearch\SearchDemand\SearchDemand
Definition: SearchDemand.php:29
‪TYPO3\CMS\Backend\Search\LiveSearch\SearchDemand\SearchDemand\DEFAULT_LIMIT
‪const DEFAULT_LIMIT
Definition: SearchDemand.php:30
‪TYPO3\CMS\Backend\Search\LiveSearch\SearchDemand\SearchDemand\fromRequest
‪static fromRequest(ServerRequestInterface $request)
Definition: SearchDemand.php:89
‪TYPO3\CMS\Backend\Search\LiveSearch\SearchRepository
Definition: SearchRepository.php:33
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Backend\Controller
Definition: AboutController.php:18
‪TYPO3\CMS\Core\Pagination\SlidingWindowPagination
Definition: SlidingWindowPagination.php:21
‪TYPO3\CMS\Backend\Controller\LiveSearchController
Definition: LiveSearchController.php:36