xref: /webtrees/app/Statistics/Google/ChartDivorce.php (revision 8136679edbff8170fb92bd6e8dd8df8eb645bb1a)
18add1155SRico Sonntag<?php
28add1155SRico Sonntag/**
38add1155SRico Sonntag * webtrees: online genealogy
48add1155SRico Sonntag * Copyright (C) 2018 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\I18N;
21*8136679eSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
228add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\AbstractGoogle;
238add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Helper\Century;
248add1155SRico Sonntaguse Fisharebest\Webtrees\Theme;
258add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
268add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
278add1155SRico Sonntag
288add1155SRico Sonntag/**
298add1155SRico Sonntag *
308add1155SRico Sonntag */
318add1155SRico Sonntagclass ChartDivorce extends AbstractGoogle
328add1155SRico Sonntag{
338add1155SRico Sonntag    /**
348add1155SRico Sonntag     * @var Tree
358add1155SRico Sonntag     */
368add1155SRico Sonntag    private $tree;
378add1155SRico Sonntag
388add1155SRico Sonntag    /**
398add1155SRico Sonntag     * @var Century
408add1155SRico Sonntag     */
418add1155SRico Sonntag    private $centuryHelper;
428add1155SRico Sonntag
438add1155SRico Sonntag    /**
448add1155SRico Sonntag     * Constructor.
458add1155SRico Sonntag     *
468add1155SRico Sonntag     * @param Tree $tree
478add1155SRico Sonntag     */
488add1155SRico Sonntag    public function __construct(Tree $tree)
498add1155SRico Sonntag    {
508add1155SRico Sonntag        $this->tree          = $tree;
518add1155SRico Sonntag        $this->centuryHelper = new Century();
528add1155SRico Sonntag    }
538add1155SRico Sonntag
548add1155SRico Sonntag    /**
558add1155SRico Sonntag     * Returns the related database records.
568add1155SRico Sonntag     *
578add1155SRico Sonntag     * @return \stdClass[]
588add1155SRico Sonntag     */
598add1155SRico Sonntag    private function queryRecords(): array
608add1155SRico Sonntag    {
618add1155SRico Sonntag        $query = DB::table('dates')
628add1155SRico Sonntag            ->selectRaw('FLOOR(d_year / 100 + 1) AS century')
638add1155SRico Sonntag            ->selectRaw('COUNT(*) AS total')
648add1155SRico Sonntag            ->where('d_file', '=', $this->tree->id())
658add1155SRico Sonntag            ->where('d_year', '<>', 0)
668add1155SRico Sonntag            ->where('d_fact', '=', 'DIV')
678add1155SRico Sonntag            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
688add1155SRico Sonntag            ->groupBy(['century'])
698add1155SRico Sonntag            ->orderBy('century');
708add1155SRico Sonntag
718add1155SRico Sonntag        return $query->get()->all();
728add1155SRico Sonntag    }
738add1155SRico Sonntag
748add1155SRico Sonntag    /**
758add1155SRico Sonntag     * General query on divorces.
768add1155SRico Sonntag     *
778add1155SRico Sonntag     * @param string|null $size
788add1155SRico Sonntag     * @param string|null $color_from
798add1155SRico Sonntag     * @param string|null $color_to
808add1155SRico Sonntag     *
818add1155SRico Sonntag     * @return string
828add1155SRico Sonntag     */
838add1155SRico Sonntag    public function chartDivorce(string $size = null, string $color_from = null, string $color_to = null): string
848add1155SRico Sonntag    {
85*8136679eSGreg Roach        $chart_color1 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-no-values');
86*8136679eSGreg Roach        $chart_color2 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-high-values');
87*8136679eSGreg Roach        $chart_x      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-x');
88*8136679eSGreg Roach        $chart_y      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y');
898add1155SRico Sonntag
908add1155SRico Sonntag        $size       = $size ?? ($chart_x . 'x' . $chart_y);
918add1155SRico Sonntag        $color_from = $color_from ?? $chart_color1;
928add1155SRico Sonntag        $color_to   = $color_to ?? $chart_color2;
938add1155SRico Sonntag
948add1155SRico Sonntag        $sizes = explode('x', $size);
958add1155SRico Sonntag        $tot   = 0;
968add1155SRico Sonntag        $rows  = $this->queryRecords();
978add1155SRico Sonntag
988add1155SRico Sonntag        foreach ($rows as $values) {
998add1155SRico Sonntag            $values->total = (int) $values->total;
1008add1155SRico Sonntag            $tot += $values->total;
1018add1155SRico Sonntag        }
1028add1155SRico Sonntag        // Beware divide by zero
1038add1155SRico Sonntag        if ($tot === 0) {
1048add1155SRico Sonntag            return '';
1058add1155SRico Sonntag        }
1068add1155SRico Sonntag        $centuries = '';
1078add1155SRico Sonntag        $counts    = [];
1088add1155SRico Sonntag
1098add1155SRico Sonntag        foreach ($rows as $values) {
1108add1155SRico Sonntag            $counts[] = intdiv(100 * $values->total, $tot);
1118add1155SRico Sonntag            $centuries .= $this->centuryHelper->centuryName((int) $values->century) . ' - ' . I18N::number($values->total) . '|';
1128add1155SRico Sonntag        }
1138add1155SRico Sonntag
1148add1155SRico Sonntag        $chd    = $this->arrayToExtendedEncoding($counts);
1158add1155SRico Sonntag        $chl    = substr($centuries, 0, -1);
1168add1155SRico Sonntag        $colors = [$color_from, $color_to];
1178add1155SRico Sonntag
1188add1155SRico Sonntag        return view(
1198add1155SRico Sonntag            'statistics/other/chart-google',
1208add1155SRico Sonntag            [
1218add1155SRico Sonntag                'chart_title' => I18N::translate('Divorces by century'),
1228add1155SRico Sonntag                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
1238add1155SRico Sonntag                'sizes'       => $sizes,
1248add1155SRico Sonntag            ]
1258add1155SRico Sonntag        );
1268add1155SRico Sonntag    }
1278add1155SRico Sonntag}
128