[CONTROLLER][UserPanel][UTIL][FORM][ActorForms] Move UserPanel::personalInfo to ActorForms::personalInfo

This commit is contained in:
Hugo Sales 2021-12-23 17:10:56 +00:00 committed by Diogo Peralta Cordeiro
parent 1e6bc5b6ab
commit 2e69eac63e
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 113 additions and 58 deletions

View File

@ -37,7 +37,6 @@ 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;
@ -46,25 +45,19 @@ use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\AuthenticationException; use App\Util\Exception\AuthenticationException;
use App\Util\Exception\NicknameEmptyException;
use App\Util\Exception\NicknameInvalidException;
use App\Util\Exception\NicknameNotAllowedException;
use App\Util\Exception\NicknameTakenException;
use App\Util\Exception\NicknameTooLongException;
use App\Util\Exception\NoLoggedInUser; use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\Form\ActorArrayTransformer; use App\Util\Form\ActorArrayTransformer;
use App\Util\Form\ActorForms;
use App\Util\Form\FormFields; use App\Util\Form\FormFields;
use App\Util\Formatting; use App\Util\Formatting;
use Component\Language\Controller\Language as LanguageController; use Component\Language\Controller\Language as LanguageController;
use Component\Notification\Entity\UserNotificationPrefs; use Component\Notification\Entity\UserNotificationPrefs;
use Doctrine\DBAL\Types\Types; use Doctrine\DBAL\Types\Types;
use Exception; use Exception;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Form\Extension\Core\Type\PasswordType;
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\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -80,12 +73,15 @@ class UserPanel extends Controller
*/ */
public function allSettings(Request $request, LanguageController $language): array public function allSettings(Request $request, LanguageController $language): array
{ {
$personal_form = $this->personalInfo($request); // Ensure the user is logged in and retrieve Actor object for given user
$email_form = $this->email($request); $user = Common::ensureLoggedIn();
$password_form = $this->password($request); $actor = $user->getActor();
$language_form = $language->settings($request);
$notifications_form_array = $this->notifications($request); $personal_form = ActorForms::personalInfo($request, $actor);
$email_form = self::email($request);
$password_form = self::password($request);
$notifications_form_array = self::notifications($request);
$language_form = $language->settings($request);
return [ return [
'_template' => 'settings/base.html.twig', '_template' => 'settings/base.html.twig',
@ -104,7 +100,7 @@ class UserPanel extends Controller
* @throws NoLoggedInUser * @throws NoLoggedInUser
* @throws ServerException * @throws ServerException
*/ */
public function email(Request $request): FormInterface private static function email(Request $request): FormInterface
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
// TODO Add support missing settings // TODO Add support missing settings
@ -137,7 +133,7 @@ class UserPanel extends Controller
* @throws NoLoggedInUser * @throws NoLoggedInUser
* @throws ServerException * @throws ServerException
*/ */
public function password(Request $request): FormInterface private static function password(Request $request): FormInterface
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
// TODO Add support missing settings // TODO Add support missing settings
@ -170,52 +166,10 @@ class UserPanel extends Controller
return $form; return $form;
} }
/**
* Local user personal information panel
*
* @throws NicknameEmptyException
* @throws NicknameInvalidException
* @throws NicknameNotAllowedException
* @throws NicknameTakenException
* @throws NicknameTooLongException
* @throws NoLoggedInUser
* @throws ServerException
*/
public function personalInfo(Request $request): mixed
{
// Ensure the user is logged in and retrieve Actor object for given user
$user = Common::ensureLoggedIn();
$actor = $user->getActor();
// Defining the various form fields
$form_definition = [
['nickname', TextType::class, ['label' => _m('Nickname'), 'required' => true, 'help' => _m('1-64 lowercase letters or numbers, no punctuation or spaces.')]],
['full_name', TextType::class, ['label' => _m('Full Name'), 'required' => false, 'help' => _m('A full name is required, if empty it will be set to your nickname.')]],
['homepage', TextType::class, ['label' => _m('Homepage'), 'required' => false, 'help' => _m('URL of your homepage, blog, or profile on another site.')]],
['bio', TextareaType::class, ['label' => _m('Bio'), 'required' => false, 'help' => _m('Describe yourself and your interests.')]],
['phone_number', PhoneNumberType::class, ['label' => _m('Phone number'), 'required' => false, 'help' => _m('Your phone number'), 'data_class' => null]],
['location', TextType::class, ['label' => _m('Location'), 'required' => false, 'help' => _m('Where you are, like "City, State (or Region), Country".')]],
['save_personal_info', SubmitType::class, ['label' => _m('Save personal info')]],
];
// Setting nickname normalised and setting actor cache
$extra_step = function ($data, $extra_args) use ($user, $actor) {
$user->setNicknameSanitizedAndCached($data['nickname'], $actor->getId());
};
return Form::handle(
$form_definition,
$request,
target: $actor,
extra_args: [],
extra_step: $extra_step,
);
}
/** /**
* Local user notification settings tabbed panel * Local user notification settings tabbed panel
*/ */
public function notifications(Request $request): array private static function notifications(Request $request): array
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$schema = DB::getConnection()->getSchemaManager(); $schema = DB::getConnection()->getSchemaManager();

View File

@ -0,0 +1,101 @@
<?php
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
/**
* Transform between string and list of typed profiles
*
* @package GNUsocial
* @category Form
*
* @author Hugo Sales <hugo@hsal.es>
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace App\Util\Form;
use App\Core\Cache;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Util\Exception\NicknameEmptyException;
use App\Util\Exception\NicknameInvalidException;
use App\Util\Exception\NicknameNotAllowedException;
use App\Util\Exception\NicknameTakenException;
use App\Util\Exception\NicknameTooLongException;
use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\ServerException;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
class ActorForms
{
/**
* Actor personal information panel
*
* @throws NicknameEmptyException
* @throws NicknameInvalidException
* @throws NicknameNotAllowedException
* @throws NicknameTakenException
* @throws NicknameTooLongException
* @throws NoLoggedInUser
* @throws ServerException
*/
public static function personalInfo(Request $request, Actor $target, ?LocalUser $user = null): mixed
{
// Defining the various form fields
$form_definition = [
['nickname', TextType::class, ['label' => _m('Nickname'), 'required' => true, 'help' => _m('1-64 lowercase letters or numbers, no punctuation or spaces.')]],
['full_name', TextType::class, ['label' => _m('Full Name'), 'required' => false, 'help' => _m('A full name is required, if empty it will be set to your nickname.')]],
['homepage', TextType::class, ['label' => _m('Homepage'), 'required' => false, 'help' => _m('URL of your homepage, blog, or profile on another site.')]],
['bio', TextareaType::class, ['label' => _m('Bio'), 'required' => false, 'help' => _m('Describe yourself and your interests.')]],
['phone_number', PhoneNumberType::class, ['label' => _m('Phone number'), 'required' => false, 'help' => _m('Your phone number'), 'data_class' => null]],
['location', TextType::class, ['label' => _m('Location'), 'required' => false, 'help' => _m('Where you are, like "City, State (or Region), Country".')]],
['save_personal_info', SubmitType::class, ['label' => _m('Save personal info')]],
];
// Setting nickname normalised and setting actor cache
$extra_step = function ($data, $extra_args) use ($user, $target) {
if (!\is_null($user)) {
$user->setNicknameSanitizedAndCached($data['nickname']);
}
$cache_keys = Actor::cacheKeys($target->getId());
foreach (['id', 'nickname', 'fullname'] as $key) {
Cache::delete($cache_keys[$key]);
}
};
return Form::handle(
$form_definition,
$request,
target: $target,
extra_args: [],
extra_step: $extra_step,
);
}
}