Search.php 4.8 KB

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