xref: /webtrees/app/Http/RequestHandlers/TreePageUpdate.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
18e0e1b25SGreg Roach<?php
28e0e1b25SGreg Roach
38e0e1b25SGreg Roach/**
48e0e1b25SGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
68e0e1b25SGreg Roach * This program is free software: you can redistribute it and/or modify
78e0e1b25SGreg Roach * it under the terms of the GNU General Public License as published by
88e0e1b25SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98e0e1b25SGreg Roach * (at your option) any later version.
108e0e1b25SGreg Roach * This program is distributed in the hope that it will be useful,
118e0e1b25SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128e0e1b25SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138e0e1b25SGreg Roach * GNU General Public License for more details.
148e0e1b25SGreg Roach * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168e0e1b25SGreg Roach */
178e0e1b25SGreg Roach
188e0e1b25SGreg Roachdeclare(strict_types=1);
198e0e1b25SGreg Roach
208e0e1b25SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
218e0e1b25SGreg Roach
22f6924bc8SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
238e0e1b25SGreg Roachuse Fisharebest\Webtrees\Module\ModuleBlockInterface;
248e0e1b25SGreg Roachuse Fisharebest\Webtrees\Services\HomePageService;
258e0e1b25SGreg Roachuse Fisharebest\Webtrees\Tree;
268e0e1b25SGreg Roachuse Illuminate\Support\Collection;
278e0e1b25SGreg Roachuse Psr\Http\Message\ResponseInterface;
288e0e1b25SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
298e0e1b25SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
308e0e1b25SGreg Roach
318e0e1b25SGreg Roachuse function assert;
328e0e1b25SGreg Roachuse function redirect;
338e0e1b25SGreg Roachuse function route;
348e0e1b25SGreg Roach
358e0e1b25SGreg Roach/**
367893c81aSGreg Roach * Save updated blocks on a tree's page.
378e0e1b25SGreg Roach */
388e0e1b25SGreg Roachclass TreePageUpdate implements RequestHandlerInterface
398e0e1b25SGreg Roach{
408e0e1b25SGreg Roach    /** @var HomePageService */
418e0e1b25SGreg Roach    private $home_page_service;
428e0e1b25SGreg Roach
438e0e1b25SGreg Roach    /**
448e0e1b25SGreg Roach     * @param HomePageService $home_page_service
458e0e1b25SGreg Roach     */
468e0e1b25SGreg Roach    public function __construct(HomePageService $home_page_service)
478e0e1b25SGreg Roach    {
488e0e1b25SGreg Roach        $this->home_page_service = $home_page_service;
498e0e1b25SGreg Roach    }
508e0e1b25SGreg Roach
518e0e1b25SGreg Roach    /**
528e0e1b25SGreg Roach     * @param ServerRequestInterface $request
538e0e1b25SGreg Roach     *
548e0e1b25SGreg Roach     * @return ResponseInterface
558e0e1b25SGreg Roach     */
568e0e1b25SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
578e0e1b25SGreg Roach    {
588e0e1b25SGreg Roach        $tree = $request->getAttribute('tree');
598e0e1b25SGreg Roach        assert($tree instanceof Tree);
608e0e1b25SGreg Roach
61f6924bc8SGreg Roach        $user = $request->getAttribute('user');
62f6924bc8SGreg Roach        assert($user instanceof UserInterface);
63f6924bc8SGreg Roach
648e0e1b25SGreg Roach        $params = (array) $request->getParsedBody();
658e0e1b25SGreg Roach
668e0e1b25SGreg Roach        $defaults = (bool) ($params['defaults'] ?? false);
678e0e1b25SGreg Roach
688e0e1b25SGreg Roach        if ($defaults) {
69f6924bc8SGreg Roach            $default_tree = new Tree(-1, 'DEFAULT', 'DEFAULT');
70f6924bc8SGreg Roach
71f6924bc8SGreg Roach            $main_blocks = $this->home_page_service->treeBlocks($default_tree, $user, ModuleBlockInterface::MAIN_BLOCKS)
728e0e1b25SGreg Roach                ->map(static function (ModuleBlockInterface $block) {
738e0e1b25SGreg Roach                    return $block->name();
748e0e1b25SGreg Roach                });
75f6924bc8SGreg Roach            $side_blocks = $this->home_page_service->treeBlocks($default_tree, $user, ModuleBlockInterface::SIDE_BLOCKS)
768e0e1b25SGreg Roach                ->map(static function (ModuleBlockInterface $block) {
778e0e1b25SGreg Roach                    return $block->name();
788e0e1b25SGreg Roach                });
798e0e1b25SGreg Roach        } else {
808e0e1b25SGreg Roach            $main_blocks = new Collection($params[ModuleBlockInterface::MAIN_BLOCKS] ?? []);
818e0e1b25SGreg Roach            $side_blocks = new Collection($params[ModuleBlockInterface::SIDE_BLOCKS] ?? []);
828e0e1b25SGreg Roach        }
838e0e1b25SGreg Roach
848e0e1b25SGreg Roach        $this->home_page_service->updateTreeBlocks($tree->id(), $main_blocks, $side_blocks);
858e0e1b25SGreg Roach
868e0e1b25SGreg Roach        return redirect(route(TreePage::class, ['tree' => $tree->name()]));
878e0e1b25SGreg Roach    }
888e0e1b25SGreg Roach}
89