Change the global configuration notification to AJAX

Former-commit-id: a9017377c8ed6780170c0f418ec3e9bc39f7782c
This commit is contained in:
fermin831 2019-02-07 17:53:09 +01:00
parent fb814680db
commit 7fa344c2a4
2 changed files with 123 additions and 49 deletions

View File

@ -56,36 +56,31 @@ if (get_parameter('remove_source_on_database', 0)) {
return;
}
// Form actions.
if (get_parameter('update_config', 0)) {
$res_global = array_reduce(
notifications_get_all_sources(),
function ($carry, $source) {
$id = notifications_desc_to_id($source['description']);
if (empty($id)) {
return false;
}
$source = (int) get_parameter('source', 0);
$element = (string) get_parameter('element', '');
$value = (int) get_parameter('value', 0);
$enable_value = switch_to_int(get_parameter("enable-$id"));
$mail_value = (int) get_parameter("mail-{$id}", 0);
$user_value = (int) get_parameter("user-{$id}", 0);
$postpone_value = (int) get_parameter("postpone-{$id}", 0);
$all_users = (int) get_parameter("all-{$id}", 0);
$res = db_process_sql_update(
// Update the label value.
ob_clean();
$res = false;
switch ($element) {
// All users has other action.
case 'all_users':
$res = $value ? notifications_add_group_to_source($source, [0]) : notifications_remove_group_from_source($source, [0]);
break;
default:
$res = (bool) db_process_sql_update(
'tnotification_source',
[
'enabled' => $enable_value,
'user_editable' => $user_value,
'also_mail' => $mail_value,
'max_postpone_time' => $postpone_value,
],
['id' => $source['id']]
[$element => $value],
['id' => $source]
);
$all_users_res = $all_users ? notifications_add_group_to_source($source['id'], [0]) : notifications_remove_group_from_source($source['id'], [0]);
return $all_users_res && $res && $carry;
},
true
);
break;
}
echo json_encode(['result' => $res]);
return;
}
// Notification table. It is just a wrapper.
@ -103,22 +98,11 @@ $table_content->data = array_map(
},
notifications_get_all_sources()
);
$table_content->data[] = html_print_submit_button(
__('Update'),
'update_button',
false,
'class="sub upd" style="display: flex; "',
true
);
echo '<form id="form_enable" method="post">';
html_print_input_hidden('update_config', 1);
html_print_table($table_content);
echo '</form>';
?>
<script>
// Get the source id
function notifications_get_source_id(id) {
var matched = id.match(/.*-(.*)/);
@ -268,4 +252,92 @@ function remove_source_elements(id, source_id) {
"json"
);
}
function notifications_handle_change_element(event) {
event.preventDefault();
var match = /nt-([0-9]+)-(.*)/.exec(event.target.id);
if (!match) {
console.error(
"Cannot handle change element. Id not valid: ", event.target.id
);
return;
}
var action = {source: match[1], bit: match[2]};
var element = document.getElementById(event.target.id);
if (element === null) {
console.error(
"Cannot get element. Id: ", event.target.id
);
return;
}
var value;
switch (action.bit) {
case 'enabled':
case 'also_mail':
case 'user_editable':
case 'all_users':
value = element.checked ? 1 : 0;
break;
case 'max_postpone_time':
value = element.value;
break;
default:
console.error("Unregonized action", action.bit, '.');
return;
}
jQuery.post ("ajax.php",
{
"page" : "godmode/setup/setup_notifications",
"update_config" : 1,
"source" : match[1],
"element" : match[2],
"value": value
},
function (data, status) {
if (!data.result) {
console.error("Error changing configuration in database.");
} else {
switch (action.bit) {
case 'enabled':
case 'also_mail':
case 'user_editable':
case 'all_users':
element.checked = !element.checked;
break;
case 'max_postpone_time':
value = element.value;
break;
default:
console.error("Unregonized action (insert on db)", action.bit, '.');
return;
}
}
},
"json"
)
.done(function(m){})
.fail(function(xhr, textStatus, errorThrown){
console.error(
"Cannot change configuration in database. Server error.",
xhr.responseText
);
});
}
(function(){
// Add listener to all componentes marked
var all_clickables = document.getElementsByClassName('elem-clickable');
for (var i = 0; i < all_clickables.length; i++) {
all_clickables[i].addEventListener(
'click', notifications_handle_change_element, false
);
}
var all_changes = document.getElementsByClassName('elem-changeable');
for (var i = 0; i < all_changes.length; i++) {
all_changes[i].addEventListener(
'change', notifications_handle_change_element, false
);
}
})();
</script>

