xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 78606cf427772fbdc5222ff607ee23f43078f94b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Services\ModuleService;
26use Fisharebest\Webtrees\Statistics;
27use Fisharebest\Webtrees\Tree;
28use Fisharebest\Webtrees\Validator;
29use Illuminate\Database\Capsule\Manager as DB;
30use Illuminate\Database\Query\Expression;
31use Illuminate\Support\Str;
32use Psr\Http\Message\ServerRequestInterface;
33
34use function app;
35use function extract;
36use function uksort;
37use function view;
38
39use const EXTR_OVERWRITE;
40
41/**
42 * Class FamilyTreeStatisticsModule
43 */
44class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
45{
46    use ModuleBlockTrait;
47
48    /** Show this number of surnames by default */
49    private const DEFAULT_NUMBER_OF_SURNAMES = '10';
50
51    private ModuleService $module_service;
52
53    /**
54     * @param ModuleService $module_service
55     */
56    public function __construct(ModuleService $module_service)
57    {
58        $this->module_service = $module_service;
59    }
60
61    /**
62     * How should this module be identified in the control panel, etc.?
63     *
64     * @return string
65     */
66    public function title(): string
67    {
68        /* I18N: Name of a module */
69        return I18N::translate('Statistics');
70    }
71
72    /**
73     * A sentence describing what this module does.
74     *
75     * @return string
76     */
77    public function description(): string
78    {
79        /* I18N: Description of “Statistics” module */
80        return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
81    }
82
83    /**
84     * Generate the HTML content of this block.
85     *
86     * @param Tree                 $tree
87     * @param int                  $block_id
88     * @param string               $context
89     * @param array<string,string> $config
90     *
91     * @return string
92     */
93    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
94    {
95        $statistics = app(Statistics::class);
96
97        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
98        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
99        $number_of_surnames   = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
100        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
101        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
102        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
103        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
104        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
105        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
106        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
107        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
108        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
109        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
110        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
111        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
112        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
113        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
114        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
115        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
116
117        extract($config, EXTR_OVERWRITE);
118
119        if ($show_common_surnames) {
120            // Use the count of base surnames.
121            $top_surnames = DB::table('name')
122                ->where('n_file', '=', $tree->id())
123                ->where('n_type', '<>', '_MARNM')
124                ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, ''])
125                ->groupBy(['n_surn'])
126                ->orderByDesc(new Expression('COUNT(n_surn)'))
127                ->take($number_of_surnames)
128                ->pluck('n_surn');
129
130            $all_surnames = [];
131
132            foreach ($top_surnames as $top_surname) {
133                $variants = DB::table('name')
134                    ->where('n_file', '=', $tree->id())
135                    ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
136                    ->groupBy(['surname'])
137                    ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')])
138                    ->pluck('total', 'surname')
139                    ->all();
140
141                $all_surnames[$top_surname] = $variants;
142            }
143
144            uksort($all_surnames, I18N::comparator());
145
146            // Find a module providing individual lists
147            $module = $this->module_service
148                ->findByComponent(ModuleListInterface::class, $tree, Auth::user())
149                ->first(static fn (ModuleInterface $module): bool => $module instanceof IndividualListModule);
150
151            $surnames = view('lists/surnames-compact-list', [
152                'module'   => $module,
153                'totals'   => false,
154                'surnames' => $all_surnames,
155                'tree'     => $tree,
156            ]);
157        } else {
158            $surnames = '';
159        }
160
161        $content = view('modules/gedcom_stats/statistics', [
162            'show_last_update'     => $show_last_update,
163            'show_common_surnames' => $show_common_surnames,
164            'number_of_surnames'   => $number_of_surnames,
165            'stat_indi'            => $stat_indi,
166            'stat_fam'             => $stat_fam,
167            'stat_sour'            => $stat_sour,
168            'stat_media'           => $stat_media,
169            'stat_repo'            => $stat_repo,
170            'stat_surname'         => $stat_surname,
171            'stat_events'          => $stat_events,
172            'stat_users'           => $stat_users,
173            'stat_first_birth'     => $stat_first_birth,
174            'stat_last_birth'      => $stat_last_birth,
175            'stat_first_death'     => $stat_first_death,
176            'stat_last_death'      => $stat_last_death,
177            'stat_long_life'       => $stat_long_life,
178            'stat_avg_life'        => $stat_avg_life,
179            'stat_most_chil'       => $stat_most_chil,
180            'stat_avg_chil'        => $stat_avg_chil,
181            'surnames'             => $surnames,
182        ]);
183
184        $content = $statistics->embedTags($content);
185
186        if ($context !== self::CONTEXT_EMBED) {
187            return view('modules/block-template', [
188                'block'      => Str::kebab($this->name()),
189                'id'         => $block_id,
190                'config_url' => $this->configUrl($tree, $context, $block_id),
191                'title'      => $this->title(),
192                'content'    => $content,
193            ]);
194        }
195
196        return $content;
197    }
198
199    /**
200     * Should this block load asynchronously using AJAX?
201     *
202     * Simple blocks are faster in-line, more complex ones can be loaded later.
203     *
204     * @return bool
205     */
206    public function loadAjax(): bool
207    {
208        return true;
209    }
210
211    /**
212     * Can this block be shown on the user’s home page?
213     *
214     * @return bool
215     */
216    public function isUserBlock(): bool
217    {
218        return true;
219    }
220
221    /**
222     * Can this block be shown on the tree’s home page?
223     *
224     * @return bool
225     */
226    public function isTreeBlock(): bool
227    {
228        return true;
229    }
230
231    /**
232     * Update the configuration for a block.
233     *
234     * @param ServerRequestInterface $request
235     * @param int     $block_id
236     *
237     * @return void
238     */
239    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
240    {
241        $show_last_update     = Validator::parsedBody($request)->boolean('show_last_update', false);
242        $show_common_surnames = Validator::parsedBody($request)->boolean('show_common_surnames', false);
243        $number_of_surnames   = Validator::parsedBody($request)->integer('number_of_surnames');
244        $stat_indi            = Validator::parsedBody($request)->boolean('stat_indi', false);
245        $stat_fam             = Validator::parsedBody($request)->boolean('stat_fam', false);
246        $stat_sour            = Validator::parsedBody($request)->boolean('stat_sour', false);
247        $stat_other           = Validator::parsedBody($request)->boolean('stat_other', false);
248        $stat_media           = Validator::parsedBody($request)->boolean('stat_media', false);
249        $stat_repo            = Validator::parsedBody($request)->boolean('stat_repo', false);
250        $stat_surname         = Validator::parsedBody($request)->boolean('stat_surname', false);
251        $stat_events          = Validator::parsedBody($request)->boolean('stat_events', false);
252        $stat_users           = Validator::parsedBody($request)->boolean('stat_users', false);
253        $stat_first_birth     = Validator::parsedBody($request)->boolean('stat_first_birth', false);
254        $stat_last_birth      = Validator::parsedBody($request)->boolean('stat_last_birth', false);
255        $stat_first_death     = Validator::parsedBody($request)->boolean('stat_first_death', false);
256        $stat_last_death      = Validator::parsedBody($request)->boolean('stat_last_death', false);
257        $stat_long_life       = Validator::parsedBody($request)->boolean('stat_long_life', false);
258        $stat_avg_life        = Validator::parsedBody($request)->boolean('stat_avg_life', false);
259        $stat_most_chil       = Validator::parsedBody($request)->boolean('stat_most_chil', false);
260        $stat_avg_chil        = Validator::parsedBody($request)->boolean('stat_avg_chil', false);
261
262        $this->setBlockSetting($block_id, 'show_last_update', (string) $show_last_update);
263        $this->setBlockSetting($block_id, 'show_common_surnames', (string) $show_common_surnames);
264        $this->setBlockSetting($block_id, 'number_of_surnames', (string) $number_of_surnames);
265        $this->setBlockSetting($block_id, 'stat_indi', (string) $stat_indi);
266        $this->setBlockSetting($block_id, 'stat_fam', (string) $stat_fam);
267        $this->setBlockSetting($block_id, 'stat_sour', (string) $stat_sour);
268        $this->setBlockSetting($block_id, 'stat_other', (string) $stat_other);
269        $this->setBlockSetting($block_id, 'stat_media', (string) $stat_media);
270        $this->setBlockSetting($block_id, 'stat_repo', (string) $stat_repo);
271        $this->setBlockSetting($block_id, 'stat_surname', (string) $stat_surname);
272        $this->setBlockSetting($block_id, 'stat_events', (string) $stat_events);
273        $this->setBlockSetting($block_id, 'stat_users', (string) $stat_users);
274        $this->setBlockSetting($block_id, 'stat_first_birth', (string) $stat_first_birth);
275        $this->setBlockSetting($block_id, 'stat_last_birth', (string) $stat_last_birth);
276        $this->setBlockSetting($block_id, 'stat_first_death', (string) $stat_first_death);
277        $this->setBlockSetting($block_id, 'stat_last_death', (string) $stat_last_death);
278        $this->setBlockSetting($block_id, 'stat_long_life', (string) $stat_long_life);
279        $this->setBlockSetting($block_id, 'stat_avg_life', (string) $stat_avg_life);
280        $this->setBlockSetting($block_id, 'stat_most_chil', (string) $stat_most_chil);
281        $this->setBlockSetting($block_id, 'stat_avg_chil', (string) $stat_avg_chil);
282    }
283
284    /**
285     * An HTML form to edit block settings
286     *
287     * @param Tree $tree
288     * @param int  $block_id
289     *
290     * @return string
291     */
292    public function editBlockConfiguration(Tree $tree, int $block_id): string
293    {
294        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
295        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
296        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
297        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
298        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
299        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
300        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
301        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
302        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
303        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
304        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
305        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
306        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
307        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
308        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
309        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
310        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
311        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
312        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
313
314        return view('modules/gedcom_stats/config', [
315            'show_last_update'     => $show_last_update,
316            'show_common_surnames' => $show_common_surnames,
317            'number_of_surnames'   => $number_of_surnames,
318            'stat_indi'            => $stat_indi,
319            'stat_fam'             => $stat_fam,
320            'stat_sour'            => $stat_sour,
321            'stat_media'           => $stat_media,
322            'stat_repo'            => $stat_repo,
323            'stat_surname'         => $stat_surname,
324            'stat_events'          => $stat_events,
325            'stat_users'           => $stat_users,
326            'stat_first_birth'     => $stat_first_birth,
327            'stat_last_birth'      => $stat_last_birth,
328            'stat_first_death'     => $stat_first_death,
329            'stat_last_death'      => $stat_last_death,
330            'stat_long_life'       => $stat_long_life,
331            'stat_avg_life'        => $stat_avg_life,
332            'stat_most_chil'       => $stat_most_chil,
333            'stat_avg_chil'        => $stat_avg_chil,
334        ]);
335    }
336}
337