xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 59f2f229057fe08ec1b09bd435699190641eed6a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Database;
20use Fisharebest\Webtrees\Filter;
21use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Stats;
24use Fisharebest\Webtrees\Tree;
25
26/**
27 * Class FamilyTreeStatisticsModule
28 */
29class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
30{
31    /** Show this number of surnames by default */
32    const DEFAULT_NUMBER_OF_SURNAMES = 10;
33
34    /** {@inheritdoc} */
35    public function getTitle()
36    {
37        /* I18N: Name of a module */
38        return I18N::translate('Statistics');
39    }
40
41    /** {@inheritdoc} */
42    public function getDescription()
43    {
44        /* I18N: Description of “Statistics” module */
45        return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
46    }
47
48    /**
49     * Generate the HTML content of this block.
50     *
51     * @param Tree     $tree
52     * @param int      $block_id
53     * @param bool     $template
54     * @param string[] $cfg
55     *
56     * @return string
57     */
58    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
59    {
60        global $ctype;
61
62        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
63        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
64        $number_of_surnames   = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
65        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
66        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
67        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
68        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
69        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
70        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
71        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
72        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
73        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
74        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
75        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
76        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
77        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
78        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
79        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
80        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
81
82        extract($cfg, EXTR_OVERWRITE);
83
84        if ($show_common_surnames) {
85            // Use the count of base surnames.
86            $top_surnames = Database::prepare(
87                "SELECT n_surn FROM `##name`" .
88                " WHERE n_file = :tree_id AND n_type != '_MARNM' AND n_surn NOT IN ('@N.N.', '')" .
89                " GROUP BY n_surn" .
90                " ORDER BY COUNT(n_surn) DESC" .
91                " LIMIT :limit"
92            )->execute([
93                'tree_id' => $tree->getTreeId(),
94                'limit'   => $number_of_surnames,
95            ])->fetchOneColumn();
96
97            $all_surnames = [];
98            foreach ($top_surnames as $top_surname) {
99                $variants = Database::prepare(
100                    "SELECT n_surname COLLATE utf8_bin, COUNT(*) FROM `##name` WHERE n_file = :tree_id AND n_surn COLLATE :collate = :surname GROUP BY 1"
101                )->execute([
102                    'collate' => I18N::collation(),
103                    'surname' => $top_surname,
104                    'tree_id' => $tree->getTreeId(),
105                ])->fetchAssoc();
106
107                $all_surnames[$top_surname] = $variants;
108            }
109
110            uksort($all_surnames, [I18N::class, 'strcasecmp']);
111
112            $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'individual-list', $tree);
113        } else {
114            $surnames = '';
115        }
116
117        $content = view('modules/gedcom_stats/statistics', [
118            'show_last_update'     => $show_last_update,
119            'show_common_surnames' => $show_common_surnames,
120            'number_of_surnames'   => $number_of_surnames,
121            'stat_indi'            => $stat_indi,
122            'stat_fam'             => $stat_fam,
123            'stat_sour'            => $stat_sour,
124            'stat_media'           => $stat_media,
125            'stat_repo'            => $stat_repo,
126            'stat_surname'         => $stat_surname,
127            'stat_events'          => $stat_events,
128            'stat_users'           => $stat_users,
129            'stat_first_birth'     => $stat_first_birth,
130            'stat_last_birth'      => $stat_last_birth,
131            'stat_first_death'     => $stat_first_death,
132            'stat_last_death'      => $stat_last_death,
133            'stat_long_life'       => $stat_long_life,
134            'stat_avg_life'        => $stat_avg_life,
135            'stat_most_chil'       => $stat_most_chil,
136            'stat_avg_chil'        => $stat_avg_chil,
137            'stats'                => new Stats($tree),
138            'surnames'             => $surnames,
139        ]);
140
141        if ($template) {
142            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
143                $config_url = route('tree-page-block-edit', [
144                    'block_id' => $block_id,
145                    'ged'      => $tree->getName(),
146                ]);
147            } elseif ($ctype === 'user' && Auth::check()) {
148                $config_url = route('user-page-block-edit', [
149                    'block_id' => $block_id,
150                    'ged'      => $tree->getName(),
151                ]);
152            } else {
153                $config_url = '';
154            }
155
156            return view('modules/block-template', [
157                'block'      => str_replace('_', '-', $this->getName()),
158                'id'         => $block_id,
159                'config_url' => $config_url,
160                'title'      => $this->getTitle(),
161                'content'    => $content,
162            ]);
163        } else {
164            return $content;
165        }
166    }
167
168    /** {@inheritdoc} */
169    public function loadAjax(): bool
170    {
171        return true;
172    }
173
174    /** {@inheritdoc} */
175    public function isUserBlock(): bool
176    {
177        return true;
178    }
179
180    /** {@inheritdoc} */
181    public function isGedcomBlock(): bool
182    {
183        return true;
184    }
185
186    /**
187     * An HTML form to edit block settings
188     *
189     * @param Tree $tree
190     * @param int  $block_id
191     *
192     * @return void
193     */
194    public function configureBlock(Tree $tree, int $block_id)
195    {
196        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
197            $this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
198            $this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
199            $this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
200            $this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
201            $this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
202            $this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
203            $this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
204            $this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
205            $this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
206            $this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
207            $this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
208            $this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
209            $this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
210            $this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
211            $this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
212            $this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
213            $this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
214            $this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
215            $this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
216            $this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
217
218            return;
219        }
220
221        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
222        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
223        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
224        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
225        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
226        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
227        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
228        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
229        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
230        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
231        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
232        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
233        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
234        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
235        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
236        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
237        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
238        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
239        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
240
241        echo view('modules/gedcom_stats/config', [
242            'show_last_update'     => $show_last_update,
243            'show_common_surnames' => $show_common_surnames,
244            'number_of_surnames'   => $number_of_surnames,
245            'stat_indi'            => $stat_indi,
246            'stat_fam'             => $stat_fam,
247            'stat_sour'            => $stat_sour,
248            'stat_media'           => $stat_media,
249            'stat_repo'            => $stat_repo,
250            'stat_surname'         => $stat_surname,
251            'stat_events'          => $stat_events,
252            'stat_users'           => $stat_users,
253            'stat_first_birth'     => $stat_first_birth,
254            'stat_last_birth'      => $stat_last_birth,
255            'stat_first_death'     => $stat_first_death,
256            'stat_last_death'      => $stat_last_death,
257            'stat_long_life'       => $stat_long_life,
258            'stat_avg_life'        => $stat_avg_life,
259            'stat_most_chil'       => $stat_most_chil,
260            'stat_avg_chil'        => $stat_avg_chil,
261        ]);
262    }
263}
264