xref: /webtrees/app/Http/RequestHandlers/BroadcastPage.php (revision 040e7dbaf82a0aee63813e56eb6c22596f79f9f2)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Contracts\UserInterface;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\Services\MessageService;
25use Psr\Http\Message\ResponseInterface;
26use Psr\Http\Message\ServerRequestInterface;
27use Psr\Http\Server\RequestHandlerInterface;
28
29/**
30 * Compose messages from an administrator.
31 */
32class BroadcastPage implements RequestHandlerInterface
33{
34    use ViewResponseTrait;
35
36    /** @var MessageService */
37    private $message_service;
38
39    /**
40     * MessagePage constructor.
41     *
42     * @param MessageService $message_service
43     */
44    public function __construct(MessageService $message_service)
45    {
46        $this->message_service = $message_service;
47    }
48
49    /**
50     * @param ServerRequestInterface $request
51     *
52     * @return ResponseInterface
53     */
54    public function handle(ServerRequestInterface $request): ResponseInterface
55    {
56        $user    = $request->getAttribute('user');
57        $referer = $request->getHeaderLine('referer');
58        $params  = $request->getQueryParams();
59        $body    = $params['body'] ?? '';
60        $subject = $params['subject'] ?? '';
61        $to      = $params['to'];
62        $url     = $params['url'] ?? $referer;
63
64        $to_names = $this->message_service->recipientUsers($to)
65            ->map(static function (UserInterface $user): string {
66                return $user->realName();
67            });
68
69        $title = $this->message_service->recipientDescription($to);
70
71        $this->layout = 'layouts/administration';
72
73        return $this->viewResponse('admin/broadcast', [
74            'body'     => $body,
75            'from'     => $user,
76            'subject'  => $subject,
77            'title'    => $title,
78            'to'       => $to,
79            'to_names' => $to_names,
80            'url'      => $url,
81        ]);
82    }
83}
84