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