xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 242a78626998a767db1568b24919e94ae4b38ac3)
18add1155SRico Sonntag<?php
28add1155SRico Sonntag/**
38add1155SRico Sonntag * webtrees: online genealogy
4*242a7862SGreg Roach * Copyright (C) 2019 webtrees development team
58add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
68add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
78add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
88add1155SRico Sonntag * (at your option) any later version.
98add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
108add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
118add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128add1155SRico Sonntag * GNU General Public License for more details.
138add1155SRico Sonntag * You should have received a copy of the GNU General Public License
148add1155SRico Sonntag * along with this program. If not, see <http://www.gnu.org/licenses/>.
158add1155SRico Sonntag */
168add1155SRico Sonntagdeclare(strict_types=1);
178add1155SRico Sonntag
188add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
198add1155SRico Sonntag
208add1155SRico Sonntaguse Fisharebest\Webtrees\Family;
218add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
228add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\AbstractGoogle;
238add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
248add1155SRico Sonntag
258add1155SRico Sonntag/**
268add1155SRico Sonntag *
278add1155SRico Sonntag */
288add1155SRico Sonntagclass ChartFamilyLargest extends AbstractGoogle
298add1155SRico Sonntag{
308add1155SRico Sonntag    /**
318add1155SRico Sonntag     * Returns the related database records.
328add1155SRico Sonntag     *
338add1155SRico Sonntag     * @param int $total
348add1155SRico Sonntag     *
358add1155SRico Sonntag     * @return \stdClass[]
368add1155SRico Sonntag     */
378add1155SRico Sonntag    private function queryRecords(int $total): array
388add1155SRico Sonntag    {
398add1155SRico Sonntag        $query = DB::table('families')
4088de55fdSRico Sonntag            ->select(['f_numchil AS total', 'f_id AS id'])
418add1155SRico Sonntag            ->where('f_file', '=', $this->tree->id())
4288de55fdSRico Sonntag            ->orderBy('total', 'desc')
438add1155SRico Sonntag            ->limit($total);
448add1155SRico Sonntag
458add1155SRico Sonntag        return $query->get()->all();
468add1155SRico Sonntag    }
478add1155SRico Sonntag
488add1155SRico Sonntag    /**
498add1155SRico Sonntag     * Create a chart of the largest families.
508add1155SRico Sonntag     *
518add1155SRico Sonntag     * @param string|null $color_from
528add1155SRico Sonntag     * @param string|null $color_to
538add1155SRico Sonntag     * @param int         $total
548add1155SRico Sonntag     *
558add1155SRico Sonntag     * @return string
568add1155SRico Sonntag     */
578add1155SRico Sonntag    public function chartLargestFamilies(
588add1155SRico Sonntag        string $color_from = null,
598add1155SRico Sonntag        string $color_to   = null,
608add1155SRico Sonntag        int    $total      = 10
618add1155SRico Sonntag    ): string {
6288de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
6388de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
648add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
658add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
668add1155SRico Sonntag
6788de55fdSRico Sonntag        $data = [
6888de55fdSRico Sonntag            [
6988de55fdSRico Sonntag                I18N::translate('Type'),
7088de55fdSRico Sonntag                I18N::translate('Total')
7188de55fdSRico Sonntag            ],
7288de55fdSRico Sonntag        ];
738add1155SRico Sonntag
7488de55fdSRico Sonntag        foreach ($this->queryRecords($total) as $record) {
7588de55fdSRico Sonntag            $family = Family::getInstance($record->id, $this->tree);
768add1155SRico Sonntag
778add1155SRico Sonntag            if ($family && $family->canShow()) {
7888de55fdSRico Sonntag                $data[] = [
7988de55fdSRico Sonntag                    htmlspecialchars_decode(strip_tags($family->getFullName())),
8088de55fdSRico Sonntag                    $record->total
8188de55fdSRico Sonntag                ];
828add1155SRico Sonntag            }
838add1155SRico Sonntag        }
848add1155SRico Sonntag
8588de55fdSRico Sonntag        $colors = $this->interpolateRgb($color_from, $color_to, \count($data) - 1);
868add1155SRico Sonntag
878add1155SRico Sonntag        return view(
8888de55fdSRico Sonntag            'statistics/other/charts/pie',
898add1155SRico Sonntag            [
9088de55fdSRico Sonntag                'title'       => I18N::translate('Largest families'),
9188de55fdSRico Sonntag                'data'        => $data,
9288de55fdSRico Sonntag                'colors'      => $colors,
938add1155SRico Sonntag            ]
948add1155SRico Sonntag        );
958add1155SRico Sonntag    }
968add1155SRico Sonntag}
97