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