[TASK] Use a static closure call if possible for better scoping
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / View / RecordSearchBoxComponent.php
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
18 namespace TYPO3\CMS\Recordlist\View;
19
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Fluid\View\StandaloneView;
22 use TYPO3Fluid\Fluid\View\ViewInterface;
23
24 /**
25 * Renders the search box for the record listing and the element browser.
26 *
27 * @internal
28 */
29 class RecordSearchBoxComponent
30 {
31 protected ViewInterface $view;
32 protected array $allowedSearchLevels = [];
33 protected string $searchWord = '';
34 protected int $searchLevel = 0;
35
36 public function __construct(ViewInterface $view = null)
37 {
38 $this->view = $view ?? $this->getView('Search.html');
39 }
40
41 public function setSearchWord(string $searchWord): self
42 {
43 $this->searchWord = $searchWord;
44 return $this;
45 }
46
47 public function setSearchLevel(int $searchLevel): self
48 {
49 $this->searchLevel = $searchLevel;
50 return $this;
51 }
52
53 public function setAllowedSearchLevels(array $allowedSearchLevels): self
54 {
55 $this->allowedSearchLevels = $allowedSearchLevels;
56 return $this;
57 }
58
59 public function render(string $formUrl = ''): string
60 {
61 return $this->view
62 ->assignMultiple([
63 'formUrl' => $formUrl,
64 'availableSearchLevels' => $this->allowedSearchLevels,
65 'selectedSearchLevel' => $this->searchLevel,
66 'searchString' => $this->searchWord,
67 ])
68 ->render();
69 }
70
71 protected function getView(string $filename): StandaloneView
72 {
73 $view = GeneralUtility::makeInstance(StandaloneView::class);
74 $view->setTemplateRootPaths(['EXT:recordlist/Resources/Private/Templates']);
75 $view->setTemplate($filename);
76 return $view;
77 }
78 }