xref: /webtrees/app/Http/RequestHandlers/MessagePage.php (revision e381f98dae35059b6db6d6f34db84bb55bd35a4a)
1*e381f98dSGreg Roach<?php
2*e381f98dSGreg Roach
3*e381f98dSGreg Roach/**
4*e381f98dSGreg Roach * webtrees: online genealogy
5*e381f98dSGreg Roach * Copyright (C) 2019 webtrees development team
6*e381f98dSGreg Roach * This program is free software: you can redistribute it and/or modify
7*e381f98dSGreg Roach * it under the terms of the GNU General Public License as published by
8*e381f98dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*e381f98dSGreg Roach * (at your option) any later version.
10*e381f98dSGreg Roach * This program is distributed in the hope that it will be useful,
11*e381f98dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*e381f98dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*e381f98dSGreg Roach * GNU General Public License for more details.
14*e381f98dSGreg Roach * You should have received a copy of the GNU General Public License
15*e381f98dSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*e381f98dSGreg Roach */
17*e381f98dSGreg Roach
18*e381f98dSGreg Roachdeclare(strict_types=1);
19*e381f98dSGreg Roach
20*e381f98dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*e381f98dSGreg Roach
22*e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
23*e381f98dSGreg Roachuse Fisharebest\Webtrees\I18N;
24*e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
25*e381f98dSGreg Roachuse Fisharebest\Webtrees\Tree;
26*e381f98dSGreg Roachuse Psr\Http\Message\ResponseInterface;
27*e381f98dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
28*e381f98dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
29*e381f98dSGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
30*e381f98dSGreg Roach
31*e381f98dSGreg Roachuse function assert;
32*e381f98dSGreg Roach
33*e381f98dSGreg Roach/**
34*e381f98dSGreg Roach * Compose a message from a logged-in user.
35*e381f98dSGreg Roach */
36*e381f98dSGreg Roachclass MessagePage implements RequestHandlerInterface
37*e381f98dSGreg Roach{
38*e381f98dSGreg Roach    use ViewResponseTrait;
39*e381f98dSGreg Roach
40*e381f98dSGreg Roach    /** @var UserService */
41*e381f98dSGreg Roach    private $user_service;
42*e381f98dSGreg Roach
43*e381f98dSGreg Roach    /**
44*e381f98dSGreg Roach     * MessagePage constructor.
45*e381f98dSGreg Roach     *
46*e381f98dSGreg Roach     * @param UserService $user_service
47*e381f98dSGreg Roach     */
48*e381f98dSGreg Roach    public function __construct(UserService $user_service)
49*e381f98dSGreg Roach    {
50*e381f98dSGreg Roach        $this->user_service = $user_service;
51*e381f98dSGreg Roach    }
52*e381f98dSGreg Roach
53*e381f98dSGreg Roach    /**
54*e381f98dSGreg Roach     * @param ServerRequestInterface $request
55*e381f98dSGreg Roach     *
56*e381f98dSGreg Roach     * @return ResponseInterface
57*e381f98dSGreg Roach     */
58*e381f98dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
59*e381f98dSGreg Roach    {
60*e381f98dSGreg Roach        $tree = $request->getAttribute('tree');
61*e381f98dSGreg Roach        assert($tree instanceof Tree);
62*e381f98dSGreg Roach
63*e381f98dSGreg Roach        $user    = $request->getAttribute('user');
64*e381f98dSGreg Roach        $referer = $request->getHeaderLine('referer');
65*e381f98dSGreg Roach        $body    = $request->getQueryParams()['body'] ?? '';
66*e381f98dSGreg Roach        $subject = $request->getQueryParams()['subject'] ?? '';
67*e381f98dSGreg Roach        $to      = $request->getQueryParams()['to'] ?? '';
68*e381f98dSGreg Roach        $url     = $request->getQueryParams()['url'] ?? $referer;
69*e381f98dSGreg Roach        $to_user = $this->user_service->findByUserName($to);
70*e381f98dSGreg Roach
71*e381f98dSGreg Roach        if ($to_user === null || $to_user->getPreference('contactmethod') === 'none') {
72*e381f98dSGreg Roach            throw new AccessDeniedHttpException('Invalid contact user id');
73*e381f98dSGreg Roach        }
74*e381f98dSGreg Roach
75*e381f98dSGreg Roach        $title = I18N::translate('Send a message');
76*e381f98dSGreg Roach
77*e381f98dSGreg Roach        return $this->viewResponse('message-page', [
78*e381f98dSGreg Roach            'body'    => $body,
79*e381f98dSGreg Roach            'from'    => $user,
80*e381f98dSGreg Roach            'subject' => $subject,
81*e381f98dSGreg Roach            'title'   => $title,
82*e381f98dSGreg Roach            'to'      => $to_user,
83*e381f98dSGreg Roach            'tree'    => $tree,
84*e381f98dSGreg Roach            'url'     => $url,
85*e381f98dSGreg Roach        ]);
86*e381f98dSGreg Roach    }
87*e381f98dSGreg Roach}
88