xref: /webtrees/app/Http/RequestHandlers/MessagePage.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1e381f98dSGreg Roach<?php
2e381f98dSGreg Roach
3e381f98dSGreg Roach/**
4e381f98dSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6e381f98dSGreg Roach * This program is free software: you can redistribute it and/or modify
7e381f98dSGreg Roach * it under the terms of the GNU General Public License as published by
8e381f98dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e381f98dSGreg Roach * (at your option) any later version.
10e381f98dSGreg Roach * This program is distributed in the hope that it will be useful,
11e381f98dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e381f98dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e381f98dSGreg Roach * GNU General Public License for more details.
14e381f98dSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e381f98dSGreg Roach */
17e381f98dSGreg Roach
18e381f98dSGreg Roachdeclare(strict_types=1);
19e381f98dSGreg Roach
20e381f98dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21e381f98dSGreg Roach
221fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2381b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
24e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
25e381f98dSGreg Roachuse Fisharebest\Webtrees\I18N;
268cfb5e7bSGreg Roachuse Fisharebest\Webtrees\Services\MessageService;
27e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29e381f98dSGreg Roachuse Psr\Http\Message\ResponseInterface;
30e381f98dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31e381f98dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32e381f98dSGreg Roach
330bf22806SGreg Roachuse function route;
34e381f98dSGreg Roach
35e381f98dSGreg Roach/**
36e381f98dSGreg Roach * Compose a message from a logged-in user.
37e381f98dSGreg Roach */
38e381f98dSGreg Roachclass MessagePage implements RequestHandlerInterface
39e381f98dSGreg Roach{
40e381f98dSGreg Roach    use ViewResponseTrait;
41e381f98dSGreg Roach
42c4943cffSGreg Roach    private UserService $user_service;
43e381f98dSGreg Roach
44e381f98dSGreg Roach    /**
45e381f98dSGreg Roach     * @param UserService $user_service
46e381f98dSGreg Roach     */
47e381f98dSGreg Roach    public function __construct(UserService $user_service)
48e381f98dSGreg Roach    {
49e381f98dSGreg Roach        $this->user_service = $user_service;
50e381f98dSGreg Roach    }
51e381f98dSGreg Roach
52e381f98dSGreg Roach    /**
53e381f98dSGreg Roach     * @param ServerRequestInterface $request
54e381f98dSGreg Roach     *
55e381f98dSGreg Roach     * @return ResponseInterface
56e381f98dSGreg Roach     */
57e381f98dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
58e381f98dSGreg Roach    {
59b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
60b55cbc6bSGreg Roach        $user    = Validator::attributes($request)->user();
619f0bdfcdSGreg Roach        $body    = Validator::queryParams($request)->string('body', '');
629f0bdfcdSGreg Roach        $subject = Validator::queryParams($request)->string('subject', '');
639f0bdfcdSGreg Roach        $to      = Validator::queryParams($request)->string('to', '');
649f0bdfcdSGreg Roach        $url     = Validator::queryParams($request)->isLocalUrl()->string('url', route(HomePage::class));
65e381f98dSGreg Roach        $to_user = $this->user_service->findByUserName($to);
66e381f98dSGreg Roach
678cfb5e7bSGreg Roach        if ($to_user === null || $to_user->getPreference(UserInterface::PREF_CONTACT_METHOD) === MessageService::CONTACT_METHOD_NONE) {
68d501c45dSGreg Roach            throw new HttpAccessDeniedException('Invalid contact user id');
69e381f98dSGreg Roach        }
70e381f98dSGreg Roach
71e381f98dSGreg Roach        $title = I18N::translate('Send a message');
72e381f98dSGreg Roach
73e381f98dSGreg Roach        return $this->viewResponse('message-page', [
74e381f98dSGreg Roach            'body'    => $body,
75e381f98dSGreg Roach            'from'    => $user,
76e381f98dSGreg Roach            'subject' => $subject,
77e381f98dSGreg Roach            'title'   => $title,
78e381f98dSGreg Roach            'to'      => $to_user,
79e381f98dSGreg Roach            'tree'    => $tree,
80e381f98dSGreg Roach            'url'     => $url,
81e381f98dSGreg Roach        ]);
82e381f98dSGreg Roach    }
83e381f98dSGreg Roach}
84