xref: /webtrees/app/Place.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
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;
26b68caec6SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27392561bbSGreg Roachuse Illuminate\Support\Collection;
28b68caec6SGreg Roach
29f70bcff5SGreg Roachuse function e;
30f70bcff5SGreg Roachuse function is_object;
31f70bcff5SGreg Roachuse function preg_split;
32f70bcff5SGreg Roachuse function strip_tags;
3390949315SGreg Roachuse function trim;
3490949315SGreg Roach
3590949315SGreg Roachuse const PREG_SPLIT_NO_EMPTY;
3690949315SGreg Roach
37a25f0a04SGreg Roach/**
3876692c8bSGreg Roach * A GEDCOM place (PLAC) object.
39a25f0a04SGreg Roach */
40c1010edaSGreg Roachclass Place
41c1010edaSGreg Roach{
4243f2f523SGreg Roach    // "Westminster, London, England"
4343f2f523SGreg Roach    private string $place_name;
44392561bbSGreg Roach
4536779af1SGreg Roach    /** @var Collection<int,string> The parts of a place name, e.g. ["Westminster", "London", "England"] */
4643f2f523SGreg Roach    private Collection $parts;
4784caa210SGreg Roach
4843f2f523SGreg Roach    private Tree $tree;
49a25f0a04SGreg Roach
50a25f0a04SGreg Roach    /**
5176692c8bSGreg Roach     * Create a place.
5276692c8bSGreg Roach     *
53392561bbSGreg Roach     * @param string $place_name
5484caa210SGreg Roach     * @param Tree   $tree
55a25f0a04SGreg Roach     */
56392561bbSGreg Roach    public function __construct(string $place_name, Tree $tree)
57c1010edaSGreg Roach    {
58392561bbSGreg Roach        // Ignore any empty parts in place names such as "Village, , , Country".
5990949315SGreg Roach        $place_name  = trim($place_name);
6090949315SGreg Roach        $this->parts = new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name, -1, PREG_SPLIT_NO_EMPTY));
61392561bbSGreg Roach
62392561bbSGreg Roach        // Rebuild the placename in the correct format.
63392561bbSGreg Roach        $this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
64392561bbSGreg Roach
6564b16745SGreg Roach        $this->tree = $tree;
66a25f0a04SGreg Roach    }
67a25f0a04SGreg Roach
68a25f0a04SGreg Roach    /**
697bed239cSGreg Roach     * Find a place by its ID.
707bed239cSGreg Roach     *
717bed239cSGreg Roach     * @param int  $id
727bed239cSGreg Roach     * @param Tree $tree
737bed239cSGreg Roach     *
747bed239cSGreg Roach     * @return Place
757bed239cSGreg Roach     */
767bed239cSGreg Roach    public static function find(int $id, Tree $tree): Place
777bed239cSGreg Roach    {
787bed239cSGreg Roach        $parts = new Collection();
797bed239cSGreg Roach
807bed239cSGreg Roach        while ($id !== 0) {
817bed239cSGreg Roach            $row = DB::table('places')
827bed239cSGreg Roach                ->where('p_file', '=', $tree->id())
837bed239cSGreg Roach                ->where('p_id', '=', $id)
847bed239cSGreg Roach                ->first();
857bed239cSGreg Roach
86f70bcff5SGreg Roach            if (is_object($row)) {
877bed239cSGreg Roach                $id = (int) $row->p_parent_id;
887bed239cSGreg Roach                $parts->add($row->p_place);
897bed239cSGreg Roach            } else {
907bed239cSGreg Roach                $id = 0;
917bed239cSGreg Roach            }
927bed239cSGreg Roach        }
937bed239cSGreg Roach
947bed239cSGreg Roach        $place_name = $parts->implode(Gedcom::PLACE_SEPARATOR);
957bed239cSGreg Roach
967bed239cSGreg Roach        return new Place($place_name, $tree);
977bed239cSGreg Roach    }
987bed239cSGreg Roach
997bed239cSGreg Roach    /**
10076692c8bSGreg Roach     * Get the higher level place.
10176692c8bSGreg Roach     *
102a25f0a04SGreg Roach     * @return Place
103a25f0a04SGreg Roach     */
104392561bbSGreg Roach    public function parent(): Place
105c1010edaSGreg Roach    {
1068af6bbf8SGreg Roach        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree);
107392561bbSGreg Roach    }
108392561bbSGreg Roach
109392561bbSGreg Roach    /**
110392561bbSGreg Roach     * The database row that contains this place.
111392561bbSGreg Roach     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
112392561bbSGreg Roach     *
113392561bbSGreg Roach     * @return int
114392561bbSGreg Roach     */
115392561bbSGreg Roach    public function id(): int
116392561bbSGreg Roach    {
1176b9cb339SGreg Roach        return Registry::cache()->array()->remember('place-' . $this->place_name, function (): int {
118392561bbSGreg Roach            // The "top-level" place won't exist in the database.
119392561bbSGreg Roach            if ($this->parts->isEmpty()) {
120392561bbSGreg Roach                return 0;
121392561bbSGreg Roach            }
122392561bbSGreg Roach
1238af6bbf8SGreg Roach            $parent_place_id = $this->parent()->id();
124392561bbSGreg Roach
125392561bbSGreg Roach            $place_id = (int) DB::table('places')
126392561bbSGreg Roach                ->where('p_file', '=', $this->tree->id())
127c4ab7783SGreg Roach                ->where('p_place', '=', mb_substr($this->parts->first(), 0, 120))
1288af6bbf8SGreg Roach                ->where('p_parent_id', '=', $parent_place_id)
129392561bbSGreg Roach                ->value('p_id');
130392561bbSGreg Roach
131392561bbSGreg Roach            if ($place_id === 0) {
132392561bbSGreg Roach                $place = $this->parts->first();
133392561bbSGreg Roach
134392561bbSGreg Roach                DB::table('places')->insert([
135392561bbSGreg Roach                    'p_file'        => $this->tree->id(),
136c4ab7783SGreg Roach                    'p_place'       => mb_substr($place, 0, 120),
1378af6bbf8SGreg Roach                    'p_parent_id'   => $parent_place_id,
138392561bbSGreg Roach                    'p_std_soundex' => Soundex::russell($place),
139392561bbSGreg Roach                    'p_dm_soundex'  => Soundex::daitchMokotoff($place),
140392561bbSGreg Roach                ]);
141392561bbSGreg Roach
142392561bbSGreg Roach                $place_id = (int) DB::connection()->getPdo()->lastInsertId();
143392561bbSGreg Roach            }
144392561bbSGreg Roach
145392561bbSGreg Roach            return $place_id;
146392561bbSGreg Roach        });
147392561bbSGreg Roach    }
148392561bbSGreg Roach
149392561bbSGreg Roach    /**
150dbe53437SGreg Roach     * @return Tree
151dbe53437SGreg Roach     */
152dbe53437SGreg Roach    public function tree(): Tree
153dbe53437SGreg Roach    {
154dbe53437SGreg Roach        return $this->tree;
155dbe53437SGreg Roach    }
156dbe53437SGreg Roach
157dbe53437SGreg Roach    /**
158392561bbSGreg Roach     * Extract the locality (first parts) of a place name.
159392561bbSGreg Roach     *
160392561bbSGreg Roach     * @param int $n
161392561bbSGreg Roach     *
16236779af1SGreg Roach     * @return Collection<int,string>
163392561bbSGreg Roach     */
164392561bbSGreg Roach    public function firstParts(int $n): Collection
165392561bbSGreg Roach    {
166392561bbSGreg Roach        return $this->parts->slice(0, $n);
167392561bbSGreg Roach    }
168392561bbSGreg Roach
169392561bbSGreg Roach    /**
170392561bbSGreg Roach     * Extract the country (last parts) of a place name.
171392561bbSGreg Roach     *
172392561bbSGreg Roach     * @param int $n
173392561bbSGreg Roach     *
17436779af1SGreg Roach     * @return Collection<int,string>
175392561bbSGreg Roach     */
176392561bbSGreg Roach    public function lastParts(int $n): Collection
177392561bbSGreg Roach    {
178392561bbSGreg Roach        return $this->parts->slice(-$n);
179a25f0a04SGreg Roach    }
180a25f0a04SGreg Roach
181a25f0a04SGreg Roach    /**
18276692c8bSGreg Roach     * Get the lower level places.
18376692c8bSGreg Roach     *
184d7634abbSGreg Roach     * @return array<Place>
185a25f0a04SGreg Roach     */
1868f53f488SRico Sonntag    public function getChildPlaces(): array
187c1010edaSGreg Roach    {
188392561bbSGreg Roach        if ($this->place_name !== '') {
189392561bbSGreg Roach            $parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name;
190a25f0a04SGreg Roach        } else {
191a25f0a04SGreg Roach            $parent_text = '';
192a25f0a04SGreg Roach        }
193a25f0a04SGreg Roach
194b68caec6SGreg Roach        return DB::table('places')
195b68caec6SGreg Roach            ->where('p_file', '=', $this->tree->id())
196392561bbSGreg Roach            ->where('p_parent_id', '=', $this->id())
197b68caec6SGreg Roach            ->pluck('p_place')
198761b562aSGreg Roach            ->sort(I18N::comparator())
199b68caec6SGreg Roach            ->map(function (string $place) use ($parent_text): Place {
200b68caec6SGreg Roach                return new self($place . $parent_text, $this->tree);
201b68caec6SGreg Roach            })
202b68caec6SGreg Roach            ->all();
203a25f0a04SGreg Roach    }
204a25f0a04SGreg Roach
205a25f0a04SGreg Roach    /**
20676692c8bSGreg Roach     * Create a URL to the place-hierarchy page.
20776692c8bSGreg Roach     *
208a25f0a04SGreg Roach     * @return string
209a25f0a04SGreg Roach     */
210e8777cb5SGreg Roach    public function url(): string
211c1010edaSGreg Roach    {
21267992b6aSRichard Cissee        //find a module providing the place hierarchy
213*d35568b4SGreg Roach        $module = Registry::container()->get(ModuleService::class)
2140797053bSGreg Roach            ->findByComponent(ModuleListInterface::class, $this->tree, Auth::user())
2150797053bSGreg Roach            ->first(static function (ModuleInterface $module): bool {
21667992b6aSRichard Cissee                return $module instanceof PlaceHierarchyListModule;
21767992b6aSRichard Cissee            });
21867992b6aSRichard Cissee
21967992b6aSRichard Cissee        if ($module instanceof PlaceHierarchyListModule) {
22067992b6aSRichard Cissee            return $module->listUrl($this->tree, [
2217bed239cSGreg Roach                'place_id' => $this->id(),
2229022ab66SGreg Roach                'tree'     => $this->tree->name(),
223e8777cb5SGreg Roach            ]);
224e364afe4SGreg Roach        }
225e364afe4SGreg Roach
226f7721877SGreg Roach        // The place-list module is disabled...
227f7721877SGreg Roach        return '#';
22867992b6aSRichard Cissee    }
229a25f0a04SGreg Roach
230a25f0a04SGreg Roach    /**
231392561bbSGreg Roach     * Format this place for GEDCOM data.
23276692c8bSGreg Roach     *
233a25f0a04SGreg Roach     * @return string
234a25f0a04SGreg Roach     */
235392561bbSGreg Roach    public function gedcomName(): string
236c1010edaSGreg Roach    {
237392561bbSGreg Roach        return $this->place_name;
238a25f0a04SGreg Roach    }
239a25f0a04SGreg Roach
240a25f0a04SGreg Roach    /**
24176692c8bSGreg Roach     * Format this place for display on screen.
24276692c8bSGreg Roach     *
243a25f0a04SGreg Roach     * @return string
244a25f0a04SGreg Roach     */
245392561bbSGreg Roach    public function placeName(): string
246c1010edaSGreg Roach    {
247392561bbSGreg Roach        $place_name = $this->parts->first() ?? I18N::translate('unknown');
248a25f0a04SGreg Roach
249315eb316SGreg Roach        return '<bdi>' . e($place_name) . '</bdi>';
250a25f0a04SGreg Roach    }
251a25f0a04SGreg Roach
252a25f0a04SGreg Roach    /**
25376692c8bSGreg Roach     * Generate the place name for display, including the full hierarchy.
25476692c8bSGreg Roach     *
255392561bbSGreg Roach     * @param bool $link
256392561bbSGreg Roach     *
257a25f0a04SGreg Roach     * @return string
258a25f0a04SGreg Roach     */
259e364afe4SGreg Roach    public function fullName(bool $link = false): string
260c1010edaSGreg Roach    {
261392561bbSGreg Roach        if ($this->parts->isEmpty()) {
262392561bbSGreg Roach            return '';
263b2ce94c6SRico Sonntag        }
264b2ce94c6SRico Sonntag
265392561bbSGreg Roach        $full_name = $this->parts->implode(I18N::$list_separator);
266392561bbSGreg Roach
267392561bbSGreg Roach        if ($link) {
268392561bbSGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>';
269a25f0a04SGreg Roach        }
270a25f0a04SGreg Roach
271315eb316SGreg Roach        return '<bdi>' . e($full_name) . '</bdi>';
272a25f0a04SGreg Roach    }
273a25f0a04SGreg Roach
274a25f0a04SGreg Roach    /**
275a25f0a04SGreg Roach     * For lists and charts, where the full name won’t fit.
276a25f0a04SGreg Roach     *
277392561bbSGreg Roach     * @param bool $link
278a25f0a04SGreg Roach     *
279a25f0a04SGreg Roach     * @return string
280a25f0a04SGreg Roach     */
281e364afe4SGreg Roach    public function shortName(bool $link = false): string
282c1010edaSGreg Roach    {
283392561bbSGreg Roach        $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES');
284392561bbSGreg Roach
285392561bbSGreg Roach        // Abbreviate the place name, for lists
286392561bbSGreg Roach        if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) {
287392561bbSGreg Roach            $parts = $this->lastParts($SHOW_PEDIGREE_PLACES);
288392561bbSGreg Roach        } else {
289392561bbSGreg Roach            $parts = $this->firstParts($SHOW_PEDIGREE_PLACES);
290a25f0a04SGreg Roach        }
291a25f0a04SGreg Roach
292392561bbSGreg Roach        $short_name = $parts->implode(I18N::$list_separator);
293392561bbSGreg Roach
294392561bbSGreg Roach        // Add a tool-tip showing the full name
295392561bbSGreg Roach        $title = strip_tags($this->fullName());
296392561bbSGreg Roach
297392561bbSGreg Roach        if ($link) {
29840f99dd2SGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '">' . e($short_name) . '</a>';
299392561bbSGreg Roach        }
300392561bbSGreg Roach
301315eb316SGreg Roach        return '<bdi>' . e($short_name) . '</bdi>';
302a25f0a04SGreg Roach    }
303a25f0a04SGreg Roach}
304