Issues (2559)

app/Module/PlacesModule.php (1 issue)

Labels
Severity
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 Exception;
23
use Fisharebest\Webtrees\Fact;
24
use Fisharebest\Webtrees\Family;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\Individual;
27
use Fisharebest\Webtrees\Place;
28
use Fisharebest\Webtrees\PlaceLocation;
29
use Fisharebest\Webtrees\Services\LeafletJsService;
30
use Fisharebest\Webtrees\Services\ModuleService;
31
use Illuminate\Support\Collection;
32
33
class PlacesModule extends AbstractModule implements ModuleTabInterface
34
{
35
    use ModuleTabTrait;
36
37
    protected const array ICONS = [
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 37 at column 26
Loading history...
38
        'FAM:CENS'  => ['color' => 'darkcyan', 'name' => 'list fas'],
39
        'FAM:MARR'  => ['color' => 'green', 'name' => 'infinity fas'],
40
        'INDI:BAPM' => ['color' => 'hotpink', 'name' => 'water fas'],
41
        'INDI:BARM' => ['color' => 'hotpink', 'name' => 'star-of-david fas'],
42
        'INDI:BASM' => ['color' => 'hotpink', 'name' => 'star-of-david fas'],
43
        'INDI:BIRT' => ['color' => 'hotpink', 'name' => 'baby-carriage fas'],
44
        'INDI:BURI' => ['color' => 'purple', 'name' => 'times fas'],
45
        'INDI:CENS' => ['color' => 'darkcyan', 'name' => 'list fas'],
46
        'INDI:CHR'  => ['color' => 'hotpink', 'name' => 'water fas'],
47
        'INDI:CHRA' => ['color' => 'hotpink', 'name' => 'water fas'],
48
        'INDI:CREM' => ['color' => 'black', 'name' => 'times fas'],
49
        'INDI:DEAT' => ['color' => 'black', 'name' => 'times fas'],
50
        'INDI:EDUC' => ['color' => 'violet', 'name' => 'university fas'],
51
        'INDI:GRAD' => ['color' => 'violet', 'name' => 'university fas'],
52
        'INDI:OCCU' => ['color' => 'darkcyan', 'name' => 'industry fas'],
53
        'INDI:RESI' => ['color' => 'darkcyan', 'name' => 'home fas'],
54
    ];
55
56
    protected const array OWN_ICONS = [
57
        'INDI:BIRT' => ['color' => 'red', 'name' => 'baby-carriage fas'],
58
        'INDI:CHR'  => ['color' => 'red', 'name' => 'water fas'],
59
    ] + self::ICONS;
60
61
    protected const array DEFAULT_ICON = ['color' => 'gold', 'name' => 'bullseye fas'];
62
63
    private LeafletJsService $leaflet_js_service;
64
65
    private ModuleService $module_service;
66
67
    /**
68
     * @param LeafletJsService $leaflet_js_service
69
     * @param ModuleService    $module_service
70
     */
71
    public function __construct(LeafletJsService $leaflet_js_service, ModuleService $module_service)
72
    {
73
        $this->leaflet_js_service = $leaflet_js_service;
74
        $this->module_service = $module_service;
75
    }
76
77
    public function title(): string
78
    {
79
        /* I18N: Name of a module */
80
        return I18N::translate('Places');
81
    }
82
83
    public function description(): string
84
    {
85
        /* I18N: Description of the “Places” module */
86
        return I18N::translate('Show the location of events on a map.');
87
    }
88
89
    /**
90
     * The default position for this tab.  It can be changed in the control panel.
91
     *
92
     * @return int
93
     */
94
    public function defaultTabOrder(): int
95
    {
96
        return 8;
97
    }
98
99
    /**
100
     * Is this tab empty? If so, we don't always need to display it.
101
     *
102
     * @param Individual $individual
103
     *
104
     * @return bool
105
     */
106
    public function hasTabContent(Individual $individual): bool
107
    {
108
        $map_providers = $this->module_service->findByInterface(ModuleMapProviderInterface::class);
109
110
        return $map_providers->isNotEmpty() && $this->getMapData($individual)->features !== [];
111
    }
112
113
    /**
114
     * @param Individual $indi
115
     *
116
     * @return object
117
     */
118
    private function getMapData(Individual $indi): object
119
    {
120
        $facts = $this->getPersonalFacts($indi);
121
122
        $geojson = [
123
            'type'     => 'FeatureCollection',
124
            'features' => [],
125
        ];
126
127
        foreach ($facts as $id => $fact) {
128
            $location = new PlaceLocation($fact->place()->gedcomName());
129
130
            // Use the co-ordinates from the fact (if they exist).
131
            $latitude  = $fact->latitude();
132
            $longitude = $fact->longitude();
133
134
            // Use the co-ordinates from the location otherwise.
135
            if ($latitude === null || $longitude === null) {
136
                $latitude  = $location->latitude();
137
                $longitude = $location->longitude();
138
            }
139
140
            $icons = $fact->record() === $indi ? static::OWN_ICONS : static::ICONS;
141
142
            if ($latitude !== null && $longitude !== null) {
143
                $geojson['features'][] = [
144
                    'type'       => 'Feature',
145
                    'id'         => $id,
146
                    'geometry'   => [
147
                        'type'        => 'Point',
148
                        'coordinates' => [$longitude, $latitude],
149
                    ],
150
                    'properties' => [
151
                        'icon'    => $icons[$fact->tag()] ?? static::DEFAULT_ICON,
152
                        'tooltip' => $fact->place()->gedcomName(),
153
                        'summary' => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)),
154
                    ],
155
                ];
156
            }
157
        }
