xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 80536c2dcf5101ec7f670e81700e0e42cc1cf6a1)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
19*80536c2dSGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb;
22f36626dbSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
24f36626dbSGreg Roachuse Fisharebest\Webtrees\Query\QueryName;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
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 */
34f36626dbSGreg Roach    const DEFAULT_NUMBER_OF_SURNAMES = 10;
35f36626dbSGreg Roach
368c2e8227SGreg Roach    /** {@inheritdoc} */
37c1010edaSGreg Roach    public function getTitle()
38c1010edaSGreg Roach    {
391d3c0c1aSGreg Roach        return /* I18N: Name of a module */
401d3c0c1aSGreg Roach            I18N::translate('Statistics');
418c2e8227SGreg Roach    }
428c2e8227SGreg Roach
438c2e8227SGreg Roach    /** {@inheritdoc} */
44c1010edaSGreg Roach    public function getDescription()
45c1010edaSGreg Roach    {
461d3c0c1aSGreg Roach        return /* I18N: Description of “Statistics” module */
471d3c0c1aSGreg Roach            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
5576692c8bSGreg Roach     * @param bool     $template
56727f238cSGreg Roach     * @param string[] $cfg
5776692c8bSGreg Roach     *
5876692c8bSGreg Roach     * @return string
5976692c8bSGreg Roach     */
60c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
61c1010edaSGreg Roach    {
62e490cd80SGreg Roach        global $ctype;
638c2e8227SGreg Roach
64e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
65e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
66*80536c2dSGreg Roach        $number_of_surnames   = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
67e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
68e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
69e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
70e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
71e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
72e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
73e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
74e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
75e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
76e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
77e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
78e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
79e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
80e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
81e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
82e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
838c2e8227SGreg Roach
84c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
858c2e8227SGreg Roach
868c2e8227SGreg Roach        if ($show_common_surnames) {
87*80536c2dSGreg Roach            // Use the count of base surnames.
88*80536c2dSGreg Roach            $top_surnames = Database::prepare(
89*80536c2dSGreg Roach                "SELECT n_surn FROM `##name`" .
90*80536c2dSGreg Roach                " WHERE n_file = :tree_id AND n_type != '_MARNM' AND n_surn NOT IN ('@N.N.', '')" .
91*80536c2dSGreg Roach                " GROUP BY n_surn" .
92*80536c2dSGreg Roach                " ORDER BY COUNT(n_surn) DESC" .
93*80536c2dSGreg Roach                " LIMIT :limit"
94*80536c2dSGreg Roach            )->execute([
95*80536c2dSGreg Roach                'tree_id' => $tree->getTreeId(),
96*80536c2dSGreg Roach                'limit'   => $number_of_surnames,
97*80536c2dSGreg Roach            ])->fetchOneColumn();
98e0275e5bSGreg Roach
9913abd6f3SGreg Roach            $all_surnames = [];
100*80536c2dSGreg Roach            foreach ($top_surnames as $top_surname) {
101*80536c2dSGreg Roach                $variants = Database::prepare(
102*80536c2dSGreg Roach                    "SELECT n_surname COLLATE utf8_bin, COUNT(*) FROM `##name` WHERE n_file = :tree_id AND n_surn COLLATE :collate = :surname GROUP BY 1"
103*80536c2dSGreg Roach                )->execute([
104*80536c2dSGreg Roach                    'collate' => I18N::collation(),
105*80536c2dSGreg Roach                    'surname' => $top_surname,
106*80536c2dSGreg Roach                    'tree_id' => $tree->getTreeId(),
107*80536c2dSGreg Roach                ])->fetchAssoc();
108*80536c2dSGreg Roach
109*80536c2dSGreg Roach                $all_surnames[$top_surname] = $variants;
110f36626dbSGreg Roach            }
111*80536c2dSGreg Roach
112*80536c2dSGreg Roach            uksort($all_surnames, [I18N::class, 'strcasecmp']);
1131d3c0c1aSGreg Roach
114e490cd80SGreg Roach            $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'individual-list', $tree);
1151d3c0c1aSGreg Roach        } else {
1161d3c0c1aSGreg Roach            $surnames = '';
1178c2e8227SGreg Roach        }
1181d3c0c1aSGreg Roach
119147e99aaSGreg Roach        $content = view('modules/gedcom_stats/statistics', [
1201d3c0c1aSGreg Roach            'show_last_update'     => $show_last_update,
1211d3c0c1aSGreg Roach            'show_common_surnames' => $show_common_surnames,
1221d3c0c1aSGreg Roach            'number_of_surnames'   => $number_of_surnames,
1231d3c0c1aSGreg Roach            'stat_indi'            => $stat_indi,
1241d3c0c1aSGreg Roach            'stat_fam'             => $stat_fam,
1251d3c0c1aSGreg Roach            'stat_sour'            => $stat_sour,
1261d3c0c1aSGreg Roach            'stat_media'           => $stat_media,
1271d3c0c1aSGreg Roach            'stat_repo'            => $stat_repo,
1281d3c0c1aSGreg Roach            'stat_surname'         => $stat_surname,
1291d3c0c1aSGreg Roach            'stat_events'          => $stat_events,
1301d3c0c1aSGreg Roach            'stat_users'           => $stat_users,
1311d3c0c1aSGreg Roach            'stat_first_birth'     => $stat_first_birth,
1321d3c0c1aSGreg Roach            'stat_last_birth'      => $stat_last_birth,
1331d3c0c1aSGreg Roach            'stat_first_death'     => $stat_first_death,
1341d3c0c1aSGreg Roach            'stat_last_death'      => $stat_last_death,
1351d3c0c1aSGreg Roach            'stat_long_life'       => $stat_long_life,
1361d3c0c1aSGreg Roach            'stat_avg_life'        => $stat_avg_life,
1371d3c0c1aSGreg Roach            'stat_most_chil'       => $stat_most_chil,
1381d3c0c1aSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
139e490cd80SGreg Roach            'stats'                => new Stats($tree),
1401d3c0c1aSGreg Roach            'surnames'             => $surnames,
1411d3c0c1aSGreg Roach        ]);
14228941e3cSGreg Roach
1438c2e8227SGreg Roach        if ($template) {
144e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
145c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
146c1010edaSGreg Roach                    'block_id' => $block_id,
147c1010edaSGreg Roach                    'ged'      => $tree->getName(),
148c1010edaSGreg Roach                ]);
149397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
150c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
151c1010edaSGreg Roach                    'block_id' => $block_id,
152c1010edaSGreg Roach                    'ged'      => $tree->getName(),
153c1010edaSGreg Roach                ]);
1549c6524dcSGreg Roach            } else {
1559c6524dcSGreg Roach                $config_url = '';
1569c6524dcSGreg Roach            }
1579c6524dcSGreg Roach
158147e99aaSGreg Roach            return view('modules/block-template', [
1599c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1609c6524dcSGreg Roach                'id'         => $block_id,
1619c6524dcSGreg Roach                'config_url' => $config_url,
1629c6524dcSGreg Roach                'title'      => $this->getTitle(),
1639c6524dcSGreg Roach                'content'    => $content,
1649c6524dcSGreg Roach            ]);
1658c2e8227SGreg Roach        } else {
1668c2e8227SGreg Roach            return $content;
1678c2e8227SGreg Roach        }
1688c2e8227SGreg Roach    }
1698c2e8227SGreg Roach
1708c2e8227SGreg Roach    /** {@inheritdoc} */
171c1010edaSGreg Roach    public function loadAjax(): bool
172c1010edaSGreg Roach    {
1738c2e8227SGreg Roach        return true;
1748c2e8227SGreg Roach    }
1758c2e8227SGreg Roach
1768c2e8227SGreg Roach    /** {@inheritdoc} */
177c1010edaSGreg Roach    public function isUserBlock(): bool
178c1010edaSGreg Roach    {
1798c2e8227SGreg Roach        return true;
1808c2e8227SGreg Roach    }
1818c2e8227SGreg Roach
1828c2e8227SGreg Roach    /** {@inheritdoc} */
183c1010edaSGreg Roach    public function isGedcomBlock(): bool
184c1010edaSGreg Roach    {
1858c2e8227SGreg Roach        return true;
1868c2e8227SGreg Roach    }
1878c2e8227SGreg Roach
18876692c8bSGreg Roach    /**
18976692c8bSGreg Roach     * An HTML form to edit block settings
19076692c8bSGreg Roach     *
191e490cd80SGreg Roach     * @param Tree $tree
19276692c8bSGreg Roach     * @param int  $block_id
193a9430be8SGreg Roach     *
194a9430be8SGreg Roach     * @return void
19576692c8bSGreg Roach     */
196c1010edaSGreg Roach    public function configureBlock(Tree $tree, int $block_id)
197c1010edaSGreg Roach    {
198c385536dSGreg Roach        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
199e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
200e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
201f36626dbSGreg Roach            $this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
202e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
203e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
204e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
205e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
206e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
207e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
208e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
209e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
210e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
211e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
212e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
213e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
214e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
215e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
216e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
217e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
218e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
219c385536dSGreg Roach
220c385536dSGreg Roach            return;
2218c2e8227SGreg Roach        }
2228c2e8227SGreg Roach
223e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
224e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
225f36626dbSGreg Roach        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
226e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
227e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
228e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
229e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
230e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
231e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
232e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
233e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
234e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
235e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
236e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
237e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
238e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
239e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
240e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
241e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2428c2e8227SGreg Roach
243147e99aaSGreg Roach        echo view('modules/gedcom_stats/config', [
244c385536dSGreg Roach            'show_last_update'     => $show_last_update,
245c385536dSGreg Roach            'show_common_surnames' => $show_common_surnames,
246c385536dSGreg Roach            'number_of_surnames'   => $number_of_surnames,
247c385536dSGreg Roach            'stat_indi'            => $stat_indi,
248c385536dSGreg Roach            'stat_fam'             => $stat_fam,
249c385536dSGreg Roach            'stat_sour'            => $stat_sour,
250c385536dSGreg Roach            'stat_media'           => $stat_media,
251c385536dSGreg Roach            'stat_repo'            => $stat_repo,
252c385536dSGreg Roach            'stat_surname'         => $stat_surname,
253c385536dSGreg Roach            'stat_events'          => $stat_events,
254c385536dSGreg Roach            'stat_users'           => $stat_users,
255c385536dSGreg Roach            'stat_first_birth'     => $stat_first_birth,
256c385536dSGreg Roach            'stat_last_birth'      => $stat_last_birth,
257c385536dSGreg Roach            'stat_first_death'     => $stat_first_death,
258c385536dSGreg Roach            'stat_last_death'      => $stat_last_death,
259c385536dSGreg Roach            'stat_long_life'       => $stat_long_life,
260c385536dSGreg Roach            'stat_avg_life'        => $stat_avg_life,
261c385536dSGreg Roach            'stat_most_chil'       => $stat_most_chil,
262c385536dSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
263c385536dSGreg Roach        ]);
2648c2e8227SGreg Roach    }
2658c2e8227SGreg Roach}
266