xref: /webtrees/app/Http/RequestHandlers/UnconnectedPage.php (revision a091ac74647eab281b25090b737835eeea14ae10)
196716c47SGreg Roach<?php
296716c47SGreg Roach
396716c47SGreg Roach/**
496716c47SGreg Roach * webtrees: online genealogy
5*a091ac74SGreg Roach * Copyright (C) 2020 webtrees development team
696716c47SGreg Roach * This program is free software: you can redistribute it and/or modify
796716c47SGreg Roach * it under the terms of the GNU General Public License as published by
896716c47SGreg Roach * the Free Software Foundation, either version 3 of the License, or
996716c47SGreg Roach * (at your option) any later version.
1096716c47SGreg Roach * This program is distributed in the hope that it will be useful,
1196716c47SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1296716c47SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1396716c47SGreg Roach * GNU General Public License for more details.
1496716c47SGreg Roach * You should have received a copy of the GNU General Public License
1596716c47SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1696716c47SGreg Roach */
1796716c47SGreg Roach
1896716c47SGreg Roachdeclare(strict_types=1);
1996716c47SGreg Roach
2096716c47SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2196716c47SGreg Roach
2296716c47SGreg Roachuse Fisharebest\Algorithm\ConnectedComponent;
23*a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory;
2496716c47SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
2596716c47SGreg Roachuse Fisharebest\Webtrees\I18N;
2696716c47SGreg Roachuse Fisharebest\Webtrees\Individual;
2796716c47SGreg Roachuse Fisharebest\Webtrees\Tree;
2896716c47SGreg Roachuse Fisharebest\Webtrees\User;
2996716c47SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
3096716c47SGreg Roachuse Psr\Http\Message\ResponseInterface;
3196716c47SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3296716c47SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3396716c47SGreg Roach
3496716c47SGreg Roachuse function assert;
3596716c47SGreg Roach
3696716c47SGreg Roach/**
3796716c47SGreg Roach * Find groups of unrelated individuals.
3896716c47SGreg Roach */
3996716c47SGreg Roachclass UnconnectedPage implements RequestHandlerInterface
4096716c47SGreg Roach{
4196716c47SGreg Roach    use ViewResponseTrait;
4296716c47SGreg Roach
4396716c47SGreg Roach    /**
4496716c47SGreg Roach     * @param ServerRequestInterface $request
4596716c47SGreg Roach     *
4696716c47SGreg Roach     * @return ResponseInterface
4796716c47SGreg Roach     */
4896716c47SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
4996716c47SGreg Roach    {
5096716c47SGreg Roach        $tree = $request->getAttribute('tree');
5196716c47SGreg Roach        assert($tree instanceof Tree);
5296716c47SGreg Roach
5396716c47SGreg Roach        $user = $request->getAttribute('user');
5496716c47SGreg Roach        assert($user instanceof User);
5596716c47SGreg Roach
56f925fcc4SGreg Roach        $aliases    = (bool) ($request->getQueryParams()['aliases'] ?? false);
5796716c47SGreg Roach        $associates = (bool) ($request->getQueryParams()['associates'] ?? false);
5896716c47SGreg Roach
59f925fcc4SGreg Roach        // Connect individuals using these links.
6096716c47SGreg Roach        $links = ['FAMS', 'FAMC'];
61f925fcc4SGreg Roach
62f925fcc4SGreg Roach        if ($aliases) {
63f925fcc4SGreg Roach            $links[] = 'ALIA';
64f925fcc4SGreg Roach        }
65f925fcc4SGreg Roach
66f925fcc4SGreg Roach        if ($associates) {
67f925fcc4SGreg Roach            $links[] = 'ASSO';
68f925fcc4SGreg Roach            $links[] = '_ASSO';
6996716c47SGreg Roach        }
7096716c47SGreg Roach
7196716c47SGreg Roach        $rows = DB::table('link')
7296716c47SGreg Roach            ->where('l_file', '=', $tree->id())
7396716c47SGreg Roach            ->whereIn('l_type', $links)
7496716c47SGreg Roach            ->select(['l_from', 'l_to'])
7596716c47SGreg Roach            ->get();
7696716c47SGreg Roach
7712e03a90SGreg Roach        $graph = DB::table('individuals')
7812e03a90SGreg Roach            ->where('i_file', '=', $tree->id())
7912e03a90SGreg Roach            ->pluck('i_id')
8012e03a90SGreg Roach            ->mapWithKeys(static function (string $xref): array {
8112e03a90SGreg Roach                return [$xref => []];
8212e03a90SGreg Roach            })
8312e03a90SGreg Roach            ->all();
8496716c47SGreg Roach
8596716c47SGreg Roach        foreach ($rows as $row) {
8696716c47SGreg Roach            $graph[$row->l_from][$row->l_to] = 1;
8796716c47SGreg Roach            $graph[$row->l_to][$row->l_from] = 1;
8896716c47SGreg Roach        }
8996716c47SGreg Roach
9096716c47SGreg Roach        $algorithm  = new ConnectedComponent($graph);
9196716c47SGreg Roach        $components = $algorithm->findConnectedComponents();
9296716c47SGreg Roach        $root       = $tree->significantIndividual($user);
9396716c47SGreg Roach        $xref       = $root->xref();
9496716c47SGreg Roach
9596716c47SGreg Roach        /** @var Individual[][] */
9696716c47SGreg Roach        $individual_groups = [];
9796716c47SGreg Roach
9896716c47SGreg Roach        foreach ($components as $component) {
9996716c47SGreg Roach            if (!in_array($xref, $component, true)) {
10096716c47SGreg Roach                $individuals = [];
10196716c47SGreg Roach                foreach ($component as $xref) {
102*a091ac74SGreg Roach                    $individuals[] = Factory::individual()->make($xref, $tree);
10396716c47SGreg Roach                }
10496716c47SGreg Roach                // The database query may return pending additions/deletions, which may not exist.
10596716c47SGreg Roach                $individual_groups[] = array_filter($individuals);
10696716c47SGreg Roach            }
10796716c47SGreg Roach        }
10896716c47SGreg Roach
10996716c47SGreg Roach        $title = I18N::translate('Find unrelated individuals') . ' — ' . e($tree->title());
11096716c47SGreg Roach
11196716c47SGreg Roach        $this->layout = 'layouts/administration';
11296716c47SGreg Roach
11396716c47SGreg Roach        return $this->viewResponse('admin/trees-unconnected', [
114f925fcc4SGreg Roach            'aliases'           => $aliases,
11596716c47SGreg Roach            'associates'        => $associates,
11696716c47SGreg Roach            'root'              => $root,
11796716c47SGreg Roach            'individual_groups' => $individual_groups,
11896716c47SGreg Roach            'title'             => $title,
11996716c47SGreg Roach            'tree'              => $tree,
12096716c47SGreg Roach        ]);
12196716c47SGreg Roach    }
12296716c47SGreg Roach}
123