Passed
Push — 2.1 ( 6a6343...26eef6 )
by Greg
16:09 queued 07:00
created

ChartFamilyLargest::queryRecords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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 <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Statistics\Google;
21
22
use Fisharebest\Webtrees\Family;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Registry;
25
use Fisharebest\Webtrees\Statistics\Service\ColorService;
26
use Fisharebest\Webtrees\Tree;
27
use Illuminate\Database\Capsule\Manager as DB;
28
29
use function count;
30
use function htmlspecialchars_decode;
31
use function strip_tags;
32
use function view;
33
34
/**
35
 * A chart showing the largest families (Families with most children).
36
 */
37
class ChartFamilyLargest
38
{
39
    private Tree $tree;
40
41
    private ColorService $color_service;
42
43
    public function __construct(ColorService $color_service, Tree $tree)
44
    {
45
        $this->tree          = $tree;
46
        $this->color_service = $color_service;
47
    }
48
49
    public function chartLargestFamilies(
50
        ?string $color_from = null,
51
        ?string $color_to = null,
52
        int $total = 10
53
    ): string {
54
        $color_from ??= 'ffffff';
55
        $color_to ??= '84beff';
56
57
        $data = [
58
            [
59
                I18N::translate('Type'),
60
                I18N::translate('Total')
61
            ],
62
        ];
63
64
        $records = DB::table('families')
65
            ->select(['f_numchil AS total', 'f_id AS id'])
66
            ->where('f_file', '=', $this->tree->id())
67
            ->orderBy('total', 'desc')
68
            ->limit($total)
69
            ->get()
70
            ->all();
71
72
        foreach ($records as $record) {
73
            $family = Registry::familyFactory()->make($record->id, $this->tree);
74
75
            if ($family instanceof Family && $family->canShow()) {
76
                $data[] = [
77
                    htmlspecialchars_decode(strip_tags($family->fullName())),
78
                    $record->total
79
                ];
80
            }
81
        }
82
83
        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
84
85
        return view('statistics/other/charts/pie', [
86
            'title'    => I18N::translate('Largest families'),
87
            'data'     => $data,
88
            'colors'   => $colors,
89
            'language' => I18N::languageTag(),
90
        ]);
91
    }
92
}
93