xref: /webtrees/app/Module/PlacesModule.php (revision 9c2a0f6255cc2c950e1dca736f5ca9e4e640651b)
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     * @return Fact[]
173     * @throws Exception
174     */
175    private function getPersonalFacts(Individual $individual): Collection
176    {
177        $facts = $individual->facts();
178
179        foreach ($individual->spouseFamilies() as $family) {
180            $facts = $facts->merge($family->facts());
181            // Add birth of children from this family to the facts array
182            foreach ($family->children() as $child) {
183                $childsBirth = $child->facts(['BIRT'])->first();
184                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
185                    $facts->push($childsBirth);
186                }
187            }
188        }
189
190        $facts = Fact::sortFacts($facts);
191
192        return $facts->filter(static function (Fact $item): bool {
193            return $item->place()->gedcomName() !== '';
194        });
195    }
196
197    /**
198     * @param Individual $individual
199     * @param Fact       $fact
200     *
201     * @return mixed[]
202     */
203    private function summaryData(Individual $individual, Fact $fact): array
204    {
205        $record = $fact->record();
206        $name   = '';
207        $url    = '';
208        $tag    = $fact->label();
209
210        if ($record instanceof Family) {
211            // Marriage
212            $spouse = $record->spouse($individual);
213            if ($spouse instanceof Individual) {
214                $url  = $spouse->url();
215                $name = $spouse->fullName();
216            }
217        } elseif ($record !== $individual) {
218            // Birth of a child
219            $url  = $record->url();
220            $name = $record->fullName();
221            $tag  = GedcomTag::getLabel('_BIRT_CHIL', $record);
222        }
223
224        return [
225            'tag'    => $tag,
226            'url'    => $url,
227            'name'   => $name,
228            'value'  => $fact->value(),
229            'date'   => $fact->date()->display(true),
230            'place'  => $fact->place(),
231            'addtag' => false,
232        ];
233    }
234}
235