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