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