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