xref: /webtrees/app/Http/RequestHandlers/UnconnectedPage.php (revision 29a51599e39417638370fe4c2c4d77201726c9ff)
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 = DB::table('individuals')
70            ->where('i_file', '=', $tree->id())
71            ->pluck('i_id')
72            ->mapWithKeys(static function (string $xref): array {
73                return [$xref => []];
74            })
75            ->all();
76
77        foreach ($rows as $row) {
78            $graph[$row->l_from][$row->l_to] = 1;
79            $graph[$row->l_to][$row->l_from] = 1;
80        }
81
82        $algorithm  = new ConnectedComponent($graph);
83        $components = $algorithm->findConnectedComponents();
84        $root       = $tree->significantIndividual($user);
85        $xref       = $root->xref();
86
87        /** @var Individual[][] */
88        $individual_groups = [];
89
90        foreach ($components as $component) {
91            if (!in_array($xref, $component, true)) {
92                $individuals = [];
93                foreach ($component as $xref) {
94                    $individuals[] = Individual::getInstance($xref, $tree);
95                }
96                // The database query may return pending additions/deletions, which may not exist.
97                $individual_groups[] = array_filter($individuals);
98            }
99        }
100
101        $title = I18N::translate('Find unrelated individuals') . ' — ' . e($tree->title());
102
103        $this->layout = 'layouts/administration';
104
105        return $this->viewResponse('admin/trees-unconnected', [
106            'associates'        => $associates,
107            'root'              => $root,
108            'individual_groups' => $individual_groups,
109            'title'             => $title,
110            'tree'              => $tree,
111        ]);
112    }
113}
114