123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- declare(strict_types = 1);
- namespace Component\Language\Controller;
- use App\Core\Cache;
- use App\Core\Controller;
- use App\Core\DB\DB;
- use App\Core\Form;
- use function App\Core\I18n\_m;
- use App\Util\Common;
- use App\Util\Exception\NoLoggedInUser;
- use App\Util\Exception\RedirectException;
- use App\Util\Exception\ServerException;
- use App\Util\Form\FormFields;
- use Component\Language\Entity\ActorLanguage;
- use Component\Language\Entity\Language as LangEntity;
- use Functional as F;
- use Symfony\Component\Form\Extension\Core\Type\IntegerType;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- use Symfony\Component\Form\FormInterface;
- use Symfony\Component\Form\SubmitButton;
- use Symfony\Component\HttpFoundation\Request;
- class Language extends Controller
- {
-
- public function settings(Request $request): FormInterface
- {
- $user = Common::ensureLoggedIn();
-
- $form = Form::create([
- 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),
- ['save_languages', SubmitType::class, ['label' => _m('Proceed to order selected languages')]],
- ]);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- $data = $form->getData();
- if (!\is_null($data['languages'])) {
- $selected_langs = DB::findBy('language', ['locale' => $data['languages']]);
- $existing_langs = DB::dql(
- 'select l from language l join actor_language al with l.id = al.language_id where al.actor_id = :actor_id',
- ['actor_id' => $user->getId()],
- );
- $new_langs = array_udiff($selected_langs, $existing_langs, fn ($l, $r) => $l->getId() <=> $r->getId());
- $removing_langs = array_udiff($existing_langs, $selected_langs, fn ($l, $r) => $l->getId() <=> $r->getId());
- foreach ($new_langs as $l) {
- DB::persist(ActorLanguage::create(['actor_id' => $user->getId(), 'language_id' => $l->getId(), 'ordering' => 0]));
- }
- if (!empty($removing_langs)) {
- $actor_langs_to_remove = DB::findBy('actor_language', ['actor_id' => $user->getId(), 'language_id' => F\map($removing_langs, fn ($l) => $l->getId())]);
- foreach ($actor_langs_to_remove as $lang) {
- DB::remove($lang);
- }
- }
- Cache::delete(ActorLanguage::cacheKeys($user)['actor-langs']);
- ActorLanguage::normalizeOrdering($user);
- DB::flush();
- unset($data['languages']);
- throw new RedirectException('settings_sort_languages', ['_fragment' => null]);
- }
- }
- return $form;
- }
-
- public function sortLanguages(Request $request): array
- {
- $user = Common::ensureLoggedIn();
- $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()]);
- $form_entries = [];
- foreach ($langs as $l) {
- $form_entries[] = [$l['locale'], IntegerType::class, ['label' => _m($l['long_display']), 'data' => $l['ordering']]];
- }
- $form_entries[] = ['save_language_order', SubmitType::class, []];
- $form_entries[] = ['go_back', SubmitType::class, ['label' => _m('Return to settings page')]];
- $form = Form::create($form_entries);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
-
- $button = $form->get('go_back');
- $go_back = $button->isClicked();
- $data = $form->getData();
- asort($data);
- $data = array_keys($data);
- foreach ($data as $order => $locale) {
- $lang = LangEntity::getByLocale($locale);
- $actor_lang = DB::getReference('actor_language', ['actor_id' => $user->getId(), 'language_id' => $lang->getId()]);
- $actor_lang->setOrdering($order + 1);
- }
- DB::flush();
- if (!$go_back) {
-
- throw new RedirectException('settings_sort_languages');
- } else {
- throw new RedirectException('settings', ['open' => 'account', '_fragment' => 'save_account_info_languages']);
- }
- }
- return [
- '_template' => 'language/sort.html.twig',
- 'form' => $form->createView(),
- ];
- }
- }
|