xref: /webtrees/app/Location.php (revision 4a1f1d433794f86f893b59883147e57af4e94374)
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;
19
20use Fisharebest\Webtrees\Services\GedcomService;
21use Illuminate\Database\Capsule\Manager as DB;
22use Illuminate\Support\Collection;
23use stdClass;
24
25/**
26 * Class Location
27 */
28class Location
29{
30    /** @var string e.g. "Westminster, London, England" */
31    private $location_name;
32
33    /** @var Collection|string[] The parts of a location name, e.g. ["Westminster", "London", "England"] */
34    private $parts;
35
36    /**
37     * Create a location.
38     *
39     * @param string $location_name
40     */
41    public function __construct(string $location_name)
42    {
43        // Ignore any empty parts in location names such as "Village, , , Country".
44        $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name)))
45            ->filter();
46
47        // Rebuild the location name in the correct format.
48        $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
49    }
50
51    /**
52     * Get the higher level location.
53     *
54     * @return Location
55     */
56    public function parent(): Location
57    {
58        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR));
59    }
60
61    /**
62     * The database row that contains this location.
63     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
64     *
65     * @return int
66     */
67    public function id(): int
68    {
69        return app('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->location_name, function () {
70            // The "top-level" location won't exist in the database.
71            if ($this->parts->isEmpty()) {
72                return 0;
73            }
74
75            $parent_location_id = $this->parent()->id();
76
77            $location_id = (int) DB::table('placelocation')
78                ->where('pl_place', '=', $this->parts->first())
79                ->where('pl_parent_id', '=', $parent_location_id)
80                ->value('pl_id');
81
82            if ($location_id === 0) {
83                $location = $this->parts->first();
84
85                $location_id = 1 + (int) DB::table('placelocation')->max('pl_id');
86
87                DB::table('placelocation')->insert([
88                    'pl_id'        => $location_id,
89                    'pl_place'     => $location,
90                    'pl_parent_id' => $parent_location_id,
91                    'pl_level'     => $this->parts->count() - 1,
92                    'pl_lati'      => '',
93                    'pl_long'      => '',
94                    'pl_icon'      => '',
95                    'pl_zoom'      => 2,
96                ]);
97            }
98
99            return $location_id;
100        });
101    }
102
103    /**
104     * Does this location exist in the database?  Note that calls to Location::id() will
105     * create the row, so this function is only meaningful when called before a call to Location::id().
106     *
107     * @return bool
108     */
109    public function exists(): bool
110    {
111        $location_id = 0;
112
113        $this->parts->reverse()->each(static function (string $place) use (&$location_id) {
114            if ($location_id !== null) {
115                $location_id = DB::table('placelocation')
116                    ->where('pl_parent_id', '=', $location_id)
117                    ->where('pl_place', '=', $place)
118                    ->value('pl_id');
119            }
120        });
121
122        return $location_id !== null;
123    }
124
125    /**
126     * @return stdClass
127     */
128    private function details(): stdClass
129    {
130        return app('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->id(), function () {
131            // The "top-level" location won't exist in the database.
132            if ($this->parts->isEmpty()) {
133                return (object) [
134                    'pl_id'        => '0',
135                    'pl_parent_id' => '0',
136                    'pl_level' => null,
137                    'pl_place' => '',
138                    'pl_lati' => null,
139                    'pl_long' => null,
140                    'pl_zoom' => null,
141                    'pl_icon' => null,
142                    'pl_media' => null,
143                ];
144            }
145
146            return DB::table('placelocation')
147                ->where('pl_id', '=', $this->id())
148                ->first();
149        });
150    }
151
152    /**
153     * Latitude of the location.
154     *
155     * @return float
156     */
157    public function latitude(): float
158    {
159        $gedcom_service = new GedcomService();
160
161        return $gedcom_service->readLatitude($this->details()->pl_lati ?? '');
162    }
163
164    /**
165     * Latitude of the longitude.
166     *
167     * @return float
168     */
169    public function longitude(): float
170    {
171        $gedcom_service = new GedcomService();
172
173        return $gedcom_service->readLongitude($this->details()->pl_long ?? '');
174    }
175
176    /**
177     * The icon for the location.
178     *
179     * @return string
180     */
181    public function icon(): string
182    {
183        return (string) $this->details()->pl_icon;
184    }
185
186    /**
187     * Zoom level for the location.
188     *
189     * @return int
190     */
191    public function zoom(): int
192    {
193        return (int) $this->details()->pl_zoom;
194    }
195
196    /**
197     * @return string
198     */
199    public function locationName(): string
200    {
201        return (string) $this->parts->first();
202    }
203}
204