xref: /webtrees/app/Place.php (revision 761b562a674ebf0f8fc8bbb216a115a9ba0cbc62)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
26b68caec6SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27392561bbSGreg Roachuse Illuminate\Support\Collection;
28b68caec6SGreg Roach
29f70bcff5SGreg Roachuse function app;
30f70bcff5SGreg Roachuse function e;
31f70bcff5SGreg Roachuse function is_object;
32f70bcff5SGreg Roachuse function preg_split;
33f70bcff5SGreg Roachuse function strip_tags;
3490949315SGreg Roachuse function trim;
3590949315SGreg Roach
3690949315SGreg Roachuse const PREG_SPLIT_NO_EMPTY;
3790949315SGreg Roach
38a25f0a04SGreg Roach/**
3976692c8bSGreg Roach * A GEDCOM place (PLAC) object.
40a25f0a04SGreg Roach */
41c1010edaSGreg Roachclass Place
42c1010edaSGreg Roach{
4343f2f523SGreg Roach    // "Westminster, London, England"
4443f2f523SGreg Roach    private string $place_name;
45392561bbSGreg Roach
4636779af1SGreg Roach    /** @var Collection<int,string> The parts of a place name, e.g. ["Westminster", "London", "England"] */
4743f2f523SGreg Roach    private Collection $parts;
4884caa210SGreg Roach
4943f2f523SGreg Roach    private Tree $tree;
50a25f0a04SGreg Roach
51a25f0a04SGreg Roach    /**
5276692c8bSGreg Roach     * Create a place.
5376692c8bSGreg Roach     *
54392561bbSGreg Roach     * @param string $place_name
5584caa210SGreg Roach     * @param Tree   $tree
56a25f0a04SGreg Roach     */
57392561bbSGreg Roach    public function __construct(string $place_name, Tree $tree)
58c1010edaSGreg Roach    {
59392561bbSGreg Roach        // Ignore any empty parts in place names such as "Village, , , Country".
6090949315SGreg Roach        $place_name  = trim($place_name);
6190949315SGreg Roach        $this->parts = new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name, -1, PREG_SPLIT_NO_EMPTY));
62392561bbSGreg Roach
63392561bbSGreg Roach        // Rebuild the placename in the correct format.
64392561bbSGreg Roach        $this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
65392561bbSGreg Roach
6664b16745SGreg Roach        $this->tree = $tree;
67a25f0a04SGreg Roach    }
68a25f0a04SGreg Roach
69a25f0a04SGreg Roach    /**
707bed239cSGreg Roach     * Find a place by its ID.
717bed239cSGreg Roach     *
727bed239cSGreg Roach     * @param int  $id
737bed239cSGreg Roach     * @param Tree $tree
747bed239cSGreg Roach     *
757bed239cSGreg Roach     * @return Place
767bed239cSGreg Roach     */
777bed239cSGreg Roach    public static function find(int $id, Tree $tree): Place
787bed239cSGreg Roach    {
797bed239cSGreg Roach        $parts = new Collection();
807bed239cSGreg Roach
817bed239cSGreg Roach        while ($id !== 0) {
827bed239cSGreg Roach            $row = DB::table('places')
837bed239cSGreg Roach                ->where('p_file', '=', $tree->id())
847bed239cSGreg Roach                ->where('p_id', '=', $id)
857bed239cSGreg Roach                ->first();
867bed239cSGreg Roach
87f70bcff5SGreg Roach            if (is_object($row)) {
887bed239cSGreg Roach                $id = (int) $row->p_parent_id;
897bed239cSGreg Roach                $parts->add($row->p_place);
907bed239cSGreg Roach            } else {
917bed239cSGreg Roach                $id = 0;
927bed239cSGreg Roach            }
937bed239cSGreg Roach        }
947bed239cSGreg Roach
957bed239cSGreg Roach        $place_name = $parts->implode(Gedcom::PLACE_SEPARATOR);
967bed239cSGreg Roach
977bed239cSGreg Roach        return new Place($place_name, $tree);
987bed239cSGreg Roach    }
997bed239cSGreg Roach
1007bed239cSGreg Roach    /**
10176692c8bSGreg Roach     * Get the higher level place.
10276692c8bSGreg Roach     *
103a25f0a04SGreg Roach     * @return Place
104a25f0a04SGreg Roach     */
105392561bbSGreg Roach    public function parent(): Place
106c1010edaSGreg Roach    {
1078af6bbf8SGreg Roach        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree);
108392561bbSGreg Roach    }
109392561bbSGreg Roach
110392561bbSGreg Roach    /**
111392561bbSGreg Roach     * The database row that contains this place.
112392561bbSGreg Roach     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
113392561bbSGreg Roach     *
114392561bbSGreg Roach     * @return int
115392561bbSGreg Roach     */
116392561bbSGreg Roach    public function id(): int
117392561bbSGreg Roach    {
1186b9cb339SGreg Roach        return Registry::cache()->array()->remember('place-' . $this->place_name, function (): int {
119392561bbSGreg Roach            // The "top-level" place won't exist in the database.
120392561bbSGreg Roach            if ($this->parts->isEmpty()) {
121392561bbSGreg Roach                return 0;
122392561bbSGreg Roach            }
123392561bbSGreg Roach
1248af6bbf8SGreg Roach            $parent_place_id = $this->parent()->id();
125392561bbSGreg Roach
126392561bbSGreg Roach            $place_id = (int) DB::table('places')
127392561bbSGreg Roach                ->where('p_file', '=', $this->tree->id())
128c4ab7783SGreg Roach                ->where('p_place', '=', mb_substr($this->parts->first(), 0, 120))
1298af6bbf8SGreg Roach                ->where('p_parent_id', '=', $parent_place_id)
130392561bbSGreg Roach                ->value('p_id');
131392561bbSGreg Roach
132392561bbSGreg Roach            if ($place_id === 0) {
133392561bbSGreg Roach                $place = $this->parts->first();
134392561bbSGreg Roach
135392561bbSGreg Roach                DB::table('places')->insert([
136392561bbSGreg Roach                    'p_file'        => $this->tree->id(),
137c4ab7783SGreg Roach                    'p_place'       => mb_substr($place, 0, 120),
1388af6bbf8SGreg Roach                    'p_parent_id'   => $parent_place_id,
139392561bbSGreg Roach                    'p_std_soundex' => Soundex::russell($place),
140392561bbSGreg Roach                    'p_dm_soundex'  => Soundex::daitchMokotoff($place),
141392561bbSGreg Roach                ]);
142392561bbSGreg Roach
143392561bbSGreg Roach                $place_id = (int) DB::connection()->getPdo()->lastInsertId();
144392561bbSGreg Roach            }
145392561bbSGreg Roach
146392561bbSGreg Roach            return $place_id;
147392561bbSGreg Roach        });
148392561bbSGreg Roach    }
149392561bbSGreg Roach
150392561bbSGreg Roach    /**
151dbe53437SGreg Roach     * @return Tree
152dbe53437SGreg Roach     */
153dbe53437SGreg Roach    public function tree(): Tree
154dbe53437SGreg Roach    {
155dbe53437SGreg Roach        return $this->tree;
156dbe53437SGreg Roach    }
157dbe53437SGreg Roach
158dbe53437SGreg Roach    /**
159392561bbSGreg Roach     * Extract the locality (first parts) of a place name.
160392561bbSGreg Roach     *
161392561bbSGreg Roach     * @param int $n
162392561bbSGreg Roach     *
16336779af1SGreg Roach     * @return Collection<int,string>
164392561bbSGreg Roach     */
165392561bbSGreg Roach    public function firstParts(int $n): Collection
166392561bbSGreg Roach    {
167392561bbSGreg Roach        return $this->parts->slice(0, $n);
168392561bbSGreg Roach    }
169392561bbSGreg Roach
170392561bbSGreg Roach    /**
171392561bbSGreg Roach     * Extract the country (last parts) of a place name.
172392561bbSGreg Roach     *
173392561bbSGreg Roach     * @param int $n
174392561bbSGreg Roach     *
17536779af1SGreg Roach     * @return Collection<int,string>
176392561bbSGreg Roach     */
177392561bbSGreg Roach    public function lastParts(int $n): Collection
178392561bbSGreg Roach    {
179392561bbSGreg Roach        return $this->parts->slice(-$n);
180a25f0a04SGreg Roach    }
181a25f0a04SGreg Roach
182a25f0a04SGreg Roach    /**
18376692c8bSGreg Roach     * Get the lower level places.
18476692c8bSGreg Roach     *
185d7634abbSGreg Roach     * @return array<Place>
186a25f0a04SGreg Roach     */
1878f53f488SRico Sonntag    public function getChildPlaces(): array
188c1010edaSGreg Roach    {
189392561bbSGreg Roach        if ($this->place_name !== '') {
190392561bbSGreg Roach            $parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name;
191a25f0a04SGreg Roach        } else {
192a25f0a04SGreg Roach            $parent_text = '';
193a25f0a04SGreg Roach        }
194a25f0a04SGreg Roach
195b68caec6SGreg Roach        return DB::table('places')
196b68caec6SGreg Roach            ->where('p_file', '=', $this->tree->id())
197392561bbSGreg Roach            ->where('p_parent_id', '=', $this->id())
198b68caec6SGreg Roach            ->pluck('p_place')
199*761b562aSGreg Roach            ->sort(I18N::comparator())
200b68caec6SGreg Roach            ->map(function (string $place) use ($parent_text): Place {
201b68caec6SGreg Roach                return new self($place . $parent_text, $this->tree);
202b68caec6SGreg Roach            })
203b68caec6SGreg Roach            ->all();
204a25f0a04SGreg Roach    }
205a25f0a04SGreg Roach
206a25f0a04SGreg Roach    /**
20776692c8bSGreg Roach     * Create a URL to the place-hierarchy page.
20876692c8bSGreg Roach     *
209a25f0a04SGreg Roach     * @return string
210a25f0a04SGreg Roach     */
211e8777cb5SGreg Roach    public function url(): string
212c1010edaSGreg Roach    {
21367992b6aSRichard Cissee        //find a module providing the place hierarchy
2140797053bSGreg Roach        $module = app(ModuleService::class)
2150797053bSGreg Roach            ->findByComponent(ModuleListInterface::class, $this->tree, Auth::user())
2160797053bSGreg Roach            ->first(static function (ModuleInterface $module): bool {
21767992b6aSRichard Cissee                return $module instanceof PlaceHierarchyListModule;
21867992b6aSRichard Cissee            });
21967992b6aSRichard Cissee
22067992b6aSRichard Cissee        if ($module instanceof PlaceHierarchyListModule) {
22167992b6aSRichard Cissee            return $module->listUrl($this->tree, [
2227bed239cSGreg Roach                'place_id' => $this->id(),
2239022ab66SGreg Roach                'tree'     => $this->tree->name(),
224e8777cb5SGreg Roach            ]);
225e364afe4SGreg Roach        }
226e364afe4SGreg Roach
227f7721877SGreg Roach        // The place-list module is disabled...
228f7721877SGreg Roach        return '#';
22967992b6aSRichard Cissee    }
230a25f0a04SGreg Roach
231a25f0a04SGreg Roach    /**
232392561bbSGreg Roach     * Format this place for GEDCOM data.
23376692c8bSGreg Roach     *
234a25f0a04SGreg Roach     * @return string
235a25f0a04SGreg Roach     */
236392561bbSGreg Roach    public function gedcomName(): string
237c1010edaSGreg Roach    {
238392561bbSGreg Roach        return $this->place_name;
239a25f0a04SGreg Roach    }
240a25f0a04SGreg Roach
241a25f0a04SGreg Roach    /**
24276692c8bSGreg Roach     * Format this place for display on screen.
24376692c8bSGreg Roach     *
244a25f0a04SGreg Roach     * @return string
245a25f0a04SGreg Roach     */
246392561bbSGreg Roach    public function placeName(): string
247c1010edaSGreg Roach    {
248392561bbSGreg Roach        $place_name = $this->parts->first() ?? I18N::translate('unknown');
249a25f0a04SGreg Roach
250315eb316SGreg Roach        return '<bdi>' . e($place_name) . '</bdi>';
251a25f0a04SGreg Roach    }
252a25f0a04SGreg Roach
253a25f0a04SGreg Roach    /**
25476692c8bSGreg Roach     * Generate the place name for display, including the full hierarchy.
25576692c8bSGreg Roach     *
256392561bbSGreg Roach     * @param bool $link
257392561bbSGreg Roach     *
258a25f0a04SGreg Roach     * @return string
259a25f0a04SGreg Roach     */
260e364afe4SGreg Roach    public function fullName(bool $link = false): string
261c1010edaSGreg Roach    {
262392561bbSGreg Roach        if ($this->parts->isEmpty()) {
263392561bbSGreg Roach            return '';
264b2ce94c6SRico Sonntag        }
265b2ce94c6SRico Sonntag
266392561bbSGreg Roach        $full_name = $this->parts->implode(I18N::$list_separator);
267392561bbSGreg Roach
268392561bbSGreg Roach        if ($link) {
269392561bbSGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>';
270a25f0a04SGreg Roach        }
271a25f0a04SGreg Roach
272315eb316SGreg Roach        return '<bdi>' . e($full_name) . '</bdi>';
273a25f0a04SGreg Roach    }
274a25f0a04SGreg Roach
275a25f0a04SGreg Roach    /**
276a25f0a04SGreg Roach     * For lists and charts, where the full name won’t fit.
277a25f0a04SGreg Roach     *
278392561bbSGreg Roach     * @param bool $link
279a25f0a04SGreg Roach     *
280a25f0a04SGreg Roach     * @return string
281a25f0a04SGreg Roach     */
282e364afe4SGreg Roach    public function shortName(bool $link = false): string
283c1010edaSGreg Roach    {
284392561bbSGreg Roach        $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES');
285392561bbSGreg Roach
286392561bbSGreg Roach        // Abbreviate the place name, for lists
287392561bbSGreg Roach        if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) {
288392561bbSGreg Roach            $parts = $this->lastParts($SHOW_PEDIGREE_PLACES);
289392561bbSGreg Roach        } else {
290392561bbSGreg Roach            $parts = $this->firstParts($SHOW_PEDIGREE_PLACES);
291a25f0a04SGreg Roach        }
292a25f0a04SGreg Roach
293392561bbSGreg Roach        $short_name = $parts->implode(I18N::$list_separator);
294392561bbSGreg Roach
295392561bbSGreg Roach        // Add a tool-tip showing the full name
296392561bbSGreg Roach        $title = strip_tags($this->fullName());
297392561bbSGreg Roach
298392561bbSGreg Roach        if ($link) {
29940f99dd2SGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '">' . e($short_name) . '</a>';
300392561bbSGreg Roach        }
301392561bbSGreg Roach
302315eb316SGreg Roach        return '<bdi>' . e($short_name) . '</bdi>';
303a25f0a04SGreg Roach    }
304a25f0a04SGreg Roach}
305