xref: /webtrees/app/Module/PlacesModule.php (revision add3fa4120ca696c713a0d0ac9b9c86f751fe49a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Exception;
21use Fisharebest\Webtrees\Fact;
22use Fisharebest\Webtrees\Family;
23use Fisharebest\Webtrees\GedcomTag;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Location;
27use Fisharebest\Webtrees\Site;
28use Illuminate\Support\Collection;
29use stdClass;
30
31/**
32 * Class PlacesMapModule
33 */
34class PlacesModule extends AbstractModule implements ModuleTabInterface
35{
36    use ModuleTabTrait;
37
38    protected const ICONS = [
39        'BIRT' => ['color' => 'lightcoral', 'name' => 'baby-carriage'],
40        'BAPM' => ['color' => 'lightcoral', 'name' => 'water'],
41        'BARM' => ['color' => 'lightcoral', 'name' => 'star-of-david'],
42        'BASM' => ['color' => 'lightcoral', 'name' => 'star-of-david'],
43        'CHR'  => ['color' => 'lightcoral', 'name' => 'water'],
44        'CHRA' => ['color' => 'lightcoral', 'name' => 'water'],
45        'MARR' => ['color' => 'green', 'name' => 'infinity'],
46        'DEAT' => ['color' => 'black', 'name' => 'times'],
47        'BURI' => ['color' => 'sienna', 'name' => 'times'],
48        'CREM' => ['color' => 'black', 'name' => 'times'],
49        'CENS' => ['color' => 'mediumblue', 'name' => 'list'],
50        'RESI' => ['color' => 'mediumblue', 'name' => 'home'],
51        'OCCU' => ['color' => 'mediumblue', 'name' => 'industry'],
52        'GRAD' => ['color' => 'plum', 'name' => 'university'],
53        'EDUC' => ['color' => 'plum', 'name' => 'university'],
54    ];
55
56    protected const DEFAULT_ICON = ['color' => 'gold', 'name' => 'bullseye '];
57
58    /**
59     * How should this module be identified in the control panel, etc.?
60     *
61     * @return string
62     */
63    public function title(): string
64    {
65        /* I18N: Name of a module */
66        return I18N::translate('Places');
67    }
68
69    /**
70     * A sentence describing what this module does.
71     *
72     * @return string
73     */
74    public function description(): string
75    {
76        /* I18N: Description of the “OSM” module */
77        return I18N::translate('Show the location of events on a map.');
78    }
79
80    /**
81     * The default position for this tab.  It can be changed in the control panel.
82     *
83     * @return int
84     */
85    public function defaultTabOrder(): int
86    {
87        return 8;
88    }
89
90    /** {@inheritdoc} */
91    public function hasTabContent(Individual $individual): bool
92    {
93        return Site::getPreference('map-provider') !== '';
94    }
95
96    /** {@inheritdoc} */
97    public function isGrayedOut(Individual $individual): bool
98    {
99        return false;
100    }
101
102    /** {@inheritdoc} */
103    public function canLoadAjax(): bool
104    {
105        return true;
106    }
107
108    /** {@inheritdoc} */
109    public function getTabContent(Individual $individual): string
110    {
111        return view('modules/places/tab', [
112            'data' => $this->getMapData($individual),
113        ]);
114    }
115
116    /**
117     * @param Individual $indi
118     *
119     * @return stdClass
120     */
121    private function getMapData(Individual $indi): stdClass
122    {
123        $facts = $this->getPersonalFacts($indi);
124
125        $geojson = [
126            'type'     => 'FeatureCollection',
127            'features' => [],
128        ];
129
130        foreach ($facts as $id => $fact) {
131            $location = new Location($fact->place()->gedcomName());
132
133            // Use the co-ordinates from the fact (if they exist).
134            $latitude  = $fact->latitude();
135            $longitude = $fact->longitude();
136
137            // Use the co-ordinates from the location otherwise.
138            if ($latitude === 0.0 && $longitude === 0.0) {
139                $latitude  = $location->latitude();
140                $longitude = $location->longitude();
141            }
142
143            $icon = static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON;
144
145            if ($latitude !== 0.0 || $longitude !== 0.0) {
146                $geojson['features'][] = [
147                    'type'       => 'Feature',
148                    'id'         => $id,
149                    'valid'      => true,
150                    'geometry'   => [
151                        'type'        => 'Point',
152                        'coordinates' => [$longitude, $latitude],
153                    ],
154                    'properties' => [
155                        'polyline' => null,
156                        'icon'     => $icon,
157                        'tooltip'  => strip_tags($fact->place()->fullName()),
158                        'summary'  => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)),
159                        'zoom'     => $location->zoom(),
160                    ],
161                ];
162            }
163        }
164
165        return (object) $geojson;
166    }
167
168    /**
169     * @param Individual $individual
170     *
171     * @return Collection
172     * @throws Exception
173     */
174    private function getPersonalFacts(Individual $individual): Collection
175    {
176        $facts = $individual->facts();
177
178        foreach ($individual->spouseFamilies() as $family) {
179            $facts = $facts->merge($family->facts());
180            // Add birth of children from this family to the facts array
181            foreach ($family->children() as $child) {
182                $childsBirth = $child->facts(['BIRT'])->first();
183                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
184                    $facts->push($childsBirth);
185                }
186            }
187        }
188
189        $facts = Fact::sortFacts($facts);
190
191        return $facts->filter(static function (Fact $item): bool {
192            return $item->place()->gedcomName() !== '';
193        });
194    }
195
196    /**
197     * @param Individual $individual
198     * @param Fact       $fact
199     *
200     * @return mixed[]
201     */
202    private function summaryData(Individual $individual, Fact $fact): array
203    {
204        $record = $fact->record();
205        $name   = '';
206        $url    = '';
207        $tag    = $fact->label();
208
209        if ($record instanceof Family) {
210            // Marriage
211            $spouse = $record->spouse($individual);
212            if ($spouse instanceof Individual) {
213                $url  = $spouse->url();
214                $name = $spouse->fullName();
215            }
216        } elseif ($record !== $individual) {
217            // Birth of a child
218            $url  = $record->url();
219            $name = $record->fullName();
220            $tag  = GedcomTag::getLabel('_BIRT_CHIL', $record);
221        }
222
223        return [
224            'tag'    => $tag,
225            'url'    => $url,
226            'name'   => $name,
227            'value'  => $fact->value(),
228            'date'   => $fact->date()->display(true),
229            'place'  => $fact->place(),
230            'addtag' => false,
231        ];
232    }
233}
234