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