xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 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
226b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
238add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
2593ccd686SRico Sonntaguse Fisharebest\Webtrees\Tree;
268add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
278add1155SRico Sonntag
2871378461SGreg Roachuse function count;
2934b20f29SGreg Roachuse function htmlspecialchars_decode;
3034b20f29SGreg Roachuse function strip_tags;
3134b20f29SGreg Roachuse function view;
3271378461SGreg Roach
338add1155SRico Sonntag/**
3493ccd686SRico Sonntag * A chart showing the largest families (Families with most children).
358add1155SRico Sonntag */
3693ccd686SRico Sonntagclass ChartFamilyLargest
378add1155SRico Sonntag{
3834b20f29SGreg Roach    private Tree $tree;
3993ccd686SRico Sonntag
4034b20f29SGreg Roach    private ColorService $color_service;
4193ccd686SRico Sonntag
4293ccd686SRico Sonntag    /**
43f78da678SGreg Roach     * @param ColorService $color_service
4493ccd686SRico Sonntag     * @param Tree         $tree
4593ccd686SRico Sonntag     */
46f78da678SGreg Roach    public function __construct(ColorService $color_service, Tree $tree)
4793ccd686SRico Sonntag    {
4893ccd686SRico Sonntag        $this->tree          = $tree;
49f78da678SGreg Roach        $this->color_service = $color_service;
5093ccd686SRico Sonntag    }
5193ccd686SRico Sonntag
5293ccd686SRico Sonntag    /**
538add1155SRico Sonntag     * Returns the related database records.
548add1155SRico Sonntag     *
558add1155SRico Sonntag     * @param int $total
568add1155SRico Sonntag     *
57f70bcff5SGreg Roach     * @return array<object>
588add1155SRico Sonntag     */
598add1155SRico Sonntag    private function queryRecords(int $total): array
608add1155SRico Sonntag    {
618add1155SRico Sonntag        $query = DB::table('families')
6288de55fdSRico Sonntag            ->select(['f_numchil AS total', 'f_id AS id'])
638add1155SRico Sonntag            ->where('f_file', '=', $this->tree->id())
6488de55fdSRico Sonntag            ->orderBy('total', 'desc')
658add1155SRico Sonntag            ->limit($total);
668add1155SRico Sonntag
678add1155SRico Sonntag        return $query->get()->all();
688add1155SRico Sonntag    }
698add1155SRico Sonntag
708add1155SRico Sonntag    /**
718add1155SRico Sonntag     * Create a chart of the largest families.
728add1155SRico Sonntag     *
738add1155SRico Sonntag     * @param string|null $color_from
748add1155SRico Sonntag     * @param string|null $color_to
758add1155SRico Sonntag     * @param int         $total
768add1155SRico Sonntag     *
778add1155SRico Sonntag     * @return string
788add1155SRico Sonntag     */
798add1155SRico Sonntag    public function chartLargestFamilies(
808add1155SRico Sonntag        string $color_from = null,
818add1155SRico Sonntag        string $color_to = null,
828add1155SRico Sonntag        int $total = 10
838add1155SRico Sonntag    ): string {
8434b20f29SGreg Roach        $color_from = $color_from ?? 'ffffff';
8534b20f29SGreg Roach        $color_to   = $color_to ?? '84beff';
868add1155SRico Sonntag
8788de55fdSRico Sonntag        $data = [
8888de55fdSRico Sonntag            [
8988de55fdSRico Sonntag                I18N::translate('Type'),
9088de55fdSRico Sonntag                I18N::translate('Total')
9188de55fdSRico Sonntag            ],
9288de55fdSRico Sonntag        ];
938add1155SRico Sonntag
9488de55fdSRico Sonntag        foreach ($this->queryRecords($total) as $record) {
956b9cb339SGreg Roach            $family = Registry::familyFactory()->make($record->id, $this->tree);
968add1155SRico Sonntag
978add1155SRico Sonntag            if ($family && $family->canShow()) {
9888de55fdSRico Sonntag                $data[] = [
9939ca88baSGreg Roach                    htmlspecialchars_decode(strip_tags($family->fullName())),
10088de55fdSRico Sonntag                    $record->total
10188de55fdSRico Sonntag                ];
1028add1155SRico Sonntag            }
1038add1155SRico Sonntag        }
1048add1155SRico Sonntag
1056ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
1068add1155SRico Sonntag
10790a2f718SGreg Roach        return view('statistics/other/charts/pie', [
10888de55fdSRico Sonntag            'title'    => I18N::translate('Largest families'),
10988de55fdSRico Sonntag            'data'     => $data,
11088de55fdSRico Sonntag            'colors'   => $colors,
11165cf5706SGreg Roach            'language' => I18N::languageTag(),
11290a2f718SGreg Roach        ]);
1138add1155SRico Sonntag    }
1148add1155SRico Sonntag}
115