158
159
        return (object) $geojson;
160
    }
161
162
    /**
163
     * @param Individual $individual
164
     *
165
     * @return Collection<int,Fact>
166
     * @throws Exception
167
     */
168
    private function getPersonalFacts(Individual $individual): Collection
169
    {
170
        $facts = $individual->facts();
171
172
        foreach ($individual->spouseFamilies() as $family) {
173
            $facts = $facts->merge($family->facts());
174
            // Add birth of children from this family to the facts array
175
            foreach ($family->children() as $child) {
176
                $childsBirth = $child->facts(['BIRT'])->first();
177
                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
178
                    $facts->push($childsBirth);
179
                }
180
            }
181
        }
182
183
        $facts = Fact::sortFacts($facts);
184
185
        return $facts->filter(static fn (Fact $item): bool => $item->place()->gedcomName() !== '');
186
    }
187
188
    /**
189
     * @param Individual $individual
190
     * @param Fact       $fact
191
     *
192
     * @return array<string|Place>
193
     */
194
    private function summaryData(Individual $individual, Fact $fact): array
195
    {
196
        $record = $fact->record();
197
        $name   = '';
198
        $url    = '';
199
        $tag    = $fact->label();
200
201
        if ($record instanceof Family) {
202
            // Marriage
203
            $spouse = $record->spouse($individual);
204
            if ($spouse instanceof Individual) {
205
                $url  = $spouse->url();
206
                $name = $spouse->fullName();
207
            }
208
        } elseif ($record !== $individual) {
209
            // Birth of a child
210
            $url  = $record->url();
211
            $name = $record->fullName();
212
            $tag  = I18N::translate('Birth of a child');
213
        }
214
215
        return [
216
            'tag'   => $tag,
217
            'url'   => $url,
218
            'name'  => $name,
219
            'value' => $fact->value(),
220
            'date'  => $fact->date()->display($individual->tree(), null, true),
221
            'place' => $fact->place(),
222
        ];
223
    }
224
225
    /**
226
     * A greyed out tab has no actual content, but may perhaps have
227
     * options to create content.
228
     *
229
     * @param Individual $individual
230
     *
231
     * @return bool
232
     */
233
    public function isGrayedOut(Individual $individual): bool
234
    {
235
        return false;
236
    }
237
238
    /**
239
     * Can this tab load asynchronously?
240
     *
241
     * @return bool
242
     */
243
    public function canLoadAjax(): bool
244
    {
245
        return true;
246
    }
247
248
    /**
249
     * Generate the HTML content of this tab.
250
     *
251
     * @param Individual $individual
252
     *
253
     * @return string
254
     */
255
    public function getTabContent(Individual $individual): string
256
    {
257
        return view('modules/places/tab', [
258
            'data'           => $this->getMapData($individual),
259
            'leaflet_config' => $this->leaflet_js_service->config(),
260
        ]);
261
    }
262
}
263