xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17*fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
23f36626dbSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
2567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
269219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
27e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
281ae92905SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
29a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
301e7a7a28SGreg Roachuse Illuminate\Support\Str;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
328c2e8227SGreg Roach
338c2e8227SGreg Roach/**
348c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
358c2e8227SGreg Roach */
3637eb8894SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
37c1010edaSGreg Roach{
3849a243cbSGreg Roach    use ModuleBlockTrait;
3949a243cbSGreg Roach
40f36626dbSGreg Roach    /** Show this number of surnames by default */
4116d6367aSGreg Roach    private const DEFAULT_NUMBER_OF_SURNAMES = '10';
42f36626dbSGreg Roach
43961ec755SGreg Roach    /**
440cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
45961ec755SGreg Roach     *
46961ec755SGreg Roach     * @return string
47961ec755SGreg Roach     */
4849a243cbSGreg Roach    public function title(): string
49c1010edaSGreg Roach    {
50bbb76c12SGreg Roach        /* I18N: Name of a module */
51bbb76c12SGreg Roach        return I18N::translate('Statistics');
528c2e8227SGreg Roach    }
538c2e8227SGreg Roach
54961ec755SGreg Roach    /**
55961ec755SGreg Roach     * A sentence describing what this module does.
56961ec755SGreg Roach     *
57961ec755SGreg Roach     * @return string
58961ec755SGreg Roach     */
5949a243cbSGreg Roach    public function description(): string
60c1010edaSGreg Roach    {
61bbb76c12SGreg Roach        /* I18N: Description of “Statistics” module */
62bbb76c12SGreg Roach        return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
638c2e8227SGreg Roach    }
648c2e8227SGreg Roach
6576692c8bSGreg Roach    /**
6676692c8bSGreg Roach     * Generate the HTML content of this block.
6776692c8bSGreg Roach     *
68e490cd80SGreg Roach     * @param Tree     $tree
6976692c8bSGreg Roach     * @param int      $block_id
703caaa4d2SGreg Roach     * @param string   $context
713caaa4d2SGreg Roach     * @param string[] $config
7276692c8bSGreg Roach     *
7376692c8bSGreg Roach     * @return string
7476692c8bSGreg Roach     */
753caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
76c1010edaSGreg Roach    {
77cab242e7SGreg Roach        $statistics = app(Statistics::class);
78bf57b580SGreg Roach
79e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
80e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
8180536c2dSGreg Roach        $number_of_surnames   = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
82e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
83e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
84e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
85e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
86e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
87e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
88e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
89e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
90e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
91e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
92e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
93e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
94e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
95e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
96e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
97e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
988c2e8227SGreg Roach
993caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1008c2e8227SGreg Roach
1018c2e8227SGreg Roach        if ($show_common_surnames) {
10280536c2dSGreg Roach            // Use the count of base surnames.
1031ae92905SGreg Roach            $top_surnames = DB::table('name')
1041ae92905SGreg Roach                ->where('n_file', '=', $tree->id())
1051ae92905SGreg Roach                ->where('n_type', '<>', '_MARNM')
1061ae92905SGreg Roach                ->whereNotIn('n_surn', ['@N.N.', ''])
1077f5c2944SGreg Roach                ->groupBy(['n_surn'])
108a69f5655SGreg Roach                ->orderByDesc(new Expression('COUNT(n_surn)'))
1091ae92905SGreg Roach                ->take($number_of_surnames)
1101ae92905SGreg Roach                ->pluck('n_surn');
111e0275e5bSGreg Roach
11213abd6f3SGreg Roach            $all_surnames = [];
1131ae92905SGreg Roach
11480536c2dSGreg Roach            foreach ($top_surnames as $top_surname) {
1151ae92905SGreg Roach                $variants = DB::table('name')
1161ae92905SGreg Roach                    ->where('n_file', '=', $tree->id())
117a69f5655SGreg Roach                    ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
1187f5c2944SGreg Roach                    ->groupBy(['surname'])
119a69f5655SGreg Roach                    ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')])
1201ae92905SGreg Roach                    ->pluck('total', 'surname')
1211ae92905SGreg Roach                    ->all();
12280536c2dSGreg Roach
12380536c2dSGreg Roach                $all_surnames[$top_surname] = $variants;
124f36626dbSGreg Roach            }
12580536c2dSGreg Roach
12680536c2dSGreg Roach            uksort($all_surnames, [I18N::class, 'strcasecmp']);
1271d3c0c1aSGreg Roach
12867992b6aSRichard Cissee            //find a module providing individual lists
1290b5fd0a6SGreg Roach            $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(static function (ModuleInterface $module) {
13067992b6aSRichard Cissee                return $module instanceof IndividualListModule;
13167992b6aSRichard Cissee            });
13267992b6aSRichard Cissee
13367992b6aSRichard Cissee            $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, $module, $tree);
1341d3c0c1aSGreg Roach        } else {
1351d3c0c1aSGreg Roach            $surnames = '';
1368c2e8227SGreg Roach        }
1371d3c0c1aSGreg Roach
138147e99aaSGreg Roach        $content = view('modules/gedcom_stats/statistics', [
1391d3c0c1aSGreg Roach            'show_last_update'     => $show_last_update,
1401d3c0c1aSGreg Roach            'show_common_surnames' => $show_common_surnames,
1411d3c0c1aSGreg Roach            'number_of_surnames'   => $number_of_surnames,
1421d3c0c1aSGreg Roach            'stat_indi'            => $stat_indi,
1431d3c0c1aSGreg Roach            'stat_fam'             => $stat_fam,
1441d3c0c1aSGreg Roach            'stat_sour'            => $stat_sour,
1451d3c0c1aSGreg Roach            'stat_media'           => $stat_media,
1461d3c0c1aSGreg Roach            'stat_repo'            => $stat_repo,
1471d3c0c1aSGreg Roach            'stat_surname'         => $stat_surname,
1481d3c0c1aSGreg Roach            'stat_events'          => $stat_events,
1491d3c0c1aSGreg Roach            'stat_users'           => $stat_users,
1501d3c0c1aSGreg Roach            'stat_first_birth'     => $stat_first_birth,
1511d3c0c1aSGreg Roach            'stat_last_birth'      => $stat_last_birth,
1521d3c0c1aSGreg Roach            'stat_first_death'     => $stat_first_death,
1531d3c0c1aSGreg Roach            'stat_last_death'      => $stat_last_death,
1541d3c0c1aSGreg Roach            'stat_long_life'       => $stat_long_life,
1551d3c0c1aSGreg Roach            'stat_avg_life'        => $stat_avg_life,
1561d3c0c1aSGreg Roach            'stat_most_chil'       => $stat_most_chil,
1571d3c0c1aSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
158bf57b580SGreg Roach            'stats'                => $statistics,
1591d3c0c1aSGreg Roach            'surnames'             => $surnames,
1601d3c0c1aSGreg Roach        ]);
16128941e3cSGreg Roach
1623caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
163147e99aaSGreg Roach            return view('modules/block-template', [
1641e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1659c6524dcSGreg Roach                'id'         => $block_id,
1663caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
16749a243cbSGreg Roach                'title'      => $this->title(),
1689c6524dcSGreg Roach                'content'    => $content,
1699c6524dcSGreg Roach            ]);
1708c2e8227SGreg Roach        }
171b2ce94c6SRico Sonntag
172b2ce94c6SRico Sonntag        return $content;
1738c2e8227SGreg Roach    }
1748c2e8227SGreg Roach
1753caaa4d2SGreg Roach    /**
1763caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1773caaa4d2SGreg Roach     *
1783caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1793caaa4d2SGreg Roach     *
1803caaa4d2SGreg Roach     * @return bool
1813caaa4d2SGreg Roach     */
182c1010edaSGreg Roach    public function loadAjax(): bool
183c1010edaSGreg Roach    {
1848c2e8227SGreg Roach        return true;
1858c2e8227SGreg Roach    }
1868c2e8227SGreg Roach
1873caaa4d2SGreg Roach    /**
1883caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1893caaa4d2SGreg Roach     *
1903caaa4d2SGreg Roach     * @return bool
1913caaa4d2SGreg Roach     */
192c1010edaSGreg Roach    public function isUserBlock(): bool
193c1010edaSGreg Roach    {
1948c2e8227SGreg Roach        return true;
1958c2e8227SGreg Roach    }
1968c2e8227SGreg Roach
1973caaa4d2SGreg Roach    /**
1983caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1993caaa4d2SGreg Roach     *
2003caaa4d2SGreg Roach     * @return bool
2013caaa4d2SGreg Roach     */
20263276d8fSGreg Roach    public function isTreeBlock(): bool
203c1010edaSGreg Roach    {
2048c2e8227SGreg Roach        return true;
2058c2e8227SGreg Roach    }
2068c2e8227SGreg Roach
20776692c8bSGreg Roach    /**
208a45f9889SGreg Roach     * Update the configuration for a block.
209a45f9889SGreg Roach     *
2106ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
211a45f9889SGreg Roach     * @param int     $block_id
212a45f9889SGreg Roach     *
213a45f9889SGreg Roach     * @return void
214a45f9889SGreg Roach     */
2156ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
216a45f9889SGreg Roach    {
217ac5f8ed1SGreg Roach        $settings = $request->getParsedBody();
218ac5f8ed1SGreg Roach
219ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'show_last_update', $settings['show_last_update'] ?? '');
220ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'show_common_surnames', $settings['show_common_surnames'] ?? '');
221ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'number_of_surnames', $settings['number_of_surnames']);
222ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_indi', $settings['stat_indi'] ?? '');
223ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_fam', $settings['stat_fam'] ?? '');
224ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_sour', $settings['stat_sour'] ?? '');
225ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_other', $settings['stat_other'] ?? '');
226ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_media', $settings['stat_media'] ?? '');
227ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_repo', $settings['stat_repo'] ?? '');
228ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_surname', $settings['stat_surname'] ?? '');
229ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_events', $settings['stat_events'] ?? '');
230ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_users', $settings['stat_users'] ?? '');
231ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_birth', $settings['stat_first_birth'] ?? '');
232ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_birth', $settings['stat_last_birth'] ?? '');
233ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_death', $settings['stat_first_death'] ?? '');
234ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_death', $settings['stat_last_death'] ?? '');
235ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_long_life', $settings['stat_long_life'] ?? '');
236ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_life', $settings['stat_avg_life'] ?? '');
237ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_most_chil', $settings['stat_most_chil'] ?? '');
238ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_chil', $settings['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     *
2473caaa4d2SGreg Roach     * @return string
24876692c8bSGreg Roach     */
2493caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
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
2713caaa4d2SGreg Roach        return 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