xref: /webtrees/app/Location.php (revision 8af6bbf82759f6df0fe2b5b49cb4dd05118748d1)
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()->make('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                ]);
93            }
94
95            return $location_id;
96        });
97    }
98
99    /**
100     * Does this location exist in the database?  Note that calls to Location::id() will
101     * create the row, so this function is only meaningful when called before a call to Location::id().
102     *
103     * @return bool
104     */
105    public function exists(): bool {
106        $location_id = 0;
107
108        $this->parts->reverse()->each(function (string $place) use (&$location_id) {
109            if ($location_id !== null) {
110                $location_id = DB::table('placelocation')
111                    ->where('pl_parent_id', '=', $location_id)
112                    ->where('pl_place', '=', $place)
113                    ->value('pl_id');
114           }
115        });
116
117        return $location_id !== null;
118    }
119
120    /**
121     * @return stdClass
122     */
123    private function details(): stdClass
124    {
125        return app()->make('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->id(), function () {
126            // The "top-level" location won't exist in the database.
127            if ($this->parts->isEmpty()) {
128                return (object) [
129                    'pl_id'        => '0',
130                    'pl_parent_id' => '0',
131                    'pl_level' => null,
132                    'pl_place' => '',
133                    'pl_lati' => null,
134                    'pl_long' => null,
135                    'pl_zoom' => null,
136                    'pl_icon' => null,
137                    'pl_media' => null,
138                ];
139            }
140
141            return DB::table('placelocation')
142                ->where('pl_id', '=', $this->id())
143                ->first();
144        });
145    }
146
147    /**
148     * Latitude of the location.
149     *
150     * @return float
151     */
152    public function latitude(): float{
153        $gedcom_service = new GedcomService();
154
155        return $gedcom_service->readLatitude($this->details()->pl_lati ?? '');
156    }
157
158    /**
159     * Latitude of the longitude.
160     *
161     * @return float
162     */
163    public function longitude(): float{
164        $gedcom_service = new GedcomService();
165
166        return $gedcom_service->readLongitude($this->details()->pl_long ?? '');
167    }
168
169    /**
170     * The icon for the location.
171     *
172     * @return string
173     */
174    public function icon(): string
175    {
176        return (string) $this->details()->pl_icon;
177    }
178
179    /**
180     * Zoom level for the location.
181     *
182     * @return int
183     */
184    public function zoom(): int
185    {
186        return (int) $this->details()->pl_zoom;
187    }
188
189    /**
190     * @return string
191     */
192    public function locationName(): string
193    {
194        return (string) $this->parts->first();
195    }
196}
197