xref: /webtrees/app/Statistics/Google/ChartNoChildrenFamilies.php (revision 15c4f62c40de0c794536302daf7d5ec0548a1973)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Statistics\Service\CenturyService;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Query\JoinClause;
27
28use function view;
29
30/**
31 * A chart showing the number of families with no children by century.
32 */
33class ChartNoChildrenFamilies
34{
35    private Tree $tree;
36
37    private CenturyService $century_service;
38
39    /**
40     * @param CenturyService $century_service
41     * @param Tree           $tree
42     */
43    public function __construct(CenturyService $century_service, Tree $tree)
44    {
45        $this->century_service = $century_service;
46        $this->tree            = $tree;
47    }
48
49    /**
50     * Returns the related database records.
51     *
52     * @param int $year1
53     * @param int $year2
54     *
55     * @return array<object>
56     */
57    private function queryRecords(int $year1, int $year2): array
58    {
59        $query = DB::table('families')
60            ->selectRaw('ROUND((d_year + 49) / 100, 0) AS century')
61            ->selectRaw('COUNT(*) AS total')
62            ->join('dates', static function (JoinClause $join): void {
63                $join->on('d_file', '=', 'f_file')
64                    ->on('d_gid', '=', 'f_id');
65            })
66            ->where('f_file', '=', $this->tree->id())
67            ->where('f_numchil', '=', 0)
68            ->where('d_fact', '=', 'MARR')
69            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
70            ->groupBy(['century'])
71            ->orderBy('century');
72
73        if ($year1 >= 0 && $year2 >= 0) {
74            $query->whereBetween('d_year', [$year1, $year2]);
75        }
76
77        return $query->get()->all();
78    }
79
80    /**
81     * Create a chart of children with no families.
82     *
83     * @param int $no_child_fam The number of families with no children
84     * @param int $year1
85     * @param int $year2
86     *
87     * @return string
88     */
89    public function chartNoChildrenFamilies(
90        int $no_child_fam,
91        int $year1 = -1,
92        int $year2 = -1
93    ): string {
94        $data = [
95            [
96                I18N::translate('Century'),
97                I18N::translate('Total')
98            ]
99        ];
100
101        $total = 0;
102
103        foreach ($this->queryRecords($year1, $year2) as $record) {
104            $total += (int) $record->total;
105
106            $data[] = [
107                $this->century_service->centuryName((int) $record->century),
108                (int) $record->total
109            ];
110        }
111
112        if ($total > 0) {
113            $data[] = [
114                I18N::translateContext('unknown century', 'Unknown'),
115                $no_child_fam - $total,
116            ];
117        }
118
119        $chart_title   = I18N::translate('Number of families without children');
120        $chart_options = [
121            'title' => $chart_title,
122            'subtitle' => '',
123            'legend' => [
124                'position'  => 'none',
125            ],
126            'vAxis' => [
127                'title' => I18N::translate('Total families'),
128            ],
129            'hAxis' => [
130                'showTextEvery' => 1,
131                'slantedText'   => false,
132                'title'         => I18N::translate('Century'),
133            ],
134            'colors' => [
135                '#84beff'
136            ],
137        ];
138
139        return view('statistics/other/charts/column', [
140            'data'          => $data,
141            'chart_options' => $chart_options,
142            'chart_title'   => $chart_title,
143            'language'      => I18N::languageTag(),
144        ]);
145    }
146}
147