|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midgard.admin.asgard |
|
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
|
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use midcom\datamanager\datamanager; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Preferences interface |
|
14
|
|
|
* |
|
15
|
|
|
* @package midgard.admin.asgard |
|
16
|
|
|
*/ |
|
17
|
|
|
class midgard_admin_asgard_handler_preferences extends midcom_baseclasses_components_handler |
|
18
|
|
|
{ |
|
19
|
|
|
use midgard_admin_asgard_handler; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* User for the preferences page |
|
23
|
|
|
* |
|
24
|
|
|
* @var midcom_db_person |
|
25
|
|
|
*/ |
|
26
|
|
|
private $_person; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Process the UI information |
|
30
|
|
|
*/ |
|
31
|
2 |
|
private function _process_request_data(array &$data) |
|
32
|
|
|
{ |
|
33
|
2 |
|
$data['view_title'] = $this->_l10n->get('user preferences'); |
|
34
|
|
|
|
|
35
|
|
|
// Set the breadcrumb data |
|
36
|
2 |
|
$this->add_breadcrumb($this->router->generate('welcome'), $this->_l10n->get($this->_component)); |
|
37
|
2 |
|
$this->add_breadcrumb($this->router->generate('preferences'), $this->_l10n->get('user preferences')); |
|
38
|
|
|
|
|
39
|
2 |
|
if ($this->_person->guid !== midcom::get()->auth->user->guid) { |
|
40
|
1 |
|
$this->add_breadcrumb('', $this->_person->name); |
|
41
|
|
|
} |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Handle the preference request |
|
46
|
|
|
* |
|
47
|
|
|
* @param Request $request The request object |
|
48
|
|
|
* @param array $data The local request data. |
|
49
|
|
|
* @param string $guid The person GUID |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function _handler_preferences(Request $request, array &$data, $guid = null) |
|
52
|
|
|
{ |
|
53
|
2 |
|
midcom::get()->auth->require_valid_user(); |
|
54
|
|
|
|
|
55
|
2 |
|
if (isset($guid)) { |
|
56
|
1 |
|
$this->_person = new midcom_db_person($guid); |
|
57
|
|
|
} else { |
|
58
|
1 |
|
$this->_person = new midcom_db_person(midcom_connection::get_user()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Load the controller instance |
|
62
|
2 |
|
$data['controller'] = datamanager::from_schemadb($this->_config->get('schemadb_preferences')) |
|
63
|
2 |
|
->set_storage($this->_person) |
|
64
|
2 |
|
->get_controller(); |
|
65
|
|
|
|
|
66
|
2 |
|
$return_page = $request->query->get('return_uri', '__mfa/asgard/'); |
|
67
|
|
|
// Process the requested form |
|
68
|
2 |
|
switch ($data['controller']->handle($request)) { |
|
69
|
2 |
|
case 'save': |
|
70
|
|
|
midcom::get()->uimessages->add($this->_l10n->get($this->_component), $this->_l10n->get('preferences saved')); |
|
71
|
|
|
return new midcom_response_relocate($return_page); |
|
72
|
|
|
|
|
73
|
2 |
|
case 'cancel': |
|
74
|
|
|
midcom::get()->uimessages->add($this->_l10n->get($this->_component), $this->_l10n_midcom->get('cancelled')); |
|
75
|
|
|
return new midcom_response_relocate($return_page); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Load the common data |
|
79
|
2 |
|
$this->_process_request_data($data); |
|
80
|
2 |
|
return $this->get_response('midgard_admin_asgard_preferences'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the languages |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public static function get_languages() : array |
|
87
|
|
|
{ |
|
88
|
2 |
|
$lang_str = midcom::get()->i18n->get_current_language(); |
|
89
|
2 |
|
$languages = midcom::get()->i18n->list_languages(); |
|
90
|
|
|
|
|
91
|
2 |
|
if (!array_key_exists($lang_str, $languages)) { |
|
92
|
|
|
return $languages; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Initialize a new array for the current language |
|
96
|
|
|
$language = [ |
|
97
|
2 |
|
'' => midcom::get()->i18n->get_string('default setting') |
|
98
|
|
|
]; |
|
99
|
2 |
|
$language[$lang_str] = $languages[$lang_str]; |
|
100
|
|
|
|
|
101
|
|
|
// Remove the reference from the original array |
|
102
|
2 |
|
unset($languages[$lang_str]); |
|
103
|
|
|
|
|
104
|
|
|
// Join the arrays |
|
105
|
2 |
|
return array_merge($language, $languages); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* AJAX backend for saving data on the fly |
|
110
|
|
|
* |
|
111
|
|
|
* @param Request $request The request object |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function _handler_ajax(Request $request) |
|
114
|
|
|
{ |
|
115
|
1 |
|
$this->_person = new midcom_db_person(midcom_connection::get_user()); |
|
116
|
1 |
|
$this->_person->require_do('midgard:update'); |
|
117
|
|
|
|
|
118
|
|
|
// Patch for Midgard ACL problem of setting person's own parameters |
|
119
|
1 |
|
midcom::get()->auth->request_sudo('midgard.admin.asgard'); |
|
120
|
|
|
|
|
121
|
1 |
|
foreach ($request->request->all() as $key => $value) { |
|
122
|
|
|
if (is_array($value)) { |
|
123
|
|
|
$value = serialize($value); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (!$this->_person->set_parameter('midgard.admin.asgard:preferences', $key, $value)) { |
|
127
|
|
|
midcom::get()->uimessages->add($this->_l10n->get($this->_component), sprintf($this->_l10n->get('failed to save the preference for %s'), $this->_l10n->get($key))); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
debug_add("Added configuration key-value pair {$key} => {$value}"); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
midcom::get()->auth->drop_sudo(); |
|
134
|
1 |
|
return new midcom_response_json; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|