2020-02-26 11:22:25 +00:00
< ? php
namespace App\Controllers ;
2021-08-13 07:17:24 +00:00
use App\Database\Repositories\UserRepository ;
2020-04-04 17:29:00 +00:00
use App\Web\ValidationHelper ;
2020-02-26 11:22:25 +00:00
use Psr\Http\Message\ResponseInterface as Response ;
use Psr\Http\Message\ServerRequestInterface as Request ;
class ProfileController extends Controller
{
/**
2020-03-03 16:18:52 +00:00
* @ param Request $request
* @ param Response $response
2020-02-26 11:22:25 +00:00
*
2020-03-03 16:18:52 +00:00
* @ return Response
2020-02-26 11:22:25 +00:00
* @ throws \Twig\Error\LoaderError
* @ throws \Twig\Error\RuntimeError
* @ throws \Twig\Error\SyntaxError
*/
public function profile ( Request $request , Response $response ) : Response
{
2021-08-13 07:17:24 +00:00
$user = make ( UserRepository :: class ) -> get ( $request , $this -> session -> get ( 'user_id' ), true );
2020-02-26 11:22:25 +00:00
return view () -> render ( $response , 'user/edit.twig' , [
'profile' => true ,
2020-04-04 17:29:00 +00:00
'user' => $user ,
2020-02-26 11:22:25 +00:00
]);
}
/**
2020-03-03 16:18:52 +00:00
* @ param Request $request
* @ param Response $response
* @ param int $id
2020-02-26 11:22:25 +00:00
*
* @ return Response
*/
public function profileEdit ( Request $request , Response $response , int $id ) : Response
{
2021-08-13 07:17:24 +00:00
$user = make ( UserRepository :: class ) -> get ( $request , $id , true );
2020-02-26 11:22:25 +00:00
2020-04-04 17:29:00 +00:00
/** @var ValidationHelper $validator */
$validator = make ( ValidationHelper :: class )
-> alertIf ( ! filter_var ( param ( $request , 'email' ), FILTER_VALIDATE_EMAIL ), 'email_required' )
-> alertIf ( $this -> database -> query ( 'SELECT COUNT(*) AS `count` FROM `users` WHERE `email` = ? AND `email` <> ?' , [ param ( $request , 'email' ), $user -> email ]) -> fetch () -> count != 0 , 'email_taken' );
2020-02-26 11:22:25 +00:00
2020-03-03 16:18:52 +00:00
if ( $validator -> fails ()) {
2020-02-26 11:22:25 +00:00
return redirect ( $response , route ( 'profile' ));
}
if ( param ( $request , 'password' ) !== null && ! empty ( param ( $request , 'password' ))) {
2020-04-03 13:59:49 +00:00
$this -> database -> query ( 'UPDATE `users` SET `email`=?, `password`=?, `hide_uploads`=?, `copy_raw`=? WHERE `id` = ?' , [
2020-02-26 11:22:25 +00:00
param ( $request , 'email' ),
password_hash ( param ( $request , 'password' ), PASSWORD_DEFAULT ),
2020-04-03 13:59:49 +00:00
param ( $request , 'hide_uploads' ) !== null ? 1 : 0 ,
param ( $request , 'copy_raw' ) !== null ? 1 : 0 ,
2020-02-26 11:22:25 +00:00
$user -> id ,
]);
} else {
2020-04-03 13:59:49 +00:00
$this -> database -> query ( 'UPDATE `users` SET `email`=?, `hide_uploads`=?, `copy_raw`=? WHERE `id` = ?' , [
2020-02-26 11:22:25 +00:00
param ( $request , 'email' ),
2020-04-03 13:59:49 +00:00
param ( $request , 'hide_uploads' ) !== null ? 1 : 0 ,
param ( $request , 'copy_raw' ) !== null ? 1 : 0 ,
2020-02-26 11:22:25 +00:00
$user -> id ,
]);
}
2020-04-04 17:29:00 +00:00
$this -> session -> set ( 'copy_raw' , param ( $request , 'copy_raw' ) !== null ? 1 : 0 ) -> alert ( lang ( 'profile_updated' ), 'success' );
2020-02-26 11:22:25 +00:00
$this -> logger -> info ( 'User ' . $this -> session -> get ( 'username' ) . " updated profile of $user->id . " );
return redirect ( $response , route ( 'profile' ));
}
2020-02-26 11:22:40 +00:00
}