xref: /webtrees/app/Http/RequestHandlers/ManageTrees.php (revision d11be7027e34e3121be11cc025421873364403f9)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
66fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
76fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
86fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96fd01894SGreg Roach * (at your option) any later version.
106fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
116fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136fd01894SGreg Roach * GNU General Public License for more details.
146fd01894SGreg 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/>.
166fd01894SGreg Roach */
176fd01894SGreg Roach
186fd01894SGreg Roachdeclare(strict_types=1);
196fd01894SGreg Roach
206fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216fd01894SGreg Roach
226fd01894SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
236fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
256fd01894SGreg Roachuse Fisharebest\Webtrees\Services\AdminService;
266fd01894SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
276fd01894SGreg Roachuse Fisharebest\Webtrees\Site;
286fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
306fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
316fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
326fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
336fd01894SGreg Roach
346fd01894SGreg Roach/**
356fd01894SGreg Roach * Show the manager options for trees.
366fd01894SGreg Roach */
376fd01894SGreg Roachclass ManageTrees implements RequestHandlerInterface
386fd01894SGreg Roach{
396fd01894SGreg Roach    use ViewResponseTrait;
406fd01894SGreg Roach
41c4943cffSGreg Roach    private AdminService $admin_service;
426fd01894SGreg Roach
43c4943cffSGreg Roach    private TreeService $tree_service;
446fd01894SGreg Roach
456fd01894SGreg Roach    /**
466fd01894SGreg Roach     * @param AdminService $admin_service
476fd01894SGreg Roach     * @param TreeService  $tree_service
486fd01894SGreg Roach     */
496fd01894SGreg Roach    public function __construct(AdminService $admin_service, TreeService $tree_service)
506fd01894SGreg Roach    {
516fd01894SGreg Roach        $this->admin_service = $admin_service;
526fd01894SGreg Roach        $this->tree_service  = $tree_service;
536fd01894SGreg Roach    }
546fd01894SGreg Roach
556fd01894SGreg Roach    /**
566fd01894SGreg Roach     * @param ServerRequestInterface $request
576fd01894SGreg Roach     *
586fd01894SGreg Roach     * @return ResponseInterface
596fd01894SGreg Roach     */
606fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
616fd01894SGreg Roach    {
626fd01894SGreg Roach        $this->layout = 'layouts/administration';
636fd01894SGreg Roach
64b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
656fd01894SGreg Roach
666fd01894SGreg Roach        $multiple_tree_threshold = $this->admin_service->multipleTreeThreshold();
676b9cb339SGreg Roach        $gedcom_file_count       = $this->admin_service->gedcomFiles(Registry::filesystem()->data())->count();
686fd01894SGreg Roach
696fd01894SGreg Roach        $all_trees = $this->tree_service->all();
706fd01894SGreg Roach
716fd01894SGreg Roach        // On sites with hundreds or thousands of trees, this page becomes very large.
726fd01894SGreg Roach        // Just show the current tree, the default tree, and un-imported trees
736fd01894SGreg Roach        if ($gedcom_file_count >= $multiple_tree_threshold) {
746fd01894SGreg Roach            $default   = Site::getPreference('DEFAULT_GEDCOM');
756fd01894SGreg Roach            $all_trees = $all_trees->filter(static function (Tree $x) use ($tree, $default): bool {
766fd01894SGreg Roach                if ($x->getPreference('imported') === '0') {
776fd01894SGreg Roach                    return true;
786fd01894SGreg Roach                }
796fd01894SGreg Roach                if ($tree instanceof Tree && $tree->id() === $x->id()) {
806fd01894SGreg Roach                    return true;
816fd01894SGreg Roach                }
826fd01894SGreg Roach
836fd01894SGreg Roach                return $x->name() === $default;
846fd01894SGreg Roach            });
856fd01894SGreg Roach        }
866fd01894SGreg Roach
876fd01894SGreg Roach        $title = I18N::translate('Manage family trees');
886fd01894SGreg Roach
896fd01894SGreg Roach        return $this->viewResponse('admin/trees', [
906fd01894SGreg Roach            'all_trees' => $all_trees,
916fd01894SGreg Roach            'title'     => $title,
926fd01894SGreg Roach            'tree'      => $tree,
936fd01894SGreg Roach        ]);
946fd01894SGreg Roach    }
956fd01894SGreg Roach}
96