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 MyArtJaub\Webtrees\Module\GeoDispersion\GeoDispersionModule; |
23
|
|
|
use MyArtJaub\Webtrees\Module\GeoDispersion\Services\GeoAnalysisViewDataService; |
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
25
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
26
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
27
|
|
|
use Throwable; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Request handler for updating the status of a geographical analysis view. |
31
|
|
|
*/ |
32
|
|
|
class GeoAnalysisViewStatusAction implements RequestHandlerInterface |
33
|
|
|
{ |
34
|
|
|
private ?GeoDispersionModule $module; |
35
|
|
|
private GeoAnalysisViewDataService $geoview_data_service; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor for GeoAnalysisViewStatusAction Request Handler |
39
|
|
|
* |
40
|
|
|
* @param ModuleService $module_service |
41
|
|
|
* @param GeoAnalysisViewDataService $geoview_data_service |
42
|
|
|
*/ |
43
|
|
|
public function __construct(ModuleService $module_service, GeoAnalysisViewDataService $geoview_data_service) |
44
|
|
|
{ |
45
|
|
|
$this->module = $module_service->findByInterface(GeoDispersionModule::class)->first(); |
46
|
|
|
$this->geoview_data_service = $geoview_data_service; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
* @see \Psr\Http\Server\RequestHandlerInterface::handle() |
52
|
|
|
*/ |
53
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
54
|
|
|
{ |
55
|
|
|
$tree = $request->getAttribute('tree'); |
56
|
|
|
assert($tree instanceof Tree); |
57
|
|
|
|
58
|
|
|
$admin_config_route = route(AdminConfigPage::class, ['tree' => $tree->name()]); |
59
|
|
|
|
60
|
|
|
if ($this->module === null) { |
61
|
|
|
FlashMessages::addMessage( |
62
|
|
|
I18N::translate('The attached module could not be found.'), |
63
|
|
|
'danger' |
64
|
|
|
); |
65
|
|
|
return redirect($admin_config_route); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$view_id = (int) $request->getAttribute('view_id'); |
69
|
|
|
$view = $this->geoview_data_service->find($tree, $view_id, true); |
70
|
|
|
|
71
|
|
|
if ($view === null) { |
72
|
|
|
FlashMessages::addMessage( |
73
|
|
|
I18N::translate('The view with ID “%s” does not exist.', I18N::number($view_id)), |
74
|
|
|
'danger' |
75
|
|
|
); |
76
|
|
|
return redirect($admin_config_route); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$this->geoview_data_service->updateStatus($view, (bool) $request->getAttribute('enable', false)); |
81
|
|
|
FlashMessages::addMessage( |
82
|
|
|
I18N::translate('The geographical dispersion analysis view has been successfully updated.'), |
83
|
|
|
'success' |
84
|
|
|
); |
85
|
|
|
//phpcs:ignore Generic.Files.LineLength.TooLong |
86
|
|
|
Log::addConfigurationLog('Module ' . $this->module->title() . ' : View “' . $view->id() . '” has been updated.'); |
87
|
|
|
} catch (Throwable $ex) { |
88
|
|
|
FlashMessages::addMessage( |
89
|
|
|
I18N::translate('An error occured while updating the geographical dispersion analysis view.'), |
90
|
|
|
'danger' |
91
|
|
|
); |
92
|
|
|
//phpcs:ignore Generic.Files.LineLength.TooLong |
93
|
|
|
Log::addErrorLog('Module ' . $this->module->title() . ' : Error when updating view “' . $view->id() . '”: ' . $ex->getMessage()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return redirect($admin_config_route); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|