xref: /webtrees/app/Http/RequestHandlers/BroadcastPage.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://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 Fisharebest\Webtrees\Validator;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30/**
31 * Compose messages from an administrator.
32 */
33class BroadcastPage implements RequestHandlerInterface
34{
35    use ViewResponseTrait;
36
37    private MessageService $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    = Validator::attributes($request)->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