xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
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\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\Database;
22use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Stats;
25use Fisharebest\Webtrees\Tree;
26use Symfony\Component\HttpFoundation\Request;
27
28/**
29 * Class FamilyTreeStatisticsModule
30 */
31class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
32{
33    /** Show this number of surnames by default */
34    const DEFAULT_NUMBER_OF_SURNAMES = '10';
35
36    /** {@inheritdoc} */
37    public function getTitle(): string
38    {
39        /* I18N: Name of a module */
40        return I18N::translate('Statistics');
41    }
42
43    /** {@inheritdoc} */
44    public function getDescription(): string
45    {
46        /* I18N: Description of “Statistics” module */
47        return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
48    }
49
50    /**
51     * Generate the HTML content of this block.
52     *
53     * @param Tree     $tree
54     * @param int      $block_id
55     * @param string   $ctype
56     * @param string[] $cfg
57     *
58     * @return string
59     */
60    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
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->id(),
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->id(),
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 ($ctype !== '') {
142            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
143                $config_url = route('tree-page-block-edit', [
144                    'block_id' => $block_id,
145                    'ged'      => $tree->name(),
146                ]);
147            } elseif ($ctype === 'user' && Auth::check()) {
148                $config_url = route('user-page-block-edit', [
149                    'block_id' => $block_id,
150                    'ged'      => $tree->name(),
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        }
164
165        return $content;
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     * Update the configuration for a block.
188     *
189     * @param Request $request
190     * @param int     $block_id
191     *
192     * @return void
193     */
194    public function saveBlockConfiguration(Request $request, int $block_id)
195    {
196        $this->setBlockSetting($block_id, 'show_last_update', $request->get('show_last_update', ''));
197        $this->setBlockSetting($block_id, 'show_common_surnames', $request->get('show_common_surnames', ''));
198        $this->setBlockSetting($block_id, 'number_of_surnames', $request->get('number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES));
199        $this->setBlockSetting($block_id, 'stat_indi', $request->get('stat_indi', ''));
200        $this->setBlockSetting($block_id, 'stat_fam', $request->get('stat_fam', ''));
201        $this->setBlockSetting($block_id, 'stat_sour', $request->get('stat_sour', ''));
202        $this->setBlockSetting($block_id, 'stat_other', $request->get('stat_other', ''));
203        $this->setBlockSetting($block_id, 'stat_media', $request->get('stat_media', ''));
204        $this->setBlockSetting($block_id, 'stat_repo', $request->get('stat_repo', ''));
205        $this->setBlockSetting($block_id, 'stat_surname', $request->get('stat_surname', ''));
206        $this->setBlockSetting($block_id, 'stat_events', $request->get('stat_events', ''));
207        $this->setBlockSetting($block_id, 'stat_users', $request->get('stat_users', ''));
208        $this->setBlockSetting($block_id, 'stat_first_birth', $request->get('stat_first_birth', ''));
209        $this->setBlockSetting($block_id, 'stat_last_birth', $request->get('stat_last_birth', ''));
210        $this->setBlockSetting($block_id, 'stat_first_death', $request->get('stat_first_death', ''));
211        $this->setBlockSetting($block_id, 'stat_last_death', $request->get('stat_last_death', ''));
212        $this->setBlockSetting($block_id, 'stat_long_life', $request->get('stat_long_life', ''));
213        $this->setBlockSetting($block_id, 'stat_avg_life', $request->get('stat_avg_life', ''));
214        $this->setBlockSetting($block_id, 'stat_most_chil', $request->get('stat_most_chil', ''));
215        $this->setBlockSetting($block_id, 'stat_avg_chil', $request->get('stat_avg_chil', ''));
216    }
217
218    /**
219     * An HTML form to edit block settings
220     *
221     * @param Tree $tree
222     * @param int  $block_id
223     *
224     * @return void
225     */
226    public function editBlockConfiguration(Tree $tree, int $block_id)
227    {
228        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
229        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
230        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
231        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
232        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
233        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
234        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
235        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
236        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
237        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
238        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
239        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
240        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
241        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
242        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
243        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
244        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
245        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
246        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
247
248        echo view('modules/gedcom_stats/config', [
249            'show_last_update'     => $show_last_update,
250            'show_common_surnames' => $show_common_surnames,
251            'number_of_surnames'   => $number_of_surnames,
252            'stat_indi'            => $stat_indi,
253            'stat_fam'             => $stat_fam,
254            'stat_sour'            => $stat_sour,
255            'stat_media'           => $stat_media,
256            'stat_repo'            => $stat_repo,
257            'stat_surname'         => $stat_surname,
258            'stat_events'          => $stat_events,
259            'stat_users'           => $stat_users,
260            'stat_first_birth'     => $stat_first_birth,
261            'stat_last_birth'      => $stat_last_birth,
262            'stat_first_death'     => $stat_first_death,
263            'stat_last_death'      => $stat_last_death,
264            'stat_long_life'       => $stat_long_life,
265            'stat_avg_life'        => $stat_avg_life,
266            'stat_most_chil'       => $stat_most_chil,
267            'stat_avg_chil'        => $stat_avg_chil,
268        ]);
269    }
270}
271