[UI][CONTROLLER][UserPanel][ENTITY][ActorLanguage][Language][Actor] Add interface to allow user to select thier preferred languages and to order them. Rename ActorLanguage::order to ordering

This commit is contained in:
Hugo Sales 2021-11-15 17:00:58 +00:00
parent 83fc31b485
commit 774eb49af4
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
6 changed files with 135 additions and 22 deletions

View File

@ -37,15 +37,18 @@ namespace App\Controller;
// {{{ Imports // {{{ Imports
use App\Core\Cache;
use App\Core\Controller; use App\Core\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Entity\ActorLanguage;
use App\Entity\UserNotificationPrefs; use App\Entity\UserNotificationPrefs;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\AuthenticationException; use App\Util\Exception\AuthenticationException;
use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\Form\ActorArrayTransformer; use App\Util\Form\ActorArrayTransformer;
use App\Util\Form\ArrayTransformer; use App\Util\Form\ArrayTransformer;
@ -55,9 +58,8 @@ use Doctrine\DBAL\Types\Types;
use Exception; use Exception;
use Functional as F; use Functional as F;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType; use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\LocaleType; use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
@ -72,7 +74,7 @@ class UserPanel extends Controller
* *
* @throws Exception * @throws Exception
*/ */
public function all_settings(Request $request): array public function allSettings(Request $request): array
{ {
$account_form = $this->account($request); $account_form = $this->account($request);
$personal_form = $this->personal_info($request); $personal_form = $this->personal_info($request);
@ -83,7 +85,7 @@ class UserPanel extends Controller
'prof' => $personal_form->createView(), 'prof' => $personal_form->createView(),
'acc' => $account_form->createView(), 'acc' => $account_form->createView(),
'tabbed_forms_notify' => $notifications_form_array, 'tabbed_forms_notify' => $notifications_form_array,
'open_details_query' => $this->string('open') 'open_details_query' => $this->string('open'),
]; ];
} }
@ -92,7 +94,7 @@ class UserPanel extends Controller
*/ */
public function personal_info(Request $request) public function personal_info(Request $request)
{ {
$user = Common::user(); $user = Common::ensureLoggedIn();
$actor = $user->getActor(); $actor = $user->getActor();
$extra = ['self_tags' => $actor->getSelfTags()]; $extra = ['self_tags' => $actor->getSelfTags()];
$form_definition = [ $form_definition = [
@ -106,7 +108,9 @@ class UserPanel extends Controller
]; ];
$extra_step = function ($data, $extra_args) use ($user, $actor) { $extra_step = function ($data, $extra_args) use ($user, $actor) {
$user->setNickname($data['nickname']); $user->setNickname($data['nickname']);
if (!$data['full_name'] && !$actor->getFullname()) { $actor->setFullname($data['nickname']); } if (!$data['full_name'] && !$actor->getFullname()) {
$actor->setFullname($data['nickname']);
}
}; };
return Form::handle($form_definition, $request, $actor, $extra, $extra_step, [['self_tags' => $extra['self_tags']]]); return Form::handle($form_definition, $request, $actor, $extra, $extra_step, [['self_tags' => $extra['self_tags']]]);
} }
@ -116,14 +120,15 @@ class UserPanel extends Controller
*/ */
public function account(Request $request) public function account(Request $request)
{ {
$user = Common::user(); $user = Common::ensureLoggedIn();
// TODO Add support missing settings // TODO Add support missing settings
$form = Form::create([ $form = Form::create([
['outgoing_email', TextType::class, ['label' => _m('Outgoing email'), 'required' => false, 'help' => _m('Change the email we use to contact you')]], ['outgoing_email', TextType::class, ['label' => _m('Outgoing email'), 'required' => false, 'help' => _m('Change the email we use to contact you')]],
['incoming_email', TextType::class, ['label' => _m('Incoming email'), 'required' => false, 'help' => _m('Change the email you use to contact us (for posting, for instance)')]], ['incoming_email', TextType::class, ['label' => _m('Incoming email'), 'required' => false, 'help' => _m('Change the email you use to contact us (for posting, for instance)')]],
['old_password', TextType::class, ['label' => _m('Old password'), 'required' => false, 'help' => _m('Enter your old password for verification'), 'attr' => ['placeholder' => '********']]], ['old_password', TextType::class, ['label' => _m('Old password'), 'required' => false, 'help' => _m('Enter your old password for verification'), 'attr' => ['placeholder' => '********']]],
FormFields::repeated_password(['required' => false]), FormFields::repeated_password(['required' => false]),
['language', LocaleType::class, ['label' => _m('Language'), 'required' => false, 'help' => _m('Your preferred language')]], FormFields::language($user->getActor(), context_actor: null, label: 'Languages', help: 'The languages you understand, so you can see primarily content in those', multiple: true, required: false),
['phone_number', PhoneNumberType::class, ['label' => _m('Phone number'), 'required' => false, 'help' => _m('Your phone number'), 'data_class' => null]], ['phone_number', PhoneNumberType::class, ['label' => _m('Phone number'), 'required' => false, 'help' => _m('Your phone number'), 'data_class' => null]],
['save_account_info', SubmitType::class, ['label' => _m('Save account info')]], ['save_account_info', SubmitType::class, ['label' => _m('Save account info')]],
]); ]);
@ -131,16 +136,39 @@ class UserPanel extends Controller
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData(); $data = $form->getData();
if (!\is_null($data['old_password'])) { if (!\is_null($data['old_password'])) {
$data['password'] = $form->get('password')->getData(); $data['password'] = $form->get('password')->getData();
if (!($user->changePassword($data['old_password'], $data['password']))) { if (!($user->changePassword($data['old_password'], $data['password']))) {
throw new AuthenticationException(_m('The provided password is incorrect')); throw new AuthenticationException(_m('The provided password is incorrect'));
} }
} }
unset($data['old_password'], $data['password']); unset($data['old_password'], $data['password']);
$redirect_to_language_sorting = false;
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::collectionCacheKey($user));
DB::flush();
ActorLanguage::normalizeOrdering($user); // In case the user doesn't submit the other page
unset($data['languages']);
$redirect_to_language_sorting = true;
}
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
$method = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($key)); $method = 'set' . ucfirst(Formatting::snakeCaseToCamelCase($key));
if (method_exists($user, $method)) { if (method_exists($user, $method)) {
@ -148,17 +176,62 @@ class UserPanel extends Controller
} }
} }
DB::flush(); DB::flush();
if ($redirect_to_language_sorting) {
throw new RedirectException('settings_sort_languages', ['_fragment' => null]); // TODO doesn't clear fragment
}
} }
return $form; return $form;
} }
public function sortLanguages(Request $request)
{
$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()) {
$go_back = $form->get('go_back')->isClicked();
$data = $form->getData();
asort($data); // Sort by the order value
$data = array_keys($data); // This keeps the order and gives us a unique number for each
foreach ($data as $order => $locale) {
$lang = Cache::getHashMapKey('languages', $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) {
// Stay on same page, but force update and prevent resubmission
throw new RedirectException('settings_sort_languages');
} else {
throw new RedirectException('settings', ['open' => 'account', '_fragment' => 'save_account_info_languages']);
}
}
return [
'_template' => 'settings/sort_languages.html.twig',
'form' => $form->createView(),
];
}
/** /**
* Local user notification settings tabbed panel * Local user notification settings tabbed panel
*/ */
public function notifications(Request $request) public function notifications(Request $request)
{ {
$user = Common::user(); $user = Common::ensureLoggedIn();
$schema = DB::getConnection()->getSchemaManager(); $schema = DB::getConnection()->getSchemaManager();
$platform = $schema->getDatabasePlatform(); $platform = $schema->getDatabasePlatform();
$columns = Common::arrayRemoveKeys($schema->listTableColumns('user_notification_prefs'), ['user_id', 'transport', 'created', 'modified']); $columns = Common::arrayRemoveKeys($schema->listTableColumns('user_notification_prefs'), ['user_id', 'transport', 'created', 'modified']);

View File

@ -361,12 +361,12 @@ class Actor extends Entity
public function getPreferredLanguageChoices(?self $context = null): array public function getPreferredLanguageChoices(?self $context = null): array
{ {
$id = $context?->getId() ?? $this->getId(); $id = $context?->getId() ?? $this->getId();
$key = 'actor-' . $this->getId() . '-langs' . (!\is_null($context) ? '-' . $context->getId() : ''); $key = ActorLanguage::collectionCacheKey($this); // TODO handle language context
$langs = Cache::getHashMap( $langs = Cache::getHashMap(
$key, $key,
fn () => F\reindex( fn () => F\reindex(
DB::dql( DB::dql(
'select l from actor_language al join language l with al.language_id = l.id where al.actor_id = :id order by al.order ASC', 'select l from actor_language al join language l with al.language_id = l.id where al.actor_id = :id order by al.ordering ASC',
['id' => $id], ['id' => $id],
), ),
fn (Language $l) => $l->getLocale(), fn (Language $l) => $l->getLocale(),

View File

@ -42,7 +42,7 @@ class ActorLanguage extends Entity
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
private int $actor_id; private int $actor_id;
private int $language_id; private int $language_id;
private int $order; private int $ordering;
public function setActorId(int $actor_id): self public function setActorId(int $actor_id): self
{ {
@ -66,19 +66,34 @@ class ActorLanguage extends Entity
return $this->language_id; return $this->language_id;
} }
public function getOrder(): int public function getOrdering(): int
{ {
return $this->order; return $this->ordering;
} }
public function setOrder(int $order): self public function setOrdering(int $ordering): self
{ {
$this->order = $order; $this->ordering = $ordering;
return $this; return $this;
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
// }}} Autocode // }}} Autocode
public static function collectionCacheKey(LocalUser|Actor $actor)
{
return 'actor-' . $actor->getId() . '-langs';
}
public static function normalizeOrdering(LocalUser|Actor $actor)
{
$langs = DB::dql('select l.locale, al.ordering, l.id from language l join actor_language al with l.id = al.language_id where al.actor_id = :id order by al.ordering ASC', ['id' => $actor->getId()]);
usort($langs, fn ($l, $r) => [$l['ordering'], $l['locale']] <=> [$r['ordering'], $r['locale']]);
foreach ($langs as $order => $l) {
$actor_lang = DB::getReference('actor_language', ['actor_id' => $actor->getId(), 'language_id' => $l['id']]);
$actor_lang->setOrdering($order + 1);
}
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [
@ -87,7 +102,7 @@ class ActorLanguage extends Entity
'fields' => [ 'fields' => [
'actor_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to many', 'description' => 'the actor this language entry refers to'], 'actor_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to many', 'description' => 'the actor this language entry refers to'],
'language_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Language.id', 'multiplicity' => 'many to many', 'description' => 'the language this entry refers to'], 'language_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Language.id', 'multiplicity' => 'many to many', 'description' => 'the language this entry refers to'],
'order' => ['type' => 'int', 'not null' => true, 'description' => 'the order in which a user\'s language options should be displayed'], 'ordering' => ['type' => 'int', 'not null' => true, 'description' => 'the order in which a user\'s language options should be displayed'],
], ],
'primary key' => ['actor_id', 'language_id'], 'primary key' => ['actor_id', 'language_id'],
'indexes' => [ 'indexes' => [

View File

@ -27,6 +27,7 @@ use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Util\Common;
use DateTimeInterface; use DateTimeInterface;
use Functional as F; use Functional as F;
@ -122,6 +123,26 @@ class Language extends Entity
return [_m($this->getLongDisplay()) => $this->getLocale()]; return [_m($this->getLongDisplay()) => $this->getLocale()];
} }
/**
* Get all the available languages as well as the languages $actor
* prefers and are appropriate for posting in/to $context_actor
*/
public static function getSortedLanguageChoices(Actor $actor, ?Actor $context_actor, ?bool $use_short_display): array
{
$language_choices = self::getLanguageChoices();
$preferred_language_choices = $actor->getPreferredLanguageChoices($context_actor);
ksort($language_choices);
if ($use_short_display ?? Common::config('posting', 'use_short_language_display')) {
$key = array_key_first($preferred_language_choices);
$locale = $preferred_language_choices[$key];
unset($preferred_language_choices[$key], $language_choices[$key]);
$short_display = Cache::getHashMapKey('languages', $locale)->getShortDisplay();
$preferred_language_choices[$short_display] = trim($locale);
$language_choices[$short_display] = trim($locale);
}
return [$language_choices, $preferred_language_choices];
}
public static function schemaDef(): array public static function schemaDef(): array
{ {
return [ return [

View File

@ -72,6 +72,7 @@ abstract class Main
$r->connect('doc_' . $s, '/doc/' . $s, C\TemplateController::class, ['template' => 'doc/' . $s . '.html.twig']); $r->connect('doc_' . $s, '/doc/' . $s, C\TemplateController::class, ['template' => 'doc/' . $s . '.html.twig']);
} }
$r->connect('settings', '/settings', [C\UserPanel::class, 'all_settings']); $r->connect('settings', '/settings', [C\UserPanel::class, 'allSettings']);
$r->connect('settings_sort_languages', '/settings/sort_languages', [C\UserPanel::class, 'sortLanguages']);
} }
} }

View File

@ -0,0 +1,3 @@
<p>{{ 'Put the languages in the order you\'d like to see them in your language selection dropdown, when posting' | trans}}</p>
{{ form(form) }}