Completed
Push — master ( 56eca4...2839b8 )
by Greg
06:20
created

UnconnectedPage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 67
rs 10
c 1
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 58 6
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Algorithm\ConnectedComponent;
23
use Fisharebest\Webtrees\Http\ViewResponseTrait;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Individual;
26
use Fisharebest\Webtrees\Tree;
27
use Fisharebest\Webtrees\User;
28
use Illuminate\Database\Capsule\Manager as DB;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
use function assert;
34
35
/**
36
 * Find groups of unrelated individuals.
37
 */
38
class 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