xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 2c6f1bd538f46b93645991518398bb087011cb42)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
218add1155SRico Sonntag
226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
23ef475b14SGreg Roachuse Fisharebest\Webtrees\Family;
248add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
25f0c88a96SGreg Roachuse Fisharebest\Webtrees\Registry;
2693ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
2793ccd686SRico Sonntaguse Fisharebest\Webtrees\Tree;
288add1155SRico Sonntag
2971378461SGreg Roachuse function count;
3034b20f29SGreg Roachuse function htmlspecialchars_decode;
3134b20f29SGreg Roachuse function strip_tags;
3234b20f29SGreg Roachuse function view;
3371378461SGreg Roach
348add1155SRico Sonntag/**
3593ccd686SRico Sonntag * A chart showing the largest families (Families with most children).
368add1155SRico Sonntag */
3793ccd686SRico Sonntagclass ChartFamilyLargest
388add1155SRico Sonntag{
3934b20f29SGreg Roach    private Tree $tree;
4093ccd686SRico Sonntag
4134b20f29SGreg Roach    private ColorService $color_service;
4293ccd686SRico Sonntag
4393ccd686SRico Sonntag    /**
44f78da678SGreg Roach     * @param ColorService $color_service
4593ccd686SRico Sonntag     * @param Tree         $tree
4693ccd686SRico Sonntag     */
47f78da678SGreg Roach    public function __construct(ColorService $color_service, Tree $tree)
4893ccd686SRico Sonntag    {
4993ccd686SRico Sonntag        $this->tree          = $tree;
50f78da678SGreg Roach        $this->color_service = $color_service;
5193ccd686SRico Sonntag    }
5293ccd686SRico Sonntag
5393ccd686SRico Sonntag    /**
548add1155SRico Sonntag     * Returns the related database records.
558add1155SRico Sonntag     *
568add1155SRico Sonntag     * @param int $total
578add1155SRico Sonntag     *
58f70bcff5SGreg Roach     * @return array<object>
598add1155SRico Sonntag     */
608add1155SRico Sonntag    private function queryRecords(int $total): array
618add1155SRico Sonntag    {
628add1155SRico Sonntag        $query = DB::table('families')
6388de55fdSRico Sonntag            ->select(['f_numchil AS total', 'f_id AS id'])
648add1155SRico Sonntag            ->where('f_file', '=', $this->tree->id())
6588de55fdSRico Sonntag            ->orderBy('total', 'desc')
668add1155SRico Sonntag            ->limit($total);
678add1155SRico Sonntag
688add1155SRico Sonntag        return $query->get()->all();
698add1155SRico Sonntag    }
708add1155SRico Sonntag
718add1155SRico Sonntag    /**
728add1155SRico Sonntag     * Create a chart of the largest families.
738add1155SRico Sonntag     *
748add1155SRico Sonntag     * @param string|null $color_from
758add1155SRico Sonntag     * @param string|null $color_to
768add1155SRico Sonntag     * @param int         $total
778add1155SRico Sonntag     *
788add1155SRico Sonntag     * @return string
798add1155SRico Sonntag     */
808add1155SRico Sonntag    public function chartLargestFamilies(
81*2c6f1bd5SGreg Roach        string|null $color_from = null,
82*2c6f1bd5SGreg Roach        string|null $color_to = null,
838add1155SRico Sonntag        int $total = 10
848add1155SRico Sonntag    ): string {
853529c469SGreg Roach        $color_from ??= 'ffffff';
863529c469SGreg Roach        $color_to ??= '84beff';
878add1155SRico Sonntag
8888de55fdSRico Sonntag        $data = [
8988de55fdSRico Sonntag            [
9088de55fdSRico Sonntag                I18N::translate('Type'),
9188de55fdSRico Sonntag                I18N::translate('Total')
9288de55fdSRico Sonntag            ],
9388de55fdSRico Sonntag        ];
948add1155SRico Sonntag
9588de55fdSRico Sonntag        foreach ($this->queryRecords($total) as $record) {
966b9cb339SGreg Roach            $family = Registry::familyFactory()->make($record->id, $this->tree);
978add1155SRico Sonntag
98ef475b14SGreg Roach            if ($family instanceof Family && $family->canShow()) {
9988de55fdSRico Sonntag                $data[] = [
10039ca88baSGreg Roach                    htmlspecialchars_decode(strip_tags($family->fullName())),
10188de55fdSRico Sonntag                    $record->total
10288de55fdSRico Sonntag                ];
1038add1155SRico Sonntag            }
1048add1155SRico Sonntag        }
1058add1155SRico Sonntag
1066ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
1078add1155SRico Sonntag
10890a2f718SGreg Roach        return view('statistics/other/charts/pie', [
10988de55fdSRico Sonntag            'title'    => I18N::translate('Largest families'),
11088de55fdSRico Sonntag            'data'     => $data,
11188de55fdSRico Sonntag            'colors'   => $colors,
11265cf5706SGreg Roach            'language' => I18N::languageTag(),
11390a2f718SGreg Roach        ]);
1148add1155SRico Sonntag    }
1158add1155SRico Sonntag}
116