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