From 9a85fa0652ca7dd4b961db0a634a550d336817e3 Mon Sep 17 00:00:00 2001 From: tronsmit Date: Wed, 7 Apr 2021 12:26:38 +0200 Subject: [PATCH] Finer control of abbreviate place names Motivation: Including highest level place name when abbrevaiting from back is not always desirable. This change will allow you to request the highest level to keep while always trying to show SHOW_PEDIGREE_PLACES number of levels Implementation: - add function backParts to Places - keep lastParts as it is used by census handlers - expand scope of SHOW_PEDIGREE_PLACES_SUFFIX so that 1=keep top level, 2=only keep to 2nd highest level, etc. Note: help text probably needs work --- app/Place.php | 18 ++++++++++++++++-- resources/views/admin/trees-preferences.phtml | 8 +++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/Place.php b/app/Place.php index 6a73120b578..1c1458e6c80 100644 --- a/app/Place.php +++ b/app/Place.php @@ -165,6 +165,19 @@ public function firstParts(int $n): Collection return $this->parts->slice(0, $n); } + /** + * Extract the last parts of a place name, omitting top levels if possible. + * + * @param int $num : of parts to include - has highest priority + * @param int $omit : where to start counting backwards 1=top level, 2=next etc. + * + * @return Collection + */ + public function backParts(int $num, $omit): Collection + { + return $this->parts->slice(-($omit - 1) - $num, $num); + } + /** * Extract the country (last parts) of a place name. * @@ -280,10 +293,11 @@ public function fullName(bool $link = false): string public function shortName(bool $link = false): string { $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES'); + $SHOW_PEDIGREE_PLACES_SUFFIX = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX'); // Abbreviate the place name, for lists - if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) { - $parts = $this->lastParts($SHOW_PEDIGREE_PLACES); + if ($SHOW_PEDIGREE_PLACES_SUFFIX) { + $parts = $this->backParts($SHOW_PEDIGREE_PLACES, $SHOW_PEDIGREE_PLACES_SUFFIX); } else { $parts = $this->firstParts($SHOW_PEDIGREE_PLACES); } diff --git a/resources/views/admin/trees-preferences.phtml b/resources/views/admin/trees-preferences.phtml index 0fe268137b1..6eeb503211e 100644 --- a/resources/views/admin/trees-preferences.phtml +++ b/resources/views/admin/trees-preferences.phtml @@ -594,7 +594,13 @@ use Illuminate\Support\Collection;
'SHOW_PEDIGREE_PLACES_SUFFIX', 'selected' => $tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX'), 'options' => ['0' => I18N::translateContext('Show the [first/last] [N] parts of a place name.', 'first'), '1' => I18N::translateContext('Show the [first/last] [N] parts of a place name.', 'last')]]), + view('components/select', ['name' => 'SHOW_PEDIGREE_PLACES_SUFFIX', 'selected' => $tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX'), + 'options' => ['0' => I18N::translateContext('Show the [first/last] [N] parts of a place name.', 'first'), + '1' => I18N::translateContext('Show the [first/last] [N] parts of a place name.', 'last'), + '2' => I18N::translateContext('Show the [first/last] [N] parts of a place name.', 'last, omitting top level'), + '3' => I18N::translateContext('Show the [first/last] [N] parts of a place name.', 'last, omitting top 2 levels'), + '4' => I18N::translateContext('Show the [first/last] [N] parts of a place name.', 'last, omitting top 3 levels'), + ]]), view('components/select-number', ['name' => 'SHOW_PEDIGREE_PLACES', 'selected' => $tree->getPreference('SHOW_PEDIGREE_PLACES'), 'options' => range(1, 9)]) ) ?>