xref: /webtrees/app/Module/PlacesModule.php (revision c9c6f2ec6e88594e58f14a6418e0de5aaa1484bd)
11f374598SGreg Roach<?php
23976b470SGreg Roach
31f374598SGreg Roach/**
41f374598SGreg Roach * webtrees: online genealogy
590949315SGreg Roach * Copyright (C) 2021 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
251f374598SGreg Roachuse Fisharebest\Webtrees\I18N;
261f374598SGreg Roachuse Fisharebest\Webtrees\Individual;
275333da53SGreg Roachuse Fisharebest\Webtrees\PlaceLocation;
28*c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\LeafletJsService;
29*c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
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
60*c9c6f2ecSGreg Roach    private LeafletJsService $leaflet_js_service;
61*c9c6f2ecSGreg Roach
62*c9c6f2ecSGreg Roach    private ModuleService $module_service;
63*c9c6f2ecSGreg Roach
64*c9c6f2ecSGreg Roach    /**
65*c9c6f2ecSGreg Roach     * PlacesModule constructor.
66*c9c6f2ecSGreg Roach     *
67*c9c6f2ecSGreg Roach     * @param LeafletJsService $leaflet_js_service
68*c9c6f2ecSGreg Roach     * @param ModuleService    $module_service
69*c9c6f2ecSGreg Roach     */
70*c9c6f2ecSGreg Roach    public function __construct(LeafletJsService $leaflet_js_service, ModuleService $module_service)
71*c9c6f2ecSGreg Roach    {
72*c9c6f2ecSGreg Roach        $this->leaflet_js_service = $leaflet_js_service;
73*c9c6f2ecSGreg Roach        $this->module_service = $module_service;
74*c9c6f2ecSGreg Roach    }
75*c9c6f2ecSGreg Roach
76961ec755SGreg Roach    /**
770cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
78961ec755SGreg Roach     *
79961ec755SGreg Roach     * @return string
80961ec755SGreg Roach     */
8149a243cbSGreg Roach    public function title(): string
821f374598SGreg Roach    {
83bbb76c12SGreg Roach        /* I18N: Name of a module */
84bbb76c12SGreg Roach        return I18N::translate('Places');
851f374598SGreg Roach    }
861f374598SGreg Roach
87961ec755SGreg Roach    /**
88961ec755SGreg Roach     * A sentence describing what this module does.
89961ec755SGreg Roach     *
90961ec755SGreg Roach     * @return string
91961ec755SGreg Roach     */
9249a243cbSGreg Roach    public function description(): string
931f374598SGreg Roach    {
94d21f0b10SGreg Roach        /* I18N: Description of the “Places” module */
95bbb76c12SGreg Roach        return I18N::translate('Show the location of events on a map.');
961f374598SGreg Roach    }
971f374598SGreg Roach
9849a243cbSGreg Roach    /**
9949a243cbSGreg Roach     * The default position for this tab.  It can be changed in the control panel.
10049a243cbSGreg Roach     *
10149a243cbSGreg Roach     * @return int
10249a243cbSGreg Roach     */
103cbf4b7faSGreg Roach    public function defaultTabOrder(): int
104cbf4b7faSGreg Roach    {
105fb7a0427SGreg Roach        return 8;
1061f374598SGreg Roach    }
1071f374598SGreg Roach
1083caaa4d2SGreg Roach    /**
1093caaa4d2SGreg Roach     * Is this tab empty? If so, we don't always need to display it.
1103caaa4d2SGreg Roach     *
1113caaa4d2SGreg Roach     * @param Individual $individual
1123caaa4d2SGreg Roach     *
1133caaa4d2SGreg Roach     * @return bool
1143caaa4d2SGreg Roach     */
1158f53f488SRico Sonntag    public function hasTabContent(Individual $individual): bool
1161f374598SGreg Roach    {
117*c9c6f2ecSGreg Roach        $map_providers = $this->module_service->findByInterface(ModuleMapProviderInterface::class);
1181f374598SGreg Roach
119*c9c6f2ecSGreg Roach        return $map_providers->isNotEmpty() && $this->getMapData($individual)->features !== [];
1201f374598SGreg Roach    }
1211f374598SGreg Roach
1221f374598SGreg Roach    /**
123e93111adSRico Sonntag     * @param Individual $indi
1241f374598SGreg Roach     *
12505ff554cSGreg Roach     * @return stdClass
1261f374598SGreg Roach     */
12705ff554cSGreg Roach    private function getMapData(Individual $indi): stdClass
1281f374598SGreg Roach    {
12905ff554cSGreg Roach        $facts = $this->getPersonalFacts($indi);
1301f374598SGreg Roach
1311f374598SGreg Roach        $geojson = [
1321f374598SGreg Roach            'type'     => 'FeatureCollection',
1331f374598SGreg Roach            'features' => [],
1341f374598SGreg Roach        ];
13505ff554cSGreg Roach
1361f374598SGreg Roach        foreach ($facts as $id => $fact) {
1375333da53SGreg Roach            $location = new PlaceLocation($fact->place()->gedcomName());
1388af6bbf8SGreg Roach
1398af6bbf8SGreg Roach            // Use the co-ordinates from the fact (if they exist).
1408af6bbf8SGreg Roach            $latitude  = $fact->latitude();
1418af6bbf8SGreg Roach            $longitude = $fact->longitude();
1428af6bbf8SGreg Roach
1438af6bbf8SGreg Roach            // Use the co-ordinates from the location otherwise.
14490949315SGreg Roach            if ($latitude === null || $longitude === null) {
1458af6bbf8SGreg Roach                $latitude  = $location->latitude();
1468af6bbf8SGreg Roach                $longitude = $location->longitude();
1478af6bbf8SGreg Roach            }
1488af6bbf8SGreg Roach
14990949315SGreg Roach            if ($latitude !== null && $longitude !== null) {
1501f374598SGreg Roach                $geojson['features'][] = [
1511f374598SGreg Roach                    'type'       => 'Feature',
1521f374598SGreg Roach                    'id'         => $id,
1531f374598SGreg Roach                    'geometry'   => [
1541f374598SGreg Roach                        'type'        => 'Point',
155f465e1eaSGreg Roach                        'coordinates' => [$longitude, $latitude],
1561f374598SGreg Roach                    ],
1571f374598SGreg Roach                    'properties' => [
1587fe676e5SGreg Roach                        'icon'    => static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON,
159497231cdSGreg Roach                        'tooltip' => $fact->place()->gedcomName(),
1608af6bbf8SGreg Roach                        'summary' => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)),
1611f374598SGreg Roach                    ],
1621f374598SGreg Roach                ];
1631f374598SGreg Roach            }
1641f374598SGreg Roach        }
1651f374598SGreg Roach
16605ff554cSGreg Roach        return (object) $geojson;
1671f374598SGreg Roach    }
1681f374598SGreg Roach
1691f374598SGreg Roach    /**
17005ff554cSGreg Roach     * @param Individual $individual
171c8a5344dSGreg Roach     *
172b5c8fd7eSGreg Roach     * @return Collection<Fact>
173c8a5344dSGreg Roach     * @throws Exception
174c8a5344dSGreg Roach     */
175c8a5344dSGreg Roach    private function getPersonalFacts(Individual $individual): Collection
176c8a5344dSGreg Roach    {
177c8a5344dSGreg Roach        $facts = $individual->facts();
178c8a5344dSGreg Roach
179c8a5344dSGreg Roach        foreach ($individual->spouseFamilies() as $family) {
180c8a5344dSGreg Roach            $facts = $facts->merge($family->facts());
181c8a5344dSGreg Roach            // Add birth of children from this family to the facts array
182c8a5344dSGreg Roach            foreach ($family->children() as $child) {
183c8a5344dSGreg Roach                $childsBirth = $child->facts(['BIRT'])->first();
184c8a5344dSGreg Roach                if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') {
185c8a5344dSGreg Roach                    $facts->push($childsBirth);
186c8a5344dSGreg Roach                }
187c8a5344dSGreg Roach            }
188c8a5344dSGreg Roach        }
189c8a5344dSGreg Roach
190c8a5344dSGreg Roach        $facts = Fact::sortFacts($facts);
191c8a5344dSGreg Roach
192c8a5344dSGreg Roach        return $facts->filter(static function (Fact $item): bool {
193c8a5344dSGreg Roach            return $item->place()->gedcomName() !== '';
194c8a5344dSGreg Roach        });
195c8a5344dSGreg Roach    }
196c8a5344dSGreg Roach
197c8a5344dSGreg Roach    /**
198c8a5344dSGreg Roach     * @param Individual $individual
1998af6bbf8SGreg Roach     * @param Fact       $fact
2008af6bbf8SGreg Roach     *
2018af6bbf8SGreg Roach     * @return mixed[]
2028af6bbf8SGreg Roach     */
2038af6bbf8SGreg Roach    private function summaryData(Individual $individual, Fact $fact): array
2048af6bbf8SGreg Roach    {
2058af6bbf8SGreg Roach        $record = $fact->record();
2068af6bbf8SGreg Roach        $name   = '';
2078af6bbf8SGreg Roach        $url    = '';
2088af6bbf8SGreg Roach        $tag    = $fact->label();
2098af6bbf8SGreg Roach
2108af6bbf8SGreg Roach        if ($record instanceof Family) {
2118af6bbf8SGreg Roach            // Marriage
21239ca88baSGreg Roach            $spouse = $record->spouse($individual);
2138af6bbf8SGreg Roach            if ($spouse instanceof Individual) {
2148af6bbf8SGreg Roach                $url  = $spouse->url();
21539ca88baSGreg Roach                $name = $spouse->fullName();
2168af6bbf8SGreg Roach            }
2178af6bbf8SGreg Roach        } elseif ($record !== $individual) {
2188af6bbf8SGreg Roach            // Birth of a child
2198af6bbf8SGreg Roach            $url  = $record->url();
22039ca88baSGreg Roach            $name = $record->fullName();
22125cac958SGreg Roach            $tag  = I18N::translate('Birth of a child');
2228af6bbf8SGreg Roach        }
2238af6bbf8SGreg Roach
2248af6bbf8SGreg Roach        return [
2258af6bbf8SGreg Roach            'tag'   => $tag,
2268af6bbf8SGreg Roach            'url'   => $url,
2278af6bbf8SGreg Roach            'name'  => $name,
2288af6bbf8SGreg Roach            'value' => $fact->value(),
2298af6bbf8SGreg Roach            'date'  => $fact->date()->display(true),
2308af6bbf8SGreg Roach            'place' => $fact->place(),
2318af6bbf8SGreg Roach        ];
2328af6bbf8SGreg Roach    }
233*c9c6f2ecSGreg Roach
234*c9c6f2ecSGreg Roach    /**
235*c9c6f2ecSGreg Roach     * A greyed out tab has no actual content, but may perhaps have
236*c9c6f2ecSGreg Roach     * options to create content.
237*c9c6f2ecSGreg Roach     *
238*c9c6f2ecSGreg Roach     * @param Individual $individual
239*c9c6f2ecSGreg Roach     *
240*c9c6f2ecSGreg Roach     * @return bool
241*c9c6f2ecSGreg Roach     */
242*c9c6f2ecSGreg Roach    public function isGrayedOut(Individual $individual): bool
243*c9c6f2ecSGreg Roach    {
244*c9c6f2ecSGreg Roach        return false;
245*c9c6f2ecSGreg Roach    }
246*c9c6f2ecSGreg Roach
247*c9c6f2ecSGreg Roach    /**
248*c9c6f2ecSGreg Roach     * Can this tab load asynchronously?
249*c9c6f2ecSGreg Roach     *
250*c9c6f2ecSGreg Roach     * @return bool
251*c9c6f2ecSGreg Roach     */
252*c9c6f2ecSGreg Roach    public function canLoadAjax(): bool
253*c9c6f2ecSGreg Roach    {
254*c9c6f2ecSGreg Roach        return true;
255*c9c6f2ecSGreg Roach    }
256*c9c6f2ecSGreg Roach
257*c9c6f2ecSGreg Roach    /**
258*c9c6f2ecSGreg Roach     * Generate the HTML content of this tab.
259*c9c6f2ecSGreg Roach     *
260*c9c6f2ecSGreg Roach     * @param Individual $individual
261*c9c6f2ecSGreg Roach     *
262*c9c6f2ecSGreg Roach     * @return string
263*c9c6f2ecSGreg Roach     */
264*c9c6f2ecSGreg Roach    public function getTabContent(Individual $individual): string
265*c9c6f2ecSGreg Roach    {
266*c9c6f2ecSGreg Roach        return view('modules/places/tab', [
267*c9c6f2ecSGreg Roach            'data'           => $this->getMapData($individual),
268*c9c6f2ecSGreg Roach            'leaflet_config' => $this->leaflet_js_service->config(),
269*c9c6f2ecSGreg Roach        ]);
270*c9c6f2ecSGreg Roach    }
2711f374598SGreg Roach}
272