xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
18add1155SRico Sonntag<?php
2*3976b470SGreg 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 */
178add1155SRico Sonntagdeclare(strict_types=1);
188add1155SRico Sonntag
198add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
208add1155SRico Sonntag
216ccdf4f0SGreg Roachuse function count;
22*3976b470SGreg Roach
238add1155SRico Sonntaguse Fisharebest\Webtrees\Family;
248add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2593ccd686SRico Sonntaguse Fisharebest\Webtrees\Module\ModuleThemeInterface;
2693ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
2793ccd686SRico Sonntaguse Fisharebest\Webtrees\Tree;
288add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
296ccdf4f0SGreg Roachuse stdClass;
308add1155SRico Sonntag
318add1155SRico Sonntag/**
3293ccd686SRico Sonntag * A chart showing the largest families (Families with most children).
338add1155SRico Sonntag */
3493ccd686SRico Sonntagclass ChartFamilyLargest
358add1155SRico Sonntag{
368add1155SRico Sonntag    /**
3793ccd686SRico Sonntag     * @var Tree
3893ccd686SRico Sonntag     */
3993ccd686SRico Sonntag    private $tree;
4093ccd686SRico Sonntag
4193ccd686SRico Sonntag    /**
4293ccd686SRico Sonntag     * @var ModuleThemeInterface
4393ccd686SRico Sonntag     */
4493ccd686SRico Sonntag    private $theme;
4593ccd686SRico Sonntag
4693ccd686SRico Sonntag    /**
4793ccd686SRico Sonntag     * @var ColorService
4893ccd686SRico Sonntag     */
4993ccd686SRico Sonntag    private $color_service;
5093ccd686SRico Sonntag
5193ccd686SRico Sonntag    /**
5293ccd686SRico Sonntag     * Constructor.
5393ccd686SRico Sonntag     *
5493ccd686SRico Sonntag     * @param Tree $tree
5593ccd686SRico Sonntag     */
5693ccd686SRico Sonntag    public function __construct(Tree $tree)
5793ccd686SRico Sonntag    {
5893ccd686SRico Sonntag        $this->tree          = $tree;
59cab242e7SGreg Roach        $this->theme         = app(ModuleThemeInterface::class);
6093ccd686SRico Sonntag        $this->color_service = new ColorService();
6193ccd686SRico Sonntag    }
6293ccd686SRico Sonntag
6393ccd686SRico Sonntag    /**
648add1155SRico Sonntag     * Returns the related database records.
658add1155SRico Sonntag     *
668add1155SRico Sonntag     * @param int $total
678add1155SRico Sonntag     *
686ccdf4f0SGreg Roach     * @return stdClass[]
698add1155SRico Sonntag     */
708add1155SRico Sonntag    private function queryRecords(int $total): array
718add1155SRico Sonntag    {
728add1155SRico Sonntag        $query = DB::table('families')
7388de55fdSRico Sonntag            ->select(['f_numchil AS total', 'f_id AS id'])
748add1155SRico Sonntag            ->where('f_file', '=', $this->tree->id())
7588de55fdSRico Sonntag            ->orderBy('total', 'desc')
768add1155SRico Sonntag            ->limit($total);
778add1155SRico Sonntag
788add1155SRico Sonntag        return $query->get()->all();
798add1155SRico Sonntag    }
808add1155SRico Sonntag
818add1155SRico Sonntag    /**
828add1155SRico Sonntag     * Create a chart of the largest families.
838add1155SRico Sonntag     *
848add1155SRico Sonntag     * @param string|null $color_from
858add1155SRico Sonntag     * @param string|null $color_to
868add1155SRico Sonntag     * @param int         $total
878add1155SRico Sonntag     *
888add1155SRico Sonntag     * @return string
898add1155SRico Sonntag     */
908add1155SRico Sonntag    public function chartLargestFamilies(
918add1155SRico Sonntag        string $color_from = null,
928add1155SRico Sonntag        string $color_to = null,
938add1155SRico Sonntag        int $total = 10
948add1155SRico Sonntag    ): string {
9588de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
9688de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
978add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
988add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
998add1155SRico Sonntag
10088de55fdSRico Sonntag        $data = [
10188de55fdSRico Sonntag            [
10288de55fdSRico Sonntag                I18N::translate('Type'),
10388de55fdSRico Sonntag                I18N::translate('Total')
10488de55fdSRico Sonntag            ],
10588de55fdSRico Sonntag        ];
1068add1155SRico Sonntag
10788de55fdSRico Sonntag        foreach ($this->queryRecords($total) as $record) {
10888de55fdSRico Sonntag            $family = Family::getInstance($record->id, $this->tree);
1098add1155SRico Sonntag
1108add1155SRico Sonntag            if ($family && $family->canShow()) {
11188de55fdSRico Sonntag                $data[] = [
11239ca88baSGreg Roach                    htmlspecialchars_decode(strip_tags($family->fullName())),
11388de55fdSRico Sonntag                    $record->total
11488de55fdSRico Sonntag                ];
1158add1155SRico Sonntag            }
1168add1155SRico Sonntag        }
1178add1155SRico Sonntag
1186ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
1198add1155SRico Sonntag
1208add1155SRico Sonntag        return view(
12188de55fdSRico Sonntag            'statistics/other/charts/pie',
1228add1155SRico Sonntag            [
12388de55fdSRico Sonntag                'title'       => I18N::translate('Largest families'),
12488de55fdSRico Sonntag                'data'        => $data,
12588de55fdSRico Sonntag                'colors'      => $colors,
1268add1155SRico Sonntag            ]
1278add1155SRico Sonntag        );
1288add1155SRico Sonntag    }
1298add1155SRico Sonntag}
130