xref: /webtrees/app/Http/RequestHandlers/ModulesAllAction.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
19f667ff2SGreg Roach<?php
29f667ff2SGreg Roach
39f667ff2SGreg Roach/**
49f667ff2SGreg Roach * webtrees: online genealogy
59f667ff2SGreg Roach * Copyright (C) 20 webtrees development team
69f667ff2SGreg Roach * This program is free software: you can redistribute it and/or modify
79f667ff2SGreg Roach * it under the terms of the GNU General Public License as published by
89f667ff2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
99f667ff2SGreg Roach * (at your option) any later version.
109f667ff2SGreg Roach * This program is distributed in the hope that it will be useful,
119f667ff2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
129f667ff2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
139f667ff2SGreg Roach * GNU General Public License for more details.
149f667ff2SGreg 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/>.
169f667ff2SGreg Roach */
179f667ff2SGreg Roach
189f667ff2SGreg Roachdeclare(strict_types=1);
199f667ff2SGreg Roach
209f667ff2SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
219f667ff2SGreg Roach
22*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
239f667ff2SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
249f667ff2SGreg Roachuse Fisharebest\Webtrees\I18N;
259f667ff2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
26748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
279f667ff2SGreg Roachuse Psr\Http\Message\ResponseInterface;
289f667ff2SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
299f667ff2SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
309f667ff2SGreg Roach
319f667ff2SGreg Roachuse function redirect;
329f667ff2SGreg Roachuse function route;
339f667ff2SGreg Roach
349f667ff2SGreg Roach/**
359f667ff2SGreg Roach * Update a list of modules.
369f667ff2SGreg Roach */
379f667ff2SGreg Roachclass ModulesAllAction implements RequestHandlerInterface
389f667ff2SGreg Roach{
39c4943cffSGreg Roach    private ModuleService $module_service;
409f667ff2SGreg Roach
419f667ff2SGreg Roach    /**
429f667ff2SGreg Roach     * @param ModuleService $module_service
439f667ff2SGreg Roach     */
449f667ff2SGreg Roach    public function __construct(ModuleService $module_service)
459f667ff2SGreg Roach    {
469f667ff2SGreg Roach        $this->module_service = $module_service;
479f667ff2SGreg Roach    }
489f667ff2SGreg Roach
499f667ff2SGreg Roach    /**
509f667ff2SGreg Roach     * @param ServerRequestInterface $request
519f667ff2SGreg Roach     *
529f667ff2SGreg Roach     * @return ResponseInterface
539f667ff2SGreg Roach     */
549f667ff2SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
559f667ff2SGreg Roach    {
569f667ff2SGreg Roach        $modules = $this->module_service->all(true);
579f667ff2SGreg Roach
589f667ff2SGreg Roach        foreach ($modules as $module) {
59748dbe15SGreg Roach            $new_status = Validator::parsedBody($request)->boolean('status-' . $module->name(), false);
609f667ff2SGreg Roach            $old_status = $module->isEnabled();
619f667ff2SGreg Roach
629f667ff2SGreg Roach            if ($new_status !== $old_status) {
639f667ff2SGreg Roach                DB::table('module')
649f667ff2SGreg Roach                    ->where('module_name', '=', $module->name())
659f667ff2SGreg Roach                    ->update(['status' => $new_status ? 'enabled' : 'disabled']);
669f667ff2SGreg Roach
679f667ff2SGreg Roach                if ($new_status) {
68748dbe15SGreg Roach                    $message = I18N::translate('The module “%s” has been enabled.', $module->title());
699f667ff2SGreg Roach                } else {
70748dbe15SGreg Roach                    $message = I18N::translate('The module “%s” has been disabled.', $module->title());
719f667ff2SGreg Roach                }
72748dbe15SGreg Roach
73748dbe15SGreg Roach                FlashMessages::addMessage($message, 'success');
749f667ff2SGreg Roach            }
759f667ff2SGreg Roach        }
769f667ff2SGreg Roach
779f667ff2SGreg Roach        return redirect(route(ModulesAllPage::class));
789f667ff2SGreg Roach    }
799f667ff2SGreg Roach}
80