xref: /webtrees/app/Http/RequestHandlers/BroadcastPage.php (revision e381f98dae35059b6db6d6f34db84bb55bd35a4a)
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 Fisharebest\Webtrees\Services\UserService;
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    /** @var MessageService */
38    private $message_service;
39
40    /** @var UserService */
41    private $user_service;
42
43    /**
44     * MessagePage constructor.
45     *
46     * @param MessageService $message_service
47     * @param UserService    $user_service
48     */
49    public function __construct(MessageService $message_service, UserService $user_service)
50    {
51        $this->user_service    = $user_service;
52        $this->message_service = $message_service;
53    }
54
55    /**
56     * @param ServerRequestInterface $request
57     *
58     * @return ResponseInterface
59     */
60    public function handle(ServerRequestInterface $request): ResponseInterface
61    {
62        $user    = $request->getAttribute('user');
63        $referer = $request->getHeaderLine('referer');
64        $params  = $request->getQueryParams();
65        $body    = $params['body'] ?? '';
66        $subject = $params['subject'] ?? '';
67        $to      = $params['to'];
68        $url     = $params['url'] ?? $referer;
69
70        $to_names = $this->message_service->recipientUsers($to)
71            ->map(static function (UserInterface $user): string {
72                return $user->realName();
73            });
74
75        $title = $this->message_service->recipientDescription($to);
76
77        $this->layout = 'layouts/administration';
78
79        return $this->viewResponse('admin/broadcast', [
80            'body'     => $body,
81            'from'     => $user,
82            'subject'  => $subject,
83            'title'    => $title,
84            'to'       => $to,
85            'to_names' => $to_names,
86            'url'      => $url,
87        ]);
88    }
89}
90