xref: /webtrees/app/Statistics/Google/ChartNoChildrenFamilies.php (revision 4a52049613a3fd380896a97f74b94d45ccccc97c)
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\AbstractGoogle;
22use Fisharebest\Webtrees\Statistics\Helper\Century;
23use Fisharebest\Webtrees\Tree;
24use Illuminate\Database\Capsule\Manager as DB;
25use Illuminate\Database\Query\JoinClause;
26
27/**
28 *
29 */
30class ChartNoChildrenFamilies extends AbstractGoogle
31{
32    /**
33     * @var Century
34     */
35    private $centuryHelper;
36
37    /**
38     * Constructor.
39     *
40     * @param Tree $tree
41     */
42    public function __construct(Tree $tree)
43    {
44        parent::__construct($tree);
45
46        $this->centuryHelper = new Century();
47    }
48
49    /**
50     * Returns the related database records.
51     *
52     * @param int $year1
53     * @param int $year2
54     *
55     * @return \stdClass[]
56     */
57    private function queryRecords(int $year1, int $year2): array
58    {
59        $query = DB::table('families')
60            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
61            ->selectRaw('COUNT(*) AS total')
62            ->join('dates', function (JoinClause $join) {
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 += $record->total;
105
106            $data[] = [
107                $this->centuryHelper->centuryName((int) $record->century),
108                $record->total
109            ];
110        }
111
112        if ($total) {
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                'title' => I18N::translate('Century'),
131            ],
132            'colors' => [
133                '#84beff'
134            ],
135        ];
136
137        return view(
138            'statistics/other/charts/column',
139            [
140                'data'          => $data,
141                'chart_options' => $chart_options,
142                'chart_title'   => $chart_title,
143            ]
144        );
145    }
146}
147