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