xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
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;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb;
21f36626dbSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
23f36626dbSGreg Roachuse Fisharebest\Webtrees\Query\QueryName;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
268c2e8227SGreg Roach
278c2e8227SGreg Roach/**
288c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
298c2e8227SGreg Roach */
30*c1010edaSGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
31*c1010edaSGreg Roach{
32f36626dbSGreg Roach    /** Show this number of surnames by default */
33f36626dbSGreg Roach    const DEFAULT_NUMBER_OF_SURNAMES = 10;
34f36626dbSGreg Roach
358c2e8227SGreg Roach    /** {@inheritdoc} */
36*c1010edaSGreg Roach    public function getTitle()
37*c1010edaSGreg Roach    {
381d3c0c1aSGreg Roach        return /* I18N: Name of a module */
391d3c0c1aSGreg Roach            I18N::translate('Statistics');
408c2e8227SGreg Roach    }
418c2e8227SGreg Roach
428c2e8227SGreg Roach    /** {@inheritdoc} */
43*c1010edaSGreg Roach    public function getDescription()
44*c1010edaSGreg Roach    {
451d3c0c1aSGreg Roach        return /* I18N: Description of “Statistics” module */
461d3c0c1aSGreg Roach            I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
478c2e8227SGreg Roach    }
488c2e8227SGreg Roach
4976692c8bSGreg Roach    /**
5076692c8bSGreg Roach     * Generate the HTML content of this block.
5176692c8bSGreg Roach     *
52e490cd80SGreg Roach     * @param Tree     $tree
5376692c8bSGreg Roach     * @param int      $block_id
5476692c8bSGreg Roach     * @param bool     $template
55727f238cSGreg Roach     * @param string[] $cfg
5676692c8bSGreg Roach     *
5776692c8bSGreg Roach     * @return string
5876692c8bSGreg Roach     */
59*c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
60*c1010edaSGreg Roach    {
61e490cd80SGreg Roach        global $ctype;
628c2e8227SGreg Roach
63e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
64e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
65f36626dbSGreg Roach        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
66e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
67e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
68e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
69e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
70e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
71e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
72e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
73e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
74e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
75e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
76e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
77e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
78e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
79e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
80e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
81e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
828c2e8227SGreg Roach
83c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
848c2e8227SGreg Roach
858c2e8227SGreg Roach        if ($show_common_surnames) {
86e490cd80SGreg Roach            $surnames = FunctionsDb::getTopSurnames($tree->getTreeId(), 0, (int)$number_of_surnames);
87e0275e5bSGreg Roach
8813abd6f3SGreg Roach            $all_surnames = [];
89f36626dbSGreg Roach            foreach (array_keys($surnames) as $surname) {
90e490cd80SGreg Roach                $all_surnames = array_merge($all_surnames, QueryName::surnames($tree, $surname, '', false, false));
91f36626dbSGreg Roach            }
92f36626dbSGreg Roach            ksort($all_surnames);
931d3c0c1aSGreg Roach
94e490cd80SGreg Roach            $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'individual-list', $tree);
951d3c0c1aSGreg Roach        } else {
961d3c0c1aSGreg Roach            $surnames = '';
978c2e8227SGreg Roach        }
981d3c0c1aSGreg Roach
99147e99aaSGreg Roach        $content = view('modules/gedcom_stats/statistics', [
1001d3c0c1aSGreg Roach            'show_last_update'     => $show_last_update,
1011d3c0c1aSGreg Roach            'show_common_surnames' => $show_common_surnames,
1021d3c0c1aSGreg Roach            'number_of_surnames'   => $number_of_surnames,
1031d3c0c1aSGreg Roach            'stat_indi'            => $stat_indi,
1041d3c0c1aSGreg Roach            'stat_fam'             => $stat_fam,
1051d3c0c1aSGreg Roach            'stat_sour'            => $stat_sour,
1061d3c0c1aSGreg Roach            'stat_media'           => $stat_media,
1071d3c0c1aSGreg Roach            'stat_repo'            => $stat_repo,
1081d3c0c1aSGreg Roach            'stat_surname'         => $stat_surname,
1091d3c0c1aSGreg Roach            'stat_events'          => $stat_events,
1101d3c0c1aSGreg Roach            'stat_users'           => $stat_users,
1111d3c0c1aSGreg Roach            'stat_first_birth'     => $stat_first_birth,
1121d3c0c1aSGreg Roach            'stat_last_birth'      => $stat_last_birth,
1131d3c0c1aSGreg Roach            'stat_first_death'     => $stat_first_death,
1141d3c0c1aSGreg Roach            'stat_last_death'      => $stat_last_death,
1151d3c0c1aSGreg Roach            'stat_long_life'       => $stat_long_life,
1161d3c0c1aSGreg Roach            'stat_avg_life'        => $stat_avg_life,
1171d3c0c1aSGreg Roach            'stat_most_chil'       => $stat_most_chil,
1181d3c0c1aSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
119e490cd80SGreg Roach            'stats'                => new Stats($tree),
1201d3c0c1aSGreg Roach            'surnames'             => $surnames,
1211d3c0c1aSGreg Roach        ]);
12228941e3cSGreg Roach
1238c2e8227SGreg Roach        if ($template) {
124e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
125*c1010edaSGreg Roach                $config_url = route('tree-page-block-edit', [
126*c1010edaSGreg Roach                    'block_id' => $block_id,
127*c1010edaSGreg Roach                    'ged'      => $tree->getName(),
128*c1010edaSGreg Roach                ]);
129397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
130*c1010edaSGreg Roach                $config_url = route('user-page-block-edit', [
131*c1010edaSGreg Roach                    'block_id' => $block_id,
132*c1010edaSGreg Roach                    'ged'      => $tree->getName(),
133*c1010edaSGreg Roach                ]);
1349c6524dcSGreg Roach            } else {
1359c6524dcSGreg Roach                $config_url = '';
1369c6524dcSGreg Roach            }
1379c6524dcSGreg Roach
138147e99aaSGreg Roach            return view('modules/block-template', [
1399c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1409c6524dcSGreg Roach                'id'         => $block_id,
1419c6524dcSGreg Roach                'config_url' => $config_url,
1429c6524dcSGreg Roach                'title'      => $this->getTitle(),
1439c6524dcSGreg Roach                'content'    => $content,
1449c6524dcSGreg Roach            ]);
1458c2e8227SGreg Roach        } else {
1468c2e8227SGreg Roach            return $content;
1478c2e8227SGreg Roach        }
1488c2e8227SGreg Roach    }
1498c2e8227SGreg Roach
1508c2e8227SGreg Roach    /** {@inheritdoc} */
151*c1010edaSGreg Roach    public function loadAjax(): bool
152*c1010edaSGreg Roach    {
1538c2e8227SGreg Roach        return true;
1548c2e8227SGreg Roach    }
1558c2e8227SGreg Roach
1568c2e8227SGreg Roach    /** {@inheritdoc} */
157*c1010edaSGreg Roach    public function isUserBlock(): bool
158*c1010edaSGreg Roach    {
1598c2e8227SGreg Roach        return true;
1608c2e8227SGreg Roach    }
1618c2e8227SGreg Roach
1628c2e8227SGreg Roach    /** {@inheritdoc} */
163*c1010edaSGreg Roach    public function isGedcomBlock(): bool
164*c1010edaSGreg Roach    {
1658c2e8227SGreg Roach        return true;
1668c2e8227SGreg Roach    }
1678c2e8227SGreg Roach
16876692c8bSGreg Roach    /**
16976692c8bSGreg Roach     * An HTML form to edit block settings
17076692c8bSGreg Roach     *
171e490cd80SGreg Roach     * @param Tree $tree
17276692c8bSGreg Roach     * @param int  $block_id
173a9430be8SGreg Roach     *
174a9430be8SGreg Roach     * @return void
17576692c8bSGreg Roach     */
176*c1010edaSGreg Roach    public function configureBlock(Tree $tree, int $block_id)
177*c1010edaSGreg Roach    {
178c385536dSGreg Roach        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
179e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
180e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
181f36626dbSGreg Roach            $this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
182e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
183e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
184e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
185e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
186e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
187e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
188e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
189e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
190e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
191e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
192e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
193e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
194e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
195e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
196e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
197e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
198e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
199c385536dSGreg Roach
200c385536dSGreg Roach            return;
2018c2e8227SGreg Roach        }
2028c2e8227SGreg Roach
203e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
204e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
205f36626dbSGreg Roach        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
206e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
207e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
208e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
209e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
210e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
211e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
212e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
213e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
214e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
215e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
216e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
217e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
218e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
219e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
220e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
221e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
2228c2e8227SGreg Roach
223147e99aaSGreg Roach        echo view('modules/gedcom_stats/config', [
224c385536dSGreg Roach            'show_last_update'     => $show_last_update,
225c385536dSGreg Roach            'show_common_surnames' => $show_common_surnames,
226c385536dSGreg Roach            'number_of_surnames'   => $number_of_surnames,
227c385536dSGreg Roach            'stat_indi'            => $stat_indi,
228c385536dSGreg Roach            'stat_fam'             => $stat_fam,
229c385536dSGreg Roach            'stat_sour'            => $stat_sour,
230c385536dSGreg Roach            'stat_media'           => $stat_media,
231c385536dSGreg Roach            'stat_repo'            => $stat_repo,
232c385536dSGreg Roach            'stat_surname'         => $stat_surname,
233c385536dSGreg Roach            'stat_events'          => $stat_events,
234c385536dSGreg Roach            'stat_users'           => $stat_users,
235c385536dSGreg Roach            'stat_first_birth'     => $stat_first_birth,
236c385536dSGreg Roach            'stat_last_birth'      => $stat_last_birth,
237c385536dSGreg Roach            'stat_first_death'     => $stat_first_death,
238c385536dSGreg Roach            'stat_last_death'      => $stat_last_death,
239c385536dSGreg Roach            'stat_long_life'       => $stat_long_life,
240c385536dSGreg Roach            'stat_avg_life'        => $stat_avg_life,
241c385536dSGreg Roach            'stat_most_chil'       => $stat_most_chil,
242c385536dSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
243c385536dSGreg Roach        ]);
2448c2e8227SGreg Roach    }
2458c2e8227SGreg Roach}
246