|
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) 2009-2021, 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\Http\RequestHandlers; |
|
16
|
|
|
|
|
17
|
|
|
use Fisharebest\Webtrees\FlashMessages; |
|
18
|
|
|
use Fisharebest\Webtrees\I18N; |
|
19
|
|
|
use Fisharebest\Webtrees\Log; |
|
20
|
|
|
use Fisharebest\Webtrees\Tree; |
|
21
|
|
|
use Fisharebest\Webtrees\Services\ModuleService; |
|
22
|
|
|
use Illuminate\Contracts\Container\BindingResolutionException; |
|
23
|
|
|
use MyArtJaub\Webtrees\Contracts\GeoDispersion\GeoAnalysisInterface; |
|
24
|
|
|
use MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule; |
|
25
|
|
|
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\GeoAnalysisViewDataService; |
|
26
|
|
|
use MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView; |
|
27
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
28
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
29
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Request handler for editing a geographical analysis view. |
|
33
|
|
|
*/ |
|
34
|
|
|
class GeoAnalysisViewEditAction implements RequestHandlerInterface |
|
35
|
|
|
{ |
|
36
|
|
|
private ?GeoDispersionModule $module; |
|
37
|
|
|
private GeoAnalysisViewDataService $geoview_data_service; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Constructor for GeoAnalysisViewEditAction Request Handler |
|
41
|
|
|
* |
|
42
|
|
|
* @param ModuleService $module_service |
|
43
|
|
|
* @param GeoAnalysisViewDataService $geoview_data_service |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(ModuleService $module_service, GeoAnalysisViewDataService $geoview_data_service) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->module = $module_service->findByInterface(GeoDispersionModule::class)->first(); |
|
48
|
|
|
$this->geoview_data_service = $geoview_data_service; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
* @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
54
|
|
|
*/ |
|
55
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
56
|
|
|
{ |
|
57
|
|
|
$tree = $request->getAttribute('tree'); |
|
58
|
|
|
assert($tree instanceof Tree); |
|
59
|
|
|
|
|
60
|
|
|
$admin_config_route = route(AdminConfigPage::class, ['tree' => $tree->name()]); |
|
61
|
|
|
|
|
62
|
|
|
if ($this->module === null) { |
|
63
|
|
|
FlashMessages::addMessage( |
|
64
|
|
|
I18N::translate('The attached module could not be found.'), |
|
65
|
|
|
'danger' |
|
66
|
|
|
); |
|
67
|
|
|
return redirect($admin_config_route); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$view_id = (int) $request->getAttribute('view_id'); |
|
72
|
|
|
$view = $this->geoview_data_service->find($tree, $view_id, true); |
|
73
|
|
|
|
|
74
|
|
|
$params = (array) $request->getParsedBody(); |
|
75
|
|
|
|
|
76
|
|
|
$description = $params['view_description'] ?? ''; |
|
77
|
|
|
$place_depth = (int) ($params['view_depth'] ?? 1); |
|
78
|
|
|
$top_places = (int) ($params['view_top_places'] ?? 0); |
|
79
|
|
|
|
|
80
|
|
|
$analysis = null; |
|
|
|
|
|
|
81
|
|
|
try { |
|
82
|
|
|
$analysis = app($params['view_analysis'] ?? ''); |
|
83
|
|
|
} catch (BindingResolutionException $ex) { |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ( |
|
87
|
|
|
$view === null |
|
88
|
|
|
|| $analysis === null || !($analysis instanceof GeoAnalysisInterface) |
|
89
|
|
|
|| $place_depth <= 0 && $top_places < 0 |
|
90
|
|
|
) { |
|
91
|
|
|
FlashMessages::addMessage( |
|
92
|
|
|
I18N::translate('The parameters for view with ID “%d” are not valid.', I18N::number($view_id)), |
|
93
|
|
|
'danger' |
|
94
|
|
|
); |
|
95
|
|
|
return redirect($admin_config_route); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$new_view = $view |
|
99
|
|
|
->with($view->isEnabled(), $description, $analysis, $place_depth, $top_places) |
|
100
|
|
|
->withGlobalSettingsUpdate($request); |
|
101
|
|
|
|
|
102
|
|
|
if ($this->geoview_data_service->update($new_view) > 0) { |
|
103
|
|
|
FlashMessages::addMessage( |
|
104
|
|
|
I18N::translate('The geographical dispersion analysis view has been successfully updated'), |
|
105
|
|
|
'success' |
|
106
|
|
|
); |
|
107
|
|
|
//phpcs:ignore Generic.Files.LineLength.TooLong |
|
108
|
|
|
Log::addConfigurationLog('Module ' . $this->module->title() . ' : View “' . $view->id() . '” has been updated.'); |
|
109
|
|
|
} else { |
|
110
|
|
|
FlashMessages::addMessage( |
|
111
|
|
|
I18N::translate('An error occured while updating the geographical dispersion analysis view'), |
|
112
|
|
|
'danger' |
|
113
|
|
|
); |
|
114
|
|
|
//phpcs:ignore Generic.Files.LineLength.TooLong |
|
115
|
|
|
Log::addConfigurationLog('Module ' . $this->module->title() . ' : View “' . $view->id() . '” could not be updated. See error log.'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return redirect($admin_config_route); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|