xref: /webtrees/app/Place.php (revision 87cca37c8b5ee0f07397179f377cdfde768951bb)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
2067992b6aSRichard Cisseeuse Fisharebest\Webtrees\Auth;
2167992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\ModuleInterface;
22*87cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleListInterface;
2367992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
2467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
25b68caec6SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
26392561bbSGreg Roachuse Illuminate\Support\Collection;
27b68caec6SGreg Roach
28a25f0a04SGreg Roach/**
2976692c8bSGreg Roach * A GEDCOM place (PLAC) object.
30a25f0a04SGreg Roach */
31c1010edaSGreg Roachclass Place
32c1010edaSGreg Roach{
33392561bbSGreg Roach    /** @var string e.g. "Westminster, London, England" */
34392561bbSGreg Roach    private $place_name;
35392561bbSGreg Roach
36392561bbSGreg Roach    /** @var Collection|string[] The parts of a place name, e.g. ["Westminster", "London", "England"] */
37392561bbSGreg Roach    private $parts;
3884caa210SGreg Roach
3976692c8bSGreg Roach    /** @var Tree We may have the same place name in different trees. */
4084caa210SGreg Roach    private $tree;
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach    /**
4376692c8bSGreg Roach     * Create a place.
4476692c8bSGreg Roach     *
45392561bbSGreg Roach     * @param string $place_name
4684caa210SGreg Roach     * @param Tree   $tree
47a25f0a04SGreg Roach     */
48392561bbSGreg Roach    public function __construct(string $place_name, Tree $tree)
49c1010edaSGreg Roach    {
50392561bbSGreg Roach        // Ignore any empty parts in place names such as "Village, , , Country".
51392561bbSGreg Roach        $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name)))
52392561bbSGreg Roach            ->filter();
53392561bbSGreg Roach
54392561bbSGreg Roach        // Rebuild the placename in the correct format.
55392561bbSGreg Roach        $this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
56392561bbSGreg Roach
5764b16745SGreg Roach        $this->tree = $tree;
58a25f0a04SGreg Roach    }
59a25f0a04SGreg Roach
60a25f0a04SGreg Roach    /**
6176692c8bSGreg Roach     * Get the higher level place.
6276692c8bSGreg Roach     *
63a25f0a04SGreg Roach     * @return Place
64a25f0a04SGreg Roach     */
65392561bbSGreg Roach    public function parent(): Place
66c1010edaSGreg Roach    {
678af6bbf8SGreg Roach        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree);
68392561bbSGreg Roach    }
69392561bbSGreg Roach
70392561bbSGreg Roach    /**
71392561bbSGreg Roach     * The database row that contains this place.
72392561bbSGreg Roach     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
73392561bbSGreg Roach     *
74392561bbSGreg Roach     * @return int
75392561bbSGreg Roach     */
76392561bbSGreg Roach    public function id(): int
77392561bbSGreg Roach    {
788af6bbf8SGreg Roach        return app()->make('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->place_name, function () {
79392561bbSGreg Roach            // The "top-level" place won't exist in the database.
80392561bbSGreg Roach            if ($this->parts->isEmpty()) {
81392561bbSGreg Roach                return 0;
82392561bbSGreg Roach            }
83392561bbSGreg Roach
848af6bbf8SGreg Roach            $parent_place_id = $this->parent()->id();
85392561bbSGreg Roach
86392561bbSGreg Roach            $place_id = (int) DB::table('places')
87392561bbSGreg Roach                ->where('p_file', '=', $this->tree->id())
88392561bbSGreg Roach                ->where('p_place', '=', $this->parts->first())
898af6bbf8SGreg Roach                ->where('p_parent_id', '=', $parent_place_id)
90392561bbSGreg Roach                ->value('p_id');
91392561bbSGreg Roach
92392561bbSGreg Roach            if ($place_id === 0) {
93392561bbSGreg Roach                $place = $this->parts->first();
94392561bbSGreg Roach
95392561bbSGreg Roach                DB::table('places')->insert([
96392561bbSGreg Roach                    'p_file'        => $this->tree->id(),
97392561bbSGreg Roach                    'p_place'       => $place,
988af6bbf8SGreg Roach                    'p_parent_id'   => $parent_place_id,
99392561bbSGreg Roach                    'p_std_soundex' => Soundex::russell($place),
100392561bbSGreg Roach                    'p_dm_soundex'  => Soundex::daitchMokotoff($place),
101392561bbSGreg Roach                ]);
102392561bbSGreg Roach
103392561bbSGreg Roach                $place_id = (int) DB::connection()->getPdo()->lastInsertId();
104392561bbSGreg Roach            }
105392561bbSGreg Roach
106392561bbSGreg Roach            return $place_id;
107392561bbSGreg Roach        });
108392561bbSGreg Roach    }
109392561bbSGreg Roach
110392561bbSGreg Roach    /**
111392561bbSGreg Roach     * Extract the locality (first parts) of a place name.
112392561bbSGreg Roach     *
113392561bbSGreg Roach     * @param int $n
114392561bbSGreg Roach     *
115392561bbSGreg Roach     * @return Collection
116392561bbSGreg Roach     */
117392561bbSGreg Roach    public function firstParts(int $n): Collection
118392561bbSGreg Roach    {
119392561bbSGreg Roach        return $this->parts->slice(0, $n);
120392561bbSGreg Roach    }
121392561bbSGreg Roach
122392561bbSGreg Roach    /**
123392561bbSGreg Roach     * Extract the country (last parts) of a place name.
124392561bbSGreg Roach     *
125392561bbSGreg Roach     * @param int $n
126392561bbSGreg Roach     *
127392561bbSGreg Roach     * @return Collection
128392561bbSGreg Roach     */
129392561bbSGreg Roach    public function lastParts(int $n): Collection
130392561bbSGreg Roach    {
131392561bbSGreg Roach        return $this->parts->slice(-$n);
132a25f0a04SGreg Roach    }
133a25f0a04SGreg Roach
134a25f0a04SGreg Roach    /**
13576692c8bSGreg Roach     * Get the lower level places.
13676692c8bSGreg Roach     *
137a25f0a04SGreg Roach     * @return Place[]
138a25f0a04SGreg Roach     */
1398f53f488SRico Sonntag    public function getChildPlaces(): array
140c1010edaSGreg Roach    {
141392561bbSGreg Roach        if ($this->place_name !== '') {
142392561bbSGreg Roach            $parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name;
143a25f0a04SGreg Roach        } else {
144a25f0a04SGreg Roach            $parent_text = '';
145a25f0a04SGreg Roach        }
146a25f0a04SGreg Roach
147b68caec6SGreg Roach        return DB::table('places')
148b68caec6SGreg Roach            ->where('p_file', '=', $this->tree->id())
149392561bbSGreg Roach            ->where('p_parent_id', '=', $this->id())
150b68caec6SGreg Roach            ->orderBy(DB::raw('p_place /*! COLLATE ' . I18N::collation() . ' */'))
151b68caec6SGreg Roach            ->pluck('p_place')
152b68caec6SGreg Roach            ->map(function (string $place) use ($parent_text): Place {
153b68caec6SGreg Roach                return new self($place . $parent_text, $this->tree);
154b68caec6SGreg Roach            })
155b68caec6SGreg Roach            ->all();
156a25f0a04SGreg Roach    }
157a25f0a04SGreg Roach
158a25f0a04SGreg Roach    /**
15976692c8bSGreg Roach     * Create a URL to the place-hierarchy page.
16076692c8bSGreg Roach     *
161a25f0a04SGreg Roach     * @return string
162a25f0a04SGreg Roach     */
163e8777cb5SGreg Roach    public function url(): string
164c1010edaSGreg Roach    {
16567992b6aSRichard Cissee        //find a module providing the place hierarchy
166*87cca37cSGreg Roach        $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $this->tree, Auth::user())->first(function (ModuleInterface $module) {
16767992b6aSRichard Cissee            return $module instanceof PlaceHierarchyListModule;
16867992b6aSRichard Cissee        });
16967992b6aSRichard Cissee
17067992b6aSRichard Cissee        if ($module instanceof PlaceHierarchyListModule) {
17167992b6aSRichard Cissee            return $module->listUrl($this->tree, [
172392561bbSGreg Roach                'parent' => $this->parts->reverse()->all(),
173aa6f03bbSGreg Roach                'ged'    => $this->tree->name(),
174e8777cb5SGreg Roach            ]);
17567992b6aSRichard Cissee        } else {
17667992b6aSRichard Cissee            //TODO: should we be allowed to return null here?
17767992b6aSRichard Cissee            return \Fisharebest\Webtrees\Html::url('index.php', []);
17867992b6aSRichard Cissee        }
179a25f0a04SGreg Roach    }
180a25f0a04SGreg Roach
181a25f0a04SGreg Roach    /**
182392561bbSGreg Roach     * Format this place for GEDCOM data.
18376692c8bSGreg Roach     *
184a25f0a04SGreg Roach     * @return string
185a25f0a04SGreg Roach     */
186392561bbSGreg Roach    public function gedcomName(): string
187c1010edaSGreg Roach    {
188392561bbSGreg Roach        return $this->place_name;
189a25f0a04SGreg Roach    }
190a25f0a04SGreg Roach
191a25f0a04SGreg Roach    /**
19276692c8bSGreg Roach     * Format this place for display on screen.
19376692c8bSGreg Roach     *
194a25f0a04SGreg Roach     * @return string
195a25f0a04SGreg Roach     */
196392561bbSGreg Roach    public function placeName(): string
197c1010edaSGreg Roach    {
198392561bbSGreg Roach        $place_name = $this->parts->first() ?? I18N::translate('unknown');
199a25f0a04SGreg Roach
200392561bbSGreg Roach        return '<span dir="auto">' . e($place_name) . '</span>';
201a25f0a04SGreg Roach    }
202a25f0a04SGreg Roach
203a25f0a04SGreg Roach    /**
20476692c8bSGreg Roach     * Generate the place name for display, including the full hierarchy.
20576692c8bSGreg Roach     *
206392561bbSGreg Roach     * @param bool $link
207392561bbSGreg Roach     *
208a25f0a04SGreg Roach     * @return string
209a25f0a04SGreg Roach     */
210392561bbSGreg Roach    public function fullName(bool $link = false)
211c1010edaSGreg Roach    {
212392561bbSGreg Roach        if ($this->parts->isEmpty()) {
213392561bbSGreg Roach            return '';
214b2ce94c6SRico Sonntag        }
215b2ce94c6SRico Sonntag
216392561bbSGreg Roach        $full_name = $this->parts->implode(I18N::$list_separator);
217392561bbSGreg Roach
218392561bbSGreg Roach        if ($link) {
219392561bbSGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>';
220a25f0a04SGreg Roach        }
221a25f0a04SGreg Roach
222392561bbSGreg Roach        return '<span dir="auto">' . e($full_name) . '</span>';
223a25f0a04SGreg Roach    }
224a25f0a04SGreg Roach
225a25f0a04SGreg Roach    /**
226a25f0a04SGreg Roach     * For lists and charts, where the full name won’t fit.
227a25f0a04SGreg Roach     *
228392561bbSGreg Roach     * @param bool $link
229a25f0a04SGreg Roach     *
230a25f0a04SGreg Roach     * @return string
231a25f0a04SGreg Roach     */
232392561bbSGreg Roach    public function shortName(bool $link = false)
233c1010edaSGreg Roach    {
234392561bbSGreg Roach        $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES');
235392561bbSGreg Roach
236392561bbSGreg Roach        // Abbreviate the place name, for lists
237392561bbSGreg Roach        if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) {
238392561bbSGreg Roach            $parts = $this->lastParts($SHOW_PEDIGREE_PLACES);
239392561bbSGreg Roach        } else {
240392561bbSGreg Roach            $parts = $this->firstParts($SHOW_PEDIGREE_PLACES);
241a25f0a04SGreg Roach        }
242a25f0a04SGreg Roach
243392561bbSGreg Roach        $short_name = $parts->implode(I18N::$list_separator);
244392561bbSGreg Roach
245392561bbSGreg Roach        // Add a tool-tip showing the full name
246392561bbSGreg Roach        $title = strip_tags($this->fullName());
247392561bbSGreg Roach
248392561bbSGreg Roach        if ($link) {
249392561bbSGreg Roach            return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '"">' . e($short_name) . '</a>';
250392561bbSGreg Roach        }
251392561bbSGreg Roach
252392561bbSGreg Roach        return '<span dir="auto">' . e($short_name) . '</span>';
253a25f0a04SGreg Roach    }
254a25f0a04SGreg Roach}
255