Search.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Util\Common;
  25. use App\Util\Exception\ClientException;
  26. use App\Util\Exception\RedirectException;
  27. use App\Util\Formatting;
  28. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  29. use Symfony\Component\Form\Extension\Core\Type\TextType;
  30. use Symfony\Component\Form\FormView;
  31. use Symfony\Component\Form\SubmitButton;
  32. use Symfony\Component\HttpFoundation\Request;
  33. class Search extends Component
  34. {
  35. public function onAddRoute($r)
  36. {
  37. $r->connect('search', '/search', Controller\Search::class);
  38. }
  39. /**
  40. * Helper function for generating and processing the search form, so it can be embedded in
  41. * multiple places. Can be provided with a $query, which will prefill the query field. If
  42. * $add_subscribe, allow the user to add the current query to their left panel
  43. */
  44. public static function searchForm(Request $request, ?string $query = null, bool $add_subscribe = false): FormView
  45. {
  46. $actor = Common::actor();
  47. if (\is_null($actor)) {
  48. $add_subscribe = false;
  49. }
  50. $form_definition = [
  51. ['search_query', TextType::class, [
  52. 'attr' => ['placeholder' => _m('Input desired query...'), 'value' => $query],
  53. ]],
  54. ];
  55. if ($add_subscribe) {
  56. $form_definition[] = [
  57. 'title', TextType::class,
  58. [
  59. 'label' => _m('Subscribe to search query'),
  60. 'help' => _m('By subscribing to a search query, a new feed link will be added to left panel\'s feed navigation menu'),
  61. 'required' => false,
  62. 'attr' => [
  63. 'title' => _m('Title for this new feed in your left panel'),
  64. 'placeholder' => _m('Input desired title...'),
  65. ],
  66. ],
  67. ];
  68. $form_definition[] = [
  69. 'subscribe_to_search',
  70. SubmitType::class,
  71. [
  72. 'label' => _m('Subscribe'),
  73. 'attr' => [
  74. 'title' => _m('Add this search as a feed in your feeds section of the left panel'),
  75. ],
  76. ],
  77. ];
  78. }
  79. $form_definition[] = [
  80. $form_name = 'submit_search',
  81. SubmitType::class,
  82. [
  83. 'label' => _m('Search'),
  84. 'attr' => [
  85. //'class' => 'button-container search-button-container',
  86. 'title' => _m('Perform search'),
  87. ],
  88. ],
  89. ];
  90. $form = Form::create($form_definition);
  91. if ('POST' === $request->getMethod() && $request->request->has($form_name)) {
  92. $form->handleRequest($request);
  93. if ($form->isSubmitted() && $form->isValid()) {
  94. $data = $form->getData();
  95. $redirect = false;
  96. if ($add_subscribe) {
  97. /** @var SubmitButton $subscribe */
  98. $subscribe = $form->get('subscribe_to_search');
  99. if ($subscribe->isClicked()) {
  100. if (!\is_null($data['title'])) {
  101. Event::handle('AppendFeed', [$actor, $data['title'], 'search', ['q' => $data['search_query']]]);
  102. } else {
  103. throw new ClientException(_m('Empty title is not allowed.'));
  104. }
  105. $redirect = true;
  106. }
  107. }
  108. /** @var SubmitButton $submit */
  109. $submit = $form->get($form_name);
  110. if ($submit->isClicked() || $redirect) {
  111. throw new RedirectException('search', ['q' => $data['search_query']]);
  112. }
  113. }
  114. }
  115. return $form->createView();
  116. }
  117. /**
  118. * Add the search form to the site header
  119. *
  120. * @throws RedirectException
  121. */
  122. public function onPrependRightPanelBlock(Request $request, array &$elements): bool
  123. {
  124. $elements[] = Formatting::twigRenderFile('cards/search/view.html.twig', ['search' => self::searchForm($request)]);
  125. return Event::next;
  126. }
  127. /**
  128. * Output our dedicated stylesheet
  129. *
  130. * @param array $styles stylesheets path
  131. *
  132. * @return bool hook value; true means continue processing, false means stop
  133. */
  134. public function onEndShowStyles(array &$styles, string $route): bool
  135. {
  136. $styles[] = 'components/Search/assets/css/view.css';
  137. return Event::next;
  138. }
  139. }