xref: /webtrees/app/Module/PlacesModule.php (revision 25cac958f1b72502fb13d2de663ace454f15e119)
11f374598SGreg Roach<?php
23976b470SGreg Roach
31f374598SGreg Roach/**
41f374598SGreg Roach * webtrees: online genealogy
55333da53SGreg Roach * Copyright (C) 2020 webtrees development team
61f374598SGreg Roach * This program is free software: you can redistribute it and/or modify
71f374598SGreg Roach * it under the terms of the GNU General Public License as published by
81f374598SGreg Roach * the Free Software Foundation, either version 3 of the License, or
91f374598SGreg Roach * (at your option) any later version.
101f374598SGreg Roach * This program is distributed in the hope that it will be useful,
111f374598SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
121f374598SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131f374598SGreg Roach * GNU General Public License for more details.
141f374598SGreg Roach * You should have received a copy of the GNU General Public License
151f374598SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
161f374598SGreg Roach */
17fcfa147eSGreg Roach
181f374598SGreg Roachdeclare(strict_types=1);
191f374598SGreg Roach
201f374598SGreg Roachnamespace Fisharebest\Webtrees\Module;
211f374598SGreg Roach
221f374598SGreg Roachuse Exception;
231f374598SGreg Roachuse Fisharebest\Webtrees\Fact;
248af6bbf8SGreg Roachuse Fisharebest\Webtrees\Family;
258af6bbf8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
261f374598SGreg Roachuse Fisharebest\Webtrees\I18N;
271f374598SGreg Roachuse Fisharebest\Webtrees\Individual;
285333da53SGreg Roachuse Fisharebest\Webtrees\PlaceLocation;
29840bd0d7SGreg Roachuse Fisharebest\Webtrees\Site;
3039ca88baSGreg Roachuse Illuminate\Support\Collection;
3105ff554cSGreg Roachuse stdClass;
321f374598SGreg Roach
331f374598SGreg Roach/**
341f374598SGreg Roach * Class PlacesMapModule
351f374598SGreg Roach */
3637eb8894SGreg Roachclass PlacesModule extends AbstractModule implements ModuleTabInterface
371f374598SGreg Roach{
3849a243cbSGreg Roach    use ModuleTabTrait;
3949a243cbSGreg Roach
401191c60cSGreg Roach    protected const ICONS = [
4180993423SGreg Roach        'BIRT' => ['color' => 'pink', 'name' => 'baby-carriage fas'],
4280993423SGreg Roach        'BAPM' => ['color' => 'pink', 'name' => 'water fas'],
4380993423SGreg Roach        'BARM' => ['color' => 'pink', 'name' => 'star-of-david fas'],
4480993423SGreg Roach        'BASM' => ['color' => 'pink', 'name' => 'star-of-david fas'],
4580993423SGreg Roach        'CHR'  => ['color' => 'pink', 'name' => 'water fas'],
4680993423SGreg Roach        'CHRA' => ['color' => 'pink', 'name' => 'water fas'],
4780993423SGreg Roach        'MARR' => ['color' => 'green', 'name' => 'infinity fas'],
4880993423SGreg Roach        'DEAT' => ['color' => 'black', 'name' => 'times fas'],
4980993423SGreg Roach        'BURI' => ['color' => 'purple', 'name' => 'times fas'],
5080993423SGreg Roach        'CREM' => ['color' => 'black', 'name' => 'times fas'],
5180993423SGreg Roach        'CENS' => ['color' => 'cyan', 'name' => 'list fas'],
5280993423SGreg Roach        'RESI' => ['color' => 'cyan', 'name' => 'home fas'],
5380993423SGreg Roach        'OCCU' => ['color' => 'cyan', 'name' => 'industry fas'],
5480993423SGreg Roach        'GRAD' => ['color' => 'violet', 'name' => 'university fas'],
5580993423SGreg Roach        'EDUC' => ['color' => 'violet', 'name' => 'university fas'],
568af6bbf8SGreg Roach    ];
578af6bbf8SGreg Roach
5857862dd5SDavid Drury    protected const DEFAULT_ICON = ['color' => 'gold', 'name' => 'bullseye fas'];
598af6bbf8SGreg Roach
60961ec755SGreg Roach    /**
610cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
62961ec755SGreg Roach     *
63961ec755SGreg Roach     * @return string
64961ec755SGreg Roach     */
6549a243cbSGreg Roach    public function title(): string
661f374598SGreg Roach    {
67bbb76c12SGreg Roach        /* I18N: Name of a module */
68bbb76c12SGreg Roach        return I18N::translate('Places');
691f374598SGreg Roach    }
701f374598SGreg Roach
71961ec755SGreg Roach    /**
72961ec755SGreg Roach     * A sentence describing what this module does.
73961ec755SGreg Roach     *
74961ec755SGreg Roach     * @return string
75961ec755SGreg Roach     */
7649a243cbSGreg Roach    public function description(): string
771f374598SGreg Roach    {
78d21f0b10SGreg Roach        /* I18N: Description of the “Places” module */
79bbb76c12SGreg Roach        return I18N::translate('Show the location of events on a map.');
801f374598SGreg Roach    }
811f374598SGreg Roach
8249a243cbSGreg Roach    /**
8349a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
8449a243cbSGreg Roach     *
8549a243cbSGreg Roach     * @return int
8649a243cbSGreg Roach     */
87cbf4b7faSGreg Roach    public function defaultTabOrder(): int
88cbf4b7faSGreg Roach    {
89fb7a0427SGreg Roach        return 8;
901f374598SGreg Roach    }
911f374598SGreg Roach
923caaa4d2SGreg Roach    /**
933caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
943caaa4d2SGreg Roach     *
953caaa4d2SGreg Roach     * @param Individual $individual
963caaa4d2SGreg Roach     *
973caaa4d2SGreg Roach     * @return bool
983caaa4d2SGreg Roach     */
998f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
1001f374598SGreg Roach    {
10176092b6cSGreg Roach        return Site::getPreference('map-provider') !== '' &&
10276092b6cSGreg Roach            $this->getMapData($individual)->features !== [];
1031f374598SGreg Roach    }
1041f374598SGreg Roach
1053caaa4d2SGreg Roach    /**
1063caaa4d2SGreg Roach     * A greyed out tab has no actual content, but may perhaps have
1073caaa4d2SGreg Roach     * options to create content.
1083caaa4d2SGreg Roach     *
1093caaa4d2SGreg Roach     * @param Individual $individual
1103caaa4d2SGreg Roach     *
1113caaa4d2SGreg Roach     * @return bool
1123caaa4d2SGreg Roach     */
1138f53f488SRico Sonntag    public function isGrayedOut(Individual $individual): bool
1141f374598SGreg Roach    {
1151f374598SGreg Roach        return false;
1161f374598SGreg Roach    }
1171f374598SGreg Roach
1183caaa4d2SGreg Roach    /**
1193caaa4d2SGreg Roach     * Can this tab load asynchronously?
1203caaa4d2SGreg Roach     *
1213caaa4d2SGreg Roach     * @return bool
1223caaa4d2SGreg Roach     */
1238f53f488SRico Sonntag    public function canLoadAjax(): bool
1241f374598SGreg Roach    {
1251f374598SGreg Roach        return true;
1261f374598SGreg Roach    }
1271f374598SGreg Roach
1283caaa4d2SGreg Roach    /**
1293caaa4d2SGreg Roach     * Generate the HTML content of this tab.
1303caaa4d2SGreg Roach     *
1313caaa4d2SGreg Roach     * @param Individual $individual
1323caaa4d2SGreg Roach     *
1333caaa4d2SGreg Roach     * @return string
1343caaa4d2SGreg Roach     */
1359b34404bSGreg Roach    public function getTabContent(Individual $individual): string
1361f374598SGreg Roach    {
137aacdcb0dSGreg Roach        return view('modules/places/tab', [
13805ff554cSGreg Roach            'data'     => $this->getMapData($individual),
139597fb44bSDavid Drury            'provider' => [
1402cbb0620SDavid Drury                'url'    => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
1412cbb0620SDavid Drury                'options' => [
1422cbb0620SDavid Drury                    'attribution' => '<a href="https://www.openstreetmap.org/copyright">&copy; OpenStreetMap</a> contributors',
1432cbb0620SDavid Drury                    'max_zoom'    => 19
1442cbb0620SDavid Drury                ]
145597fb44bSDavid Drury            ]
146aacdcb0dSGreg Roach        ]);
1471f374598SGreg Roach    }
1481f374598SGreg Roach
1491f374598SGreg Roach    /**
150e93111adSRico Sonntag     * @param Individual $indi
1511f374598SGreg Roach     *
15205ff554cSGreg Roach     * @return stdClass
1531f374598SGreg Roach     */
15405ff554cSGreg Roach    private function getMapData(Individual $indi): stdClass
1551f374598SGreg Roach    {
15605ff554cSGreg Roach        $facts = $this->getPersonalFacts($indi);
1571f374598SGreg Roach
1581f374598SGreg Roach        $geojson = [
1591f374598SGreg Roach            'type'     => 'FeatureCollection',
1601f374598SGreg Roach            'features' => [],
1611f374598SGreg Roach        ];
16205ff554cSGreg Roach
1631f374598SGreg Roach        foreach ($facts as $id => $fact) {
1645333da53SGreg Roach            $location = new PlaceLocation($fact->place()->gedcomName());
1658af6bbf8SGreg Roach
1668af6bbf8SGreg Roach            // Use the co-ordinates from the fact (if they exist).
1678af6bbf8SGreg Roach            $latitude  = $fact->latitude();
1688af6bbf8SGreg Roach            $longitude = $fact->longitude();
1698af6bbf8SGreg Roach
1708af6bbf8SGreg Roach            // Use the co-ordinates from the location otherwise.
1718af6bbf8SGreg Roach            if ($latitude === 0.0 && $longitude === 0.0) {
1728af6bbf8SGreg Roach                $latitude  = $location->latitude();
1738af6bbf8SGreg Roach                $longitude = $location->longitude();
1748af6bbf8SGreg Roach            }
1758af6bbf8SGreg Roach
1768af6bbf8SGreg Roach            if ($latitude !== 0.0 || $longitude !== 0.0) {
1771f374598SGreg Roach                $geojson['features'][] = [
1781f374598SGreg Roach                    'type'       => 'Feature',
1791f374598SGreg Roach                    'id'         => $id,
1801f374598SGreg Roach                    'geometry'   => [
1811f374598SGreg Roach                        'type'        => 'Point',
182f465e1eaSGreg Roach                        'coordinates' => [$longitude, $latitude],
1831f374598SGreg Roach                    ],
1841f374598SGreg Roach                    'properties' => [
1859ecabc6aSGreg Roach                        'icon'     => static::ICONS[$fact->tag()] ?? static::DEFAULT_ICON,
1868af6bbf8SGreg Roach                        'tooltip'  => strip_tags($fact->place()->fullName()),
1878af6bbf8SGreg Roach                        'summary'  => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)),
1888af6bbf8SGreg Roach                        'zoom'     => $location->zoom(),
1891f374598SGreg Roach                    ],
1901f374598SGreg Roach                ];
1911f374598SGreg Roach            }
1921f374598SGreg Roach        }
1931f374598SGreg Roach
19405ff554cSGreg Roach        return (object) $geojson;
1951f374598SGreg Roach    }
1961f374598SGreg Roach
1971f374598SGreg Roach    /**
19805ff554cSGreg Roach     * @param Individual $individual
199c8a5344dSGreg Roach     *
200b5c8fd7eSGreg Roach     * @return Collection<Fact>
201c8a5344dSGreg Roach     * @throws Exception
202c8a5344dSGreg Roach     */
203c8a5344dSGreg Roach    private function getPersonalFacts(Individual $individual): Collection
204c8a5344dSGreg Roach    {
205c8a5344dSGreg Roach        $facts = $individual->facts();
206c8a5344dSGreg Roach
207c8a5344dSGreg Roach        foreach ($individual->spouseFamilies() as $family) {
208c8a5344dSGreg Roach            $facts = $facts->merge($family->facts());
209c8a5344dSGreg Roach            // Add birth of children from this family to the facts array
210c8a5344dSGreg Roach            foreach ($family->children() as $child) {
211c8a5344dSGreg Roach                $childsBirth = $child->facts(['BIRT'])->first();
212c8a5344dSGreg Roach                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
213c8a5344dSGreg Roach                    $facts->push($childsBirth);
214c8a5344dSGreg Roach                }
215c8a5344dSGreg Roach            }
216c8a5344dSGreg Roach        }
217c8a5344dSGreg Roach
218c8a5344dSGreg Roach        $facts = Fact::sortFacts($facts);
219c8a5344dSGreg Roach
220c8a5344dSGreg Roach        return $facts->filter(static function (Fact $item): bool {
221c8a5344dSGreg Roach            return $item->place()->gedcomName() !== '';
222c8a5344dSGreg Roach        });
223c8a5344dSGreg Roach    }
224c8a5344dSGreg Roach
225c8a5344dSGreg Roach    /**
226c8a5344dSGreg Roach     * @param Individual $individual
2278af6bbf8SGreg Roach     * @param Fact       $fact
2288af6bbf8SGreg Roach     *
2298af6bbf8SGreg Roach     * @return mixed[]
2308af6bbf8SGreg Roach     */
2318af6bbf8SGreg Roach    private function summaryData(Individual $individual, Fact $fact): array
2328af6bbf8SGreg Roach    {
2338af6bbf8SGreg Roach        $record = $fact->record();
2348af6bbf8SGreg Roach        $name   = '';
2358af6bbf8SGreg Roach        $url    = '';
2368af6bbf8SGreg Roach        $tag    = $fact->label();
2378af6bbf8SGreg Roach
2388af6bbf8SGreg Roach        if ($record instanceof Family) {
2398af6bbf8SGreg Roach            // Marriage
24039ca88baSGreg Roach            $spouse = $record->spouse($individual);
2418af6bbf8SGreg Roach            if ($spouse instanceof Individual) {
2428af6bbf8SGreg Roach                $url  = $spouse->url();
24339ca88baSGreg Roach                $name = $spouse->fullName();
2448af6bbf8SGreg Roach            }
2458af6bbf8SGreg Roach        } elseif ($record !== $individual) {
2468af6bbf8SGreg Roach            // Birth of a child
2478af6bbf8SGreg Roach            $url  = $record->url();
24839ca88baSGreg Roach            $name = $record->fullName();
249*25cac958SGreg Roach            $tag  = I18N::translate('Birth of a child');
2508af6bbf8SGreg Roach        }
2518af6bbf8SGreg Roach
2528af6bbf8SGreg Roach        return [
2538af6bbf8SGreg Roach            'tag'    => $tag,
2548af6bbf8SGreg Roach            'url'    => $url,
2558af6bbf8SGreg Roach            'name'   => $name,
2568af6bbf8SGreg Roach            'value'  => $fact->value(),
2578af6bbf8SGreg Roach            'date'   => $fact->date()->display(true),
2588af6bbf8SGreg Roach            'place'  => $fact->place(),
2598af6bbf8SGreg Roach            'addtag' => false,
2608af6bbf8SGreg Roach        ];
2618af6bbf8SGreg Roach    }
2621f374598SGreg Roach}
263