xref: /webtrees/app/Statistics/Google/ChartNoChildrenFamilies.php (revision 34b20f294deb14f65541b617e67c4eb4ff84d013)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\I18N;
23use Fisharebest\Webtrees\Statistics\Service\CenturyService;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
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     * Constructor.
41     *
42     * @param Tree $tree
43     */
44    public function __construct(Tree $tree)
45    {
46        $this->tree            = $tree;
47        $this->century_service = new CenturyService();
48    }
49
50    /**
51     * Returns the related database records.
52     *
53     * @param int $year1
54     * @param int $year2
55     *
56     * @return array<object>
57     */
58    private function queryRecords(int $year1, int $year2): array
59    {
60        $query = DB::table('families')
61            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
62            ->selectRaw('COUNT(*) AS total')
63            ->join('dates', static function (JoinClause $join): void {
64                $join->on('d_file', '=', 'f_file')
65                    ->on('d_gid', '=', 'f_id');
66            })
67            ->where('f_file', '=', $this->tree->id())
68            ->where('f_numchil', '=', 0)
69            ->where('d_fact', '=', 'MARR')
70            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
71            ->groupBy(['century'])
72            ->orderBy('century');
73
74        if ($year1 >= 0 && $year2 >= 0) {
75            $query->whereBetween('d_year', [$year1, $year2]);
76        }
77
78        return $query->get()->all();
79    }
80
81    /**
82     * Create a chart of children with no families.
83     *
84     * @param int $no_child_fam The number of families with no children
85     * @param int $year1
86     * @param int $year2
87     *
88     * @return string
89     */
90    public function chartNoChildrenFamilies(
91        int $no_child_fam,
92        int $year1 = -1,
93        int $year2 = -1
94    ): string {
95        $data = [
96            [
97                I18N::translate('Century'),
98                I18N::translate('Total')
99            ]
100        ];
101
102        $total = 0;
103
104        foreach ($this->queryRecords($year1, $year2) as $record) {
105            $total += (int) $record->total;
106
107            $data[] = [
108                $this->century_service->centuryName((int) $record->century),
109                (int) $record->total
110            ];
111        }
112
113        if ($total) {
114            $data[] = [
115                I18N::translateContext('unknown century', 'Unknown'),
116                $no_child_fam - $total,
117            ];
118        }
119
120        $chart_title   = I18N::translate('Number of families without children');
121        $chart_options = [
122            'title' => $chart_title,
123            'subtitle' => '',
124            'legend' => [
125                'position'  => 'none',
126            ],
127            'vAxis' => [
128                'title' => I18N::translate('Total families'),
129            ],
130            'hAxis' => [
131                'title' => I18N::translate('Century'),
132            ],
133            'colors' => [
134                '#84beff'
135            ],
136        ];
137
138        return view('statistics/other/charts/column', [
139            'data'          => $data,
140            'chart_options' => $chart_options,
141            'chart_title'   => $chart_title,
142            'language'      => I18N::languageTag(),
143        ]);
144    }
145}
146