Search.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\Controller;
  20. use App\Core\Form;
  21. use function App\Core\I18n\_m;
  22. use App\Util\Common;
  23. use App\Util\Exception\BugFoundException;
  24. use App\Util\Exception\RedirectException;
  25. use App\Util\Form\FormFields;
  26. use App\Util\Formatting;
  27. use Component\Collection\Util\Controller\FeedController;
  28. use Component\Search as Comp;
  29. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  30. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  31. use Symfony\Component\Form\Extension\Core\Type\TextType;
  32. use Symfony\Component\HttpFoundation\Request;
  33. class Search extends FeedController
  34. {
  35. /**
  36. * Handle a search query
  37. */
  38. public function handle(Request $request)
  39. {
  40. $actor = Common::actor();
  41. $language = !\is_null($actor) ? $actor->getTopLanguage()->getLocale() : null;
  42. $q = $this->string('q');
  43. $data = $this->query(query: $q, locale: $language);
  44. $notes = $data['notes'];
  45. $actors = $data['actors'];
  46. $search_builder_form = Form::create([
  47. ['include_actors', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include people/actors')]],
  48. ['include_actors_groups', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include groups')]],
  49. ['include_actors_lists', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include people lists')]],
  50. ['include_actors_people', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include people')]],
  51. ['include_actors_businesses', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include businesses')]],
  52. ['include_actors_organizations', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include organizations')]],
  53. ['include_actors_bots', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include bots')]],
  54. ['include_notes', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include notes')]],
  55. ['include_notes_text', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include text notes')]],
  56. ['include_notes_media', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include media notes')]],
  57. ['include_notes_polls', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include polls')]],
  58. ['include_notes_bookmarks', CheckboxType::class, ['required' => false, 'data' => true, 'label' => _m('Include bookmarks')]],
  59. /* note_langs */ FormFields::language($actor, context_actor: null, label: _m('Search for notes in these languages'), multiple: true, required: false, use_short_display: false, form_id: 'note_langs', use_no_selection: true),
  60. ['note_tags', TextType::class, ['required' => false, 'label' => _m('Include only notes with all the following tags')]],
  61. /* note_actor_langs */ FormFields::language($actor, context_actor: null, label: _m('Search for notes by people who know these languages'), multiple: true, required: false, use_short_display: false, form_id: 'note_actor_langs', use_no_selection: true),
  62. ['note_actor_tags', TextType::class, ['required' => false, 'label' => _m('Include only notes by people with all the following tags')]],
  63. /* actor_langs */ FormFields::language($actor, context_actor: null, label: _m('Search for people that know these languages'), multiple: true, required: false, use_short_display: false, form_id: 'actor_langs', use_no_selection: true),
  64. ['actor_tags', TextType::class, ['required' => false, 'label' => _m('Include only people with all the following tags')]],
  65. [$form_name = 'search_builder', SubmitType::class, ['label' => _m('Search')]],
  66. ]);
  67. if ('POST' === $request->getMethod() && $request->request->has($form_name)) {
  68. $search_builder_form->handleRequest($request);
  69. if ($search_builder_form->isSubmitted() && $search_builder_form->isValid()) {
  70. $data = $search_builder_form->getData();
  71. $query = [];
  72. $include_notes_query = [];
  73. $include_actors_query = [];
  74. $exception = new BugFoundException('Search builder form seems to have new fields the code did not expect');
  75. foreach ($data as $key => $value) {
  76. if (!\is_null($value) && !empty($value)) {
  77. if (str_contains($key, 'tags')) {
  78. $query[] = "{$key}:#{$value}";
  79. } elseif (str_contains($key, 'lang')) {
  80. if (!\in_array('null', $value)) {
  81. $langs = implode(',', $value);
  82. $query[] = "{$key}:{$langs}";
  83. }
  84. } elseif (str_contains($key, 'include')) {
  85. if (str_contains($key, 'notes')) {
  86. if ($key === 'include_notes') {
  87. if (!$data[$key]) {
  88. $include_notes_query = null;
  89. }
  90. } elseif ($data[$key] && !\is_null($include_notes_query)) {
  91. $include_notes_query[] = Formatting::removePrefix($key, 'include_notes_');
  92. }
  93. } elseif (str_contains($key, 'actors')) {
  94. if ($key === 'include_actors') {
  95. if (!$data[$key]) {
  96. $include_actors_query = null;
  97. }
  98. } elseif ($data[$key] && !\is_null($include_actors_query)) {
  99. $include_actors_query[] = Formatting::removePrefix($key, 'include_actors_');
  100. }
  101. } else {
  102. throw $exception;
  103. }
  104. } else {
  105. throw $exception;
  106. }
  107. }
  108. }
  109. if (!\is_null($include_notes_query) && !empty($include_notes_query)) {
  110. $query[] = 'note-types:' . implode(',', $include_notes_query);
  111. }
  112. if (!\is_null($include_actors_query) && !empty($include_actors_query)) {
  113. $query[] = 'actor-types:' . implode(',', $include_actors_query);
  114. }
  115. $query = implode(' ', $query);
  116. throw new RedirectException('search', ['q' => $query]);
  117. }
  118. }
  119. return [
  120. '_template' => 'search/view.html.twig',
  121. 'actor' => $actor,
  122. 'search_form' => Comp\Search::searchForm($request, query: $q, add_subscribe: !\is_null($actor)),
  123. 'search_builder_form' => $search_builder_form->createView(),
  124. 'notes' => $notes ?? [],
  125. 'actors' => $actors ?? [],
  126. 'page' => 1, // TODO paginate
  127. ];
  128. }
  129. }