Search.php 4.9 KB

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