xref: /webtrees/app/Http/RequestHandlers/BroadcastAction.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\FlashMessages;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\MessageService;
26use Fisharebest\Webtrees\Services\UserService;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30
31use function e;
32use function redirect;
33use function route;
34
35/**
36 * Send messages from an administrator.
37 */
38class BroadcastAction implements RequestHandlerInterface
39{
40    use ViewResponseTrait;
41
42    /** @var MessageService */
43    private $message_service;
44
45    /** @var UserService */
46    private $user_service;
47
48    /**
49     * MessagePage constructor.
50     *
51     * @param MessageService $message_service
52     * @param UserService    $user_service
53     */
54    public function __construct(MessageService $message_service, UserService $user_service)
55    {
56        $this->user_service    = $user_service;
57        $this->message_service = $message_service;
58    }
59
60    /**
61     * @param ServerRequestInterface $request
62     *
63     * @return ResponseInterface
64     */
65    public function handle(ServerRequestInterface $request): ResponseInterface
66    {
67        $user    = $request->getAttribute('user');
68        $params  = $request->getParsedBody();
69        $body    = $params['body'];
70        $subject = $params['subject'];
71        $to      = $params['to'];
72
73        $ip       = $request->getAttribute('client-ip');
74        $to_users = $this->message_service->recipientUsers($to);
75
76        if ($body === '' || $subject === '') {
77            return redirect(route(BroadcastPage::class, [
78                'body'    => $body,
79                'subject' => $subject,
80                'to'      => $to,
81            ]));
82        }
83
84        $errors = false;
85
86        foreach ($to_users as $to_user) {
87            if ($this->message_service->deliverMessage($user, $to_user, $subject, $body, '', $ip)) {
88                FlashMessages::addMessage(I18N::translate('The message was successfully sent to %s.', e($to_user->realName())), 'success');
89            } else {
90                $errors = true;
91            }
92        }
93
94        if ($errors) {
95            FlashMessages::addMessage(I18N::translate('The message was not sent.'), 'danger');
96        }
97
98        return redirect(route(ControlPanel::class));
99    }
100}
101