xref: /webtrees/app/PlaceLocation.php (revision 94e359174878c0eebf102ead543b66cb58a757d0)
15333da53SGreg Roach<?php
25333da53SGreg Roach
35333da53SGreg Roach/**
45333da53SGreg Roach * webtrees: online genealogy
55333da53SGreg Roach * Copyright (C) 2020 webtrees development team
65333da53SGreg Roach * This program is free software: you can redistribute it and/or modify
75333da53SGreg Roach * it under the terms of the GNU General Public License as published by
85333da53SGreg Roach * the Free Software Foundation, either version 3 of the License, or
95333da53SGreg Roach * (at your option) any later version.
105333da53SGreg Roach * This program is distributed in the hope that it will be useful,
115333da53SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
125333da53SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135333da53SGreg Roach * GNU General Public License for more details.
145333da53SGreg Roach * You should have received a copy of the GNU General Public License
155333da53SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
165333da53SGreg Roach */
175333da53SGreg Roach
185333da53SGreg Roachdeclare(strict_types=1);
195333da53SGreg Roach
205333da53SGreg Roachnamespace Fisharebest\Webtrees;
215333da53SGreg Roach
225333da53SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService;
235333da53SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
245333da53SGreg Roachuse Illuminate\Support\Collection;
255333da53SGreg Roachuse stdClass;
265333da53SGreg Roach
2779de923dSGreg Roachuse function app;
2879de923dSGreg Roachuse function preg_split;
2979de923dSGreg Roach
305333da53SGreg Roach/**
315333da53SGreg Roach * Class PlaceLocation
325333da53SGreg Roach */
335333da53SGreg Roachclass PlaceLocation
345333da53SGreg Roach{
355333da53SGreg Roach    /** @var string e.g. "Westminster, London, England" */
365333da53SGreg Roach    private $location_name;
375333da53SGreg Roach
38*94e35917SGreg Roach    /** @var Collection<string> The parts of a location name, e.g. ["Westminster", "London", "England"] */
395333da53SGreg Roach    private $parts;
405333da53SGreg Roach
415333da53SGreg Roach    /**
425333da53SGreg Roach     * Create a place-location.
435333da53SGreg Roach     *
445333da53SGreg Roach     * @param string $location_name
455333da53SGreg Roach     */
465333da53SGreg Roach    public function __construct(string $location_name)
475333da53SGreg Roach    {
485333da53SGreg Roach        // Ignore any empty parts in location names such as "Village, , , Country".
495333da53SGreg Roach        $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name)))
505333da53SGreg Roach            ->filter();
515333da53SGreg Roach
525333da53SGreg Roach        // Rebuild the location name in the correct format.
535333da53SGreg Roach        $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
545333da53SGreg Roach    }
555333da53SGreg Roach
565333da53SGreg Roach    /**
575333da53SGreg Roach     * Get the higher level location.
585333da53SGreg Roach     *
595333da53SGreg Roach     * @return PlaceLocation
605333da53SGreg Roach     */
615333da53SGreg Roach    public function parent(): PlaceLocation
625333da53SGreg Roach    {
635333da53SGreg Roach        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR));
645333da53SGreg Roach    }
655333da53SGreg Roach
665333da53SGreg Roach    /**
675333da53SGreg Roach     * The database row that contains this location.
685333da53SGreg Roach     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
695333da53SGreg Roach     *
705333da53SGreg Roach     * @return int
715333da53SGreg Roach     */
725333da53SGreg Roach    public function id(): int
735333da53SGreg Roach    {
745333da53SGreg Roach        return app('cache.array')->remember('location-' . $this->location_name, function () {
755333da53SGreg Roach            // The "top-level" location won't exist in the database.
765333da53SGreg Roach            if ($this->parts->isEmpty()) {
775333da53SGreg Roach                return 0;
785333da53SGreg Roach            }
795333da53SGreg Roach
805333da53SGreg Roach            $parent_location_id = $this->parent()->id();
815333da53SGreg Roach
825333da53SGreg Roach            $location_id = (int) DB::table('placelocation')
83c4ab7783SGreg Roach                ->where('pl_place', '=', mb_substr($this->parts->first(), 0, 120))
845333da53SGreg Roach                ->where('pl_parent_id', '=', $parent_location_id)
855333da53SGreg Roach                ->value('pl_id');
865333da53SGreg Roach
875333da53SGreg Roach            if ($location_id === 0) {
885333da53SGreg Roach                $location = $this->parts->first();
895333da53SGreg Roach
905333da53SGreg Roach                $location_id = 1 + (int) DB::table('placelocation')->max('pl_id');
915333da53SGreg Roach
925333da53SGreg Roach                DB::table('placelocation')->insert([
935333da53SGreg Roach                    'pl_id'        => $location_id,
94c4ab7783SGreg Roach                    'pl_place'     => mb_substr($location, 0, 120),
955333da53SGreg Roach                    'pl_parent_id' => $parent_location_id,
965333da53SGreg Roach                    'pl_level'     => $this->parts->count() - 1,
975333da53SGreg Roach                    'pl_lati'      => '',
985333da53SGreg Roach                    'pl_long'      => '',
995333da53SGreg Roach                    'pl_icon'      => '',
1005333da53SGreg Roach                    'pl_zoom'      => 2,
1015333da53SGreg Roach                ]);
1025333da53SGreg Roach            }
1035333da53SGreg Roach
1045333da53SGreg Roach            return $location_id;
1055333da53SGreg Roach        });
1065333da53SGreg Roach    }
1075333da53SGreg Roach
1085333da53SGreg Roach    /**
1095333da53SGreg Roach     * Does this location exist in the database?  Note that calls to PlaceLocation::id() will
1105333da53SGreg Roach     * create the row, so this function is only meaningful when called before a call to PlaceLocation::id().
1115333da53SGreg Roach     *
1125333da53SGreg Roach     * @return bool
1135333da53SGreg Roach     */
1145333da53SGreg Roach    public function exists(): bool
1155333da53SGreg Roach    {
1165333da53SGreg Roach        $location_id = 0;
1175333da53SGreg Roach
1185333da53SGreg Roach        $this->parts->reverse()->each(static function (string $place) use (&$location_id) {
1195333da53SGreg Roach            if ($location_id !== null) {
1205333da53SGreg Roach                $location_id = DB::table('placelocation')
1215333da53SGreg Roach                    ->where('pl_parent_id', '=', $location_id)
122c4ab7783SGreg Roach                    ->where('pl_place', '=', mb_substr($place, 0, 120))
1235333da53SGreg Roach                    ->value('pl_id');
1245333da53SGreg Roach            }
1255333da53SGreg Roach        });
1265333da53SGreg Roach
1275333da53SGreg Roach        return $location_id !== null;
1285333da53SGreg Roach    }
1295333da53SGreg Roach
1305333da53SGreg Roach    /**
1315333da53SGreg Roach     * @return stdClass
1325333da53SGreg Roach     */
1335333da53SGreg Roach    private function details(): stdClass
1345333da53SGreg Roach    {
1355333da53SGreg Roach        return app('cache.array')->remember('location-details-' . $this->id(), function () {
1365333da53SGreg Roach            // The "top-level" location won't exist in the database.
1375333da53SGreg Roach            if ($this->parts->isEmpty()) {
1385333da53SGreg Roach                return (object) [
1395333da53SGreg Roach                    'pl_id'        => '0',
1405333da53SGreg Roach                    'pl_parent_id' => '0',
1415333da53SGreg Roach                    'pl_level' => null,
1425333da53SGreg Roach                    'pl_place' => '',
1435333da53SGreg Roach                    'pl_lati' => null,
1445333da53SGreg Roach                    'pl_long' => null,
1455333da53SGreg Roach                    'pl_zoom' => null,
1465333da53SGreg Roach                    'pl_icon' => null,
1475333da53SGreg Roach                    'pl_media' => null,
1485333da53SGreg Roach                ];
1495333da53SGreg Roach            }
1505333da53SGreg Roach
1515333da53SGreg Roach            return DB::table('placelocation')
1525333da53SGreg Roach                ->where('pl_id', '=', $this->id())
1535333da53SGreg Roach                ->first();
1545333da53SGreg Roach        });
1555333da53SGreg Roach    }
1565333da53SGreg Roach
1575333da53SGreg Roach    /**
1585333da53SGreg Roach     * Latitude of the location.
1595333da53SGreg Roach     *
1605333da53SGreg Roach     * @return float
1615333da53SGreg Roach     */
1625333da53SGreg Roach    public function latitude(): float
1635333da53SGreg Roach    {
1645333da53SGreg Roach        $gedcom_service = new GedcomService();
1655333da53SGreg Roach
166e340fe65SGreg Roach        $tmp = $this;
167e340fe65SGreg Roach        do {
168e340fe65SGreg Roach            $pl_lati = (string) $tmp->details()->pl_lati;
169e340fe65SGreg Roach            $tmp = $tmp->parent();
170e340fe65SGreg Roach        } while ($pl_lati === '' && $tmp->id() !== 0);
171e340fe65SGreg Roach
172e340fe65SGreg Roach        return $gedcom_service->readLatitude($pl_lati);
1735333da53SGreg Roach    }
1745333da53SGreg Roach
1755333da53SGreg Roach    /**
1765333da53SGreg Roach     * Latitude of the longitude.
1775333da53SGreg Roach     *
1785333da53SGreg Roach     * @return float
1795333da53SGreg Roach     */
1805333da53SGreg Roach    public function longitude(): float
1815333da53SGreg Roach    {
1825333da53SGreg Roach        $gedcom_service = new GedcomService();
1835333da53SGreg Roach
184e340fe65SGreg Roach        $tmp = $this;
185e340fe65SGreg Roach        do {
186e340fe65SGreg Roach            $pl_long = (string) $tmp->details()->pl_long;
187e340fe65SGreg Roach            $tmp = $tmp->parent();
188e340fe65SGreg Roach        } while ($pl_long === '' && $tmp->id() !== 0);
189e340fe65SGreg Roach
190e340fe65SGreg Roach        return $gedcom_service->readLongitude($pl_long);
1915333da53SGreg Roach    }
1925333da53SGreg Roach
1935333da53SGreg Roach    /**
1945333da53SGreg Roach     * The icon for the location.
1955333da53SGreg Roach     *
1965333da53SGreg Roach     * @return string
1975333da53SGreg Roach     */
1985333da53SGreg Roach    public function icon(): string
1995333da53SGreg Roach    {
2005333da53SGreg Roach        return (string) $this->details()->pl_icon;
2015333da53SGreg Roach    }
2025333da53SGreg Roach
2035333da53SGreg Roach    /**
2045333da53SGreg Roach     * Zoom level for the location.
2055333da53SGreg Roach     *
2065333da53SGreg Roach     * @return int
2075333da53SGreg Roach     */
2085333da53SGreg Roach    public function zoom(): int
2095333da53SGreg Roach    {
210e340fe65SGreg Roach        return (int) $this->details()->pl_zoom ?: 2;
2115333da53SGreg Roach    }
2125333da53SGreg Roach
2135333da53SGreg Roach    /**
2145333da53SGreg Roach     * @return string
2155333da53SGreg Roach     */
2165333da53SGreg Roach    public function locationName(): string
2175333da53SGreg Roach    {
2185333da53SGreg Roach        return (string) $this->parts->first();
2195333da53SGreg Roach    }
22079de923dSGreg Roach
22179de923dSGreg Roach    /**
22279de923dSGreg Roach     * Find a rectangle that (approximately) encloses this place.
22379de923dSGreg Roach     *
22479de923dSGreg Roach     * @return array<array<float>>
22579de923dSGreg Roach     */
22679de923dSGreg Roach    public function boundingRectangle(): array
22779de923dSGreg Roach    {
2289a9dfcf7SGreg Roach        if ($this->id() === 0) {
2299a9dfcf7SGreg Roach            return [[-180.0, -90.0], [180.0, 90.0]];
23079de923dSGreg Roach        }
23179de923dSGreg Roach
2329a9dfcf7SGreg Roach        // Find our own co-ordinates and those of any child places
2339a9dfcf7SGreg Roach        $latitudes = DB::table('placelocation')
2349a9dfcf7SGreg Roach            ->where('pl_parent_id', '=', $this->id())
2359a9dfcf7SGreg Roach            ->orWhere('pl_id', '=', $this->id())
2369a9dfcf7SGreg Roach            ->pluck('pl_lati')
2379a9dfcf7SGreg Roach            ->filter()
2389a9dfcf7SGreg Roach            ->map(static function (string $x): float {
2399a9dfcf7SGreg Roach                return (new GedcomService())->readLatitude($x);
2409a9dfcf7SGreg Roach            });
2419a9dfcf7SGreg Roach
2429a9dfcf7SGreg Roach        $longitudes = DB::table('placelocation')
2439a9dfcf7SGreg Roach            ->where('pl_parent_id', '=', $this->id())
2449a9dfcf7SGreg Roach            ->orWhere('pl_id', '=', $this->id())
2459a9dfcf7SGreg Roach            ->pluck('pl_long')
2469a9dfcf7SGreg Roach            ->filter()
2479a9dfcf7SGreg Roach            ->map(static function (string $x): float {
2489a9dfcf7SGreg Roach                return (new GedcomService())->readLongitude($x);
2499a9dfcf7SGreg Roach            });
2509a9dfcf7SGreg Roach
2519a9dfcf7SGreg Roach        if ($latitudes->count() > 1 || $longitudes->count() > 1) {
2529a9dfcf7SGreg Roach            return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]];
25379de923dSGreg Roach        }
25479de923dSGreg Roach
2559a9dfcf7SGreg Roach        return $this->parent()->boundingRectangle();
25679de923dSGreg Roach    }
2575333da53SGreg Roach}
258