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