xref: /webtrees/app/Module/PlacesModule.php (revision 83615acfc72bfb50678c6481f2a00bab04041a87)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Exception;
22use Fisharebest\Webtrees\Fact;
23use Fisharebest\Webtrees\Family;
24use Fisharebest\Webtrees\GedcomTag;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Location;
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' => 'lightcoral', 'name' => 'baby-carriage'],
41        'BAPM' => ['color' => 'lightcoral', 'name' => 'water'],
42        'BARM' => ['color' => 'lightcoral', 'name' => 'star-of-david'],
43        'BASM' => ['color' => 'lightcoral', 'name' => 'star-of-david'],
44        'CHR'  => ['color' => 'lightcoral', 'name' => 'water'],
45        'CHRA' => ['color' => 'lightcoral', 'name' => 'water'],
46        'MARR' => ['color' => 'green', 'name' => 'infinity'],
47        'DEAT' => ['color' => 'black', 'name' => 'times'],
48        'BURI' => ['color' => 'sienna', 'name' => 'times'],
49        'CREM' => ['color' => 'black', 'name' => 'times'],
50        'CENS' => ['color' => 'mediumblue', 'name' => 'list'],
51        'RESI' => ['color' => 'mediumblue', 'name' => 'home'],
52        'OCCU' => ['color' => 'mediumblue', 'name' => 'industry'],
53        'GRAD' => ['color' => 'plum', 'name' => 'university'],
54        'EDUC' => ['color' => 'plum', 'name' => 'university'],
55    ];
56
57    protected const DEFAULT_ICON = ['color' => 'gold', 'name' => 'bullseye '];
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    }
102
103    /**
104     * A greyed out tab has no actual content, but may perhaps have
105     * options to create content.
106     *
107     * @param Individual $individual
108     *
109     * @return bool
110     */
111    public function isGrayedOut(Individual $individual): bool
112    {
113        return false;
114    }
115
116    /**
117     * Can this tab load asynchronously?
118     *
119     * @return bool
120     */
121    public function canLoadAjax(): bool
122    {
123        return true;
124    }
125
126    /**
127     * Generate the HTML content of this tab.
128     *
129     * @param Individual $individual
130     *
131     * @return string
132     */
133    public function getTabContent(Individual $individual): string
134    {
135        return view('modules/places/tab', [
136            'data' => $this->getMapData($individual),
137        ]);
138    }
139
140    /**
141     * @param Individual $indi
142     *
143     * @return stdClass
144     */
145    private function getMapData(Individual $indi): stdClass
146    {
147        $facts = $this->getPersonalFacts($indi);
148
149        $geojson = [
150            'type'     => 'FeatureCollection',
151            'features' => [],
152        ];
153
154        foreach ($facts as $id => $fact) {
155            $location = new Location($fact->place()->gedcomName());
156
157            // Use the co-ordinates from the fact (if they exist).
158            $latitude  = $fact->latitude();
159            $longitude = $fact->longitude();
160
161            // Use the co-ordinates from the location otherwise.
162            if ($latitude === 0.0 && $longitude === 0.0) {
163                $latitude  = $location->latitude();
164                $longitude = $location->longitude();
165            }
166
167            $icon = static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON;
168
169            if ($latitude !== 0.0 || $longitude !== 0.0) {
170                $geojson['features'][] = [
171                    'type'       => 'Feature',
172                    'id'         => $id,
173                    'valid'      => true,
174                    'geometry'   => [
175                        'type'        => 'Point',
176                        'coordinates' => [$longitude, $latitude],
177                    ],
178                    'properties' => [
179                        'polyline' => null,
180                        'icon'     => $icon,
181                        'tooltip'  => strip_tags($fact->place()->fullName()),
182                        'summary'  => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)),
183                        'zoom'     => $location->zoom(),
184                    ],
185                ];
186            }
187        }
188
189        return (object) $geojson;
190    }
191
192    /**
193     * @param Individual $individual
194     *
195     * @return Collection
196     * @throws Exception
197     */
198    private function getPersonalFacts(Individual $individual): Collection
199    {
200        $facts = $individual->facts();
201
202        foreach ($individual->spouseFamilies() as $family) {
203            $facts = $facts->merge($family->facts());
204            // Add birth of children from this family to the facts array
205            foreach ($family->children() as $child) {
206                $childsBirth = $child->facts(['BIRT'])->first();
207                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
208                    $facts->push($childsBirth);
209                }
210            }
211        }
212
213        $facts = Fact::sortFacts($facts);
214
215        return $facts->filter(static function (Fact $item): bool {
216            return $item->place()->gedcomName() !== '';
217        });
218    }
219
220    /**
221     * @param Individual $individual
222     * @param Fact       $fact
223     *
224     * @return mixed[]
225     */
226    private function summaryData(Individual $individual, Fact $fact): array
227    {
228        $record = $fact->record();
229        $name   = '';
230        $url    = '';
231        $tag    = $fact->label();
232
233        if ($record instanceof Family) {
234            // Marriage
235            $spouse = $record->spouse($individual);
236            if ($spouse instanceof Individual) {
237                $url  = $spouse->url();
238                $name = $spouse->fullName();
239            }
240        } elseif ($record !== $individual) {
241            // Birth of a child
242            $url  = $record->url();
243            $name = $record->fullName();
244            $tag  = GedcomTag::getLabel('_BIRT_CHIL', $record);
245        }
246
247        return [
248            'tag'    => $tag,
249            'url'    => $url,
250            'name'   => $name,
251            'value'  => $fact->value(),
252            'date'   => $fact->date()->display(true),
253            'place'  => $fact->place(),
254            'addtag' => false,
255        ];
256    }
257}
258