xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 9219296a1acfac69c7d7f13505951ddeee5f8899)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
21f36626dbSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23*9219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
24e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
251ae92905SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
308c2e8227SGreg Roach */
3137eb8894SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleBlockTrait;
3449a243cbSGreg Roach
35f36626dbSGreg Roach    /** Show this number of surnames by default */
3616d6367aSGreg Roach    private const DEFAULT_NUMBER_OF_SURNAMES = '10';
37f36626dbSGreg Roach
38961ec755SGreg Roach    /**
39*9219296aSGreg Roach     * @var Statistics
40*9219296aSGreg Roach     */
41*9219296aSGreg Roach    private $statistics;
42*9219296aSGreg Roach
43*9219296aSGreg Roach    /**
44*9219296aSGreg Roach     * TopGivenNamesModule constructor.
45*9219296aSGreg Roach     *
46*9219296aSGreg Roach     * @param Statistics $statistics
47*9219296aSGreg Roach     */
48*9219296aSGreg Roach    public function __construct(Statistics $statistics) {
49*9219296aSGreg Roach        $this->statistics = $statistics;
50*9219296aSGreg Roach    }
51*9219296aSGreg Roach
52*9219296aSGreg Roach    /**
53961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
54961ec755SGreg Roach     *
55961ec755SGreg Roach     * @return string
56961ec755SGreg Roach     */
5749a243cbSGreg Roach    public function title(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Name of a module */
60bbb76c12SGreg Roach        return I18N::translate('Statistics');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
63961ec755SGreg Roach    /**
64961ec755SGreg Roach     * A sentence describing what this module does.
65961ec755SGreg Roach     *
66961ec755SGreg Roach     * @return string
67961ec755SGreg Roach     */
6849a243cbSGreg Roach    public function description(): string
69c1010edaSGreg Roach    {
70bbb76c12SGreg Roach        /* I18N: Description of “Statistics” module */
71bbb76c12SGreg Roach        return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
728c2e8227SGreg Roach    }
738c2e8227SGreg Roach
7476692c8bSGreg Roach    /**
7576692c8bSGreg Roach     * Generate the HTML content of this block.
7676692c8bSGreg Roach     *
77e490cd80SGreg Roach     * @param Tree     $tree
7876692c8bSGreg Roach     * @param int      $block_id
795f2ae573SGreg Roach     * @param string   $ctype
80727f238cSGreg Roach     * @param string[] $cfg
8176692c8bSGreg Roach     *
8276692c8bSGreg Roach     * @return string
8376692c8bSGreg Roach     */
845f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
85c1010edaSGreg Roach    {
86e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
87e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
8880536c2dSGreg Roach        $number_of_surnames   = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
89e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
90e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
91e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
92e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
93e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
94e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
95e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
96e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
97e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
98e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
99e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
100e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
101e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
102e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
103e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
104e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
1058c2e8227SGreg Roach
106c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
1078c2e8227SGreg Roach
1088c2e8227SGreg Roach        if ($show_common_surnames) {
10980536c2dSGreg Roach            // Use the count of base surnames.
1101ae92905SGreg Roach            $top_surnames = DB::table('name')
1111ae92905SGreg Roach                ->where('n_file', '=', $tree->id())
1121ae92905SGreg Roach                ->where('n_type', '<>', '_MARNM')
1131ae92905SGreg Roach                ->whereNotIn('n_surn', ['@N.N.', ''])
1141ae92905SGreg Roach                ->groupBy('n_surn')
1151ae92905SGreg Roach                ->orderByDesc(DB::raw('COUNT(n_surn)'))
1161ae92905SGreg Roach                ->take($number_of_surnames)
1171ae92905SGreg Roach                ->pluck('n_surn');
118e0275e5bSGreg Roach
11913abd6f3SGreg Roach            $all_surnames = [];
1201ae92905SGreg Roach
12180536c2dSGreg Roach            foreach ($top_surnames as $top_surname) {
1221ae92905SGreg Roach                $variants = DB::table('name')
1231ae92905SGreg Roach                    ->where('n_file', '=', $tree->id())
1241ae92905SGreg Roach                    ->where(DB::raw('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
1251ae92905SGreg Roach                    ->groupBy('surname')
1261ae92905SGreg Roach                    ->select([DB::raw('n_surname /*! COLLATE utf8_bin */ AS surname'), DB::raw('count(*) AS total')])
1271ae92905SGreg Roach                    ->pluck('total', 'surname')
1281ae92905SGreg Roach                    ->all();
12980536c2dSGreg Roach
13080536c2dSGreg Roach                $all_surnames[$top_surname] = $variants;
131f36626dbSGreg Roach            }
13280536c2dSGreg Roach
13380536c2dSGreg Roach            uksort($all_surnames, [I18N::class, 'strcasecmp']);
1341d3c0c1aSGreg Roach
135e490cd80SGreg Roach            $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'individual-list', $tree);
1361d3c0c1aSGreg Roach        } else {
1371d3c0c1aSGreg Roach            $surnames = '';
1388c2e8227SGreg Roach        }
1391d3c0c1aSGreg Roach
140147e99aaSGreg Roach        $content = view('modules/gedcom_stats/statistics', [
1411d3c0c1aSGreg Roach            'show_last_update'     => $show_last_update,
1421d3c0c1aSGreg Roach            'show_common_surnames' => $show_common_surnames,
1431d3c0c1aSGreg Roach            'number_of_surnames'   => $number_of_surnames,
1441d3c0c1aSGreg Roach            'stat_indi'            => $stat_indi,
1451d3c0c1aSGreg Roach            'stat_fam'             => $stat_fam,
1461d3c0c1aSGreg Roach            'stat_sour'            => $stat_sour,
1471d3c0c1aSGreg Roach            'stat_media'           => $stat_media,
1481d3c0c1aSGreg Roach            'stat_repo'            => $stat_repo,
1491d3c0c1aSGreg Roach            'stat_surname'         => $stat_surname,
1501d3c0c1aSGreg Roach            'stat_events'          => $stat_events,
1511d3c0c1aSGreg Roach            'stat_users'           => $stat_users,
1521d3c0c1aSGreg Roach            'stat_first_birth'     => $stat_first_birth,
1531d3c0c1aSGreg Roach            'stat_last_birth'      => $stat_last_birth,
1541d3c0c1aSGreg Roach            'stat_first_death'     => $stat_first_death,
1551d3c0c1aSGreg Roach            'stat_last_death'      => $stat_last_death,
1561d3c0c1aSGreg Roach            'stat_long_life'       => $stat_long_life,
1571d3c0c1aSGreg Roach            'stat_avg_life'        => $stat_avg_life,
1581d3c0c1aSGreg Roach            'stat_most_chil'       => $stat_most_chil,
1591d3c0c1aSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
160*9219296aSGreg Roach            'stats'                => $this->statistics,
1611d3c0c1aSGreg Roach            'surnames'             => $surnames,
1621d3c0c1aSGreg Roach        ]);
16328941e3cSGreg Roach
1646a8879feSGreg Roach        if ($ctype !== '') {
165e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
166c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
167c1010edaSGreg Roach                    'block_id' => $block_id,
168aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
169c1010edaSGreg Roach                ]);
170397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
171c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
172c1010edaSGreg Roach                    'block_id' => $block_id,
173aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
174c1010edaSGreg Roach                ]);
1759c6524dcSGreg Roach            } else {
1769c6524dcSGreg Roach                $config_url = '';
1779c6524dcSGreg Roach            }
1789c6524dcSGreg Roach
179147e99aaSGreg Roach            return view('modules/block-template', [
18026684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1819c6524dcSGreg Roach                'id'         => $block_id,
1829c6524dcSGreg Roach                'config_url' => $config_url,
18349a243cbSGreg Roach                'title'      => $this->title(),
1849c6524dcSGreg Roach                'content'    => $content,
1859c6524dcSGreg Roach            ]);
1868c2e8227SGreg Roach        }
187b2ce94c6SRico Sonntag
188b2ce94c6SRico Sonntag        return $content;
1898c2e8227SGreg Roach    }
1908c2e8227SGreg Roach
1918c2e8227SGreg Roach    /** {@inheritdoc} */
192c1010edaSGreg Roach    public function loadAjax(): bool
193c1010edaSGreg Roach    {
1948c2e8227SGreg Roach        return true;
1958c2e8227SGreg Roach    }
1968c2e8227SGreg Roach
1978c2e8227SGreg Roach    /** {@inheritdoc} */
198c1010edaSGreg Roach    public function isUserBlock(): bool
199c1010edaSGreg Roach    {
2008c2e8227SGreg Roach        return true;
2018c2e8227SGreg Roach    }
2028c2e8227SGreg Roach
2038c2e8227SGreg Roach    /** {@inheritdoc} */
20463276d8fSGreg Roach    public function isTreeBlock(): bool
205c1010edaSGreg Roach    {
2068c2e8227SGreg Roach        return true;
2078c2e8227SGreg Roach    }
2088c2e8227SGreg Roach
20976692c8bSGreg Roach    /**
210a45f9889SGreg Roach     * Update the configuration for a block.
211a45f9889SGreg Roach     *
212a45f9889SGreg Roach     * @param Request $request
213a45f9889SGreg Roach     * @param int     $block_id
214a45f9889SGreg Roach     *
215a45f9889SGreg Roach     * @return void
216a45f9889SGreg Roach     */
217a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
218a45f9889SGreg Roach    {
219a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_last_update', $request->get('show_last_update', ''));
220a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_common_surnames', $request->get('show_common_surnames', ''));
221a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'number_of_surnames', $request->get('number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES));
222a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_indi', $request->get('stat_indi', ''));
223a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_fam', $request->get('stat_fam', ''));
224a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_sour', $request->get('stat_sour', ''));
225a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_other', $request->get('stat_other', ''));
226a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_media', $request->get('stat_media', ''));
227a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_repo', $request->get('stat_repo', ''));
228a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_surname', $request->get('stat_surname', ''));
229a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_events', $request->get('stat_events', ''));
230a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_users', $request->get('stat_users', ''));
231a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_birth', $request->get('stat_first_birth', ''));
232a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_birth', $request->get('stat_last_birth', ''));
233a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_death', $request->get('stat_first_death', ''));
234a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_death', $request->get('stat_last_death', ''));
235a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_long_life', $request->get('stat_long_life', ''));
236a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_life', $request->get('stat_avg_life', ''));
237a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_most_chil', $request->get('stat_most_chil', ''));
238a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_chil', $request->get('stat_avg_chil', ''));
239a45f9889SGreg Roach    }
240a45f9889SGreg Roach
241a45f9889SGreg Roach    /**
24276692c8bSGreg Roach     * An HTML form to edit block settings
24376692c8bSGreg Roach     *
244e490cd80SGreg Roach     * @param Tree $tree
24576692c8bSGreg Roach     * @param int  $block_id
246a9430be8SGreg Roach     *
247a9430be8SGreg Roach     * @return void
24876692c8bSGreg Roach     */
249a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
250c1010edaSGreg Roach    {
251e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
252e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
253f36626dbSGreg Roach        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
254e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
255e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
256e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
257e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
258e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
259e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
260e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
261e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
262e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
263e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
264e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
265e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
266e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
267e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
268e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
269e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2708c2e8227SGreg Roach
271147e99aaSGreg Roach        echo view('modules/gedcom_stats/config', [
272c385536dSGreg Roach            'show_last_update'     => $show_last_update,
273c385536dSGreg Roach            'show_common_surnames' => $show_common_surnames,
274c385536dSGreg Roach            'number_of_surnames'   => $number_of_surnames,
275c385536dSGreg Roach            'stat_indi'            => $stat_indi,
276c385536dSGreg Roach            'stat_fam'             => $stat_fam,
277c385536dSGreg Roach            'stat_sour'            => $stat_sour,
278c385536dSGreg Roach            'stat_media'           => $stat_media,
279c385536dSGreg Roach            'stat_repo'            => $stat_repo,
280c385536dSGreg Roach            'stat_surname'         => $stat_surname,
281c385536dSGreg Roach            'stat_events'          => $stat_events,
282c385536dSGreg Roach            'stat_users'           => $stat_users,
283c385536dSGreg Roach            'stat_first_birth'     => $stat_first_birth,
284c385536dSGreg Roach            'stat_last_birth'      => $stat_last_birth,
285c385536dSGreg Roach            'stat_first_death'     => $stat_first_death,
286c385536dSGreg Roach            'stat_last_death'      => $stat_last_death,
287c385536dSGreg Roach            'stat_long_life'       => $stat_long_life,
288c385536dSGreg Roach            'stat_avg_life'        => $stat_avg_life,
289c385536dSGreg Roach            'stat_most_chil'       => $stat_most_chil,
290c385536dSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
291c385536dSGreg Roach        ]);
2928c2e8227SGreg Roach    }
2938c2e8227SGreg Roach}
294