xref: /webtrees/app/Module/PlacesModule.php (revision 9ba014a766385c38470d363a384f5a2c9642a7b8)
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    /**
91     * Is this tab empty? If so, we don't always need to display it.
92     *
93     * @param Individual $individual
94     *
95     * @return bool
96     */
97    public function hasTabContent(Individual $individual): bool
98    {
99        return Site::getPreference('map-provider') !== '';
100    }
101
102    /**
103     * A greyed out tab has no actual content, but may perhaps have
104     * options to create content.
105     *
106     * @param Individual $individual
107     *
108     * @return bool
109     */
110    public function isGrayedOut(Individual $individual): bool
111    {
112        return false;
113    }
114
115    /**
116     * Can this tab load asynchronously?
117     *
118     * @return bool
119     */
120    public function canLoadAjax(): bool
121    {
122        return true;
123    }
124
125    /**
126     * Generate the HTML content of this tab.
127     *
128     * @param Individual $individual
129     *
130     * @return string
131     */
132    public function getTabContent(Individual $individual): string
133    {
134        return view('modules/places/tab', [
135            'data' => $this->getMapData($individual),
136        ]);
137    }
138
139    /**
140     * @param Individual $indi
141     *
142     * @return stdClass
143     */
144    private function getMapData(Individual $indi): stdClass
145    {
146        $facts = $this->getPersonalFacts($indi);
147
148        $geojson = [
149            'type'     => 'FeatureCollection',
150            'features' => [],
151        ];
152
153        foreach ($facts as $id => $fact) {
154            $location = new Location($fact->place()->gedcomName());
155
156            // Use the co-ordinates from the fact (if they exist).
157            $latitude  = $fact->latitude();
158            $longitude = $fact->longitude();
159
160            // Use the co-ordinates from the location otherwise.
161            if ($latitude === 0.0 && $longitude === 0.0) {
162                $latitude  = $location->latitude();
163                $longitude = $location->longitude();
164            }
165
166            $icon = static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON;
167
168            if ($latitude !== 0.0 || $longitude !== 0.0) {
169                $geojson['features'][] = [
170                    'type'       => 'Feature',
171                    'id'         => $id,
172                    'valid'      => true,
173                    'geometry'   => [
174                        'type'        => 'Point',
175                        'coordinates' => [$longitude, $latitude],
176                    ],
177                    'properties' => [
178                        'polyline' => null,
179                        'icon'     => $icon,
180                        'tooltip'  => strip_tags($fact->place()->fullName()),
181                        'summary'  => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)),
182                        'zoom'     => $location->zoom(),
183                    ],
184                ];
185            }
186        }
187
188        return (object) $geojson;
189    }
190
191    /**
192     * @param Individual $individual
193     *
194     * @return Collection
195     * @throws Exception
196     */
197    private function getPersonalFacts(Individual $individual): Collection
198    {
199        $facts = $individual->facts();
200
201        foreach ($individual->spouseFamilies() as $family) {
202            $facts = $facts->merge($family->facts());
203            // Add birth of children from this family to the facts array
204            foreach ($family->children() as $child) {
205                $childsBirth = $child->facts(['BIRT'])->first();
206                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
207                    $facts->push($childsBirth);
208                }
209            }
210        }
211
212        $facts = Fact::sortFacts($facts);
213
214        return $facts->filter(static function (Fact $item): bool {
215            return $item->place()->gedcomName() !== '';
216        });
217    }
218
219    /**
220     * @param Individual $individual
221     * @param Fact       $fact
222     *
223     * @return mixed[]
224     */
225    private function summaryData(Individual $individual, Fact $fact): array
226    {
227        $record = $fact->record();
228        $name   = '';
229        $url    = '';
230        $tag    = $fact->label();
231
232        if ($record instanceof Family) {
233            // Marriage
234            $spouse = $record->spouse($individual);
235            if ($spouse instanceof Individual) {
236                $url  = $spouse->url();
237                $name = $spouse->fullName();
238            }
239        } elseif ($record !== $individual) {
240            // Birth of a child
241            $url  = $record->url();
242            $name = $record->fullName();
243            $tag  = GedcomTag::getLabel('_BIRT_CHIL', $record);
244        }
245
246        return [
247            'tag'    => $tag,
248            'url'    => $url,
249            'name'   => $name,
250            'value'  => $fact->value(),
251            'date'   => $fact->date()->display(true),
252            'place'  => $fact->place(),
253            'addtag' => false,
254        ];
255    }
256}
257