xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg 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;
2180536c2dSGreg Roachuse Fisharebest\Webtrees\Database;
22f36626dbSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
308c2e8227SGreg Roach */
31c1010edaSGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
33f36626dbSGreg Roach    /** Show this number of surnames by default */
34acf76a54SGreg Roach    const DEFAULT_NUMBER_OF_SURNAMES = '10';
35f36626dbSGreg Roach
368c2e8227SGreg Roach    /** {@inheritdoc} */
378f53f488SRico Sonntag    public function getTitle(): string
38c1010edaSGreg Roach    {
39bbb76c12SGreg Roach        /* I18N: Name of a module */
40bbb76c12SGreg Roach        return I18N::translate('Statistics');
418c2e8227SGreg Roach    }
428c2e8227SGreg Roach
438c2e8227SGreg Roach    /** {@inheritdoc} */
448f53f488SRico Sonntag    public function getDescription(): string
45c1010edaSGreg Roach    {
46bbb76c12SGreg Roach        /* I18N: Description of “Statistics” module */
47bbb76c12SGreg Roach        return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
488c2e8227SGreg Roach    }
498c2e8227SGreg Roach
5076692c8bSGreg Roach    /**
5176692c8bSGreg Roach     * Generate the HTML content of this block.
5276692c8bSGreg Roach     *
53e490cd80SGreg Roach     * @param Tree     $tree
5476692c8bSGreg Roach     * @param int      $block_id
555f2ae573SGreg Roach     * @param string   $ctype
56727f238cSGreg Roach     * @param string[] $cfg
5776692c8bSGreg Roach     *
5876692c8bSGreg Roach     * @return string
5976692c8bSGreg Roach     */
605f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
61c1010edaSGreg Roach    {
62e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
63e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
6480536c2dSGreg Roach        $number_of_surnames   = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
65e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
66e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
67e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
68e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
69e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
70e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
71e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
72e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
73e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
74e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
75e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
76e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
77e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
78e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
79e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
80e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
818c2e8227SGreg Roach
82c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
838c2e8227SGreg Roach
848c2e8227SGreg Roach        if ($show_common_surnames) {
8580536c2dSGreg Roach            // Use the count of base surnames.
8680536c2dSGreg Roach            $top_surnames = Database::prepare(
8780536c2dSGreg Roach                "SELECT n_surn FROM `##name`" .
8880536c2dSGreg Roach                " WHERE n_file = :tree_id AND n_type != '_MARNM' AND n_surn NOT IN ('@N.N.', '')" .
8980536c2dSGreg Roach                " GROUP BY n_surn" .
9080536c2dSGreg Roach                " ORDER BY COUNT(n_surn) DESC" .
9180536c2dSGreg Roach                " LIMIT :limit"
9280536c2dSGreg Roach            )->execute([
9372cf66d4SGreg Roach                'tree_id' => $tree->id(),
9480536c2dSGreg Roach                'limit'   => $number_of_surnames,
9580536c2dSGreg Roach            ])->fetchOneColumn();
96e0275e5bSGreg Roach
9713abd6f3SGreg Roach            $all_surnames = [];
9880536c2dSGreg Roach            foreach ($top_surnames as $top_surname) {
9980536c2dSGreg Roach                $variants = Database::prepare(
10080536c2dSGreg Roach                    "SELECT n_surname COLLATE utf8_bin, COUNT(*) FROM `##name` WHERE n_file = :tree_id AND n_surn COLLATE :collate = :surname GROUP BY 1"
10180536c2dSGreg Roach                )->execute([
10280536c2dSGreg Roach                    'collate' => I18N::collation(),
10380536c2dSGreg Roach                    'surname' => $top_surname,
10472cf66d4SGreg Roach                    'tree_id' => $tree->id(),
10580536c2dSGreg Roach                ])->fetchAssoc();
10680536c2dSGreg Roach
10780536c2dSGreg Roach                $all_surnames[$top_surname] = $variants;
108f36626dbSGreg Roach            }
10980536c2dSGreg Roach
11080536c2dSGreg Roach            uksort($all_surnames, [I18N::class, 'strcasecmp']);
1111d3c0c1aSGreg Roach
112e490cd80SGreg Roach            $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'individual-list', $tree);
1131d3c0c1aSGreg Roach        } else {
1141d3c0c1aSGreg Roach            $surnames = '';
1158c2e8227SGreg Roach        }
1161d3c0c1aSGreg Roach
117147e99aaSGreg Roach        $content = view('modules/gedcom_stats/statistics', [
1181d3c0c1aSGreg Roach            'show_last_update'     => $show_last_update,
1191d3c0c1aSGreg Roach            'show_common_surnames' => $show_common_surnames,
1201d3c0c1aSGreg Roach            'number_of_surnames'   => $number_of_surnames,
1211d3c0c1aSGreg Roach            'stat_indi'            => $stat_indi,
1221d3c0c1aSGreg Roach            'stat_fam'             => $stat_fam,
1231d3c0c1aSGreg Roach            'stat_sour'            => $stat_sour,
1241d3c0c1aSGreg Roach            'stat_media'           => $stat_media,
1251d3c0c1aSGreg Roach            'stat_repo'            => $stat_repo,
1261d3c0c1aSGreg Roach            'stat_surname'         => $stat_surname,
1271d3c0c1aSGreg Roach            'stat_events'          => $stat_events,
1281d3c0c1aSGreg Roach            'stat_users'           => $stat_users,
1291d3c0c1aSGreg Roach            'stat_first_birth'     => $stat_first_birth,
1301d3c0c1aSGreg Roach            'stat_last_birth'      => $stat_last_birth,
1311d3c0c1aSGreg Roach            'stat_first_death'     => $stat_first_death,
1321d3c0c1aSGreg Roach            'stat_last_death'      => $stat_last_death,
1331d3c0c1aSGreg Roach            'stat_long_life'       => $stat_long_life,
1341d3c0c1aSGreg Roach            'stat_avg_life'        => $stat_avg_life,
1351d3c0c1aSGreg Roach            'stat_most_chil'       => $stat_most_chil,
1361d3c0c1aSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
137e490cd80SGreg Roach            'stats'                => new Stats($tree),
1381d3c0c1aSGreg Roach            'surnames'             => $surnames,
1391d3c0c1aSGreg Roach        ]);
14028941e3cSGreg Roach
1416a8879feSGreg Roach        if ($ctype !== '') {
142e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
143c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
144c1010edaSGreg Roach                    'block_id' => $block_id,
145aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
146c1010edaSGreg Roach                ]);
147397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
148c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
149c1010edaSGreg Roach                    'block_id' => $block_id,
150aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
151c1010edaSGreg Roach                ]);
1529c6524dcSGreg Roach            } else {
1539c6524dcSGreg Roach                $config_url = '';
1549c6524dcSGreg Roach            }
1559c6524dcSGreg Roach
156147e99aaSGreg Roach            return view('modules/block-template', [
1579c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1589c6524dcSGreg Roach                'id'         => $block_id,
1599c6524dcSGreg Roach                'config_url' => $config_url,
1609c6524dcSGreg Roach                'title'      => $this->getTitle(),
1619c6524dcSGreg Roach                'content'    => $content,
1629c6524dcSGreg Roach            ]);
1638c2e8227SGreg Roach        }
164b2ce94c6SRico Sonntag
165b2ce94c6SRico Sonntag        return $content;
1668c2e8227SGreg Roach    }
1678c2e8227SGreg Roach
1688c2e8227SGreg Roach    /** {@inheritdoc} */
169c1010edaSGreg Roach    public function loadAjax(): bool
170c1010edaSGreg Roach    {
1718c2e8227SGreg Roach        return true;
1728c2e8227SGreg Roach    }
1738c2e8227SGreg Roach
1748c2e8227SGreg Roach    /** {@inheritdoc} */
175c1010edaSGreg Roach    public function isUserBlock(): bool
176c1010edaSGreg Roach    {
1778c2e8227SGreg Roach        return true;
1788c2e8227SGreg Roach    }
1798c2e8227SGreg Roach
1808c2e8227SGreg Roach    /** {@inheritdoc} */
181c1010edaSGreg Roach    public function isGedcomBlock(): bool
182c1010edaSGreg Roach    {
1838c2e8227SGreg Roach        return true;
1848c2e8227SGreg Roach    }
1858c2e8227SGreg Roach
18676692c8bSGreg Roach    /**
187a45f9889SGreg Roach     * Update the configuration for a block.
188a45f9889SGreg Roach     *
189a45f9889SGreg Roach     * @param Request $request
190a45f9889SGreg Roach     * @param int     $block_id
191a45f9889SGreg Roach     *
192a45f9889SGreg Roach     * @return void
193a45f9889SGreg Roach     */
194a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
195a45f9889SGreg Roach    {
196a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_last_update', $request->get('show_last_update', ''));
197a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'show_common_surnames', $request->get('show_common_surnames', ''));
198a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'number_of_surnames', $request->get('number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES));
199a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_indi', $request->get('stat_indi', ''));
200a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_fam', $request->get('stat_fam', ''));
201a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_sour', $request->get('stat_sour', ''));
202a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_other', $request->get('stat_other', ''));
203a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_media', $request->get('stat_media', ''));
204a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_repo', $request->get('stat_repo', ''));
205a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_surname', $request->get('stat_surname', ''));
206a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_events', $request->get('stat_events', ''));
207a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_users', $request->get('stat_users', ''));
208a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_birth', $request->get('stat_first_birth', ''));
209a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_birth', $request->get('stat_last_birth', ''));
210a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_death', $request->get('stat_first_death', ''));
211a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_death', $request->get('stat_last_death', ''));
212a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_long_life', $request->get('stat_long_life', ''));
213a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_life', $request->get('stat_avg_life', ''));
214a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_most_chil', $request->get('stat_most_chil', ''));
215a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_chil', $request->get('stat_avg_chil', ''));
216a45f9889SGreg Roach    }
217a45f9889SGreg Roach
218a45f9889SGreg Roach    /**
21976692c8bSGreg Roach     * An HTML form to edit block settings
22076692c8bSGreg Roach     *
221e490cd80SGreg Roach     * @param Tree $tree
22276692c8bSGreg Roach     * @param int  $block_id
223a9430be8SGreg Roach     *
224a9430be8SGreg Roach     * @return void
22576692c8bSGreg Roach     */
226a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
227c1010edaSGreg Roach    {
228e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
229e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
230f36626dbSGreg Roach        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
231e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
232e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
233e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
234e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
235e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
236e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
237e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
238e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
239e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
240e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
241e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
242e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
243e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
244e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
245e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
246e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2478c2e8227SGreg Roach
248147e99aaSGreg Roach        echo view('modules/gedcom_stats/config', [
249c385536dSGreg Roach            'show_last_update'     => $show_last_update,
250c385536dSGreg Roach            'show_common_surnames' => $show_common_surnames,
251c385536dSGreg Roach            'number_of_surnames'   => $number_of_surnames,
252c385536dSGreg Roach            'stat_indi'            => $stat_indi,
253c385536dSGreg Roach            'stat_fam'             => $stat_fam,
254c385536dSGreg Roach            'stat_sour'            => $stat_sour,
255c385536dSGreg Roach            'stat_media'           => $stat_media,
256c385536dSGreg Roach            'stat_repo'            => $stat_repo,
257c385536dSGreg Roach            'stat_surname'         => $stat_surname,
258c385536dSGreg Roach            'stat_events'          => $stat_events,
259c385536dSGreg Roach            'stat_users'           => $stat_users,
260c385536dSGreg Roach            'stat_first_birth'     => $stat_first_birth,
261c385536dSGreg Roach            'stat_last_birth'      => $stat_last_birth,
262c385536dSGreg Roach            'stat_first_death'     => $stat_first_death,
263c385536dSGreg Roach            'stat_last_death'      => $stat_last_death,
264c385536dSGreg Roach            'stat_long_life'       => $stat_long_life,
265c385536dSGreg Roach            'stat_avg_life'        => $stat_avg_life,
266c385536dSGreg Roach            'stat_most_chil'       => $stat_most_chil,
267c385536dSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
268c385536dSGreg Roach        ]);
2698c2e8227SGreg Roach    }
2708c2e8227SGreg Roach}
271