xref: /webtrees/app/Http/RequestHandlers/ManageTrees.php (revision 6fd01894a78d321fac365dd0291a2fc52129fa03)
1*6fd01894SGreg Roach<?php
2*6fd01894SGreg Roach
3*6fd01894SGreg Roach/**
4*6fd01894SGreg Roach * webtrees: online genealogy
5*6fd01894SGreg Roach * Copyright (C) 2020 webtrees development team
6*6fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
7*6fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
8*6fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*6fd01894SGreg Roach * (at your option) any later version.
10*6fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
11*6fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*6fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*6fd01894SGreg Roach * GNU General Public License for more details.
14*6fd01894SGreg Roach * You should have received a copy of the GNU General Public License
15*6fd01894SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*6fd01894SGreg Roach */
17*6fd01894SGreg Roach
18*6fd01894SGreg Roachdeclare(strict_types=1);
19*6fd01894SGreg Roach
20*6fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*6fd01894SGreg Roach
22*6fd01894SGreg Roachuse Fisharebest\Webtrees\Factory;
23*6fd01894SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
24*6fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
25*6fd01894SGreg Roachuse Fisharebest\Webtrees\Services\AdminService;
26*6fd01894SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
27*6fd01894SGreg Roachuse Fisharebest\Webtrees\Site;
28*6fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
29*6fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
30*6fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31*6fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32*6fd01894SGreg Roach
33*6fd01894SGreg Roachuse function app;
34*6fd01894SGreg Roach
35*6fd01894SGreg Roach/**
36*6fd01894SGreg Roach * Show the manager options for trees.
37*6fd01894SGreg Roach */
38*6fd01894SGreg Roachclass ManageTrees implements RequestHandlerInterface
39*6fd01894SGreg Roach{
40*6fd01894SGreg Roach    use ViewResponseTrait;
41*6fd01894SGreg Roach
42*6fd01894SGreg Roach    /** @var AdminService */
43*6fd01894SGreg Roach    private $admin_service;
44*6fd01894SGreg Roach
45*6fd01894SGreg Roach    /** @var TreeService */
46*6fd01894SGreg Roach    private $tree_service;
47*6fd01894SGreg Roach
48*6fd01894SGreg Roach    /**
49*6fd01894SGreg Roach     * @param AdminService $admin_service
50*6fd01894SGreg Roach     * @param TreeService  $tree_service
51*6fd01894SGreg Roach     */
52*6fd01894SGreg Roach    public function __construct(AdminService $admin_service, TreeService $tree_service)
53*6fd01894SGreg Roach    {
54*6fd01894SGreg Roach        $this->admin_service = $admin_service;
55*6fd01894SGreg Roach        $this->tree_service  = $tree_service;
56*6fd01894SGreg Roach    }
57*6fd01894SGreg Roach
58*6fd01894SGreg Roach    /**
59*6fd01894SGreg Roach     * @param ServerRequestInterface $request
60*6fd01894SGreg Roach     *
61*6fd01894SGreg Roach     * @return ResponseInterface
62*6fd01894SGreg Roach     */
63*6fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
64*6fd01894SGreg Roach    {
65*6fd01894SGreg Roach        $this->layout = 'layouts/administration';
66*6fd01894SGreg Roach
67*6fd01894SGreg Roach        $tree = $request->getAttribute('tree');
68*6fd01894SGreg Roach
69*6fd01894SGreg Roach        $multiple_tree_threshold = $this->admin_service->multipleTreeThreshold();
70*6fd01894SGreg Roach        $gedcom_file_count       = $this->admin_service->gedcomFiles(Factory::filesystem()->data())->count();
71*6fd01894SGreg Roach
72*6fd01894SGreg Roach        $all_trees = $this->tree_service->all();
73*6fd01894SGreg Roach
74*6fd01894SGreg Roach        // On sites with hundreds or thousands of trees, this page becomes very large.
75*6fd01894SGreg Roach        // Just show the current tree, the default tree, and un-imported trees
76*6fd01894SGreg Roach        if ($gedcom_file_count >= $multiple_tree_threshold) {
77*6fd01894SGreg Roach            $default   = Site::getPreference('DEFAULT_GEDCOM');
78*6fd01894SGreg Roach            $all_trees = $all_trees->filter(static function (Tree $x) use ($tree, $default): bool {
79*6fd01894SGreg Roach                if ($x->getPreference('imported') === '0') {
80*6fd01894SGreg Roach                    return true;
81*6fd01894SGreg Roach                }
82*6fd01894SGreg Roach                if ($tree instanceof Tree && $tree->id() === $x->id()) {
83*6fd01894SGreg Roach                    return true;
84*6fd01894SGreg Roach                }
85*6fd01894SGreg Roach
86*6fd01894SGreg Roach                return $x->name() === $default;
87*6fd01894SGreg Roach            });
88*6fd01894SGreg Roach        }
89*6fd01894SGreg Roach
90*6fd01894SGreg Roach        $title = I18N::translate('Manage family trees');
91*6fd01894SGreg Roach
92*6fd01894SGreg Roach        $base_url = app(ServerRequestInterface::class)->getAttribute('base_url');
93*6fd01894SGreg Roach
94*6fd01894SGreg Roach        return $this->viewResponse('admin/trees', [
95*6fd01894SGreg Roach            'all_trees' => $all_trees,
96*6fd01894SGreg Roach            'base_url'  => $base_url,
97*6fd01894SGreg Roach            'title'     => $title,
98*6fd01894SGreg Roach            'tree'      => $tree,
99*6fd01894SGreg Roach        ]);
100*6fd01894SGreg Roach    }
101*6fd01894SGreg Roach}
102