xref: /webtrees/app/Module/PlacesModule.php (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
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    }
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        ]);
139    }
140
141    /**
142     * @param Individual $indi
143     *
144     * @return stdClass
145     */
146    private function getMapData(Individual $indi): stdClass
147    {
148        $facts = $this->getPersonalFacts($indi);
149
150        $geojson = [
151            'type'     => 'FeatureCollection',
152            'features' => [],
153        ];
154
155        foreach ($facts as $id => $fact) {
156            $location = new Location($fact->place()->gedcomName());
157
158            // Use the co-ordinates from the fact (if they exist).
159            $latitude  = $fact->latitude();
160            $longitude = $fact->longitude();
161
162            // Use the co-ordinates from the location otherwise.
163            if ($latitude === 0.0 && $longitude === 0.0) {
164                $latitude  = $location->latitude();
165                $longitude = $location->longitude();
166            }
167
168            if ($latitude !== 0.0 || $longitude !== 0.0) {
169                $geojson['features'][] = [
170                    'type'       => 'Feature',
171                    'id'         => $id,
172                    'geometry'   => [
173                        'type'        => 'Point',
174                        'coordinates' => [$longitude, $latitude],
175                    ],
176                    'properties' => [
177                        'icon'     => static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON,
178                        'tooltip'  => strip_tags($fact->place()->fullName()),
179                        'summary'  => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)),
180                        'zoom'     => $location->zoom(),
181                    ],
182                ];
183            }
184        }
185
186        return (object) $geojson;
187    }
188
189    /**
190     * @param Individual $individual
191     *
192     * @return Collection
193     * @throws Exception
194     */
195    private function getPersonalFacts(Individual $individual): Collection
196    {
197        $facts = $individual->facts();
198
199        foreach ($individual->spouseFamilies() as $family) {
200            $facts = $facts->merge($family->facts());
201            // Add birth of children from this family to the facts array
202            foreach ($family->children() as $child) {
203                $childsBirth = $child->facts(['BIRT'])->first();
204                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
205                    $facts->push($childsBirth);
206                }
207            }
208        }
209
210        $facts = Fact::sortFacts($facts);
211
212        return $facts->filter(static function (Fact $item): bool {
213            return $item->place()->gedcomName() !== '';
214        });
215    }
216
217    /**
218     * @param Individual $individual
219     * @param Fact       $fact
220     *
221     * @return mixed[]
222     */
223    private function summaryData(Individual $individual, Fact $fact): array
224    {
225        $record = $fact->record();
226        $name   = '';
227        $url    = '';
228        $tag    = $fact->label();
229
230        if ($record instanceof Family) {
231            // Marriage
232            $spouse = $record->spouse($individual);
233            if ($spouse instanceof Individual) {
234                $url  = $spouse->url();
235                $name = $spouse->fullName();
236            }
237        } elseif ($record !== $individual) {
238            // Birth of a child
239            $url  = $record->url();
240            $name = $record->fullName();
241            $tag  = GedcomTag::getLabel('_BIRT_CHIL', $record);
242        }
243
244        return [
245            'tag'    => $tag,
246            'url'    => $url,
247            'name'   => $name,
248            'value'  => $fact->value(),
249            'date'   => $fact->date()->display(true),
250            'place'  => $fact->place(),
251            'addtag' => false,
252        ];
253    }
254}
255