xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 65cf57062fc712114f930335533210fd95170cea)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5242a7862SGreg Roach * Copyright (C) 2019 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
158add1155SRico Sonntag * along with this program. If not, see <http://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
218add1155SRico Sonntag
228add1155SRico Sonntaguse Fisharebest\Webtrees\Family;
238add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Module\ModuleThemeInterface;
2593ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
2693ccd686SRico Sonntaguse Fisharebest\Webtrees\Tree;
278add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
286ccdf4f0SGreg Roachuse stdClass;
298add1155SRico Sonntag
3090a2f718SGreg Roachuse function app;
3171378461SGreg Roachuse function count;
3271378461SGreg Roach
338add1155SRico Sonntag/**
3493ccd686SRico Sonntag * A chart showing the largest families (Families with most children).
358add1155SRico Sonntag */
3693ccd686SRico Sonntagclass ChartFamilyLargest
378add1155SRico Sonntag{
388add1155SRico Sonntag    /**
3993ccd686SRico Sonntag     * @var Tree
4093ccd686SRico Sonntag     */
4193ccd686SRico Sonntag    private $tree;
4293ccd686SRico Sonntag
4393ccd686SRico Sonntag    /**
4493ccd686SRico Sonntag     * @var ModuleThemeInterface
4593ccd686SRico Sonntag     */
4693ccd686SRico Sonntag    private $theme;
4793ccd686SRico Sonntag
4893ccd686SRico Sonntag    /**
4993ccd686SRico Sonntag     * @var ColorService
5093ccd686SRico Sonntag     */
5193ccd686SRico Sonntag    private $color_service;
5293ccd686SRico Sonntag
5393ccd686SRico Sonntag    /**
5493ccd686SRico Sonntag     * Constructor.
5593ccd686SRico Sonntag     *
5693ccd686SRico Sonntag     * @param Tree $tree
5793ccd686SRico Sonntag     */
5893ccd686SRico Sonntag    public function __construct(Tree $tree)
5993ccd686SRico Sonntag    {
6093ccd686SRico Sonntag        $this->tree          = $tree;
61cab242e7SGreg Roach        $this->theme         = app(ModuleThemeInterface::class);
6293ccd686SRico Sonntag        $this->color_service = new ColorService();
6393ccd686SRico Sonntag    }
6493ccd686SRico Sonntag
6593ccd686SRico Sonntag    /**
668add1155SRico Sonntag     * Returns the related database records.
678add1155SRico Sonntag     *
688add1155SRico Sonntag     * @param int $total
698add1155SRico Sonntag     *
706ccdf4f0SGreg Roach     * @return stdClass[]
718add1155SRico Sonntag     */
728add1155SRico Sonntag    private function queryRecords(int $total): array
738add1155SRico Sonntag    {
748add1155SRico Sonntag        $query = DB::table('families')
7588de55fdSRico Sonntag            ->select(['f_numchil AS total', 'f_id AS id'])
768add1155SRico Sonntag            ->where('f_file', '=', $this->tree->id())
7788de55fdSRico Sonntag            ->orderBy('total', 'desc')
788add1155SRico Sonntag            ->limit($total);
798add1155SRico Sonntag
808add1155SRico Sonntag        return $query->get()->all();
818add1155SRico Sonntag    }
828add1155SRico Sonntag
838add1155SRico Sonntag    /**
848add1155SRico Sonntag     * Create a chart of the largest families.
858add1155SRico Sonntag     *
868add1155SRico Sonntag     * @param string|null $color_from
878add1155SRico Sonntag     * @param string|null $color_to
888add1155SRico Sonntag     * @param int         $total
898add1155SRico Sonntag     *
908add1155SRico Sonntag     * @return string
918add1155SRico Sonntag     */
928add1155SRico Sonntag    public function chartLargestFamilies(
938add1155SRico Sonntag        string $color_from = null,
948add1155SRico Sonntag        string $color_to = null,
958add1155SRico Sonntag        int $total = 10
968add1155SRico Sonntag    ): string {
9788de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
9888de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
998add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
1008add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
1018add1155SRico Sonntag
10288de55fdSRico Sonntag        $data = [
10388de55fdSRico Sonntag            [
10488de55fdSRico Sonntag                I18N::translate('Type'),
10588de55fdSRico Sonntag                I18N::translate('Total')
10688de55fdSRico Sonntag            ],
10788de55fdSRico Sonntag        ];
1088add1155SRico Sonntag
10988de55fdSRico Sonntag        foreach ($this->queryRecords($total) as $record) {
11088de55fdSRico Sonntag            $family = Family::getInstance($record->id, $this->tree);
1118add1155SRico Sonntag
1128add1155SRico Sonntag            if ($family && $family->canShow()) {
11388de55fdSRico Sonntag                $data[] = [
11439ca88baSGreg Roach                    htmlspecialchars_decode(strip_tags($family->fullName())),
11588de55fdSRico Sonntag                    $record->total
11688de55fdSRico Sonntag                ];
1178add1155SRico Sonntag            }
1188add1155SRico Sonntag        }
1198add1155SRico Sonntag
1206ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
1218add1155SRico Sonntag
12290a2f718SGreg Roach        return view('statistics/other/charts/pie', [
12388de55fdSRico Sonntag            'title'    => I18N::translate('Largest families'),
12488de55fdSRico Sonntag            'data'     => $data,
12588de55fdSRico Sonntag            'colors'   => $colors,
126*65cf5706SGreg Roach            'language' => I18N::languageTag(),
12790a2f718SGreg Roach        ]);
1288add1155SRico Sonntag    }
1298add1155SRico Sonntag}
130