Passed
Push — feature/code-analysis ( 00c5b4...e321b8 )
by Jonathan
13:27
created

PatronymicLineageModule::allSurnames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage PatronymicLineage
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2025, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\PatronymicLineage;
16
17
use Aura\Router\Map;
18
use Fisharebest\Webtrees\I18N;
19
use Fisharebest\Webtrees\Individual;
20
use Fisharebest\Webtrees\Registry;
21
use Fisharebest\Webtrees\Tree;
22
use Fisharebest\Webtrees\Validator;
23
use Fisharebest\Webtrees\Module\IndividualListModule;
24
use Fisharebest\Webtrees\Module\ModuleGlobalInterface;
25
use Fisharebest\Webtrees\Module\ModuleGlobalTrait;
26
use Fisharebest\Webtrees\Module\ModuleListInterface;
27
use Fisharebest\Webtrees\Module\ModuleListTrait;
28
use Illuminate\Support\Collection;
29
use Illuminate\Support\Str;
30
use MyArtJaub\Webtrees\Module\ModuleMyArtJaubInterface;
31
use MyArtJaub\Webtrees\Module\ModuleMyArtJaubTrait;
32
use MyArtJaub\Webtrees\Module\PatronymicLineage\Http\RequestHandlers\LineagesPage;
33
use MyArtJaub\Webtrees\Module\PatronymicLineage\Http\RequestHandlers\SurnamesList;
34
use Psr\Http\Message\ServerRequestInterface;
35
use ReflectionMethod;
36
37
/**
38
 * Patronymic Lineage Module.
39
 * Display lineages of people with the same surname.
40
 */
41
class PatronymicLineageModule extends IndividualListModule implements
42
    ModuleMyArtJaubInterface,
43
    ModuleListInterface,
44
    ModuleGlobalInterface
