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