View File

@ -340,17 +340,15 @@ function notifications_remove_users_from_source($source_id, $users)
*/
function notifications_add_group_to_source($source_id, $groups)
{
// Source id is mandatory
// Source id is mandatory.
if (!isset($source_id)) {
return false;
}
// Insert into database all groups passed
// Insert into database all groups passed.
$res = true;
foreach ($groups as $group) {
if (empty($group)) {
continue;
}
if (!isset($group)) continue;
$res = $res && db_process_sql_insert(
'tnotification_source_group',
@ -560,11 +558,13 @@ function notifications_print_ball()
*/
function notifications_print_global_source_configuration($source)
{
// Get some values to generate the title
// Get some values to generate the title.
$id = notifications_desc_to_id($source['description']);
$switch_values = [
'name' => 'enable-'.$id,
'value' => $source['enabled'],
'id' => 'nt-'.$source['id'].'-enabled',
'class' => 'elem-clickable',
];
// Search if group all is set and handle that situation
@ -585,17 +585,16 @@ function notifications_print_global_source_configuration($source)
$html_selectors .= notifications_print_source_select_box(notifications_get_user_sources_for_select($source['id']), 'users', $id, $is_group_all);
$html_selectors .= notifications_print_source_select_box($source_groups, 'groups', $id, $is_group_all);
$html_selectors .= '</div>';
// Generate the checkboxes and time select
$html_checkboxes = "<div class='global-config-notification-checkboxes'>";
$html_checkboxes .= ' <span>';
$html_checkboxes .= html_print_checkbox("all-$id", 1, $is_group_all, true, false, 'notifications_disable_source(event)');
$html_checkboxes .= html_print_checkbox_extended("all-$id", 1, $is_group_all, false, '', 'class= "elem-clickable"', true, 'id="nt-'.$source['id'].'-all_users"');
$html_checkboxes .= __('Notify all users');
$html_checkboxes .= ' </span><br><span>';
$html_checkboxes .= html_print_checkbox("mail-$id", 1, $source['also_mail'], true);
$html_checkboxes .= html_print_checkbox_extended("mail-$id", 1, $source['also_mail'], false, '', 'class= "elem-clickable"', true, 'id="nt-'.$source['id'].'-also_mail"');
$html_checkboxes .= __('Also email users with notification content');
$html_checkboxes .= ' </span><br><span>';
$html_checkboxes .= html_print_checkbox("user-$id", 1, $source['user_editable'], true);
$html_checkboxes .= html_print_checkbox_extended("user-$id", 1, $source['user_editable'], false, '', 'class= "elem-clickable"', true, 'id="nt-'.$source['id'].'-user_editable"');
$html_checkboxes .= __('Users can modify notification preferences');
$html_checkboxes .= ' </span>';
$html_checkboxes .= '</div>';
@ -613,12 +612,15 @@ function notifications_print_global_source_configuration($source)
SECONDS_1MONTH => __('1 month'),
NOTIFICATIONS_POSTPONE_FOREVER => __('forever'),
],
"postpone-{$id}",
'nt-'.$source['id'].'-max_postpone_time',
$source['max_postpone_time'],
'',
'',
0,
true
true,
false,
true,
'elem-changeable'
);
// Return all html