xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision ac5f8ed16774e57c5f89faca528d55bc4a3971e1)
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;
2367992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
249219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
261ae92905SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
271e7a7a28SGreg Roachuse Illuminate\Support\Str;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
328c2e8227SGreg Roach */
3337eb8894SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
37f36626dbSGreg Roach    /** Show this number of surnames by default */
3816d6367aSGreg Roach    private const DEFAULT_NUMBER_OF_SURNAMES = '10';
39f36626dbSGreg Roach
40961ec755SGreg Roach    /**
410cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
42961ec755SGreg Roach     *
43961ec755SGreg Roach     * @return string
44961ec755SGreg Roach     */
4549a243cbSGreg Roach    public function title(): string
46c1010edaSGreg Roach    {
47bbb76c12SGreg Roach        /* I18N: Name of a module */
48bbb76c12SGreg Roach        return I18N::translate('Statistics');
498c2e8227SGreg Roach    }
508c2e8227SGreg Roach
51961ec755SGreg Roach    /**
52961ec755SGreg Roach     * A sentence describing what this module does.
53961ec755SGreg Roach     *
54961ec755SGreg Roach     * @return string
55961ec755SGreg Roach     */
5649a243cbSGreg Roach    public function description(): string
57c1010edaSGreg Roach    {
58bbb76c12SGreg Roach        /* I18N: Description of “Statistics” module */
59bbb76c12SGreg Roach        return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
608c2e8227SGreg Roach    }
618c2e8227SGreg Roach
6276692c8bSGreg Roach    /**
6376692c8bSGreg Roach     * Generate the HTML content of this block.
6476692c8bSGreg Roach     *
65e490cd80SGreg Roach     * @param Tree     $tree
6676692c8bSGreg Roach     * @param int      $block_id
675f2ae573SGreg Roach     * @param string   $ctype
68727f238cSGreg Roach     * @param string[] $cfg
6976692c8bSGreg Roach     *
7076692c8bSGreg Roach     * @return string
7176692c8bSGreg Roach     */
725f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
73c1010edaSGreg Roach    {
74cab242e7SGreg Roach        $statistics = app(Statistics::class);
75bf57b580SGreg Roach
76e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
77e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
7880536c2dSGreg Roach        $number_of_surnames   = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
79e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
80e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
81e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
82e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
83e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
84e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
85e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
86e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
87e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
88e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
89e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
90e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
91e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
92e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
93e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
94e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
958c2e8227SGreg Roach
96c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
978c2e8227SGreg Roach
988c2e8227SGreg Roach        if ($show_common_surnames) {
9980536c2dSGreg Roach            // Use the count of base surnames.
1001ae92905SGreg Roach            $top_surnames = DB::table('name')
1011ae92905SGreg Roach                ->where('n_file', '=', $tree->id())
1021ae92905SGreg Roach                ->where('n_type', '<>', '_MARNM')
1031ae92905SGreg Roach                ->whereNotIn('n_surn', ['@N.N.', ''])
1041ae92905SGreg Roach                ->groupBy('n_surn')
1051ae92905SGreg Roach                ->orderByDesc(DB::raw('COUNT(n_surn)'))
1061ae92905SGreg Roach                ->take($number_of_surnames)
1071ae92905SGreg Roach                ->pluck('n_surn');
108e0275e5bSGreg Roach
10913abd6f3SGreg Roach            $all_surnames = [];
1101ae92905SGreg Roach
11180536c2dSGreg Roach            foreach ($top_surnames as $top_surname) {
1121ae92905SGreg Roach                $variants = DB::table('name')
1131ae92905SGreg Roach                    ->where('n_file', '=', $tree->id())
1141ae92905SGreg Roach                    ->where(DB::raw('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
1151ae92905SGreg Roach                    ->groupBy('surname')
1161ae92905SGreg Roach                    ->select([DB::raw('n_surname /*! COLLATE utf8_bin */ AS surname'), DB::raw('count(*) AS total')])
1171ae92905SGreg Roach                    ->pluck('total', 'surname')
1181ae92905SGreg Roach                    ->all();
11980536c2dSGreg Roach
12080536c2dSGreg Roach                $all_surnames[$top_surname] = $variants;
121f36626dbSGreg Roach            }
12280536c2dSGreg Roach
12380536c2dSGreg Roach            uksort($all_surnames, [I18N::class, 'strcasecmp']);
1241d3c0c1aSGreg Roach
12567992b6aSRichard Cissee            //find a module providing individual lists
1260b5fd0a6SGreg Roach            $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(static function (ModuleInterface $module) {
12767992b6aSRichard Cissee                return $module instanceof IndividualListModule;
12867992b6aSRichard Cissee            });
12967992b6aSRichard Cissee
13067992b6aSRichard Cissee            $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, $module, $tree);
1311d3c0c1aSGreg Roach        } else {
1321d3c0c1aSGreg Roach            $surnames = '';
1338c2e8227SGreg Roach        }
1341d3c0c1aSGreg Roach
135147e99aaSGreg Roach        $content = view('modules/gedcom_stats/statistics', [
1361d3c0c1aSGreg Roach            'show_last_update'     => $show_last_update,
1371d3c0c1aSGreg Roach            'show_common_surnames' => $show_common_surnames,
1381d3c0c1aSGreg Roach            'number_of_surnames'   => $number_of_surnames,
1391d3c0c1aSGreg Roach            'stat_indi'            => $stat_indi,
1401d3c0c1aSGreg Roach            'stat_fam'             => $stat_fam,
1411d3c0c1aSGreg Roach            'stat_sour'            => $stat_sour,
1421d3c0c1aSGreg Roach            'stat_media'           => $stat_media,
1431d3c0c1aSGreg Roach            'stat_repo'            => $stat_repo,
1441d3c0c1aSGreg Roach            'stat_surname'         => $stat_surname,
1451d3c0c1aSGreg Roach            'stat_events'          => $stat_events,
1461d3c0c1aSGreg Roach            'stat_users'           => $stat_users,
1471d3c0c1aSGreg Roach            'stat_first_birth'     => $stat_first_birth,
1481d3c0c1aSGreg Roach            'stat_last_birth'      => $stat_last_birth,
1491d3c0c1aSGreg Roach            'stat_first_death'     => $stat_first_death,
1501d3c0c1aSGreg Roach            'stat_last_death'      => $stat_last_death,
1511d3c0c1aSGreg Roach            'stat_long_life'       => $stat_long_life,
1521d3c0c1aSGreg Roach            'stat_avg_life'        => $stat_avg_life,
1531d3c0c1aSGreg Roach            'stat_most_chil'       => $stat_most_chil,
1541d3c0c1aSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
155bf57b580SGreg Roach            'stats'                => $statistics,
1561d3c0c1aSGreg Roach            'surnames'             => $surnames,
1571d3c0c1aSGreg Roach        ]);
15828941e3cSGreg Roach
1596a8879feSGreg Roach        if ($ctype !== '') {
160e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
161c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
162c1010edaSGreg Roach                    'block_id' => $block_id,
163aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
164c1010edaSGreg Roach                ]);
165397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
166c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
167c1010edaSGreg Roach                    'block_id' => $block_id,
168aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
169c1010edaSGreg Roach                ]);
1709c6524dcSGreg Roach            } else {
1719c6524dcSGreg Roach                $config_url = '';
1729c6524dcSGreg Roach            }
1739c6524dcSGreg Roach
174147e99aaSGreg Roach            return view('modules/block-template', [
1751e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1769c6524dcSGreg Roach                'id'         => $block_id,
1779c6524dcSGreg Roach                'config_url' => $config_url,
17849a243cbSGreg Roach                'title'      => $this->title(),
1799c6524dcSGreg Roach                'content'    => $content,
1809c6524dcSGreg Roach            ]);
1818c2e8227SGreg Roach        }
182b2ce94c6SRico Sonntag
183b2ce94c6SRico Sonntag        return $content;
1848c2e8227SGreg Roach    }
1858c2e8227SGreg Roach
1868c2e8227SGreg Roach    /** {@inheritdoc} */
187c1010edaSGreg Roach    public function loadAjax(): bool
188c1010edaSGreg Roach    {
1898c2e8227SGreg Roach        return true;
1908c2e8227SGreg Roach    }
1918c2e8227SGreg Roach
1928c2e8227SGreg Roach    /** {@inheritdoc} */
193c1010edaSGreg Roach    public function isUserBlock(): bool
194c1010edaSGreg Roach    {
1958c2e8227SGreg Roach        return true;
1968c2e8227SGreg Roach    }
1978c2e8227SGreg Roach
1988c2e8227SGreg Roach    /** {@inheritdoc} */
19963276d8fSGreg Roach    public function isTreeBlock(): bool
200c1010edaSGreg Roach    {
2018c2e8227SGreg Roach        return true;
2028c2e8227SGreg Roach    }
2038c2e8227SGreg Roach
20476692c8bSGreg Roach    /**
205a45f9889SGreg Roach     * Update the configuration for a block.
206a45f9889SGreg Roach     *
2076ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
208a45f9889SGreg Roach     * @param int     $block_id
209a45f9889SGreg Roach     *
210a45f9889SGreg Roach     * @return void
211a45f9889SGreg Roach     */
2126ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
213a45f9889SGreg Roach    {
214*ac5f8ed1SGreg Roach        $settings = $request->getParsedBody();
215*ac5f8ed1SGreg Roach
216*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'show_last_update', $settings['show_last_update'] ?? '');
217*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'show_common_surnames', $settings['show_common_surnames'] ?? '');
218*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'number_of_surnames', $settings['number_of_surnames']);
219*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_indi', $settings['stat_indi'] ?? '');
220*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_fam', $settings['stat_fam'] ?? '');
221*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_sour', $settings['stat_sour'] ?? '');
222*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_other', $settings['stat_other'] ?? '');
223*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_media', $settings['stat_media'] ?? '');
224*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_repo', $settings['stat_repo'] ?? '');
225*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_surname', $settings['stat_surname'] ?? '');
226*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_events', $settings['stat_events'] ?? '');
227*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_users', $settings['stat_users'] ?? '');
228*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_birth', $settings['stat_first_birth'] ?? '');
229*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_birth', $settings['stat_last_birth'] ?? '');
230*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_death', $settings['stat_first_death'] ?? '');
231*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_death', $settings['stat_last_death'] ?? '');
232*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_long_life', $settings['stat_long_life'] ?? '');
233*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_life', $settings['stat_avg_life'] ?? '');
234*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_most_chil', $settings['stat_most_chil'] ?? '');
235*ac5f8ed1SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_chil', $settings['stat_avg_chil'] ?? '');
236a45f9889SGreg Roach    }
237a45f9889SGreg Roach
238a45f9889SGreg Roach    /**
23976692c8bSGreg Roach     * An HTML form to edit block settings
24076692c8bSGreg Roach     *
241e490cd80SGreg Roach     * @param Tree $tree
24276692c8bSGreg Roach     * @param int  $block_id
243a9430be8SGreg Roach     *
244a9430be8SGreg Roach     * @return void
24576692c8bSGreg Roach     */
246e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
247c1010edaSGreg Roach    {
248e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
249e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
250f36626dbSGreg Roach        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
251e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
252e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
253e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
254e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
255e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
256e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
257e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
258e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
259e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
260e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
261e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
262e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
263e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
264e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
265e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
266e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2678c2e8227SGreg Roach
268147e99aaSGreg Roach        echo view('modules/gedcom_stats/config', [
269c385536dSGreg Roach            'show_last_update'     => $show_last_update,
270c385536dSGreg Roach            'show_common_surnames' => $show_common_surnames,
271c385536dSGreg Roach            'number_of_surnames'   => $number_of_surnames,
272c385536dSGreg Roach            'stat_indi'            => $stat_indi,
273c385536dSGreg Roach            'stat_fam'             => $stat_fam,
274c385536dSGreg Roach            'stat_sour'            => $stat_sour,
275c385536dSGreg Roach            'stat_media'           => $stat_media,
276c385536dSGreg Roach            'stat_repo'            => $stat_repo,
277c385536dSGreg Roach            'stat_surname'         => $stat_surname,
278c385536dSGreg Roach            'stat_events'          => $stat_events,
279c385536dSGreg Roach            'stat_users'           => $stat_users,
280c385536dSGreg Roach            'stat_first_birth'     => $stat_first_birth,
281c385536dSGreg Roach            'stat_last_birth'      => $stat_last_birth,
282c385536dSGreg Roach            'stat_first_death'     => $stat_first_death,
283c385536dSGreg Roach            'stat_last_death'      => $stat_last_death,
284c385536dSGreg Roach            'stat_long_life'       => $stat_long_life,
285c385536dSGreg Roach            'stat_avg_life'        => $stat_avg_life,
286c385536dSGreg Roach            'stat_most_chil'       => $stat_most_chil,
287c385536dSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
288c385536dSGreg Roach        ]);
2898c2e8227SGreg Roach    }
2908c2e8227SGreg Roach}
291