xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision da83637ca6236094f5a00d6e54530cd25ac7aa0e)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsDb;
21use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Query\QueryName;
24use Fisharebest\Webtrees\Stats;
25use Fisharebest\Webtrees\Tree;
26
27/**
28 * Class FamilyTreeStatisticsModule
29 */
30class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
31{
32    /** Show this number of surnames by default */
33    const DEFAULT_NUMBER_OF_SURNAMES = 10;
34
35    /** {@inheritdoc} */
36    public function getTitle()
37    {
38        return /* I18N: Name of a module */
39            I18N::translate('Statistics');
40    }
41
42    /** {@inheritdoc} */
43    public function getDescription()
44    {
45        return /* I18N: Description of “Statistics” module */
46            I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
47    }
48
49    /**
50     * Generate the HTML content of this block.
51     *
52     * @param Tree     $tree
53     * @param int      $block_id
54     * @param bool     $template
55     * @param string[] $cfg
56     *
57     * @return string
58     */
59    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
60    {
61        global $ctype;
62
63        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
64        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
65        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
66        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
67        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
68        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
69        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
70        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
71        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
72        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
73        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
74        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
75        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
76        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
77        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
78        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
79        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
80        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
81        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
82
83        extract($cfg, EXTR_OVERWRITE);
84
85        if ($show_common_surnames) {
86            $surnames = FunctionsDb::getTopSurnames($tree->getTreeId(), 0, (int)$number_of_surnames);
87
88            $all_surnames = [];
89            foreach (array_keys($surnames) as $surname) {
90                $all_surnames = array_merge($all_surnames, QueryName::surnames($tree, $surname, '', false, false));
91            }
92            ksort($all_surnames);
93
94            $surnames = FunctionsPrintLists::surnameList($all_surnames, 2, false, 'individual-list', $tree);
95        } else {
96            $surnames = '';
97        }
98
99        $content = view('modules/gedcom_stats/statistics', [
100            'show_last_update'     => $show_last_update,
101            'show_common_surnames' => $show_common_surnames,
102            'number_of_surnames'   => $number_of_surnames,
103            'stat_indi'            => $stat_indi,
104            'stat_fam'             => $stat_fam,
105            'stat_sour'            => $stat_sour,
106            'stat_media'           => $stat_media,
107            'stat_repo'            => $stat_repo,
108            'stat_surname'         => $stat_surname,
109            'stat_events'          => $stat_events,
110            'stat_users'           => $stat_users,
111            'stat_first_birth'     => $stat_first_birth,
112            'stat_last_birth'      => $stat_last_birth,
113            'stat_first_death'     => $stat_first_death,
114            'stat_last_death'      => $stat_last_death,
115            'stat_long_life'       => $stat_long_life,
116            'stat_avg_life'        => $stat_avg_life,
117            'stat_most_chil'       => $stat_most_chil,
118            'stat_avg_chil'        => $stat_avg_chil,
119            'stats'                => new Stats($tree),
120            'surnames'             => $surnames,
121        ]);
122
123        if ($template) {
124            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
125                $config_url = route('tree-page-block-edit', [
126                    'block_id' => $block_id,
127                    'ged'      => $tree->getName(),
128                ]);
129            } elseif ($ctype === 'user' && Auth::check()) {
130                $config_url = route('user-page-block-edit', [
131                    'block_id' => $block_id,
132                    'ged'      => $tree->getName(),
133                ]);
134            } else {
135                $config_url = '';
136            }
137
138            return view('modules/block-template', [
139                'block'      => str_replace('_', '-', $this->getName()),
140                'id'         => $block_id,
141                'config_url' => $config_url,
142                'title'      => $this->getTitle(),
143                'content'    => $content,
144            ]);
145        } else {
146            return $content;
147        }
148    }
149
150    /** {@inheritdoc} */
151    public function loadAjax(): bool
152    {
153        return true;
154    }
155
156    /** {@inheritdoc} */
157    public function isUserBlock(): bool
158    {
159        return true;
160    }
161
162    /** {@inheritdoc} */
163    public function isGedcomBlock(): bool
164    {
165        return true;
166    }
167
168    /**
169     * An HTML form to edit block settings
170     *
171     * @param Tree $tree
172     * @param int  $block_id
173     *
174     * @return void
175     */
176    public function configureBlock(Tree $tree, int $block_id)
177    {
178        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
179            $this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
180            $this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
181            $this->setBlockSetting($block_id, 'number_of_surnames', Filter::postInteger('number_of_surnames'));
182            $this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
183            $this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
184            $this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
185            $this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
186            $this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
187            $this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
188            $this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
189            $this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
190            $this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
191            $this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
192            $this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
193            $this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
194            $this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
195            $this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
196            $this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
197            $this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
198            $this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
199
200            return;
201        }
202
203        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
204        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
205        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
206        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
207        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
208        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
209        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
210        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
211        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
212        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
213        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
214        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
215        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
216        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
217        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
218        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
219        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
220        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
221        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
222
223        echo view('modules/gedcom_stats/config', [
224            'show_last_update'     => $show_last_update,
225            'show_common_surnames' => $show_common_surnames,
226            'number_of_surnames'   => $number_of_surnames,
227            'stat_indi'            => $stat_indi,
228            'stat_fam'             => $stat_fam,
229            'stat_sour'            => $stat_sour,
230            'stat_media'           => $stat_media,
231            'stat_repo'            => $stat_repo,
232            'stat_surname'         => $stat_surname,
233            'stat_events'          => $stat_events,
234            'stat_users'           => $stat_users,
235            'stat_first_birth'     => $stat_first_birth,
236            'stat_last_birth'      => $stat_last_birth,
237            'stat_first_death'     => $stat_first_death,
238            'stat_last_death'      => $stat_last_death,
239            'stat_long_life'       => $stat_long_life,
240            'stat_avg_life'        => $stat_avg_life,
241            'stat_most_chil'       => $stat_most_chil,
242            'stat_avg_chil'        => $stat_avg_chil,
243        ]);
244    }
245}
246