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