45
{
46
    use ModuleMyArtJaubTrait;
47
    use ModuleListTrait;
48
    use ModuleGlobalTrait;
49
50
     /**
51
     * {@inheritDoc}
52
     * @see \Fisharebest\Webtrees\Module\AbstractModule::title()
53
     */
54
    public function title(): string
55
    {
56
        return /* I18N: Name of the “Patronymic lineage” module */ I18N::translate('Patronymic Lineages');
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     * @see \Fisharebest\Webtrees\Module\AbstractModule::description()
62
     */
63
    public function description(): string
64
    {
65
        //phpcs:ignore Generic.Files.LineLength.TooLong
66
        return /* I18N: Description of the “Patronymic lineage” module */ I18N::translate('Display lineages of people holding the same surname.');
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     * @see \MyArtJaub\Webtrees\Module\ModuleMyArtJaubInterface::loadRoutes()
72
     */
73
    public function loadRoutes(Map $router): void
74
    {
75
        $router->attach('', '', static function (Map $router): void {
76
77
            $router->attach('', '/module-maj/lineages', static function (Map $router): void {
78
79
                $router->attach('', '/Page', static function (Map $router): void {
80
81
                    $router->get(SurnamesList::class, '/{tree}/list{/alpha}', SurnamesList::class);
82
                    $router->get(LineagesPage::class, '/{tree}/lineage/{surname}', LineagesPage::class);
83
                });
84
            });
85
        });
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     * @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleVersion()
91
     */
92
    public function customModuleVersion(): string
93
    {
94
        return '2.1.18-v.1';
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     * @see \Fisharebest\Webtrees\Module\ModuleListInterface::listUrl()
100
     *
101
     * @param array<bool|int|string|array<mixed>|null> $parameters
102
     */
103
    public function listUrl(Tree $tree, array $parameters = []): string
104
    {
105
        $surname = $parameters['surname'] ?? null;
106
        $surname = is_string($surname) ? $surname : null;
107
108
        $request = app(ServerRequestInterface::class);
109
110
        // If a surname is already in the query attribute, use it
111
        if ($surname === null) {
112
            $surname_attr =  Validator::attributes($request)->string('surname', '');
113
            $surname_param =  Validator::queryParams($request)->string('surname', '');
114
            $surname_body =  Validator::parsedBody($request)->string('surname', '');
115
            $surname = $surname_attr !== '' ? $surname_attr : (
116
                $surname_param !== '' ? $surname_param : (
117
                $surname_body !== '' ? $surname_body : null
118
            ));
119
        }
120
121
        // If nothing found, and on an individual page, use its name
122
        $xref = Validator::attributes($request)->isXref()->string('xref', '');
123
        if ($xref !== '') {
124
            $individual = Registry::individualFactory()->make($xref, $tree);
125
            if ($individual instanceof Individual && $individual->canShow()) {
126
                $primary_name = $individual->getPrimaryName();
127
                $surname ??= $individual->getAllNames()[$primary_name]['surn'] ?? null;
128
            }
129
        }
130
131
        if (Str::length($surname ?? '') > 0 && $surname !== Individual::NOMEN_NESCIO) {
132
            return route(LineagesPage::class, [
133
                'tree'      =>  $tree->name(),
134
                'surname'   =>  $surname
135
            ] + $parameters);
136
        }
137
        return route(SurnamesList::class, ['tree'  =>  $tree->name() ] + $parameters);
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     * @see \Fisharebest\Webtrees\Module\ModuleListInterface::listMenuClass()
143
     */
144
    public function listMenuClass(): string
145
    {
146
        return 'menu-maj-patrolineage';
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     * @see \Fisharebest\Webtrees\Module\ModuleGlobalInterface::headContent()
152
     */
153
    public function headContent(): string
154
    {
155
        return '<link rel="stylesheet" href="' . e($this->moduleCssUrl()) . '">';
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     * @see \Fisharebest\Webtrees\Module\IndividualListModule::individuals()
161
     *
162
     * Implemented to set the visibility to public.
163
     * This should probably be in a service, but this hack allows for reuse of mainstream code.
164
     */
165
    public function individuals(
166
        Tree $tree,
167
        array $surns_to_show,
168
        string $galpha,
169
        bool $marnm,
170
        bool $fams
171
    ): Collection {
172
        return parent::individuals($tree, $surns_to_show, $galpha, $marnm, $fams);
173
    }
174
175
    /**
176
     * {@inheritDoc}
177
     * @see \Fisharebest\Webtrees\Module\IndividualListModule::surnameData()
178
     *
179
     * Implemented to set the visibility to public.
180
     * This should probably be in a service, but this hack allows for reuse of mainstream code.
181
     *
182
     * @return array<object{n_surn:string,n_surname:string,total:int}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<object{n_surn:stri...name:string,total:int}> at position 2 could not be parsed: Expected '>' at position 2, but found 'object'.
Loading history...
183
     */
184
    public function surnameData(Tree $tree, bool $marnm, bool $fams): array
185
    {
186
        $reflectionMethod = new ReflectionMethod(IndividualListModule::class, 'surnameData');
187
        $reflectionMethod->setAccessible(true);
188
189
        return $reflectionMethod->invoke($this, $tree, $marnm, $fams);
190
    }
191
192
    /**
193
     * {@inheritDoc}
194
     * @see \Fisharebest\Webtrees\Module\IndividualListModule::allSurnames()
195
     *
196
     * Implemented to set the visibility to public.
197
     * This should probably be in a service, but this hack allows for reuse of mainstream code.
198
     */
199
    public function allSurnames(array $surname_data): array
200
    {
201
        return parent::allSurnames($surname_data);
202
    }
203
204
    /**
205
     * {@inheritDoc}
206
     * @see \Fisharebest\Webtrees\Module\IndividualListModule::surnameInitials()
207
     *
208
     * Implemented to set the visibility to public.
209
     * This should probably be in a service, but this hack allows for reuse of mainstream code.
210
     */
211
    public function surnameInitials(array $surname_data): array
212
    {
213
        return parent::surnameInitials($surname_data);
214
    }
215
}
216