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\FlashMessages; 23e381f98dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 24e381f98dSGreg Roachuse Fisharebest\Webtrees\I18N; 25e381f98dSGreg Roachuse Fisharebest\Webtrees\Services\MessageService; 26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 27e381f98dSGreg Roachuse Psr\Http\Message\ResponseInterface; 28e381f98dSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 29e381f98dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 30e381f98dSGreg Roach 31e381f98dSGreg Roachuse function e; 32e381f98dSGreg Roachuse function redirect; 33e381f98dSGreg Roachuse function route; 34e381f98dSGreg Roach 35e381f98dSGreg Roach/** 36e381f98dSGreg Roach * Send messages from an administrator. 37e381f98dSGreg Roach */ 38e381f98dSGreg Roachclass BroadcastAction implements RequestHandlerInterface 39e381f98dSGreg Roach{ 40e381f98dSGreg Roach use ViewResponseTrait; 41e381f98dSGreg Roach 42c4943cffSGreg Roach private MessageService $message_service; 43e381f98dSGreg Roach 44e381f98dSGreg Roach /** 45e381f98dSGreg Roach * @param MessageService $message_service 46e381f98dSGreg Roach */ 473d51159aSGreg Roach public function __construct(MessageService $message_service) 48e381f98dSGreg Roach { 49e381f98dSGreg Roach $this->message_service = $message_service; 50e381f98dSGreg Roach } 51e381f98dSGreg Roach 52e381f98dSGreg Roach /** 53e381f98dSGreg Roach * @param ServerRequestInterface $request 54e381f98dSGreg Roach * 55e381f98dSGreg Roach * @return ResponseInterface 56e381f98dSGreg Roach */ 57e381f98dSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 58e381f98dSGreg Roach { 59a46dd5a6SGreg Roach $recipients = $this->message_service->recipientTypes(); 60e381f98dSGreg Roach 61a46dd5a6SGreg Roach $user = Validator::attributes($request)->user(); 62a46dd5a6SGreg Roach $to = Validator::attributes($request)->isInArrayKeys($recipients)->string('to'); 63b55cbc6bSGreg Roach $ip = Validator::attributes($request)->string('client-ip'); 64a46dd5a6SGreg Roach $body = Validator::parsedBody($request)->isNotEmpty()->string('body'); 65a46dd5a6SGreg Roach $subject = Validator::parsedBody($request)->isNotEmpty()->string('subject'); 66e381f98dSGreg Roach 67e381f98dSGreg Roach if ($body === '' || $subject === '') { 68e381f98dSGreg Roach return redirect(route(BroadcastPage::class, [ 69e381f98dSGreg Roach 'body' => $body, 70e381f98dSGreg Roach 'subject' => $subject, 71e381f98dSGreg Roach 'to' => $to, 72e381f98dSGreg Roach ])); 73e381f98dSGreg Roach } 74e381f98dSGreg Roach 75a46dd5a6SGreg Roach foreach ($this->message_service->recipientUsers($to) as $to_user) { 76e381f98dSGreg Roach if ($this->message_service->deliverMessage($user, $to_user, $subject, $body, '', $ip)) { 77a46dd5a6SGreg Roach FlashMessages::addMessage( 78a46dd5a6SGreg Roach I18N::translate('The message was successfully sent to %s.', e($to_user->realName())), 79a46dd5a6SGreg Roach 'success' 80a46dd5a6SGreg Roach ); 81e381f98dSGreg Roach } else { 82a46dd5a6SGreg Roach FlashMessages::addMessage( 83a46dd5a6SGreg Roach I18N::translate('The message was not sent to %s.', e($to_user->realName())), 84a46dd5a6SGreg Roach 'danger' 85a46dd5a6SGreg Roach ); 86e381f98dSGreg Roach } 87e381f98dSGreg Roach } 88e381f98dSGreg Roach 89e381f98dSGreg Roach return redirect(route(ControlPanel::class)); 90e381f98dSGreg Roach } 91e381f98dSGreg Roach} 92