. */ declare(strict_types=1); namespace Fisharebest\Webtrees; use Fisharebest\Webtrees\Module\ModuleInterface; use Fisharebest\Webtrees\Module\ModuleListInterface; use Fisharebest\Webtrees\Module\PlaceHierarchyListModule; use Fisharebest\Webtrees\Services\ModuleService; use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Support\Collection; /** * A GEDCOM place (PLAC) object. */ class Place { /** @var string e.g. "Westminster, London, England" */ private $place_name; /** @var Collection|string[] The parts of a place name, e.g. ["Westminster", "London", "England"] */ private $parts; /** @var Tree We may have the same place name in different trees. */ private $tree; /** * Create a place. * * @param string $place_name * @param Tree $tree */ public function __construct(string $place_name, Tree $tree) { // Ignore any empty parts in place names such as "Village, , , Country". $this->parts = Collection::make(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name)) ->filter(); // Rebuild the placename in the correct format. $this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); $this->tree = $tree; } /** * Get the higher level place. * * @return Place */ public function parent(): Place { return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree); } /** * The database row that contains this place. * Note that due to database collation, both "Quebec" and "Québec" will share the same row. * * @return int */ public function id(): int { return app('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->place_name, function () { // The "top-level" place won't exist in the database. if ($this->parts->isEmpty()) { return 0; } $parent_place_id = $this->parent()->id(); $place_id = (int) DB::table('places') ->where('p_file', '=', $this->tree->id()) ->where('p_place', '=', $this->parts->first()) ->where('p_parent_id', '=', $parent_place_id) ->value('p_id'); if ($place_id === 0) { $place = $this->parts->first(); DB::table('places')->insert([ 'p_file' => $this->tree->id(), 'p_place' => $place, 'p_parent_id' => $parent_place_id, 'p_std_soundex' => Soundex::russell($place), 'p_dm_soundex' => Soundex::daitchMokotoff($place), ]); $place_id = (int) DB::connection()->getPdo()->lastInsertId(); } return $place_id; }); } /** * Extract the locality (first parts) of a place name. * * @param int $n * * @return Collection */ public function firstParts(int $n): Collection { return $this->parts->slice(0, $n); } /** * Extract the country (last parts) of a place name. * * @param int $n * * @return Collection */ public function lastParts(int $n): Collection { return $this->parts->slice(-$n); } /** * Get the lower level places. * * @return Place[] */ public function getChildPlaces(): array { if ($this->place_name !== '') { $parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name; } else { $parent_text = ''; } return DB::table('places') ->where('p_file', '=', $this->tree->id()) ->where('p_parent_id', '=', $this->id()) ->orderBy(DB::raw('p_place /*! COLLATE ' . I18N::collation() . ' */')) ->pluck('p_place') ->map(function (string $place) use ($parent_text): Place { return new self($place . $parent_text, $this->tree); }) ->all(); } /** * Create a URL to the place-hierarchy page. * * @return string */ public function url(): string { //find a module providing the place hierarchy $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $this->tree, Auth::user())->first(function (ModuleInterface $module) { return $module instanceof PlaceHierarchyListModule; }); if ($module instanceof PlaceHierarchyListModule) { return $module->listUrl($this->tree, [ 'parent' => $this->parts->reverse()->all(), 'ged' => $this->tree->name(), ]); } else { //TODO: should we be allowed to return null here? return \Fisharebest\Webtrees\Html::url('index.php', []); } } /** * Format this place for GEDCOM data. * * @return string */ public function gedcomName(): string { return $this->place_name; } /** * Format this place for display on screen. * * @return string */ public function placeName(): string { $place_name = $this->parts->first() ?? I18N::translate('unknown'); return '' . e($place_name) . ''; } /** * Generate the place name for display, including the full hierarchy. * * @param bool $link * * @return string */ public function fullName(bool $link = false) { if ($this->parts->isEmpty()) { return ''; } $full_name = $this->parts->implode(I18N::$list_separator); if ($link) { return '' . e($full_name) . ''; } return '' . e($full_name) . ''; } /** * For lists and charts, where the full name won’t fit. * * @param bool $link * * @return string */ public function shortName(bool $link = false) { $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES'); // Abbreviate the place name, for lists if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) { $parts = $this->lastParts($SHOW_PEDIGREE_PLACES); } else { $parts = $this->firstParts($SHOW_PEDIGREE_PLACES); } $short_name = $parts->implode(I18N::$list_separator); // Add a tool-tip showing the full name $title = strip_tags($this->fullName()); if ($link) { return '' . e($short_name) . ''; } return '' . e($short_name) . ''; } }