Added functionallity to Add element on notification global config

Former-commit-id: b51cf2d5079019a69fd7c4344dfcff2bf38147db
This commit is contained in:
fermin831 2019-02-05 17:40:44 +01:00
parent b4f3e94b13
commit 7df3fe2dac
3 changed files with 95 additions and 8 deletions

View File

@ -28,8 +28,7 @@ if (! check_acl($config['id_user'], 0, 'PM') && ! is_user_admin($config['id_user
return;
}
// Actions
// AJAX
// AJAX actions.
if (get_parameter('get_selection_two_ways_form', 0)) {
$source_id = get_parameter('source_id', '');
$users = get_parameter('users', '');
@ -45,6 +44,22 @@ if (get_parameter('get_selection_two_ways_form', 0)) {
);
return;
}
if (get_parameter('add_source_to_database', 0)) {
$source_id = get_parameter('source_id', '');
$users = get_parameter('users', '');
$elements = get_parameter('elements', array());
$id = get_notification_source_id($source_id);
$res = $users === "users"
? notifications_add_users_to_source($id, $elements)
: notifications_add_group_to_source($id, $elements);
$result = array(
'result' => $res
);
echo json_encode($result);
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']);
@ -113,23 +128,32 @@ function notifications_disable_source(event) {
});
}
// Get index of two ways element dialog.
function notifications_two_ways_element_get_dialog (id, source_id) {
return 'global_config_notifications_dialog_add-' + id + '-' + source_id;
}
// Get index of two ways element form.
function notifications_two_ways_element_get_sufix (id, source_id) {
return 'multi-' + id + '-' + source_id;
}
// Open a dialog with selector of source elements.
function add_source_dialog(users, source_id) {
// Display the dialog
var dialog_id = 'global_config_notifications_dialog_add-' + users + '-' + source_id;
var dialog_id = notifications_two_ways_element_get_dialog(users, source_id);
// Clean id element.
var previous_dialog = document.getElementById(dialog_id);
if (previous_dialog !== null) previous_dialog.remove();
// Create or recreate the content.
var not_dialog = document.createElement('div');
not_dialog.setAttribute('class', 'global_config_notifications_dialog_add');
not_dialog.setAttribute('class', 'global_config_notifications_dialog_add_wrapper');
not_dialog.setAttribute('id', dialog_id);
document.body.appendChild(not_dialog);
$("#" + dialog_id).dialog({
resizable: false,
draggable: true,
modal: true,
dialogClass: "global_config_notifications_dialog_add_wrapper",
dialogClass: "global_config_notifications_dialog_add_full",
overlay: {
opacity: 0.5,
background: "black"
@ -153,7 +177,7 @@ function add_source_dialog(users, source_id) {
// Move from selected and not selected source elements.
function notifications_modify_two_ways_element (id, source_id, operation) {
var index_sufix = 'multi-' + id + '-' + source_id;
var index_sufix = notifications_two_ways_element_get_sufix (id, source_id);
var start_id = operation === 'add' ? 'all-' : 'selected-';
var end_id = operation !== 'add' ? 'all-' : 'selected-';
var select = document.getElementById(
@ -168,4 +192,36 @@ function notifications_modify_two_ways_element (id, source_id, operation) {
}
}
}
// Add elements to database and close dialog
function notifications_add_source_element_to_database(id, source_id) {
var index = 'selected-' + notifications_two_ways_element_get_sufix (id, source_id);
var select = document.getElementById(index);
var selected = [];
for (var i = select.options.length - 1; i >= 0; i--) {
selected.push(select.options[i].value);
}
jQuery.post ("ajax.php",
{"page" : "godmode/setup/setup_notifications",
"add_source_to_database" : 1,
"users" : id,
"source_id" : source_id,
"elements": selected
},
function (data, status) {
if (data.result) {
// Append to other element
var out_select = document.getElementById('multi-' + id + '-' + source_id);
for (var i = select.options.length - 1; i >= 0; i--) {
out_select.appendChild(select.options[i]);
}
// Close the dialog
$("#" + notifications_two_ways_element_get_dialog(id, source_id)).dialog("close");
} else {
console.log("Cannot update element.");
}
},
"json"
);
}
</script>

View File

@ -265,7 +265,8 @@ function notifications_add_group_to_source ($source_id, $groups) {
// Insert into database all groups passed
$res = true;
foreach ($groups as $group) {
$res = db_process_sql_insert(
if (empty($group)) continue;
$res = $res && db_process_sql_insert(
'tnotification_source_group',
array(
'id_group' => $group,
@ -275,6 +276,32 @@ function notifications_add_group_to_source ($source_id, $groups) {
return $res;
}
/**
* Insert a set of users to notification source
*
* @param int Source id
* @param array Id of users to be deleted
*
* @return bool True if success. False otherwise.
*/
function notifications_add_users_to_source ($source_id, $users) {
// Source id is mandatory
if (!isset($source_id)) return false;
// Insert into database all groups passed
$res = true;
foreach ($users as $user) {
if (empty($user)) continue;
$res = $res && db_process_sql_insert(
'tnotification_source_user',
array(
'id_user' => $user,
'id_source' => $source_id)
) !== false;
}
return $res;
}
/**
* Get the groups that not own to a source and, for that reason, they can be
* added to the source.
@ -433,12 +460,15 @@ function notifications_print_source_select_box($info_selec, $id, $source_id, $di
* @return string HTML with the select code.
*/
function notifications_print_two_ways_select($info_selec, $users, $source_id) {
$html_select = "<div class='global_config_notifications_dialog_add'>";
$html_select .= html_print_select(empty($info_selec) ? true : $info_selec, "all-multi-{$users}-{$source_id}[]", 0, false, '', '', true, true, true,'');
$html_select .= "<div class='global_config_notifications_two_ways_form_arrows'>";
$html_select .= html_print_image('images/darrowright.png', true, array('title' => $add_title, 'onclick' => "notifications_modify_two_ways_element('$users', '$source_id', 'add')"));
$html_select .= html_print_image('images/darrowleft.png', true, array('title' => $add_title, 'onclick' => "notifications_modify_two_ways_element('$users', '$source_id', 'remove')"));
$html_select .= "</div>";
$html_select .= html_print_select(true, "selected-multi-{$users}-{$source_id}[]", 0, false, '', '', true, true, true, '');
$html_select .= "</div>";
$html_select .= html_print_button(__('Add'), 'Add', false, "notifications_add_source_element_to_database('$users', '$source_id')", "class='sub add'", true);
return $html_select;
}

View File

@ -4965,7 +4965,8 @@ div#dialog_messages table th:last-child {
width: 99%;
}
.global-config-notification-single-selector :last-child {
.global-config-notification-single-selector :last-child,
.global_config_notifications_dialog_add_wrapper {
flex-direction: column;
display: flex;
justify-content: flex-end;