Issues (2559)

app/Module/DescendancyModule.php (4 issues)

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Family;
0 ignored issues
show
The type Fisharebest\Webtrees\Family was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Fisharebest\Webtrees\Individual;
0 ignored issues
show
The type Fisharebest\Webtrees\Individual was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Services\SearchService;
0 ignored issues
show
The type Fisharebest\Webtrees\Services\SearchService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Fisharebest\Webtrees\Validator;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
31
use function strlen;
32
use function view;
33
34
class DescendancyModule extends AbstractModule implements ModuleSidebarInterface
35
{
36
    use ModuleSidebarTrait;
37
38
    private SearchService $search_service;
39
40
    /**
41
     * @param SearchService $search_service
42
     */
43
    public function __construct(SearchService $search_service)
44
    {
45
        $this->search_service = $search_service;
46
    }
47
48
    public function title(): string
49
    {
50
        /* I18N: Name of a module/sidebar */
51
        return I18N::translate('Descendants');
52
    }
53
54
    public function description(): string
55
    {
56
        /* I18N: Description of the “Descendants” module */
57
        return I18N::translate('A sidebar showing the descendants of an individual.');
58
    }
59
60
    /**
61
     * The default position for this sidebar.  It can be changed in the control panel.
62
     *
63
     * @return int
64
     */
65
    public function defaultSidebarOrder(): int
66
    {
67
        return 3;
68
    }
69
70
    /**
71
     * @param ServerRequestInterface $request
72
     *
73
     * @return ResponseInterface
74
     */
75
    public function getSearchAction(ServerRequestInterface $request): ResponseInterface
76
    {
77
        $tree   = Validator::attributes($request)->tree();
78
        $search = Validator::queryParams($request)->string('search');
79
80
        $html = '';
81
82
        if (strlen($search) >= 2) {
83
            $html = $this->search_service
84
                ->searchIndividualNames([$tree], [$search])
85
                ->map(fn (Individual $individual): string => $this->getPersonLi($individual))
86
                ->implode('');
87
        }
88
89
        if ($html !== '') {
90
            $html = '<ul>' . $html . '</ul>';
91
        }
92
93
        return response($html);
94
    }
95
96
    /**
97
     * @param ServerRequestInterface $request
98
     *
99
     * @return ResponseInterface
100
     */
101
    public function getDescendantsAction(ServerRequestInterface $request): ResponseInterface
102
    {
103
        $tree = Validator::attributes($request)->tree();
104
        $xref = Validator::queryParams($request)->isXref()->string('xref');
105
106
        $individual = Registry::individualFactory()->make($xref, $tree);
107
108
        if ($individual !== null && $individual->canShow()) {
109
            $html = $this->loadSpouses($individual, 1);
110
        } else {
111
            $html = '';
112
        }
113
114
        return response($html);
115
    }
116
117
    /**
118
     * @param Individual $individual
119
     *
120
     * @return bool
121
     */
122
    public function hasSidebarContent(Individual $individual): bool
123
    {
124
        return true;
125
    }
126
127
    /**
128
     * Load this sidebar synchronously.
129
     *
130
     * @param Individual $individual
131
     *
132
     * @return string
133
     */
134
    public function getSidebarContent(Individual $individual): string
135
    {
136
        return view('modules/descendancy/sidebar', [
137
            'individual_list' => $this->getPersonLi($individual, 1),
138
            'tree'            => $individual->tree(),
139
        ]);
140
    }
141
142
    /**
143
     * Format an individual in a list.
144
     *
145
     * @param Individual $person
146
     * @param int        $generations
147
     *
148
     * @return string
149
     */
150
    public function getPersonLi(Individual $person, int $generations = 0): string
151
    {
152
        $icon     = $generations > 0 ? 'icon-minus' : 'icon-plus';
153
        $lifespan = $person->canShow() ? '(' . $person->lifespan() . ')' : '';
154
        $spouses  = $generations > 0 ? $this->loadSpouses($person, 0) : '';
155
156
        return
157
            '<li class="sb_desc_indi_li">' .
158
            '<a class="sb_desc_indi" href="#" data-wt-href="' . e(route('module', [
159
                'module' => $this->name(),
160
                'action' => 'Descendants',
161
                'tree'    => $person->tree()->name(),
162
                'xref'   => $person->xref(),
163
            ])) . '">' .
164
            '<i class="plusminus ' . $icon . '"></i>' .
165
            '<small>' . view('icons/sex', ['sex' => $person->sex()]) . '</small>' . $person->fullName() . $lifespan .
166
            '</a>' .
167
            '<a href="' . e($person->url()) . '" title="' . strip_tags($person->fullName()) . '">' . view('icons/individual') . '</a>' .
168
            '<div>' . $spouses . '</div>' .
169
            '</li>';
170
    }
171
172
    /**
173
     * Format a family in a list.
174
     *
175
     * @param Family     $family
176
     * @param Individual $person
177
     * @param int        $generations
178
     *
179
     * @return string
180
     */
181
    public function getFamilyLi(Family $family, Individual $person, int $generations = 0): string
182
    {
183
        $spouse = $family->spouse($person);
184
        if ($spouse instanceof Individual) {
185
            $spouse_name = '<small>' . view('icons/sex', ['sex' => $spouse->sex()]) . '</small>' . $spouse->fullName();
186
            $spouse_link = '<a href="' . e($spouse->url()) . '" title="' . strip_tags($spouse->fullName()) . '">' . view('icons/individual') . '</a>';
187
        } else {
188
            $spouse_name = '';
189
            $spouse_link = '';
190
        }
191
192
        $family_link = '<a href="' . e($family->url()) . '" title="' . strip_tags($family->fullName()) . '">' . view('icons/family') . '</a>';
193
194
        $marryear = $family->getMarriageYear();
195
        $marr     = $marryear ? '<i class="icon-rings"></i>' . $marryear : '';
196
197
        return
198
            '<li class="sb_desc_indi_li">' .
199
            '<a class="sb_desc_indi" href="#" data-wt-href="#"><i class="plusminus icon-minus"></i>' .
200
            $spouse_name .
201
            $marr .
202
            '</a>' .
203
            $spouse_link .
204
            $family_link .
205
            '<div>' . $this->loadChildren($family, $generations) . '</div>' .
206
            '</li>';
207
    }
208
209
    /**
210
     * Display spouses.
211
     *
212
     * @param Individual $individual
213
     * @param int        $generations
214
     *
215
     * @return string
216
     */
217
    public function loadSpouses(Individual $individual, int $generations): string
218
    {
219
        $out = '';
220
        if ($individual->canShow()) {
221
            foreach ($individual->spouseFamilies() as $family) {
222
                $out .= $this->getFamilyLi($family, $individual, $generations - 1);
223
            }
224
        }
225
        if ($out !== '') {
226
            return '<ul>' . $out . '</ul>';
227
        }
228
229
        return '';
230
    }
231
232
    /**
233
     * Display descendants.
234
     *
235
     * @param Family $family
236
     * @param int    $generations
237
     *
238
     * @return string
239
     */
240
    public function loadChildren(Family $family, int $generations): string
241
    {
242
        $out = '';
243
        if ($family->canShow()) {
244
            $children = $family->children();
245
246
            if ($children->isNotEmpty()) {
247
                foreach ($children as $child) {
248
                    $out .= $this->getPersonLi($child, $generations - 1);
249
                }
250
            } else {
251
                $out .= '<li class="sb_desc_none">' . I18N::translate('No children') . '</li>';
252
            }
253
        }
254
        if ($out !== '') {
255
            return '<ul>' . $out . '</ul>';
256
        }
257
258
        return '';
259
    }
260
}
261