xref: /webtrees/app/Module/PlacesModule.php (revision f289f086829969e7321ef4a025706451a46fe995)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://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 === 0.0 && $longitude === 0.0) {
171                $latitude  = $location->latitude();
172                $longitude = $location->longitude();
173            }
174
175            if ($latitude !== 0.0 || $longitude !== 0.0) {
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                        'zoom'     => $location->zoom(),
188                    ],
189                ];
190            }
191        }
192
193        return (object) $geojson;
194    }
195
196    /**
197     * @param Individual $individual
198     *
199     * @return Collection<Fact>
200     * @throws Exception
201     */
202    private function getPersonalFacts(Individual $individual): Collection
203    {
204        $facts = $individual->facts();
205
206        foreach ($individual->spouseFamilies() as $family) {
207            $facts = $facts->merge($family->facts());
208            // Add birth of children from this family to the facts array
209            foreach ($family->children() as $child) {
210                $childsBirth = $child->facts(['BIRT'])->first();
211                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
212                    $facts->push($childsBirth);
213                }
214            }
215        }
216
217        $facts = Fact::sortFacts($facts);
218
219        return $facts->filter(static function (Fact $item): bool {
220            return $item->place()->gedcomName() !== '';
221        });
222    }
223
224    /**
225     * @param Individual $individual
226     * @param Fact       $fact
227     *
228     * @return mixed[]
229     */
230    private function summaryData(Individual $individual, Fact $fact): array
231    {
232        $record = $fact->record();
233        $name   = '';
234        $url    = '';
235        $tag    = $fact->label();
236
237        if ($record instanceof Family) {
238            // Marriage
239            $spouse = $record->spouse($individual);
240            if ($spouse instanceof Individual) {
241                $url  = $spouse->url();
242                $name = $spouse->fullName();
243            }
244        } elseif ($record !== $individual) {
245            // Birth of a child
246            $url  = $record->url();
247            $name = $record->fullName();
248            $tag  = I18N::translate('Birth of a child');
249        }
250
251        return [
252            'tag'    => $tag,
253            'url'    => $url,
254            'name'   => $name,
255            'value'  => $fact->value(),
256            'date'   => $fact->date()->display(true),
257            'place'  => $fact->place(),
258            'addtag' => false,
259        ];
260    }
261}
262