xref: /webtrees/app/Location.php (revision 3cfcc809af53e831fa6cafac7b274a2cb407db6e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees;
20
21use Fisharebest\Webtrees\Services\GedcomService;
22use Illuminate\Database\Capsule\Manager as DB;
23use Illuminate\Support\Collection;
24use stdClass;
25
26/**
27 * Class Location
28 */
29class Location
30{
31    /** @var string e.g. "Westminster, London, England" */
32    private $location_name;
33
34    /** @var Collection|string[] The parts of a location name, e.g. ["Westminster", "London", "England"] */
35    private $parts;
36
37    /**
38     * Create a location.
39     *
40     * @param string $location_name
41     */
42    public function __construct(string $location_name)
43    {
44        // Ignore any empty parts in location names such as "Village, , , Country".
45        $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name)))
46            ->filter();
47
48        // Rebuild the location name in the correct format.
49        $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
50    }
51
52    /**
53     * Get the higher level location.
54     *
55     * @return Location
56     */
57    public function parent(): Location
58    {
59        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR));
60    }
61
62    /**
63     * The database row that contains this location.
64     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
65     *
66     * @return int
67     */
68    public function id(): int
69    {
70        return app('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->location_name, function () {
71            // The "top-level" location won't exist in the database.
72            if ($this->parts->isEmpty()) {
73                return 0;
74            }
75
76            $parent_location_id = $this->parent()->id();
77
78            $location_id = (int) DB::table('placelocation')
79                ->where('pl_place', '=', $this->parts->first())
80                ->where('pl_parent_id', '=', $parent_location_id)
81                ->value('pl_id');
82
83            if ($location_id === 0) {
84                $location = $this->parts->first();
85
86                $location_id = 1 + (int) DB::table('placelocation')->max('pl_id');
87
88                DB::table('placelocation')->insert([
89                    'pl_id'        => $location_id,
90                    'pl_place'     => $location,
91                    'pl_parent_id' => $parent_location_id,
92                    'pl_level'     => $this->parts->count() - 1,
93                    'pl_lati'      => '',
94                    'pl_long'      => '',
95                    'pl_icon'      => '',
96                    'pl_zoom'      => 2,
97                ]);
98            }
99
100            return $location_id;
101        });
102    }
103
104    /**
105     * Does this location exist in the database?  Note that calls to Location::id() will
106     * create the row, so this function is only meaningful when called before a call to Location::id().
107     *
108     * @return bool
109     */
110    public function exists(): bool
111    {
112        $location_id = 0;
113
114        $this->parts->reverse()->each(static function (string $place) use (&$location_id) {
115            if ($location_id !== null) {
116                $location_id = DB::table('placelocation')
117                    ->where('pl_parent_id', '=', $location_id)
118                    ->where('pl_place', '=', $place)
119                    ->value('pl_id');
120            }
121        });
122
123        return $location_id !== null;
124    }
125
126    /**
127     * @return stdClass
128     */
129    private function details(): stdClass
130    {
131        return app('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->id(), function () {
132            // The "top-level" location won't exist in the database.
133            if ($this->parts->isEmpty()) {
134                return (object) [
135                    'pl_id'        => '0',
136                    'pl_parent_id' => '0',
137                    'pl_level' => null,
138                    'pl_place' => '',
139                    'pl_lati' => null,
140                    'pl_long' => null,
141                    'pl_zoom' => null,
142                    'pl_icon' => null,
143                    'pl_media' => null,
144                ];
145            }
146
147            return DB::table('placelocation')
148                ->where('pl_id', '=', $this->id())
149                ->first();
150        });
151    }
152
153    /**
154     * Latitude of the location.
155     *
156     * @return float
157     */
158    public function latitude(): float
159    {
160        $gedcom_service = new GedcomService();
161
162        return $gedcom_service->readLatitude($this->details()->pl_lati ?? '');
163    }
164
165    /**
166     * Latitude of the longitude.
167     *
168     * @return float
169     */
170    public function longitude(): float
171    {
172        $gedcom_service = new GedcomService();
173
174        return $gedcom_service->readLongitude($this->details()->pl_long ?? '');
175    }
176
177    /**
178     * The icon for the location.
179     *
180     * @return string
181     */
182    public function icon(): string
183    {
184        return (string) $this->details()->pl_icon;
185    }
186
187    /**
188     * Zoom level for the location.
189     *
190     * @return int
191     */
192    public function zoom(): int
193    {
194        return (int) $this->details()->pl_zoom;
195    }
196
197    /**
198     * @return string
199     */
200    public function locationName(): string
201    {
202        return (string) $this->parts->first();
203    }
204}
205