xref: /webtrees/app/Http/RequestHandlers/BroadcastPage.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1e381f98dSGreg Roach<?php
2e381f98dSGreg Roach
3e381f98dSGreg Roach/**
4e381f98dSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6e381f98dSGreg Roach * This program is free software: you can redistribute it and/or modify
7e381f98dSGreg Roach * it under the terms of the GNU General Public License as published by
8e381f98dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e381f98dSGreg Roach * (at your option) any later version.
10e381f98dSGreg Roach * This program is distributed in the hope that it will be useful,
11e381f98dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e381f98dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e381f98dSGreg Roach * GNU General Public License for more details.
14e381f98dSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e381f98dSGreg Roach */
17e381f98dSGreg Roach
18e381f98dSGreg Roachdeclare(strict_types=1);
19e381f98dSGreg Roach
20e381f98dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21e381f98dSGreg Roach
22e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
23e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\MessageService;
24b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
25e381f98dSGreg Roachuse Psr\Http\Message\ResponseInterface;
26e381f98dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27e381f98dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28e381f98dSGreg Roach
29e381f98dSGreg Roach/**
30e381f98dSGreg Roach * Compose messages from an administrator.
31e381f98dSGreg Roach */
32e381f98dSGreg Roachclass BroadcastPage implements RequestHandlerInterface
33e381f98dSGreg Roach{
34e381f98dSGreg Roach    use ViewResponseTrait;
35e381f98dSGreg Roach
36c4943cffSGreg Roach    private MessageService $message_service;
37e381f98dSGreg Roach
38e381f98dSGreg Roach    /**
39e381f98dSGreg Roach     * @param MessageService $message_service
40e381f98dSGreg Roach     */
413d51159aSGreg Roach    public function __construct(MessageService $message_service)
42e381f98dSGreg Roach    {
43e381f98dSGreg Roach        $this->message_service = $message_service;
44e381f98dSGreg Roach    }
45e381f98dSGreg Roach
46e381f98dSGreg Roach    /**
47e381f98dSGreg Roach     * @param ServerRequestInterface $request
48e381f98dSGreg Roach     *
49e381f98dSGreg Roach     * @return ResponseInterface
50e381f98dSGreg Roach     */
51e381f98dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
52e381f98dSGreg Roach    {
53a46dd5a6SGreg Roach        $recipient_types = $this->message_service->recipientTypes();
54a46dd5a6SGreg Roach
55b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
56a46dd5a6SGreg Roach        $to   = Validator::attributes($request)->isInArrayKeys($recipient_types)->string('to');
57e381f98dSGreg Roach
58a46dd5a6SGreg Roach        $title = $recipient_types[$to];
59e381f98dSGreg Roach
60e381f98dSGreg Roach        $this->layout = 'layouts/administration';
61e381f98dSGreg Roach
62e381f98dSGreg Roach        return $this->viewResponse('admin/broadcast', [
63e381f98dSGreg Roach            'from'       => $user,
64e381f98dSGreg Roach            'title'      => $title,
65e381f98dSGreg Roach            'to'         => $to,
66a46dd5a6SGreg Roach            'recipients' => $this->message_service->recipientUsers($to),
67e381f98dSGreg Roach        ]);
68e381f98dSGreg Roach    }
69e381f98dSGreg Roach}
70