-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathXmlExportSettingsAction.php
155 lines (138 loc) · 7.36 KB
/
XmlExportSettingsAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2022 webtrees development team
* <http://webtrees.net>
*
* RepositoryHierarchy (webtrees custom module):
* Copyright (C) 2022 Markus Hemprich
* <http://www.familienforschung-hemprich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Jefferson49\Webtrees\Module\RepositoryHierarchy;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Services\ModuleService;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* View a modal to change XML export settings.
*/
class XmlExportSettingsAction implements RequestHandlerInterface
{
//Module service to search and find modules
private ModuleService $module_service;
/**
* Constructor.
*
* @param ModuleService $module_service
*/
public function __construct(ModuleService $module_service)
{
$this->module_service = $module_service;
}
/**
* Handle a request to view a modal XML export settings
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$tree = Validator::attributes($request)->tree();
$user = Validator::attributes($request)->user();
$repository_xref = Validator::attributes($request)->string('xref');
$command = Validator::attributes($request)->string('command');
$params = (array) $request->getQueryParams();
$admin_user_id = RepositoryHierarchy::ADMIN_USER_STRING;
$repository_hierarchy = $this->module_service->findByName(RepositoryHierarchy::activeModuleName());
if ($command === RepositoryHierarchy::CMD_LOAD_ADMIN_XML_SETTINGS) {
return response(
[
'html' => view(
RepositoryHierarchy::viewsNamespace() . '::modals/xml-export-settings',
[
'tree' => $tree,
'xref' => $repository_xref,
'fi_aid_title' => $repository_hierarchy->getPreference(RepositoryHierarchy::PREF_FINDING_AID_TITLE . $tree->id() . '_' . $repository_xref . '_' . $admin_user_id, ''),
'country_code' => $repository_hierarchy->getPreference(RepositoryHierarchy::PREF_COUNTRY_CODE . $tree->id() . '_' . $repository_xref . '_' . $admin_user_id, ''),
'agency_code' => $repository_hierarchy->getPreference(RepositoryHierarchy::PREF_MAIN_AGENCY_CODE . $tree->id() . '_' . $repository_xref . '_' . $admin_user_id, ''),
'fi_aid_identifier' => $repository_hierarchy->getPreference(RepositoryHierarchy::PREF_FINDING_AID_IDENTIFIER . $tree->id() . '_' . $repository_xref . '_' . $admin_user_id, ''),
'fi_aid_url' => $repository_hierarchy->getPreference(RepositoryHierarchy::PREF_FINDING_AID_URL . $tree->id() . '_' . $repository_xref . '_' . $admin_user_id, ''),
'fi_aid_publisher' => $repository_hierarchy->getPreference(RepositoryHierarchy::PREF_FINDING_AID_PUBLISHER . $tree->id() . '_' . $repository_xref . '_' . $admin_user_id, ''),
'show_load_from_admin' => false,
]
),
]
);
}
//Save received values to preferences
$this->savePreferences($request, false);
//If user is administrator, also save received values to preferences as administrator
if (Auth::isManager($tree, $user)) {
$this->savePreferences($request, true);
}
if ($params['show_load_from_admin']) {
return response(
[
'html' => view(
RepositoryHierarchy::viewsNamespace() . '::modals/message',
[
'title' => I18N::translate('EAD XML settings'),
'text' => I18N::translate('The EAD XML settings have been changed'),
]
)
]
);
} else {
return response();
}
}
/**
* Save preferences
*
* @param ServerRequestInterface $request
* @param bool $save_as_admin
*
* @return void
*/
private function savePreferences(ServerRequestInterface $request, bool $save_as_admin)
{
$tree = Validator::attributes($request)->tree();
$repository_xref = Validator::attributes($request)->string('xref');
$user = Validator::attributes($request)->user();
$finding_aid_title = Validator::parsedBody($request)->string('finding_aid_title', '');
$country_code = Validator::parsedBody($request)->string('country_code', '');
$main_agency_code = Validator::parsedBody($request)->string('main_agency_code', '');
$finding_aid_identifier = Validator::parsedBody($request)->string('finding_aid_identifier', '');
$finding_aid_url = Validator::parsedBody($request)->string('finding_aid_url', '');
$finding_aid_publisher = Validator::parsedBody($request)->string('finding_aid_publisher', '');
if ($save_as_admin) {
$user_id = RepositoryHierarchy::ADMIN_USER_STRING;
} else {
$user_id = $user->id();
}
$repository_hierarchy = $this->module_service->findByName(RepositoryHierarchy::activeModuleName());
//Save received values to preferences
$repository_hierarchy->setPreference(RepositoryHierarchy::PREF_FINDING_AID_TITLE . $tree->id() . '_' . $repository_xref . '_' . $user_id, $finding_aid_title);
$repository_hierarchy->setPreference(RepositoryHierarchy::PREF_COUNTRY_CODE . $tree->id() . '_' . $repository_xref . '_' . $user_id, $country_code);
$repository_hierarchy->setPreference(RepositoryHierarchy::PREF_MAIN_AGENCY_CODE . $tree->id() . '_' . $repository_xref . '_' . $user_id, $main_agency_code);
$repository_hierarchy->setPreference(RepositoryHierarchy::PREF_FINDING_AID_IDENTIFIER . $tree->id() . '_' . $repository_xref . '_' . $user_id, $finding_aid_identifier);
$repository_hierarchy->setPreference(RepositoryHierarchy::PREF_FINDING_AID_URL . $tree->id() . '_' . $repository_xref . '_' . $user_id, $finding_aid_url);
$repository_hierarchy->setPreference(RepositoryHierarchy::PREF_FINDING_AID_PUBLISHER . $tree->id() . '_' . $repository_xref . '_' . $user_id, $finding_aid_publisher);
}
}