xref: /webtrees/app/Place.php (revision f25fc0f929f69ab8124cf0cecde45e457db7574a)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg 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/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
2267992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\ModuleInterface;
2387cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleListInterface;
2467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
2567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
26392561bbSGreg Roachuse Illuminate\Support\Collection;
27b68caec6SGreg Roach
28f70bcff5SGreg Roachuse function e;
29f70bcff5SGreg Roachuse function is_object;
30f70bcff5SGreg Roachuse function preg_split;
31f70bcff5SGreg Roachuse function strip_tags;
3290949315SGreg Roachuse function trim;
3390949315SGreg Roach
3490949315SGreg Roachuse const PREG_SPLIT_NO_EMPTY;
3590949315SGreg Roach
36a25f0a04SGreg Roach/**
3776692c8bSGreg Roach * A GEDCOM place (PLAC) object.
38a25f0a04SGreg Roach */
39c1010edaSGreg Roachclass Place
40c1010edaSGreg Roach{
4143f2f523SGreg Roach    // "Westminster, London, England"
4243f2f523SGreg Roach    private string $place_name;
43392561bbSGreg Roach
4436779af1SGreg Roach    /** @var Collection<int,string> The parts of a place name, e.g. ["Westminster", "London", "England"] */
4543f2f523SGreg Roach    private Collection $parts;
4684caa210SGreg Roach
4743f2f523SGreg Roach    private Tree $tree;
48a25f0a04SGreg Roach
49a25f0a04SGreg Roach    /**
5076692c8bSGreg Roach     * Create a place.
5176692c8bSGreg Roach     *
52392561bbSGreg Roach     * @param string $place_name
5384caa210SGreg Roach     * @param Tree   $tree
54a25f0a04SGreg Roach     */
55392561bbSGreg Roach    public function __construct(string $place_name, Tree $tree)
56c1010edaSGreg Roach    {
57392561bbSGreg Roach        // Ignore any empty parts in place names such as "Village, , , Country".
5890949315SGreg Roach        $place_name  = trim($place_name);
5990949315SGreg Roach        $this->parts = new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name, -1, PREG_SPLIT_NO_EMPTY));
60392561bbSGreg Roach
61392561bbSGreg Roach        // Rebuild the placename in the correct format.
62392561bbSGreg Roach        $this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
63392561bbSGreg Roach
6464b16745SGreg Roach        $this->tree = $tree;
65a25f0a04SGreg Roach    }
66a25f0a04SGreg Roach
67a25f0a04SGreg Roach    /**
687bed239cSGreg Roach     * Find a place by its ID.
697bed239cSGreg Roach     *
707bed239cSGreg Roach     * @param int  $id
717bed239cSGreg Roach     * @param Tree $tree
727bed239cSGreg Roach     *
737bed239cSGreg Roach     * @return Place
747bed239cSGreg Roach     */
757bed239cSGreg Roach    public static function find(int $id, Tree $tree): Place
767bed239cSGreg Roach    {
777bed239cSGreg Roach        $parts = new Collection();
787bed239cSGreg Roach
797bed239cSGreg Roach        while ($id !== 0) {
807bed239cSGreg Roach            $row = DB::table('places')
817bed239cSGreg Roach                ->where('p_file', '=', $tree->id())
827bed239cSGreg Roach                ->where('p_id', '=', $id)
837bed239cSGreg Roach                ->first();
847bed239cSGreg Roach
85f70bcff5SGreg Roach            if (is_object($row)) {
867bed239cSGreg Roach                $id = (int) $row->p_parent_id;
877bed239cSGreg Roach                $parts->add($row->p_place);
887bed239cSGreg Roach            } else {
897bed239cSGreg Roach                $id = 0;
907bed239cSGreg Roach            }
917bed239cSGreg Roach        }
927bed239cSGreg Roach
937bed239cSGreg Roach        $place_name = $parts->implode(Gedcom::PLACE_SEPARATOR);
947bed239cSGreg Roach
957bed239cSGreg Roach        return new Place($place_name, $tree);
967bed239cSGreg Roach    }
977bed239cSGreg Roach
987bed239cSGreg Roach    /**
9976692c8bSGreg Roach     * Get the higher level place.
10076692c8bSGreg Roach     *
101a25f0a04SGreg Roach     * @return Place
102a25f0a04SGreg Roach     */
103392561bbSGreg Roach    public function parent(): Place
104c1010edaSGreg Roach    {
1058af6bbf8SGreg Roach        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree);
106392561bbSGreg Roach    }
107392561bbSGreg Roach
108392561bbSGreg Roach    /**
109392561bbSGreg Roach     * The database row that contains this place.
110392561bbSGreg Roach     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
111392561bbSGreg Roach     *
112392561bbSGreg Roach     * @return int
113392561bbSGreg Roach     */
114392561bbSGreg Roach    public function id(): int
115392561bbSGreg Roach    {
1166b9cb339SGreg Roach        return Registry::cache()->array()->remember('place-' . $this->place_name, function (): int {
117392561bbSGreg Roach            // The "top-level" place won't exist in the database.
118392561bbSGreg Roach            if ($this->parts->isEmpty()) {
119392561bbSGreg Roach                return 0;
120392561bbSGreg Roach            }
121392561bbSGreg Roach
1228af6bbf8SGreg Roach            $parent_place_id = $this->parent()->id();
123392561bbSGreg Roach
124392561bbSGreg Roach            $place_id = (int) DB::table('places')
125392561bbSGreg Roach                ->where('p_file', '=', $this->tree->id())
126c4ab7783SGreg Roach                ->where('p_place', '=', mb_substr($this->parts->first(), 0, 120))
1278af6bbf8SGreg Roach                ->where('p_parent_id', '=', $parent_place_id)
128392561bbSGreg Roach                ->value('p_id');
129392561bbSGreg Roach
130392561bbSGreg Roach            if ($place_id === 0) {
131392561bbSGreg Roach                $place = $this->parts->first();
132392561bbSGreg Roach
133392561bbSGreg Roach                DB::table('places')->insert([
134392561bbSGreg Roach                    'p_file'        => $this->tree->id(),
135c4ab7783SGreg Roach                    'p_place'       => mb_substr($place, 0, 120),
1368af6bbf8SGreg Roach                    'p_parent_id'   => $parent_place_id,
137392561bbSGreg Roach                    'p_std_soundex' => Soundex::russell($place),
138392561bbSGreg Roach                    'p_dm_soundex'  => Soundex::daitchMokotoff($place),
139392561bbSGreg Roach                ]);
140392561bbSGreg Roach
141392561bbSGreg Roach                $place_id = (int) DB::connection()->getPdo()->lastInsertId();
142392561bbSGreg Roach            }
143392561bbSGreg Roach
144392561bbSGreg Roach            return $place_id;
145392561bbSGreg Roach        });
146392561bbSGreg Roach    }
147392561bbSGreg Roach
148392561bbSGreg Roach    /**
149dbe53437SGreg Roach     * @return Tree
150dbe53437SGreg Roach     */
151dbe53437SGreg Roach    public function tree(): Tree
152dbe53437SGreg Roach    {
153dbe53437SGreg Roach        return $this->tree;
154dbe53437SGreg Roach    }
155dbe53437SGreg Roach
156dbe53437SGreg Roach    /**
157392561bbSGreg Roach     * Extract the locality (first parts) of a place name.
158392561bbSGreg Roach     *
159392561bbSGreg Roach     * @param int $n
160392561bbSGreg Roach     *
16136779af1SGreg Roach     * @return Collection<int,string>
162392561bbSGreg Roach     */
163392561bbSGreg Roach    public function firstParts(int $n): Collection
164392561bbSGreg Roach    {
165392561bbSGreg Roach        return $this->parts->slice(0, $n);
166392561bbSGreg Roach    }
167392561bbSGreg Roach
168392561bbSGreg Roach    /**
169392561bbSGreg Roach     * Extract the country (last parts) of a place name.
170392561bbSGreg Roach     *
171392561bbSGreg Roach     * @param int $n
172392561bbSGreg Roach     *
17336779af1SGreg Roach     * @return Collection<int,string>
174392561bbSGreg Roach     */
175392561bbSGreg Roach    public function lastParts(int $n): Collection
176392561bbSGreg Roach    {
177392561bbSGreg Roach        return $this->parts->slice(-$n);
178a25f0a04SGreg Roach    }
179a25f0a04SGreg Roach
180a25f0a04SGreg Roach    /**
18176692c8bSGreg Roach     * Get the lower level places.
18276692c8bSGreg Roach     *
183d7634abbSGreg Roach     * @return array<Place>
184a25f0a04SGreg Roach     */
1858f53f488SRico Sonntag    public function getChildPlaces(): array
186c1010edaSGreg Roach    {
187392561bbSGreg Roach        if ($this->place_name !== '') {
188392561bbSGreg Roach            $parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name;
189a25f0a04SGreg Roach        } else {
190a25f0a04SGreg Roach            $parent_text = '';
191a25f0a04SGreg Roach        }
192a25f0a04SGreg Roach
193b68caec6SGreg Roach        return DB::table('places')
194b68caec6SGreg Roach            ->where('p_file', '=', $this->tree->id())
195392561bbSGreg Roach            ->where('p_parent_id', '=', $this->id())
196b68caec6SGreg Roach            ->pluck('p_place')
197761b562aSGreg Roach            ->sort(I18N::comparator())
198*f25fc0f9SGreg Roach            ->map(fn(string $place): Place => new self($place . $parent_text, $this->tree))
199b68caec6SGreg Roach            ->all();
200a25f0a04SGreg Roach    }
201a25f0a04SGreg Roach
202a25f0a04SGreg Roach    /**
20376692c8bSGreg Roach     * Create a URL to the place-hierarchy page.
20476692c8bSGreg Roach     *
205a25f0a04SGreg Roach     * @return string
206a25f0a04SGreg Roach     */
207e8777cb5SGreg Roach    public function url(): string
208c1010edaSGreg Roach    {
20967992b6aSRichard Cissee        //find a module providing the place hierarchy
210d35568b4SGreg Roach        $module = Registry::container()->get(ModuleService::class)
2110797053bSGreg Roach            ->findByComponent(ModuleListInterface::class, $this->tree, Auth::user())
212*f25fc0f9SGreg Roach            ->first(static fn(ModuleInterface $module): bool => $module instanceof PlaceHierarchyListModule);
21367992b6aSRichard Cissee
21467992b6aSRichard Cissee        if ($module instanceof PlaceHierarchyListModule) {
21567992b6aSRichard Cissee            return $module->listUrl($this->tree, [
2167bed239cSGreg Roach                'place_id' => $this->id(),
2179022ab66SGreg Roach                'tree'     => $this->tree->name(),
218e8777cb5SGreg Roach            ]);
219e364afe4SGreg Roach        }
220e364afe4SGreg Roach
221f7721877SGreg Roach        // The place-list module is disabled...
222f7721877SGreg Roach        return '#';
22367992b6aSRichard Cissee    }
224a25f0a04SGreg Roach
225a25f0a04SGreg Roach    /**
226392561bbSGreg Roach     * Format this place for GEDCOM data.
22776692c8bSGreg Roach     *
228a25f0a04SGreg Roach     * @return string
229a25f0a04SGreg Roach     */
230392561bbSGreg Roach    public function gedcomName(): string
231c1010edaSGreg Roach    {
232392561bbSGreg Roach        return $this->place_name;
233a25f0a04SGreg Roach    }
234a25f0a04SGreg Roach
235a25f0a04SGreg Roach    /**
23676692c8bSGreg Roach     * Format this place for display on screen.
23776692c8bSGreg Roach     *
238a25f0a04SGreg Roach     * @return string
239a25f0a04SGreg Roach     */
240392561bbSGreg Roach    public function placeName(): string
241c1010edaSGreg Roach    {
242392561bbSGreg Roach        $place_name = $this->parts->first() ?? I18N::translate('unknown');
243a25f0a04SGreg Roach
244315eb316SGreg Roach        return '<bdi>' . e($place_name) . '</bdi>';
245a25f0a04SGreg Roach    }
246a25f0a04SGreg Roach
247a25f0a04SGreg Roach    /**
24876692c8bSGreg Roach     * Generate the place name for display, including the full hierarchy.
24976692c8bSGreg Roach     *
250392561bbSGreg Roach     * @param bool $link
251392561bbSGreg Roach     *
252a25f0a04SGreg Roach     * @return string
253a25f0a04SGreg Roach     */
254e364afe4SGreg Roach    public function fullName(bool $link = false): string
255c1010edaSGreg Roach    {
256392561bbSGreg Roach        if ($this->parts->isEmpty()) {
257392561bbSGreg Roach            return '';
258b2ce94c6SRico Sonntag        }
259b2ce94c6SRico Sonntag
260392561bbSGreg Roach        $full_name = $this->parts->implode(I18N::$list_separator);
261392561bbSGreg Roach
262392561bbSGreg Roach        if ($link) {
263392561bbSGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>';
264a25f0a04SGreg Roach        }
265a25f0a04SGreg Roach
266315eb316SGreg Roach        return '<bdi>' . e($full_name) . '</bdi>';
267a25f0a04SGreg Roach    }
268a25f0a04SGreg Roach
269a25f0a04SGreg Roach    /**
270a25f0a04SGreg Roach     * For lists and charts, where the full name won’t fit.
271a25f0a04SGreg Roach     *
272392561bbSGreg Roach     * @param bool $link
273a25f0a04SGreg Roach     *
274a25f0a04SGreg Roach     * @return string
275a25f0a04SGreg Roach     */
276e364afe4SGreg Roach    public function shortName(bool $link = false): string
277c1010edaSGreg Roach    {
278392561bbSGreg Roach        $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES');
279392561bbSGreg Roach
280392561bbSGreg Roach        // Abbreviate the place name, for lists
281b6ec1ccfSGreg Roach        if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX') === '1') {
282392561bbSGreg Roach            $parts = $this->lastParts($SHOW_PEDIGREE_PLACES);
283392561bbSGreg Roach        } else {
284392561bbSGreg Roach            $parts = $this->firstParts($SHOW_PEDIGREE_PLACES);
285a25f0a04SGreg Roach        }
286a25f0a04SGreg Roach
287392561bbSGreg Roach        $short_name = $parts->implode(I18N::$list_separator);
288392561bbSGreg Roach
289392561bbSGreg Roach        // Add a tool-tip showing the full name
290392561bbSGreg Roach        $title = strip_tags($this->fullName());
291392561bbSGreg Roach
292392561bbSGreg Roach        if ($link) {
29340f99dd2SGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '">' . e($short_name) . '</a>';
294392561bbSGreg Roach        }
295392561bbSGreg Roach
296315eb316SGreg Roach        return '<bdi>' . e($short_name) . '</bdi>';
297a25f0a04SGreg Roach    }
298a25f0a04SGreg Roach}
299