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