xref: /webtrees/app/Place.php (revision 315eb31683006273e24c08b447e6e1095d6f2147)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
590949315SGreg Roach * Copyright (C) 2021 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;
27a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
28392561bbSGreg Roachuse Illuminate\Support\Collection;
297bed239cSGreg Roachuse stdClass;
30b68caec6SGreg Roach
3190949315SGreg Roachuse function trim;
3290949315SGreg Roach
3390949315SGreg Roachuse const PREG_SPLIT_NO_EMPTY;
3490949315SGreg Roach
35a25f0a04SGreg Roach/**
3676692c8bSGreg Roach * A GEDCOM place (PLAC) object.
37a25f0a04SGreg Roach */
38c1010edaSGreg Roachclass Place
39c1010edaSGreg Roach{
40392561bbSGreg Roach    /** @var string e.g. "Westminster, London, England" */
41392561bbSGreg Roach    private $place_name;
42392561bbSGreg Roach
43b5c8fd7eSGreg Roach    /** @var Collection<string> The parts of a place name, e.g. ["Westminster", "London", "England"] */
44392561bbSGreg Roach    private $parts;
4584caa210SGreg Roach
4676692c8bSGreg Roach    /** @var Tree We may have the same place name in different trees. */
4784caa210SGreg Roach    private $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
857bed239cSGreg Roach            if ($row instanceof stdClass) {
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     *
161b5c8fd7eSGreg Roach     * @return Collection<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     *
173b5c8fd7eSGreg Roach     * @return Collection<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())
196a69f5655SGreg Roach            ->orderBy(new Expression('p_place /*! COLLATE ' . I18N::collation() . ' */'))
197b68caec6SGreg Roach            ->pluck('p_place')
198b68caec6SGreg Roach            ->map(function (string $place) use ($parent_text): Place {
199b68caec6SGreg Roach                return new self($place . $parent_text, $this->tree);
200b68caec6SGreg Roach            })
201b68caec6SGreg Roach            ->all();
202a25f0a04SGreg Roach    }
203a25f0a04SGreg Roach
204a25f0a04SGreg Roach    /**
20576692c8bSGreg Roach     * Create a URL to the place-hierarchy page.
20676692c8bSGreg Roach     *
207a25f0a04SGreg Roach     * @return string
208a25f0a04SGreg Roach     */
209e8777cb5SGreg Roach    public function url(): string
210c1010edaSGreg Roach    {
21167992b6aSRichard Cissee        //find a module providing the place hierarchy
2120797053bSGreg Roach        $module = app(ModuleService::class)
2130797053bSGreg Roach            ->findByComponent(ModuleListInterface::class, $this->tree, Auth::user())
2140797053bSGreg Roach            ->first(static function (ModuleInterface $module): bool {
21567992b6aSRichard Cissee                return $module instanceof PlaceHierarchyListModule;
21667992b6aSRichard Cissee            });
21767992b6aSRichard Cissee
21867992b6aSRichard Cissee        if ($module instanceof PlaceHierarchyListModule) {
21967992b6aSRichard Cissee            return $module->listUrl($this->tree, [
2207bed239cSGreg Roach                'place_id' => $this->id(),
2219022ab66SGreg Roach                'tree'     => $this->tree->name(),
222e8777cb5SGreg Roach            ]);
223e364afe4SGreg Roach        }
224e364afe4SGreg Roach
225f7721877SGreg Roach        // The place-list module is disabled...
226f7721877SGreg Roach        return '#';
22767992b6aSRichard Cissee    }
228a25f0a04SGreg Roach
229a25f0a04SGreg Roach    /**
230392561bbSGreg Roach     * Format this place for GEDCOM data.
23176692c8bSGreg Roach     *
232a25f0a04SGreg Roach     * @return string
233a25f0a04SGreg Roach     */
234392561bbSGreg Roach    public function gedcomName(): string
235c1010edaSGreg Roach    {
236392561bbSGreg Roach        return $this->place_name;
237a25f0a04SGreg Roach    }
238a25f0a04SGreg Roach
239a25f0a04SGreg Roach    /**
24076692c8bSGreg Roach     * Format this place for display on screen.
24176692c8bSGreg Roach     *
242a25f0a04SGreg Roach     * @return string
243a25f0a04SGreg Roach     */
244392561bbSGreg Roach    public function placeName(): string
245c1010edaSGreg Roach    {
246392561bbSGreg Roach        $place_name = $this->parts->first() ?? I18N::translate('unknown');
247a25f0a04SGreg Roach
248*315eb316SGreg Roach        return '<bdi>' . e($place_name) . '</bdi>';
249a25f0a04SGreg Roach    }
250a25f0a04SGreg Roach
251a25f0a04SGreg Roach    /**
25276692c8bSGreg Roach     * Generate the place name for display, including the full hierarchy.
25376692c8bSGreg Roach     *
254392561bbSGreg Roach     * @param bool $link
255392561bbSGreg Roach     *
256a25f0a04SGreg Roach     * @return string
257a25f0a04SGreg Roach     */
258e364afe4SGreg Roach    public function fullName(bool $link = false): string
259c1010edaSGreg Roach    {
260392561bbSGreg Roach        if ($this->parts->isEmpty()) {
261392561bbSGreg Roach            return '';
262b2ce94c6SRico Sonntag        }
263b2ce94c6SRico Sonntag
264392561bbSGreg Roach        $full_name = $this->parts->implode(I18N::$list_separator);
265392561bbSGreg Roach
266392561bbSGreg Roach        if ($link) {
267392561bbSGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>';
268a25f0a04SGreg Roach        }
269a25f0a04SGreg Roach
270*315eb316SGreg Roach        return '<bdi>' . e($full_name) . '</bdi>';
271a25f0a04SGreg Roach    }
272a25f0a04SGreg Roach
273a25f0a04SGreg Roach    /**
274a25f0a04SGreg Roach     * For lists and charts, where the full name won’t fit.
275a25f0a04SGreg Roach     *
276392561bbSGreg Roach     * @param bool $link
277a25f0a04SGreg Roach     *
278a25f0a04SGreg Roach     * @return string
279a25f0a04SGreg Roach     */
280e364afe4SGreg Roach    public function shortName(bool $link = false): string
281c1010edaSGreg Roach    {
282392561bbSGreg Roach        $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES');
283392561bbSGreg Roach
284392561bbSGreg Roach        // Abbreviate the place name, for lists
285392561bbSGreg Roach        if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) {
286392561bbSGreg Roach            $parts = $this->lastParts($SHOW_PEDIGREE_PLACES);
287392561bbSGreg Roach        } else {
288392561bbSGreg Roach            $parts = $this->firstParts($SHOW_PEDIGREE_PLACES);
289a25f0a04SGreg Roach        }
290a25f0a04SGreg Roach
291392561bbSGreg Roach        $short_name = $parts->implode(I18N::$list_separator);
292392561bbSGreg Roach
293392561bbSGreg Roach        // Add a tool-tip showing the full name
294392561bbSGreg Roach        $title = strip_tags($this->fullName());
295392561bbSGreg Roach
296392561bbSGreg Roach        if ($link) {
29740f99dd2SGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '">' . e($short_name) . '</a>';
298392561bbSGreg Roach        }
299392561bbSGreg Roach
300*315eb316SGreg Roach        return '<bdi>' . e($short_name) . '</bdi>';
301a25f0a04SGreg Roach    }
302a25f0a04SGreg Roach}
303