2020-06-22 07:56:26 +09:00
< ? php
// {{{ License
2020-08-15 07:37:45 +09:00
2020-06-22 07:56:26 +09:00
// 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/>.
2020-08-15 07:37:45 +09:00
2020-06-22 07:56:26 +09:00
// }}}
/**
* Handle network public feed
*
* @ package GNUsocial
* @ category Controller
*
2021-02-20 08:29:43 +09:00
* @ author Hugo Sales < hugo @ hsal . es >
2020-07-01 01:35:38 +09:00
* @ author Eliseu Amaro < eliseu @ fc . up . pt >
2021-02-20 08:29:43 +09:00
* @ copyright 2020 - 2021 Free Software Foundation , Inc http :// www . fsf . org
2020-06-22 07:56:26 +09:00
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
*/
namespace App\Controller ;
2020-07-31 07:47:47 +09:00
// {{{ Imports
use App\Core\DB\DB ;
use App\Core\Event ;
2020-06-27 07:44:53 +09:00
use App\Core\Form ;
2020-06-22 07:56:26 +09:00
use function App\Core\I18n\_m ;
2021-08-08 03:25:10 +09:00
use App\Entity\UserNotificationPrefs ;
2020-07-26 02:56:38 +09:00
use App\Util\Common ;
2020-07-27 12:48:35 +09:00
use App\Util\Form\ArrayTransformer ;
2021-08-06 20:12:10 +09:00
use App\Util\Form\FormFields ;
use App\Util\Formatting ;
2020-07-31 07:47:47 +09:00
use Doctrine\DBAL\Types\Types ;
use Exception ;
use Functional as F ;
2020-07-26 09:02:20 +09:00
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType ;
2020-07-20 08:22:41 +09:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController ;
2020-07-22 06:02:39 +09:00
use Symfony\Component\Form\Extension\Core\Type\CheckboxType ;
2020-07-26 09:02:20 +09:00
use Symfony\Component\Form\Extension\Core\Type\LanguageType ;
2020-06-22 07:56:26 +09:00
use Symfony\Component\Form\Extension\Core\Type\SubmitType ;
2020-07-21 06:05:10 +09:00
use Symfony\Component\Form\Extension\Core\Type\TextareaType ;
2020-06-22 07:56:26 +09:00
use Symfony\Component\Form\Extension\Core\Type\TextType ;
use Symfony\Component\HttpFoundation\Request ;
2020-07-31 07:47:47 +09:00
// }}} Imports
2020-07-20 08:22:41 +09:00
class UserPanel extends AbstractController
2020-06-22 07:56:26 +09:00
{
2021-07-30 01:29:25 +09:00
/**
* Return main settings page forms
*
* @ param Request $request
*
* @ throws Exception
*
* @ return array
*/
public function all_settings ( Request $request )
{
$personal_form = $this -> personal_info ( $request );
$account_form = $this -> account ( $request );
$notifications_form = $this -> notifications ( $request );
return [ '_template' => 'settings/base.html.twig' ,
'prof' => $personal_form -> createView (),
'acc' => $account_form -> createView (),
'tabbed_forms_notify' => $notifications_form ,
];
}
2020-11-07 04:47:15 +09:00
/**
* Local user personal information panel
*/
2020-07-26 09:02:20 +09:00
public function personal_info ( Request $request )
2020-06-22 07:56:26 +09:00
{
2020-07-26 02:56:38 +09:00
$user = Common :: user ();
2020-09-07 06:38:37 +09:00
$user = $user -> getActor ();
$extra = [ 'self_tags' => $user -> getSelfTags ()];
2020-07-26 02:56:38 +09:00
$form_definition = [
2020-07-27 12:48:35 +09:00
[ 'nickname' , TextType :: class , [ 'label' => _m ( 'Nickname' ), 'required' => true , 'help' => _m ( '1-64 lowercase letters or numbers, no punctuation or spaces.' )]],
2021-07-30 01:29:25 +09:00
[ '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.' )]],
[ 'location' , TextType :: class , [ 'label' => _m ( 'Location' ), 'required' => false , 'help' => _m ( 'Where you are, like "City, State (or Region), Country".' )]],
[ 'self_tags' , TextType :: class , [ 'label' => _m ( 'Self Tags' ), 'required' => false , 'transformer' => ArrayTransformer :: class , 'help' => _m ( 'Tags for yourself (letters, numbers, -, ., and _), comma- or space-separated.' )]],
[ 'save' , SubmitType :: class , [ 'label' => _m ( 'Save' )]],
2020-07-26 02:56:38 +09:00
];
2021-07-30 01:29:25 +09:00
$extra_step = function ( $data , $extra_args ) use ( $user ) {
$user -> setNickname ( $data [ 'nickname' ]);
};
2020-09-07 06:38:37 +09:00
$form = Form :: handle ( $form_definition , $request , $user , $extra , $extra_step , [[ 'self_tags' => $extra [ 'self_tags' ]]]);
2020-06-22 07:56:26 +09:00
2021-07-30 01:29:25 +09:00
return $form ;
//return ['_template' => 'settings/profile.html.twig', 'prof' => $form->createView()];
2020-06-22 07:56:26 +09:00
}
2020-07-22 06:02:39 +09:00
2020-11-07 04:47:15 +09:00
/**
* Local user account information panel
*/
2020-07-22 06:02:39 +09:00
public function account ( Request $request )
{
2020-07-27 12:48:35 +09:00
$user = Common :: user ();
$form_definition = [
[ 'outgoing_email' , TextType :: class , [ 'label' => _m ( 'Outgoing email' ), 'required' => true , 'help' => _m ( 'Change the email we use to contact you' )]],
2021-07-30 01:29:25 +09:00
[ 'incoming_email' , TextType :: class , [ 'label' => _m ( 'Incoming email' ), 'required' => true , 'help' => _m ( 'Change the email you use to contact us (for posting, for instance)' )]],
[ 'password' , TextType :: class , [ 'label' => _m ( 'Password' ), 'required' => false , 'help' => _m ( 'Change your password' ), 'attr' => [ 'placeholder' => '********' ]]],
[ 'old_password' , TextType :: class , [ 'label' => _m ( 'Old password' ), 'required' => false , 'help' => _m ( 'Enter your old password for verification' ), 'attr' => [ 'placeholder' => '********' ]]],
[ 'language' , LanguageType :: class , [ 'label' => _m ( 'Language' ), 'required' => false , 'help' => _m ( 'Your preferred language' )]],
[ 'phone_number' , PhoneNumberType :: class , [ 'label' => _m ( 'Phone number' ), 'required' => false , 'help' => _m ( 'Your phone number' ), 'data_class' => null ]],
[ 'save' , SubmitType :: class , [ 'label' => _m ( 'Save' )]],
2020-07-27 12:48:35 +09:00
];
$form = Form :: handle ( $form_definition , $request , $user );
2020-07-22 06:02:39 +09:00
2021-07-30 01:29:25 +09:00
return $form ;
//return ['_template' => 'settings/account.html.twig', 'acc' => $form->createView()];
2020-07-22 06:02:39 +09:00
}
2020-07-23 05:58:23 +09:00
2020-11-07 04:47:15 +09:00
/**
* Local user notification settings tabbed panel
*/
2020-07-27 00:05:07 +09:00
public function notifications ( Request $request )
2020-07-23 05:58:23 +09:00
{
2021-08-08 03:25:10 +09:00
$user = Common :: user ();
2020-07-31 07:47:47 +09:00
$schema = DB :: getConnection () -> getSchemaManager ();
$platform = $schema -> getDatabasePlatform ();
2020-08-06 01:31:39 +09:00
$columns = Common :: arrayRemoveKeys ( $schema -> listTableColumns ( 'user_notification_prefs' ), [ 'user_id' , 'transport' , 'created' , 'modified' ]);
2020-07-31 07:47:47 +09:00
$form_defs = [ 'placeholder' => []];
foreach ( $columns as $name => $col ) {
2020-08-06 01:31:39 +09:00
$type = $col -> getType ();
$val = $type -> convertToPHPValue ( $col -> getDefault (), $platform );
$type_str = lcfirst ( substr (( string ) $type , 1 ));
$label = str_replace ( '_' , ' ' , ucfirst ( $name ));
2020-08-06 08:05:06 +09:00
$labels = [
2020-08-13 10:23:22 +09:00
'target_actor_id' => 'Target Actors' ,
'dm' => 'DM' ,
2020-08-06 08:05:06 +09:00
];
$help = [
2020-08-13 10:23:22 +09:00
'target_actor_id' => 'If specified, these settings apply only to these profiles (comma- or space-separated list)' ,
2020-08-06 08:05:06 +09:00
'activity_by_followed' => 'Notify me when someone I follow has new activity' ,
'mention' => 'Notify me when mentions me in a notice' ,
'reply' => 'Notify me when someone replies to a notice made by me' ,
'follow' => 'Notify me when someone follows me or asks for permission to do so' ,
'favorite' => 'Notify me when someone favorites one of my notices' ,
'nudge' => 'Notify me when someone nudges me' ,
'dm' => 'Notify me when someone sends me a direct message' ,
'post_on_status_change' => 'Post a notice when my status in this service changes' ,
'enable_posting' => 'Enable posting from this service' ,
];
2020-08-06 01:31:39 +09:00
switch ( $type_str ) {
2020-07-31 07:47:47 +09:00
case Types :: BOOLEAN :
2020-08-06 08:05:06 +09:00
$form_defs [ 'placeholder' ][ $name ] = [ $name , CheckboxType :: class , [ 'data' => $val , 'label' => _m ( $labels [ $name ] ? ? $label ), 'help' => _m ( $help [ $name ])]];
2020-07-31 07:47:47 +09:00
break ;
case Types :: INTEGER :
2020-08-13 10:23:22 +09:00
if ( $name == 'target_actor_id' ) {
$form_defs [ 'placeholder' ][ $name ] = [ 'target_actors' , TextType :: class , [ 'data' => $val , 'label' => _m ( $labels [ $name ]), 'help' => _m ( $help [ $name ])], 'transformer' => ActorArrayTransformer :: class ];
2020-07-31 07:47:47 +09:00
}
2020-10-12 02:19:29 +09:00
break ;
2020-07-31 07:47:47 +09:00
default :
2020-10-12 02:19:29 +09:00
dd ( $type_str );
2020-07-31 07:47:47 +09:00
throw new Exception ( " Structure of table user_notification_prefs changed in a way not accounted to in notification settings ( { $name } ) " , 500 );
}
}
2021-08-08 03:25:10 +09:00
$form_defs [ 'placeholder' ][ 'save' ] = fn ( string $transport , string $form_name ) => [ $form_name , SubmitType :: class ,
[ 'label' => _m ( 'Save notification settings for {transport}' , [ 'transport' => $transport ])], ];
2021-08-08 03:24:11 +09:00
2020-10-20 03:22:59 +09:00
Event :: handle ( 'AddNotificationTransport' , [ & $form_defs ]);
2020-07-31 07:47:47 +09:00
unset ( $form_defs [ 'placeholder' ]);
$tabbed_forms = [];
foreach ( $form_defs as $transport_name => $f ) {
2021-08-08 03:24:11 +09:00
unset ( $f [ 'save' ]);
$form = Form :: create ( $f );
$tabbed_forms [ $transport_name ] = $form ;
$form -> handleRequest ( $request );
if ( $form -> isSubmitted () && $form -> isValid ()) {
$data = $form -> getData ();
2021-08-08 03:25:10 +09:00
unset ( $data [ 'translation_domain' ]);
[ $ent , $is_update ] = UserNotificationPrefs :: createOrUpdate (
array_merge ([ 'user_id' => $user -> getId (), 'transport' => $transport_name ], $data ),
find_by_keys : [ 'user_id' , 'transport' ]
);
if ( ! $is_update ) {
DB :: persist ( $ent );
}
DB :: flush ();
2021-08-08 03:24:11 +09:00
}
2020-07-31 07:47:47 +09:00
}
2021-07-30 01:29:25 +09:00
$tabbed_forms = F\map ( $tabbed_forms , function ( $f ) {
return $f -> createView ();
});
return $tabbed_forms ;
/* return [
2020-08-06 08:05:06 +09:00
'_template' => 'settings/notifications.html.twig' ,
'tabbed_forms' => $tabbed_forms ,
2021-07-30 01:29:25 +09:00
]; */
2020-07-23 05:58:23 +09:00
}
2020-07-23 23:08:31 +09:00
}