Search.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Search;
  20. use App\Core\Event;
  21. use App\Core\Form;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Modules\Component;
  24. use App\Core\Router;
  25. use App\Util\Common;
  26. use App\Util\Exception\ClientException;
  27. use App\Util\Exception\RedirectException;
  28. use App\Util\Formatting;
  29. use EventResult;
  30. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  31. use Symfony\Component\Form\Extension\Core\Type\TextType;
  32. use Symfony\Component\Form\FormView;
  33. use Symfony\Component\Form\SubmitButton;
  34. use Symfony\Component\HttpFoundation\Request;
  35. class Search extends Component
  36. {
  37. public function onAddRoute(Router $r): EventResult
  38. {
  39. $r->connect('search', '/search', Controller\Search::class);
  40. return EventResult::next;
  41. }
  42. /**
  43. * Helper function for generating and processing the search form, so it can be embedded in
  44. * multiple places. Can be provided with a $query, which will prefill the query field. If
  45. * $add_subscribe, allow the user to add the current query to their left panel
  46. */
  47. public static function searchForm(Request $request, ?string $query = null, bool $add_subscribe = false): FormView
  48. {
  49. $actor = Common::actor();
  50. if (\is_null($actor)) {
  51. $add_subscribe = false;
  52. }
  53. $form_definition = [
  54. ['search_query', TextType::class, [
  55. 'attr' => ['placeholder' => _m('Input desired query...'), 'value' => $query],
  56. ]],
  57. ];
  58. if ($add_subscribe) {
  59. $form_definition[] = [
  60. 'title', TextType::class,
  61. [
  62. 'label' => _m('Subscribe to search query'),
  63. 'help' => _m('By subscribing to a search query, a new feed link will be added to left panel\'s feed navigation menu'),
  64. 'required' => false,
  65. 'attr' => [
  66. 'title' => _m('Title for this new feed in your left panel'),
  67. 'placeholder' => _m('Input desired title...'),
  68. ],
  69. ],
  70. ];
  71. $form_definition[] = [
  72. 'subscribe_to_search',
  73. SubmitType::class,
  74. [
  75. 'label' => _m('Subscribe'),
  76. 'attr' => [
  77. 'title' => _m('Add this search as a feed in your feeds section of the left panel'),
  78. ],
  79. ],
  80. ];
  81. }
  82. $form_definition[] = [
  83. $form_name = 'submit_search',
  84. SubmitType::class,
  85. [
  86. 'label' => _m('Search'),
  87. 'attr' => [
  88. //'class' => 'button-container search-button-container',
  89. 'title' => _m('Perform search'),
  90. ],
  91. ],
  92. ];
  93. $form = Form::create($form_definition);
  94. if ('POST' === $request->getMethod() && $request->request->has($form_name)) {
  95. $form->handleRequest($request);
  96. if ($form->isSubmitted() && $form->isValid()) {
  97. $data = $form->getData();
  98. $redirect = false;
  99. if ($add_subscribe) {
  100. /** @var SubmitButton $subscribe */
  101. $subscribe = $form->get('subscribe_to_search');
  102. if ($subscribe->isClicked()) {
  103. if (!\is_null($data['title'])) {
  104. Event::handle('AppendFeed', [$actor, $data['title'], 'search', ['q' => $data['search_query']]]);
  105. } else {
  106. throw new ClientException(_m('Empty title is not allowed.'));
  107. }
  108. $redirect = true;
  109. }
  110. }
  111. /** @var SubmitButton $submit */
  112. $submit = $form->get($form_name);
  113. if ($submit->isClicked() || $redirect) {
  114. throw new RedirectException('search', ['q' => $data['search_query']]);
  115. }
  116. }
  117. }
  118. return $form->createView();
  119. }
  120. /**
  121. * Add the search form to the site header
  122. *
  123. * @param string[] $elements
  124. *
  125. * @throws RedirectException
  126. */
  127. public function onPrependRightPanelBlock(Request $request, array &$elements): EventResult
  128. {
  129. $elements[] = Formatting::twigRenderFile('cards/search/view.html.twig', ['search' => self::searchForm($request)]);
  130. return Event::next;
  131. }
  132. /**
  133. * Output our dedicated stylesheet
  134. *
  135. * @param string[] $styles stylesheets path
  136. */
  137. public function onEndShowStyles(array &$styles, string $route): EventResult
  138. {
  139. $styles[] = 'components/Search/assets/css/view.css';
  140. return Event::next;
  141. }
  142. }