xref: /webtrees/app/Http/RequestHandlers/BroadcastPage.php (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
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        $params  = $request->getQueryParams();
58        $body    = $params['body'] ?? '';
59        $subject = $params['subject'] ?? '';
60        $to      = $params['to'];
61
62        $to_names = $this->message_service->recipientUsers($to)
63            ->map(static function (UserInterface $user): string {
64                return $user->realName();
65            });
66
67        $title = $this->message_service->recipientDescription($to);
68
69        $this->layout = 'layouts/administration';
70
71        return $this->viewResponse('admin/broadcast', [
72            'body'     => $body,
73            'from'     => $user,
74            'subject'  => $subject,
75            'title'    => $title,
76            'to'       => $to,
77            'to_names' => $to_names,
78        ]);
79    }
80}
81