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