[UI][USERPANEL] Add prefilled fields, mark some as optional and handle self tags in the profile settings page

This commit is contained in:
Hugo Sales 2020-07-25 17:56:38 +00:00 committed by Hugo Sales
parent 207eeb39ca
commit 8b7dd48344
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -34,6 +34,8 @@ namespace App\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Util\Common;
use App\Util\Formatting;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 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\FileType; use Symfony\Component\Form\Extension\Core\Type\FileType;
@ -46,23 +48,36 @@ class UserPanel extends AbstractController
{ {
public function profile(Request $request) public function profile(Request $request)
{ {
$prof = Form::create([ $user = Common::user();
[_m('Nickname'), TextType::class, ['help' => '1-64 lowercase letters or numbers, no punctuation or spaces.']], $profile = $user->getProfile();
[_m('FullName'), TextType::class, ['help' => 'A full name is required, if empty it will be set to your nickname.']], $profile_tags = $profile->getSelfTags();
[_m('Homepage'), TextType::class, ['help' => 'URL of your homepage, blog, or profile on another site.']], $form_definition = [
[_m('Bio'), TextareaType::class, ['help' => 'Describe yourself and your interests.']], ['nickname', TextType::class, ['label' => _m('Nickname'), 'required' => true, 'data' => $profile->getNickname(), 'help' => _m('1-64 lowercase letters or numbers, no punctuation or spaces.')]],
[_m('Location'), TextType::class, ['help' => 'Where you are, like "City, State (or Region), Country".']], ['full_name', TextType::class, ['label' => _m('Full Name'), 'required' => false, 'data' => $profile->getFullname(), 'help' => _m('A full name is required, if empty it will be set to your nickname.')]],
[_m('Tags'), TextType::class, ['help' => 'Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated.']], ['homepage', TextType::class, ['label' => _m('Homepage'), 'required' => false, 'data' => $profile->getHomepage(), 'help' => _m('URL of your homepage, blog, or profile on another site.')]],
['save', SubmitType::class, ['label' => _m('Save')]], ]); ['bio', TextareaType::class, ['label' => _m('Bio'), 'required' => false, 'data' => $profile->getBio(), 'help' => _m('Describe yourself and your interests.')]],
['location', TextType::class, ['label' => _m('Location'), 'required' => false, 'data' => $profile->getLocation(), 'help' => _m('Where you are, like "City, State (or Region), Country".')]],
['self_tags', TextType::class, ['label' => _m('Self Tags'), 'required' => false, 'data' => Formatting::toString($profile_tags, Formatting::SPLIT_BY_SPACE), 'help' => _m('Tags for yourself (letters, numbers, -, ., and _), comma- or space-separated.')]],
['save', SubmitType::class, ['label' => _m('Save')]],
];
$prof->handleRequest($request); $form = Form::create($form_definition);
if ($prof->isSubmitted()) {
$data = $prof->getData(); $form->handleRequest($request);
if ($prof->isValid()) { if ($form->isSubmitted()) {
$profile = DB::find('\App\Entity\Profile', ['id' => 2]); $data = $form->getData();
foreach (['Nickname', 'FullName', 'Homepage', 'Bio', 'Location', 'Tags'] as $key) { if ($form->isValid()) {
$method = "set{$key}"; $user->setNickname($data['nickname']);
$profile->{$method}($data[_m($key)]); foreach (['Nickname', 'FullName', 'Homepage', 'Bio', 'Location'] as $key) {
$lkey = Formatting::camelCaseToSnakeCase($key);
if (Form::isRequired($form_definition, $lkey) || isset($data[$lkey])) {
$method = "set{$key}";
$profile->{$method}($data[$lkey]);
}
}
$tags = [];
if (isset($data['self_tags']) && Formatting::toArray($data['self_tags'], $tags)) {
$profile->setSelfTags($tags, $profile_tags, false);
} }
DB::flush(); DB::flush();
} else { } else {
@ -70,7 +85,7 @@ class UserPanel extends AbstractController
} }
} }
return ['_template' => 'settings/profile.html.twig', 'prof' => $prof->createView()]; return ['_template' => 'settings/profile.html.twig', 'prof' => $form->createView()];
} }
public function account(Request $request) public function account(Request $request)