xref: /webtrees/app/Place.php (revision 7bed239c2f6c32802d9d87c0c3123522bd4e98a6)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15a25f0a04SGreg Roach * along with this program. If not, see <http://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;
29*7bed239cSGreg Roachuse stdClass;
30b68caec6SGreg Roach
31a25f0a04SGreg Roach/**
3276692c8bSGreg Roach * A GEDCOM place (PLAC) object.
33a25f0a04SGreg Roach */
34c1010edaSGreg Roachclass Place
35c1010edaSGreg Roach{
36392561bbSGreg Roach    /** @var string e.g. "Westminster, London, England" */
37392561bbSGreg Roach    private $place_name;
38392561bbSGreg Roach
39b5c8fd7eSGreg Roach    /** @var Collection<string> The parts of a place name, e.g. ["Westminster", "London", "England"] */
40392561bbSGreg Roach    private $parts;
4184caa210SGreg Roach
4276692c8bSGreg Roach    /** @var Tree We may have the same place name in different trees. */
4384caa210SGreg Roach    private $tree;
44a25f0a04SGreg Roach
45a25f0a04SGreg Roach    /**
4676692c8bSGreg Roach     * Create a place.
4776692c8bSGreg Roach     *
48392561bbSGreg Roach     * @param string $place_name
4984caa210SGreg Roach     * @param Tree   $tree
50a25f0a04SGreg Roach     */
51392561bbSGreg Roach    public function __construct(string $place_name, Tree $tree)
52c1010edaSGreg Roach    {
53392561bbSGreg Roach        // Ignore any empty parts in place names such as "Village, , , Country".
54321a89daSGreg Roach        $this->parts = Collection::make(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name))
55392561bbSGreg Roach            ->filter();
56392561bbSGreg Roach
57392561bbSGreg Roach        // Rebuild the placename in the correct format.
58392561bbSGreg Roach        $this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
59392561bbSGreg Roach
6064b16745SGreg Roach        $this->tree = $tree;
61a25f0a04SGreg Roach    }
62a25f0a04SGreg Roach
63a25f0a04SGreg Roach    /**
64*7bed239cSGreg Roach     * Find a place by its ID.
65*7bed239cSGreg Roach     *
66*7bed239cSGreg Roach     * @param int  $id
67*7bed239cSGreg Roach     * @param Tree $tree
68*7bed239cSGreg Roach     *
69*7bed239cSGreg Roach     * @return Place
70*7bed239cSGreg Roach     */
71*7bed239cSGreg Roach    public static function find(int $id, Tree $tree): Place
72*7bed239cSGreg Roach    {
73*7bed239cSGreg Roach        $parts = new Collection();
74*7bed239cSGreg Roach
75*7bed239cSGreg Roach        while ($id !== 0) {
76*7bed239cSGreg Roach            $row = DB::table('places')
77*7bed239cSGreg Roach                ->where('p_file', '=', $tree->id())
78*7bed239cSGreg Roach                ->where('p_id', '=', $id)
79*7bed239cSGreg Roach                ->first();
80*7bed239cSGreg Roach
81*7bed239cSGreg Roach            if ($row instanceof stdClass) {
82*7bed239cSGreg Roach                $id = (int) $row->p_parent_id;
83*7bed239cSGreg Roach                $parts->add($row->p_place);
84*7bed239cSGreg Roach            } else {
85*7bed239cSGreg Roach                $id = 0;
86*7bed239cSGreg Roach            }
87*7bed239cSGreg Roach        }
88*7bed239cSGreg Roach
89*7bed239cSGreg Roach        $place_name = $parts->implode(Gedcom::PLACE_SEPARATOR);
90*7bed239cSGreg Roach
91*7bed239cSGreg Roach        return new Place($place_name, $tree);
92*7bed239cSGreg Roach    }
93*7bed239cSGreg Roach
94*7bed239cSGreg Roach    /**
9576692c8bSGreg Roach     * Get the higher level place.
9676692c8bSGreg Roach     *
97a25f0a04SGreg Roach     * @return Place
98a25f0a04SGreg Roach     */
99392561bbSGreg Roach    public function parent(): Place
100c1010edaSGreg Roach    {
1018af6bbf8SGreg Roach        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree);
102392561bbSGreg Roach    }
103392561bbSGreg Roach
104392561bbSGreg Roach    /**
105392561bbSGreg Roach     * The database row that contains this place.
106392561bbSGreg Roach     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
107392561bbSGreg Roach     *
108392561bbSGreg Roach     * @return int
109392561bbSGreg Roach     */
110392561bbSGreg Roach    public function id(): int
111392561bbSGreg Roach    {
1127476e8a5SGreg Roach        return app('cache.array')->remember('place-' . $this->place_name, function (): int {
113392561bbSGreg Roach            // The "top-level" place won't exist in the database.
114392561bbSGreg Roach            if ($this->parts->isEmpty()) {
115392561bbSGreg Roach                return 0;
116392561bbSGreg Roach            }
117392561bbSGreg Roach
1188af6bbf8SGreg Roach            $parent_place_id = $this->parent()->id();
119392561bbSGreg Roach
120392561bbSGreg Roach            $place_id = (int) DB::table('places')
121392561bbSGreg Roach                ->where('p_file', '=', $this->tree->id())
122392561bbSGreg Roach                ->where('p_place', '=', $this->parts->first())
1238af6bbf8SGreg Roach                ->where('p_parent_id', '=', $parent_place_id)
124392561bbSGreg Roach                ->value('p_id');
125392561bbSGreg Roach
126392561bbSGreg Roach            if ($place_id === 0) {
127392561bbSGreg Roach                $place = $this->parts->first();
128392561bbSGreg Roach
129392561bbSGreg Roach                DB::table('places')->insert([
130392561bbSGreg Roach                    'p_file'        => $this->tree->id(),
131392561bbSGreg Roach                    'p_place'       => $place,
1328af6bbf8SGreg Roach                    'p_parent_id'   => $parent_place_id,
133392561bbSGreg Roach                    'p_std_soundex' => Soundex::russell($place),
134392561bbSGreg Roach                    'p_dm_soundex'  => Soundex::daitchMokotoff($place),
135392561bbSGreg Roach                ]);
136392561bbSGreg Roach
137392561bbSGreg Roach                $place_id = (int) DB::connection()->getPdo()->lastInsertId();
138392561bbSGreg Roach            }
139392561bbSGreg Roach
140392561bbSGreg Roach            return $place_id;
141392561bbSGreg Roach        });
142392561bbSGreg Roach    }
143392561bbSGreg Roach
144392561bbSGreg Roach    /**
145392561bbSGreg Roach     * Extract the locality (first parts) of a place name.
146392561bbSGreg Roach     *
147392561bbSGreg Roach     * @param int $n
148392561bbSGreg Roach     *
149b5c8fd7eSGreg Roach     * @return Collection<string>
150392561bbSGreg Roach     */
151392561bbSGreg Roach    public function firstParts(int $n): Collection
152392561bbSGreg Roach    {
153392561bbSGreg Roach        return $this->parts->slice(0, $n);
154392561bbSGreg Roach    }
155392561bbSGreg Roach
156392561bbSGreg Roach    /**
157392561bbSGreg Roach     * Extract the country (last 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 lastParts(int $n): Collection
164392561bbSGreg Roach    {
165392561bbSGreg Roach        return $this->parts->slice(-$n);
166a25f0a04SGreg Roach    }
167a25f0a04SGreg Roach
168a25f0a04SGreg Roach    /**
16976692c8bSGreg Roach     * Get the lower level places.
17076692c8bSGreg Roach     *
171a25f0a04SGreg Roach     * @return Place[]
172a25f0a04SGreg Roach     */
1738f53f488SRico Sonntag    public function getChildPlaces(): array
174c1010edaSGreg Roach    {
175392561bbSGreg Roach        if ($this->place_name !== '') {
176392561bbSGreg Roach            $parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name;
177a25f0a04SGreg Roach        } else {
178a25f0a04SGreg Roach            $parent_text = '';
179a25f0a04SGreg Roach        }
180a25f0a04SGreg Roach
181b68caec6SGreg Roach        return DB::table('places')
182b68caec6SGreg Roach            ->where('p_file', '=', $this->tree->id())
183392561bbSGreg Roach            ->where('p_parent_id', '=', $this->id())
184a69f5655SGreg Roach            ->orderBy(new Expression('p_place /*! COLLATE ' . I18N::collation() . ' */'))
185b68caec6SGreg Roach            ->pluck('p_place')
186b68caec6SGreg Roach            ->map(function (string $place) use ($parent_text): Place {
187b68caec6SGreg Roach                return new self($place . $parent_text, $this->tree);
188b68caec6SGreg Roach            })
189b68caec6SGreg Roach            ->all();
190a25f0a04SGreg Roach    }
191a25f0a04SGreg Roach
192a25f0a04SGreg Roach    /**
19376692c8bSGreg Roach     * Create a URL to the place-hierarchy page.
19476692c8bSGreg Roach     *
195a25f0a04SGreg Roach     * @return string
196a25f0a04SGreg Roach     */
197e8777cb5SGreg Roach    public function url(): string
198c1010edaSGreg Roach    {
19967992b6aSRichard Cissee        //find a module providing the place hierarchy
2000797053bSGreg Roach        $module = app(ModuleService::class)
2010797053bSGreg Roach            ->findByComponent(ModuleListInterface::class, $this->tree, Auth::user())
2020797053bSGreg Roach            ->first(static function (ModuleInterface $module): bool {
20367992b6aSRichard Cissee                return $module instanceof PlaceHierarchyListModule;
20467992b6aSRichard Cissee            });
20567992b6aSRichard Cissee
20667992b6aSRichard Cissee        if ($module instanceof PlaceHierarchyListModule) {
20767992b6aSRichard Cissee            return $module->listUrl($this->tree, [
208*7bed239cSGreg Roach                'place_id' => $this->id(),
2099022ab66SGreg Roach                'tree'     => $this->tree->name(),
210e8777cb5SGreg Roach            ]);
211e364afe4SGreg Roach        }
212e364afe4SGreg Roach
213f7721877SGreg Roach        // The place-list module is disabled...
214f7721877SGreg Roach        return '#';
21567992b6aSRichard Cissee    }
216a25f0a04SGreg Roach
217a25f0a04SGreg Roach    /**
218392561bbSGreg Roach     * Format this place for GEDCOM data.
21976692c8bSGreg Roach     *
220a25f0a04SGreg Roach     * @return string
221a25f0a04SGreg Roach     */
222392561bbSGreg Roach    public function gedcomName(): string
223c1010edaSGreg Roach    {
224392561bbSGreg Roach        return $this->place_name;
225a25f0a04SGreg Roach    }
226a25f0a04SGreg Roach
227a25f0a04SGreg Roach    /**
22876692c8bSGreg Roach     * Format this place for display on screen.
22976692c8bSGreg Roach     *
230a25f0a04SGreg Roach     * @return string
231a25f0a04SGreg Roach     */
232392561bbSGreg Roach    public function placeName(): string
233c1010edaSGreg Roach    {
234392561bbSGreg Roach        $place_name = $this->parts->first() ?? I18N::translate('unknown');
235a25f0a04SGreg Roach
236392561bbSGreg Roach        return '<span dir="auto">' . e($place_name) . '</span>';
237a25f0a04SGreg Roach    }
238a25f0a04SGreg Roach
239a25f0a04SGreg Roach    /**
24076692c8bSGreg Roach     * Generate the place name for display, including the full hierarchy.
24176692c8bSGreg Roach     *
242392561bbSGreg Roach     * @param bool $link
243392561bbSGreg Roach     *
244a25f0a04SGreg Roach     * @return string
245a25f0a04SGreg Roach     */
246e364afe4SGreg Roach    public function fullName(bool $link = false): string
247c1010edaSGreg Roach    {
248392561bbSGreg Roach        if ($this->parts->isEmpty()) {
249392561bbSGreg Roach            return '';
250b2ce94c6SRico Sonntag        }
251b2ce94c6SRico Sonntag
252392561bbSGreg Roach        $full_name = $this->parts->implode(I18N::$list_separator);
253392561bbSGreg Roach
254392561bbSGreg Roach        if ($link) {
255392561bbSGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>';
256a25f0a04SGreg Roach        }
257a25f0a04SGreg Roach
258392561bbSGreg Roach        return '<span dir="auto">' . e($full_name) . '</span>';
259a25f0a04SGreg Roach    }
260a25f0a04SGreg Roach
261a25f0a04SGreg Roach    /**
262a25f0a04SGreg Roach     * For lists and charts, where the full name won’t fit.
263a25f0a04SGreg Roach     *
264392561bbSGreg Roach     * @param bool $link
265a25f0a04SGreg Roach     *
266a25f0a04SGreg Roach     * @return string
267a25f0a04SGreg Roach     */
268e364afe4SGreg Roach    public function shortName(bool $link = false): string
269c1010edaSGreg Roach    {
270392561bbSGreg Roach        $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES');
271392561bbSGreg Roach
272392561bbSGreg Roach        // Abbreviate the place name, for lists
273392561bbSGreg Roach        if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) {
274392561bbSGreg Roach            $parts = $this->lastParts($SHOW_PEDIGREE_PLACES);
275392561bbSGreg Roach        } else {
276392561bbSGreg Roach            $parts = $this->firstParts($SHOW_PEDIGREE_PLACES);
277a25f0a04SGreg Roach        }
278a25f0a04SGreg Roach
279392561bbSGreg Roach        $short_name = $parts->implode(I18N::$list_separator);
280392561bbSGreg Roach
281392561bbSGreg Roach        // Add a tool-tip showing the full name
282392561bbSGreg Roach        $title = strip_tags($this->fullName());
283392561bbSGreg Roach
284392561bbSGreg Roach        if ($link) {
28540f99dd2SGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '">' . e($short_name) . '</a>';
286392561bbSGreg Roach        }
287392561bbSGreg Roach
288392561bbSGreg Roach        return '<span dir="auto">' . e($short_name) . '</span>';
289a25f0a04SGreg Roach    }
290a25f0a04SGreg Roach}
291