|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* webtrees-lib: MyArtJaub library for webtrees |
|
5
|
|
|
* |
|
6
|
|
|
* @package MyArtJaub\Webtrees |
|
7
|
|
|
* @subpackage GeoDispersion |
|
8
|
|
|
* @author Jonathan Jaubart <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) 2021-2022, Jonathan Jaubart |
|
10
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace MyArtJaub\Webtrees\Module\GeoDispersion\PlaceMappers\Config; |
|
16
|
|
|
|
|
17
|
|
|
use Fisharebest\Webtrees\Place; |
|
18
|
|
|
use Fisharebest\Webtrees\Tree; |
|
19
|
|
|
use Fisharebest\Webtrees\Validator; |
|
20
|
|
|
use Fisharebest\Webtrees\Module\ModuleInterface; |
|
21
|
|
|
use Fisharebest\Webtrees\Services\TreeService; |
|
22
|
|
|
use Illuminate\Support\Collection; |
|
23
|
|
|
use MyArtJaub\Webtrees\Common\GeoDispersion\Config\GenericPlaceMapperConfig; |
|
24
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
25
|
|
|
use RuntimeException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Place Mapper configuration for mappers filtering on Top Places. |
|
29
|
|
|
*/ |
|
30
|
|
|
class FilteredTopPlaceMapperConfig extends GenericPlaceMapperConfig |
|
31
|
|
|
{ |
|
32
|
|
|
private TreeService $tree_service; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* FilteredTopPlaceMapperConfig |
|
36
|
|
|
* |
|
37
|
|
|
* @param TreeService $tree_service |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(TreeService $tree_service) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->tree_service = $tree_service; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the configured Top Places to filter on |
|
46
|
|
|
* |
|
47
|
|
|
* @return Collection<Place> |
|
48
|
|
|
*/ |
|
49
|
|
|
public function topPlaces(): Collection |
|
50
|
|
|
{ |
|
51
|
|
|
return collect($this->get('topPlaces', [])) |
|
52
|
|
|
->filter( |
|
53
|
|
|
/** @psalm-suppress MissingClosureParamType */ |
|
54
|
|
|
fn($item): bool => $item instanceof Place |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritDoc} |
|
60
|
|
|
* @see \MyArtJaub\Webtrees\Common\GeoDispersion\Config\GenericPlaceMapperConfig::jsonSerializeConfig() |
|
61
|
|
|
*/ |
|
62
|
|
|
public function jsonSerializeConfig() |
|
63
|
|
|
{ |
|
64
|
|
|
return [ |
|
65
|
|
|
'topPlaces' => $this->topPlaces() |
|
66
|
|
|
->map(fn(Place $place): array => [ $place->tree()->id(), $place->gedcomName() ]) |
|
67
|
|
|
->toArray() |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritDoc} |
|
73
|
|
|
* @see \MyArtJaub\Webtrees\Common\GeoDispersion\Config\GenericPlaceMapperConfig::jsonDeserialize() |
|
74
|
|
|
* |
|
75
|
|
|
* @param mixed $config |
|
76
|
|
|
* @return $this |
|
77
|
|
|
*/ |
|
78
|
|
|
public function jsonDeserialize($config): self |
|
79
|
|
|
{ |
|
80
|
|
|
if (is_string($config)) { |
|
81
|
|
|
return $this->jsonDeserialize(json_decode($config)); |
|
82
|
|
|
} |
|
83
|
|
|
if (is_array($config)) { |
|
84
|
|
|
$this->setConfig([ |
|
85
|
|
|
'topPlaces' => collect($config['topPlaces'] ?? []) |
|
86
|
|
|
->filter( |
|
87
|
|
|
/** @psalm-suppress MissingClosureParamType */ |
|
88
|
|
|
fn($item): bool => is_array($item) && count($item) === 2 |
|
89
|
|
|
)->map(function (array $item): ?Place { |
|
90
|
|
|
try { |
|
91
|
|
|
return new Place($item[1], $this->tree_service->find($item[0])); |
|
92
|
|
|
} catch (RuntimeException $ex) { |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
}) |
|
96
|
|
|
->filter() |
|
97
|
|
|
->toArray() |
|
98
|
|
|
]); |
|
99
|
|
|
} |
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritDoc} |
|
105
|
|
|
* @see \MyArtJaub\Webtrees\Common\GeoDispersion\Config\GenericPlaceMapperConfig::configContent() |
|
106
|
|
|
*/ |
|
107
|
|
|
public function configContent(ModuleInterface $module, Tree $tree): string |
|
108
|
|
|
{ |
|
109
|
|
|
return view($module->name() . '::mappers/filtered-top-config', [ |
|
110
|
|
|
'tree' => $tree, |
|
111
|
|
|
'top_places' => $this->topPlaces() |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritDoc} |
|
117
|
|
|
* @see \MyArtJaub\Webtrees\Common\GeoDispersion\Config\GenericPlaceMapperConfig::withConfigUpdate() |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
|
|
public function withConfigUpdate(ServerRequestInterface $request): self |
|
121
|
|
|
{ |
|
122
|
|
|
$tree = Validator::attributes($request)->treeOptional(); |
|
123
|
|
|
|
|
124
|
|
|
if ($tree === null) { |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$top_places = Validator::parsedBody($request)->array('mapper_filt_top_places'); |
|
129
|
|
|
$config = ['topPlaces' => []]; |
|
130
|
|
|
foreach ($top_places as $top_place_id) { |
|
131
|
|
|
$place = Place::find((int) $top_place_id, $tree); |
|
132
|
|
|
if (mb_strlen($place->gedcomName()) > 0) { |
|
133
|
|
|
$config['topPlaces'][] = $place; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
$this->setConfig($config); |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|