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