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