Added AJAX to user notification config window

Former-commit-id: 9ca09ae777d699588a76dfb619156d800a95895b
This commit is contained in:
fermin831 2019-02-07 12:22:50 +01:00
parent 6dbeaf6307
commit d5fb9f1051
2 changed files with 106 additions and 13 deletions

View File

@ -168,9 +168,9 @@ function check_notification_readable(int $id_message)
* *
* @return array with sources info * @return array with sources info
*/ */
function notifications_get_all_sources() function notifications_get_all_sources($filter=[])
{ {
return db_get_all_rows_in_table('tnotification_source'); return db_get_all_rows_filter('tnotification_source', $filter);
} }
@ -478,6 +478,27 @@ function notifications_get_user_label_status($source, $user, $label)
return notifications_build_user_enable_return($value, false); return notifications_build_user_enable_return($value, false);
} }
function notifications_set_user_label_status($source, $user, $label, $value) {
$source_info = notifications_get_all_sources(['id' => $source]);
if (!isset($source_info[0])
|| !$source_info[0]['enabled']
|| !$source_info[0][$label]
|| !$source_info[0]['user_editable']
) {
return false;
}
return (bool) db_process_sql_update(
'tnotification_source_user',
[$label => $value],
[
'id_user' => $user,
'id_source' => $source,
]
);
}
/** /**
* Print the notification ball to see unread messages * Print the notification ball to see unread messages
@ -634,6 +655,8 @@ function notifications_print_user_switch($source, $user, $label)
'name' => $label, 'name' => $label,
'value' => $status['status'], 'value' => $status['status'],
'disabled' => !$status['enabled'], 'disabled' => !$status['enabled'],
'class' => 'notifications-user-label_individual',
'id' => 'notifications-user-'.$source['id'].'-label-'.$label,
] ]
); );
} }

View File

@ -4,7 +4,6 @@
// ================================================== // ==================================================
// Copyright (c) 2005-2010 Artica Soluciones Tecnologicas // Copyright (c) 2005-2010 Artica Soluciones Tecnologicas
// Please see http://pandorafms.org for full contribution list // Please see http://pandorafms.org for full contribution list
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License // modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation for version 2. // as published by the Free Software Foundation for version 2.
@ -12,19 +11,39 @@
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// Load global vars // Load global vars
global $config; global $config;
// Includes. // Includes.
include_once ($config['homedir'] . '/include/functions_notifications.php'); require_once $config['homedir'].'/include/functions_notifications.php';
// Load the header // Load the header
require($config['homedir'] . "/operation/users/user_edit_header.php"); require $config['homedir'].'/operation/users/user_edit_header.php';
if (get_parameter('change_label', 0)) {
$label = get_parameter('label', '');
$source = get_parameter('source', 0);
$user = get_parameter('user', '');
$value = get_parameter('value', 0) ? 1 : 0;
// Update the label value.
ob_clean();
echo json_encode(
[
'result' => notifications_set_user_label_status(
$source,
$user,
$label,
$value
),
]
);
return;
}
// User notification table. It is just a wrapper. // User notification table. It is just a wrapper.
$table_content = new StdClass(); $table_content = new StdClass();
$table_content->data = array(); $table_content->data = [];
$table_content->width = '100%'; $table_content->width = '100%';
$table_content->id = 'user-notifications-wrapper'; $table_content->id = 'user-notifications-wrapper';
$table_content->class = 'databox filters'; $table_content->class = 'databox filters';
@ -33,20 +52,71 @@ $table_content->size[1] = '33%';
$table_content->size[2] = '33%'; $table_content->size[2] = '33%';
// Print the header. // Print the header.
$table_content->data[] = array ( $table_content->data[] = [
'', '',
__('Enable'), __('Enable'),
__('Also receive an email') __('Also receive an email'),
); ];
$sources = notifications_get_all_sources(); $sources = notifications_get_all_sources();
foreach ($sources as $source) { foreach ($sources as $source) {
$table_content->data[] = array( $table_content->data[] = [
$source['description'], $source['description'],
notifications_print_user_switch($source, $id, 'enabled'), notifications_print_user_switch($source, $id, 'enabled'),
notifications_print_user_switch($source, $id, 'also_mail'), notifications_print_user_switch($source, $id, 'also_mail'),
); ];
} }
html_print_table($table_content); html_print_table($table_content);
?> // Print id user to handle it on js.
html_print_input_hidden('id_user', $id);
?>
<script>
// Encapsulate the code
(function() {
function notifications_change_label(event) {
event.preventDefault();
var check = document.getElementById(event.target.id);
if (check === null) return;
var match = /notifications-user-([0-9]+)-label-(.*)/
.exec(event.target.id);
jQuery.post ("ajax.php",
{
"page" : "operation/users/user_edit_notifications",
"change_label" : 1,
"label" : match[2],
"source" : match[1],
"user" : document.getElementById('hidden-id_user').value,
"value": check.checked ? 1 : 0
},
function (data, status) {
if (!data.result) {
console.error("Error changing configuration in database.");
} else {
check.checked = !check.checked;
}
},
"json"
).done(function(m){})
.fail(function(xhr, textStatus, errorThrown){
console.error(
"Cannot change configuration in database. Server error.",
xhr.responseText
);
});
}
var all_labels = document.getElementsByClassName(
'notifications-user-label_individual'
);
for (var i = 0; i < all_labels.length; i++) {
all_labels[i].addEventListener(
'click', notifications_change_label, false
);
}
}());
</script>