xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 961ec7555e8fa1828ea16f2a1fd6a74f140ce707)
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;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
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 */
3149a243cbSGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleInterface, 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
38*961ec755SGreg Roach    /**
39*961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
40*961ec755SGreg Roach     *
41*961ec755SGreg Roach     * @return string
42*961ec755SGreg Roach     */
4349a243cbSGreg Roach    public function title(): string
44c1010edaSGreg Roach    {
45bbb76c12SGreg Roach        /* I18N: Name of a module */
46bbb76c12SGreg Roach        return I18N::translate('Statistics');
478c2e8227SGreg Roach    }
488c2e8227SGreg Roach
49*961ec755SGreg Roach    /**
50*961ec755SGreg Roach     * A sentence describing what this module does.
51*961ec755SGreg Roach     *
52*961ec755SGreg Roach     * @return string
53*961ec755SGreg Roach     */
5449a243cbSGreg Roach    public function description(): string
55c1010edaSGreg Roach    {
56bbb76c12SGreg Roach        /* I18N: Description of “Statistics” module */
57bbb76c12SGreg Roach        return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
588c2e8227SGreg Roach    }
598c2e8227SGreg Roach
6076692c8bSGreg Roach    /**
6176692c8bSGreg Roach     * Generate the HTML content of this block.
6276692c8bSGreg Roach     *
63e490cd80SGreg Roach     * @param Tree     $tree
6476692c8bSGreg Roach     * @param int      $block_id
655f2ae573SGreg Roach     * @param string   $ctype
66727f238cSGreg Roach     * @param string[] $cfg
6776692c8bSGreg Roach     *
6876692c8bSGreg Roach     * @return string
6976692c8bSGreg Roach     */
705f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
71c1010edaSGreg Roach    {
72e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
73e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
7480536c2dSGreg Roach        $number_of_surnames   = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
75e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
76e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
77e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
78e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
79e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
80e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
81e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
82e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
83e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
84e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
85e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
86e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
87e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
88e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
89e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
90e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
918c2e8227SGreg Roach
92c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
938c2e8227SGreg Roach
948c2e8227SGreg Roach        if ($show_common_surnames) {
9580536c2dSGreg Roach            // Use the count of base surnames.
961ae92905SGreg Roach            $top_surnames = DB::table('name')
971ae92905SGreg Roach                ->where('n_file', '=', $tree->id())
981ae92905SGreg Roach                ->where('n_type', '<>', '_MARNM')
991ae92905SGreg Roach                ->whereNotIn('n_surn', ['@N.N.', ''])
1001ae92905SGreg Roach                ->groupBy('n_surn')
1011ae92905SGreg Roach                ->orderByDesc(DB::raw('COUNT(n_surn)'))
1021ae92905SGreg Roach                ->take($number_of_surnames)
1031ae92905SGreg Roach                ->pluck('n_surn');
104e0275e5bSGreg Roach
10513abd6f3SGreg Roach            $all_surnames = [];
1061ae92905SGreg Roach
10780536c2dSGreg Roach            foreach ($top_surnames as $top_surname) {
1081ae92905SGreg Roach                $variants = DB::table('name')
1091ae92905SGreg Roach                    ->where('n_file', '=', $tree->id())
1101ae92905SGreg Roach                    ->where(DB::raw('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname)
1111ae92905SGreg Roach                    ->groupBy('surname')
1121ae92905SGreg Roach                    ->select([DB::raw('n_surname /*! COLLATE utf8_bin */ AS surname'), DB::raw('count(*) AS total')])
1131ae92905SGreg Roach                    ->pluck('total', 'surname')
1141ae92905SGreg Roach                    ->all();
11580536c2dSGreg Roach
11680536c2dSGreg Roach                $all_surnames[$top_surname] = $variants;
117f36626dbSGreg Roach            }
11880536c2dSGreg Roach
11980536c2dSGreg Roach            uksort($all_surnames, [I18N::class, 'strcasecmp']);
1201d3c0c1aSGreg Roach
121e490cd80SGreg Roach            $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'individual-list', $tree);
1221d3c0c1aSGreg Roach        } else {
1231d3c0c1aSGreg Roach            $surnames = '';
1248c2e8227SGreg Roach        }
1251d3c0c1aSGreg Roach
126147e99aaSGreg Roach        $content = view('modules/gedcom_stats/statistics', [
1271d3c0c1aSGreg Roach            'show_last_update'     => $show_last_update,
1281d3c0c1aSGreg Roach            'show_common_surnames' => $show_common_surnames,
1291d3c0c1aSGreg Roach            'number_of_surnames'   => $number_of_surnames,
1301d3c0c1aSGreg Roach            'stat_indi'            => $stat_indi,
1311d3c0c1aSGreg Roach            'stat_fam'             => $stat_fam,
1321d3c0c1aSGreg Roach            'stat_sour'            => $stat_sour,
1331d3c0c1aSGreg Roach            'stat_media'           => $stat_media,
1341d3c0c1aSGreg Roach            'stat_repo'            => $stat_repo,
1351d3c0c1aSGreg Roach            'stat_surname'         => $stat_surname,
1361d3c0c1aSGreg Roach            'stat_events'          => $stat_events,
1371d3c0c1aSGreg Roach            'stat_users'           => $stat_users,
1381d3c0c1aSGreg Roach            'stat_first_birth'     => $stat_first_birth,
1391d3c0c1aSGreg Roach            'stat_last_birth'      => $stat_last_birth,
1401d3c0c1aSGreg Roach            'stat_first_death'     => $stat_first_death,
1411d3c0c1aSGreg Roach            'stat_last_death'      => $stat_last_death,
1421d3c0c1aSGreg Roach            'stat_long_life'       => $stat_long_life,
1431d3c0c1aSGreg Roach            'stat_avg_life'        => $stat_avg_life,
1441d3c0c1aSGreg Roach            'stat_most_chil'       => $stat_most_chil,
1451d3c0c1aSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
146e490cd80SGreg Roach            'stats'                => new Stats($tree),
1471d3c0c1aSGreg Roach            'surnames'             => $surnames,
1481d3c0c1aSGreg Roach        ]);
14928941e3cSGreg Roach
1506a8879feSGreg Roach        if ($ctype !== '') {
151e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
152c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
153c1010edaSGreg Roach                    'block_id' => $block_id,
154aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
155c1010edaSGreg Roach                ]);
156397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
157c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
158c1010edaSGreg Roach                    'block_id' => $block_id,
159aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
160c1010edaSGreg Roach                ]);
1619c6524dcSGreg Roach            } else {
1629c6524dcSGreg Roach                $config_url = '';
1639c6524dcSGreg Roach            }
1649c6524dcSGreg Roach
165147e99aaSGreg Roach            return view('modules/block-template', [
1669c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1679c6524dcSGreg Roach                'id'         => $block_id,
1689c6524dcSGreg Roach                'config_url' => $config_url,
16949a243cbSGreg Roach                'title'      => $this->title(),
1709c6524dcSGreg Roach                'content'    => $content,
1719c6524dcSGreg Roach            ]);
1728c2e8227SGreg Roach        }
173b2ce94c6SRico Sonntag
174b2ce94c6SRico Sonntag        return $content;
1758c2e8227SGreg Roach    }
1768c2e8227SGreg Roach
1778c2e8227SGreg Roach    /** {@inheritdoc} */
178c1010edaSGreg Roach    public function loadAjax(): bool
179c1010edaSGreg Roach    {
1808c2e8227SGreg Roach        return true;
1818c2e8227SGreg Roach    }
1828c2e8227SGreg Roach
1838c2e8227SGreg Roach    /** {@inheritdoc} */
184c1010edaSGreg Roach    public function isUserBlock(): bool
185c1010edaSGreg Roach    {
1868c2e8227SGreg Roach        return true;
1878c2e8227SGreg Roach    }
1888c2e8227SGreg Roach
1898c2e8227SGreg Roach    /** {@inheritdoc} */
190c1010edaSGreg Roach    public function isGedcomBlock(): bool
191c1010edaSGreg Roach    {
1928c2e8227SGreg Roach        return true;
1938c2e8227SGreg Roach    }
1948c2e8227SGreg Roach
19576692c8bSGreg Roach    /**
196a45f9889SGreg Roach     * Update the configuration for a block.
197a45f9889SGreg Roach     *
198a45f9889SGreg Roach     * @param Request $request
199a45f9889SGreg Roach     * @param int     $block_id
200a45f9889SGreg Roach     *
201a45f9889SGreg Roach     * @return void
202a45f9889SGreg Roach     */
203a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
204a45f9889SGreg Roach    {
205a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_last_update', $request->get('show_last_update', ''));
206a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_common_surnames', $request->get('show_common_surnames', ''));
207a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'number_of_surnames', $request->get('number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES));
208a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_indi', $request->get('stat_indi', ''));
209a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_fam', $request->get('stat_fam', ''));
210a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_sour', $request->get('stat_sour', ''));
211a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_other', $request->get('stat_other', ''));
212a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_media', $request->get('stat_media', ''));
213a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_repo', $request->get('stat_repo', ''));
214a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_surname', $request->get('stat_surname', ''));
215a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_events', $request->get('stat_events', ''));
216a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_users', $request->get('stat_users', ''));
217a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_birth', $request->get('stat_first_birth', ''));
218a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_birth', $request->get('stat_last_birth', ''));
219a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_death', $request->get('stat_first_death', ''));
220a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_death', $request->get('stat_last_death', ''));
221a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_long_life', $request->get('stat_long_life', ''));
222a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_life', $request->get('stat_avg_life', ''));
223a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_most_chil', $request->get('stat_most_chil', ''));
224a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_chil', $request->get('stat_avg_chil', ''));
225a45f9889SGreg Roach    }
226a45f9889SGreg Roach
227a45f9889SGreg Roach    /**
22876692c8bSGreg Roach     * An HTML form to edit block settings
22976692c8bSGreg Roach     *
230e490cd80SGreg Roach     * @param Tree $tree
23176692c8bSGreg Roach     * @param int  $block_id
232a9430be8SGreg Roach     *
233a9430be8SGreg Roach     * @return void
23476692c8bSGreg Roach     */
235a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
236c1010edaSGreg Roach    {
237e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
238e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
239f36626dbSGreg Roach        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
240e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
241e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
242e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
243e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
244e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
245e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
246e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
247e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
248e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
249e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
250e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
251e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
252e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
253e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
254e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
255e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2568c2e8227SGreg Roach
257147e99aaSGreg Roach        echo view('modules/gedcom_stats/config', [
258c385536dSGreg Roach            'show_last_update'     => $show_last_update,
259c385536dSGreg Roach            'show_common_surnames' => $show_common_surnames,
260c385536dSGreg Roach            'number_of_surnames'   => $number_of_surnames,
261c385536dSGreg Roach            'stat_indi'            => $stat_indi,
262c385536dSGreg Roach            'stat_fam'             => $stat_fam,
263c385536dSGreg Roach            'stat_sour'            => $stat_sour,
264c385536dSGreg Roach            'stat_media'           => $stat_media,
265c385536dSGreg Roach            'stat_repo'            => $stat_repo,
266c385536dSGreg Roach            'stat_surname'         => $stat_surname,
267c385536dSGreg Roach            'stat_events'          => $stat_events,
268c385536dSGreg Roach            'stat_users'           => $stat_users,
269c385536dSGreg Roach            'stat_first_birth'     => $stat_first_birth,
270c385536dSGreg Roach            'stat_last_birth'      => $stat_last_birth,
271c385536dSGreg Roach            'stat_first_death'     => $stat_first_death,
272c385536dSGreg Roach            'stat_last_death'      => $stat_last_death,
273c385536dSGreg Roach            'stat_long_life'       => $stat_long_life,
274c385536dSGreg Roach            'stat_avg_life'        => $stat_avg_life,
275c385536dSGreg Roach            'stat_most_chil'       => $stat_most_chil,
276c385536dSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
277c385536dSGreg Roach        ]);
2788c2e8227SGreg Roach    }
2798c2e8227SGreg Roach}
280