xref: /webtrees/app/Http/RequestHandlers/UnconnectedPage.php (revision 81de533683c82a9e5c2bf2de336b47c4103fda6d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Algorithm\ConnectedComponent;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Tree;
27use Fisharebest\Webtrees\User;
28use Illuminate\Database\Capsule\Manager as DB;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function assert;
34
35/**
36 * Find groups of unrelated individuals.
37 */
38class UnconnectedPage implements RequestHandlerInterface
39{
40    use ViewResponseTrait;
41
42    /**
43     * @param ServerRequestInterface $request
44     *
45     * @return ResponseInterface
46     */
47    public function handle(ServerRequestInterface $request): ResponseInterface
48    {
49        $tree = $request->getAttribute('tree');
50        assert($tree instanceof Tree);
51
52        $user       = $request->getAttribute('user');
53        assert($user instanceof User);
54
55        $associates = (bool) ($request->getQueryParams()['associates'] ?? false);
56
57        if ($associates) {
58            $links = ['FAMS', 'FAMC', 'ASSO', '_ASSO'];
59        } else {
60            $links = ['FAMS', 'FAMC'];
61        }
62
63        $rows = DB::table('link')
64            ->where('l_file', '=', $tree->id())
65            ->whereIn('l_type', $links)
66            ->select(['l_from', 'l_to'])
67            ->get();
68
69        $graph = [];
70
71        foreach ($rows as $row) {
72            $graph[$row->l_from][$row->l_to] = 1;
73            $graph[$row->l_to][$row->l_from] = 1;
74        }
75
76        $algorithm  = new ConnectedComponent($graph);
77        $components = $algorithm->findConnectedComponents();
78        $root       = $tree->significantIndividual($user);
79        $xref       = $root->xref();
80
81        /** @var Individual[][] */
82        $individual_groups = [];
83
84        foreach ($components as $component) {
85            if (!in_array($xref, $component, true)) {
86                $individuals = [];
87                foreach ($component as $xref) {
88                    $individuals[] = Individual::getInstance($xref, $tree);
89                }
90                // The database query may return pending additions/deletions, which may not exist.
91                $individual_groups[] = array_filter($individuals);
92            }
93        }
94
95        $title = I18N::translate('Find unrelated individuals') . ' — ' . e($tree->title());
96
97        $this->layout = 'layouts/administration';
98
99        return $this->viewResponse('admin/trees-unconnected', [
100            'associates'        => $associates,
101            'root'              => $root,
102            'individual_groups' => $individual_groups,
103            'title'             => $title,
104            'tree'              => $tree,
105        ]);
106    }
107}
108