xref: /webtrees/app/Module/PlacesModule.php (revision 407d63eb26e871468afafb8aad149ce06ac142a8)
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 DEFAULT_ICON = ['color' => 'gold', 'name' => 'bullseye fas'];
60
61    private LeafletJsService $leaflet_js_service;
62
63    private ModuleService $module_service;
64
65    /**
66     * PlacesModule constructor.
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    /**
78     * How should this module be identified in the control panel, etc.?
79     *
80     * @return string
81     */
82    public function title(): string
83    {
84        /* I18N: Name of a module */
85        return I18N::translate('Places');
86    }
87
88    /**
89     * A sentence describing what this module does.
90     *
91     * @return string
92     */
93    public function description(): string
94    {
95        /* I18N: Description of the “Places” module */
96        return I18N::translate('Show the location of events on a map.');
97    }
98
99    /**
100     * The default position for this tab.  It can be changed in the control panel.
101     *
102     * @return int
103     */
104    public function defaultTabOrder(): int
105    {
106        return 8;
107    }
108
109    /**
110     * Is this tab empty? If so, we don't always need to display it.
111     *
112     * @param Individual $individual
113     *
114     * @return bool
115     */
116    public function hasTabContent(Individual $individual): bool
117    {
118        $map_providers = $this->module_service->findByInterface(ModuleMapProviderInterface::class);
119
120        return $map_providers->isNotEmpty() && $this->getMapData($individual)->features !== [];
121    }
122
123    /**
124     * @param Individual $indi
125     *
126     * @return object
127     */
128    private function getMapData(Individual $indi): object
129    {
130        $facts = $this->getPersonalFacts($indi);
131
132        $geojson = [
133            'type'     => 'FeatureCollection',
134            'features' => [],
135        ];
136
137        foreach ($facts as $id => $fact) {
138            $location = new PlaceLocation($fact->place()->gedcomName());
139
140            // Use the co-ordinates from the fact (if they exist).
141            $latitude  = $fact->latitude();
142            $longitude = $fact->longitude();
143
144            // Use the co-ordinates from the location otherwise.
145            if ($latitude === null || $longitude === null) {
146                $latitude  = $location->latitude();
147                $longitude = $location->longitude();
148            }
149
150            if ($latitude !== null && $longitude !== null) {
151                $geojson['features'][] = [
152                    'type'       => 'Feature',
153                    'id'         => $id,
154                    'geometry'   => [
155                        'type'        => 'Point',
156                        'coordinates' => [$longitude, $latitude],
157                    ],
158                    'properties' => [
159                        'icon'    => static::ICONS[$fact->tag()] ?? static::DEFAULT_ICON,
160                        'tooltip' => $fact->place()->gedcomName(),
161                        'summary' => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)),
162                    ],
163                ];
164            }
165        }
166
167        return (object) $geojson;
168    }
169
170    /**
171     * @param Individual $individual
172     *
173     * @return Collection<int,Fact>
174     * @throws Exception
175     */
176    private function getPersonalFacts(Individual $individual): Collection
177    {
178        $facts = $individual->facts();
179
180        foreach ($individual->spouseFamilies() as $family) {
181            $facts = $facts->merge($family->facts());
182            // Add birth of children from this family to the facts array
183            foreach ($family->children() as $child) {
184                $childsBirth = $child->facts(['BIRT'])->first();
185                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
186                    $facts->push($childsBirth);
187                }
188            }
189        }
190
191        $facts = Fact::sortFacts($facts);
192
193        return $facts->filter(static function (Fact $item): bool {
194            return $item->place()->gedcomName() !== '';
195        });
196    }
197
198    /**
199     * @param Individual $individual
200     * @param Fact       $fact
201     *
202     * @return array<string|Place>
203     */
204    private function summaryData(Individual $individual, Fact $fact): array
205    {
206        $record = $fact->record();
207        $name   = '';
208        $url    = '';
209        $tag    = $fact->label();
210
211        if ($record instanceof Family) {
212            // Marriage
213            $spouse = $record->spouse($individual);
214            if ($spouse instanceof Individual) {
215                $url  = $spouse->url();
216                $name = $spouse->fullName();
217            }
218        } elseif ($record !== $individual) {
219            // Birth of a child
220            $url  = $record->url();
221            $name = $record->fullName();
222            $tag  = I18N::translate('Birth of a child');
223        }
224
225        return [
226            'tag'   => $tag,
227            'url'   => $url,
228            'name'  => $name,
229            'value' => $fact->value(),
230            'date'  => $fact->date()->display($individual->tree(), null, true),
231            'place' => $fact->place(),
232        ];
233    }
234
235    /**
236     * A greyed out tab has no actual content, but may perhaps have
237     * options to create content.
238     *
239     * @param Individual $individual
240     *
241     * @return bool
242     */
243    public function isGrayedOut(Individual $individual): bool
244    {
245        return false;
246    }
247
248    /**
249     * Can this tab load asynchronously?
250     *
251     * @return bool
252     */
253    public function canLoadAjax(): bool
254    {
255        return true;
256    }
257
258    /**
259     * Generate the HTML content of this tab.
260     *
261     * @param Individual $individual
262     *
263     * @return string
264     */
265    public function getTabContent(Individual $individual): string
266    {
267        return view('modules/places/tab', [
268            'data'           => $this->getMapData($individual),
269            'leaflet_config' => $this->leaflet_js_service->config(),
270        ]);
271    }
272}
273