Language.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Language\Controller;
  20. use App\Core\Cache;
  21. use App\Core\Controller;
  22. use App\Core\DB\DB;
  23. use App\Core\Form;
  24. use function App\Core\I18n\_m;
  25. use App\Util\Common;
  26. use App\Util\Exception\NoLoggedInUser;
  27. use App\Util\Exception\RedirectException;
  28. use App\Util\Exception\ServerException;
  29. use App\Util\Form\FormFields;
  30. use Component\Language\Entity\ActorLanguage;
  31. use Component\Language\Entity\Language as LangEntity;
  32. use Functional as F;
  33. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  34. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  35. use Symfony\Component\Form\FormInterface;
  36. use Symfony\Component\Form\SubmitButton;
  37. use Symfony\Component\HttpFoundation\Request;
  38. class Language extends Controller
  39. {
  40. /**
  41. * @throws NoLoggedInUser
  42. * @throws RedirectException
  43. * @throws ServerException
  44. */
  45. public function settings(Request $request): FormInterface
  46. {
  47. $user = Common::ensureLoggedIn();
  48. // TODO Add support missing settings
  49. $form = Form::create([
  50. FormFields::language($user->getActor(), context_actor: null, label: _m('Languages'), help: _m('The languages you understand, so you can see primarily content in those'), multiple: true, required: false, use_short_display: false),
  51. ['save_languages', SubmitType::class, ['label' => _m('Proceed to order selected languages')]],
  52. ]);
  53. $form->handleRequest($request);
  54. if ($form->isSubmitted() && $form->isValid()) {
  55. $data = $form->getData();
  56. if (!\is_null($data['languages'])) {
  57. $selected_langs = DB::findBy('language', ['locale' => $data['languages']]);
  58. $existing_langs = DB::dql(
  59. 'select l from language l join actor_language al with l.id = al.language_id where al.actor_id = :actor_id',
  60. ['actor_id' => $user->getId()],
  61. );
  62. $new_langs = array_udiff($selected_langs, $existing_langs, fn ($l, $r) => $l->getId() <=> $r->getId());
  63. $removing_langs = array_udiff($existing_langs, $selected_langs, fn ($l, $r) => $l->getId() <=> $r->getId());
  64. foreach ($new_langs as $l) {
  65. DB::persist(ActorLanguage::create(['actor_id' => $user->getId(), 'language_id' => $l->getId(), 'ordering' => 0]));
  66. }
  67. if (!empty($removing_langs)) {
  68. $actor_langs_to_remove = DB::findBy('actor_language', ['actor_id' => $user->getId(), 'language_id' => F\map($removing_langs, fn ($l) => $l->getId())]);
  69. foreach ($actor_langs_to_remove as $lang) {
  70. DB::remove($lang);
  71. }
  72. }
  73. Cache::delete(ActorLanguage::cacheKeys($user)['actor-langs']);
  74. ActorLanguage::normalizeOrdering($user); // In case the user doesn't submit the other page
  75. DB::flush();
  76. unset($data['languages']);
  77. throw new RedirectException('settings_sort_languages', ['_fragment' => null]); // TODO doesn't clear fragment
  78. }
  79. }
  80. return $form;
  81. }
  82. /**
  83. * Controller for defining the ordering of a users' languages
  84. *
  85. * @throws NoLoggedInUser
  86. * @throws RedirectException
  87. * @throws ServerException
  88. */
  89. public function sortLanguages(Request $request): array
  90. {
  91. $user = Common::ensureLoggedIn();
  92. $langs = DB::dql('select l.locale, l.long_display, al.ordering from language l join actor_language al with l.id = al.language_id where al.actor_id = :id order by al.ordering ASC', ['id' => $user->getId()]);
  93. $form_entries = [];
  94. foreach ($langs as $l) {
  95. $form_entries[] = [$l['locale'], IntegerType::class, ['label' => _m($l['long_display']), 'data' => $l['ordering']]];
  96. }
  97. $form_entries[] = ['save_language_order', SubmitType::class, []];
  98. $form_entries[] = ['go_back', SubmitType::class, ['label' => _m('Return to settings page')]];
  99. $form = Form::create($form_entries);
  100. $form->handleRequest($request);
  101. if ($form->isSubmitted() && $form->isValid()) {
  102. /** @var SubmitButton $button */
  103. $button = $form->get('go_back');
  104. $go_back = $button->isClicked();
  105. $data = $form->getData();
  106. asort($data); // Sort by the order value
  107. $data = array_keys($data); // This keeps the order and gives us a unique number for each
  108. foreach ($data as $order => $locale) {
  109. $lang = LangEntity::getByLocale($locale);
  110. $actor_lang = DB::getReference('actor_language', ['actor_id' => $user->getId(), 'language_id' => $lang->getId()]);
  111. $actor_lang->setOrdering($order + 1);
  112. }
  113. DB::flush();
  114. if (!$go_back) {
  115. // Stay on same page, but force update and prevent resubmission
  116. throw new RedirectException('settings_sort_languages');
  117. } else {
  118. throw new RedirectException('settings', ['open' => 'account', '_fragment' => 'save_account_info_languages']);
  119. }
  120. }
  121. return [
  122. '_template' => 'language/sort.html.twig',
  123. 'form' => $form->createView(),
  124. ];
  125. }
  126. }