xref: /webtrees/app/Http/RequestHandlers/UnconnectedPage.php (revision b5961194694c0b5b2dc4269689207eb972e3b20c)
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        $aliases    = (bool) ($request->getQueryParams()['aliases'] ?? false);
56        $associates = (bool) ($request->getQueryParams()['associates'] ?? false);
57
58        // Connect individuals using these links.
59        $links = ['FAMS', 'FAMC'];
60
61        if ($aliases) {
62            $links[] = 'ALIA';
63        }
64
65        if ($associates) {
66            $links[] = 'ASSO';
67            $links[] = '_ASSO';
68        }
69
70        $rows = DB::table('link')
71            ->where('l_file', '=', $tree->id())
72            ->whereIn('l_type', $links)
73            ->select(['l_from', 'l_to'])
74            ->get();
75
76        $graph = DB::table('individuals')
77            ->where('i_file', '=', $tree->id())
78            ->pluck('i_id')
79            ->mapWithKeys(static function (string $xref): array {
80                return [$xref => []];
81            })
82            ->all();
83
84        foreach ($rows as $row) {
85            $graph[$row->l_from][$row->l_to] = 1;
86            $graph[$row->l_to][$row->l_from] = 1;
87        }
88
89        $algorithm  = new ConnectedComponent($graph);
90        $components = $algorithm->findConnectedComponents();
91        $root       = $tree->significantIndividual($user);
92        $xref       = $root->xref();
93
94        /** @var Individual[][] */
95        $individual_groups = [];
96
97        foreach ($components as $component) {
98            if (!in_array($xref, $component, true)) {
99                $individuals = [];
100                foreach ($component as $xref) {
101                    $individuals[] = Individual::getInstance($xref, $tree);
102                }
103                // The database query may return pending additions/deletions, which may not exist.
104                $individual_groups[] = array_filter($individuals);
105            }
106        }
107
108        $title = I18N::translate('Find unrelated individuals') . ' — ' . e($tree->title());
109
110        $this->layout = 'layouts/administration';
111
112        return $this->viewResponse('admin/trees-unconnected', [
113            'aliases'           => $aliases,
114            'associates'        => $associates,
115            'root'              => $root,
116            'individual_groups' => $individual_groups,
117            'title'             => $title,
118            'tree'              => $tree,
119        ]);
120    }
121}
122