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