2019-01-30 11:56:47 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2019-01-30 13:29:46 +01:00
|
|
|
* Library. Notification system auxiliary functions.
|
2019-01-30 11:56:47 +01:00
|
|
|
*
|
2019-01-30 13:29:46 +01:00
|
|
|
* @category Library
|
2019-01-30 11:56:47 +01:00
|
|
|
* @package Pandora FMS
|
|
|
|
* @subpackage Community
|
|
|
|
* @version 1.0.0
|
|
|
|
* @license See below
|
|
|
|
*
|
|
|
|
* ______ ___ _______ _______ ________
|
|
|
|
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
|
|
|
|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
|
|
|
|
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
|
|
|
|
*
|
|
|
|
* ============================================================================
|
|
|
|
* Copyright (c) 2005-2019 Artica Soluciones Tecnologicas
|
|
|
|
* Please see http://pandorafms.org for full contribution list
|
|
|
|
* 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 for version 2.
|
|
|
|
* 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.
|
|
|
|
* ============================================================================
|
|
|
|
*/
|
|
|
|
|
2019-01-31 20:11:43 +01:00
|
|
|
define('NOTIFICATIONS_POSTPONE_FOREVER', -1);
|
2019-01-30 11:56:47 +01:00
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-01-30 11:56:47 +01:00
|
|
|
/**
|
|
|
|
* Retrieves source ID for given source.
|
|
|
|
*
|
|
|
|
* @param string $source Source.
|
|
|
|
*
|
|
|
|
* @return integer source's id.
|
|
|
|
*/
|
|
|
|
function get_notification_source_id(string $source)
|
|
|
|
{
|
|
|
|
if (empty($source) === true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return db_get_value_sql(
|
2019-01-31 19:13:51 +01:00
|
|
|
"SELECT id
|
|
|
|
FROM `tnotification_source`
|
|
|
|
WHERE `description` LIKE '{$source}%'"
|
2019-01-30 11:56:47 +01:00
|
|
|
);
|
|
|
|
}
|
2019-01-30 13:29:46 +01:00
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-01-30 13:29:46 +01:00
|
|
|
/**
|
|
|
|
* Retrieve all targets for given message.
|
|
|
|
*
|
|
|
|
* @param integer $id_message Message id.
|
|
|
|
*
|
|
|
|
* @return array of users and groups target of this message.
|
|
|
|
*/
|
|
|
|
function get_notification_targets(int $id_message)
|
|
|
|
{
|
|
|
|
$targets = [
|
|
|
|
'users' => [],
|
|
|
|
'groups' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
if (empty($id_message)) {
|
|
|
|
return $targets;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret = db_get_all_rows_sql(
|
|
|
|
sprintf(
|
|
|
|
'SELECT id_user
|
|
|
|
FROM tnotification_user nu
|
|
|
|
WHERE nu.id_mensaje = %d',
|
|
|
|
$id_message
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (is_array($ret)) {
|
|
|
|
foreach ($ret as $row) {
|
2019-01-30 17:33:31 +01:00
|
|
|
array_push(
|
|
|
|
$targets['users'],
|
|
|
|
get_user_fullname($row['id_user'])
|
|
|
|
);
|
2019-01-30 13:29:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 17:33:31 +01:00
|
|
|
$ret = db_get_all_rows_sql(
|
2019-01-30 13:29:46 +01:00
|
|
|
sprintf(
|
2019-01-30 17:33:31 +01:00
|
|
|
'SELECT COALESCE(tg.nombre,ng.id_group) as "id_group"
|
2019-01-30 13:29:46 +01:00
|
|
|
FROM tnotification_group ng
|
2019-01-30 17:33:31 +01:00
|
|
|
LEFT JOIN tgrupo tg
|
|
|
|
ON tg.id_grupo=ng.id_group
|
2019-01-30 13:29:46 +01:00
|
|
|
WHERE ng.id_mensaje = %d',
|
|
|
|
$id_message
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (is_array($ret)) {
|
|
|
|
foreach ($ret as $row) {
|
2019-01-30 17:33:31 +01:00
|
|
|
if ($row['id_group'] == '0') {
|
|
|
|
$row['id_group'] = '<b>'.__('All').'</b>';
|
|
|
|
}
|
|
|
|
|
2019-01-30 13:29:46 +01:00
|
|
|
array_push($targets['groups'], $row['id_group']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $targets;
|
|
|
|
}
|
2019-01-30 17:33:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if current user has grants to read this notification
|
|
|
|
*
|
|
|
|
* @param integer $id_message Target message.
|
|
|
|
*
|
|
|
|
* @return boolean true, read available. False if not.
|
|
|
|
*/
|
|
|
|
function check_notification_readable(int $id_message)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if (empty($id_message)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT tm.*, utimestamp_read > 0 as "read" FROM tmensajes tm
|
|
|
|
LEFT JOIN tnotification_user nu
|
|
|
|
ON tm.id_mensaje=nu.id_mensaje
|
|
|
|
AND tm.id_mensaje=%d
|
|
|
|
LEFT JOIN (tnotification_group ng
|
|
|
|
INNER JOIN tusuario_perfil up
|
|
|
|
ON ng.id_group=up.id_grupo
|
|
|
|
AND up.id_grupo=ng.id_group
|
|
|
|
) ON tm.id_mensaje=ng.id_mensaje
|
|
|
|
WHERE utimestamp_erased is null
|
|
|
|
AND (up.id_usuario="%s" OR nu.id_user="%s" OR ng.id_group=0)',
|
|
|
|
$id_message,
|
|
|
|
$config['id_user'],
|
|
|
|
$config['id_user']
|
|
|
|
);
|
|
|
|
|
|
|
|
return (bool) db_get_value_sql($sql);
|
|
|
|
}
|
2019-01-31 15:16:56 +01:00
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-01-31 19:13:51 +01:00
|
|
|
/**
|
|
|
|
* Return all info from tnotification_source
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param array $filter Filter to table tnotification_source.
|
|
|
|
*
|
2019-01-31 19:13:51 +01:00
|
|
|
* @return array with sources info
|
|
|
|
*/
|
2019-02-07 12:22:50 +01:00
|
|
|
function notifications_get_all_sources($filter=[])
|
2019-02-06 12:51:02 +01:00
|
|
|
{
|
2019-02-07 12:22:50 +01:00
|
|
|
return db_get_all_rows_filter('tnotification_source', $filter);
|
2019-01-31 19:13:51 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-01 13:53:22 +01:00
|
|
|
/**
|
|
|
|
* Return the user sources to be inserted into a select
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param integer $source_id Source database identificator.
|
2019-02-01 13:53:22 +01:00
|
|
|
*
|
|
|
|
* @return array with the user id in keys and user id in value too
|
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_get_user_sources_for_select($source_id)
|
2019-02-06 18:20:26 +01:00
|
|
|
{
|
|
|
|
$users = notifications_get_user_sources(
|
|
|
|
['id_source' => $source_id],
|
|
|
|
['id_user']
|
|
|
|
);
|
|
|
|
|
|
|
|
return index_array($users, 'id_user', 'id_user');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user sources
|
|
|
|
*
|
|
|
|
* @param array $filter Filter of sql query.
|
|
|
|
* @param array $fields Fields to get of query.
|
|
|
|
*
|
|
|
|
* @return array Array with user sources data.
|
|
|
|
*/
|
|
|
|
function notifications_get_user_sources($filter=[], $fields=[])
|
2019-02-06 12:51:02 +01:00
|
|
|
{
|
2019-02-01 13:46:58 +01:00
|
|
|
$users = db_get_all_rows_filter(
|
|
|
|
'tnotification_source_user',
|
2019-02-06 18:20:26 +01:00
|
|
|
$filter,
|
|
|
|
$fields
|
2019-02-01 13:46:58 +01:00
|
|
|
);
|
2019-02-06 18:20:26 +01:00
|
|
|
// If fails or no one is selected, return empty array.
|
2019-02-06 12:51:02 +01:00
|
|
|
if ($users === false) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-02-01 13:46:58 +01:00
|
|
|
|
2019-02-06 18:20:26 +01:00
|
|
|
return $users;
|
2019-02-01 13:46:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-01 13:53:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the groups sources to be inserted into a select
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param integer $source_id Source database identificator.
|
2019-02-01 13:53:22 +01:00
|
|
|
*
|
|
|
|
* @return array with the group id in keys and group name in value
|
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_get_group_sources_for_select($source_id)
|
|
|
|
{
|
|
|
|
$groups = notifications_get_group_sources(
|
|
|
|
['id_source' => $source_id],
|
|
|
|
['id_group']
|
2019-02-05 13:36:16 +01:00
|
|
|
);
|
|
|
|
return index_array($groups, 'id_group', 'name');
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 13:36:16 +01:00
|
|
|
/**
|
|
|
|
* Get the group sources
|
|
|
|
*
|
|
|
|
* @param array $filter Filter of sql query.
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param array $fields Fields retrieved.
|
|
|
|
*
|
|
|
|
* @return array With the group info
|
2019-02-05 13:36:16 +01:00
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_get_group_sources($filter=[], $fields=[])
|
|
|
|
{
|
2019-02-05 13:36:16 +01:00
|
|
|
// Get only the tnotifications_source_group fields in addition to group name.
|
2019-02-06 12:51:02 +01:00
|
|
|
if (empty($fields)) {
|
|
|
|
$fields[] = 'tnsg.*';
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = array_map(
|
|
|
|
function ($field) {
|
|
|
|
if (!preg_match('/^tnsg./', $field)) {
|
|
|
|
$field = "tnsg.{$field}";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
},
|
|
|
|
$fields
|
|
|
|
);
|
2019-02-05 13:36:16 +01:00
|
|
|
|
|
|
|
// Get groups.
|
2019-02-01 14:48:52 +01:00
|
|
|
$groups = db_get_all_rows_filter(
|
2019-02-01 13:46:58 +01:00
|
|
|
'tnotification_source_group tnsg
|
2019-02-01 14:48:52 +01:00
|
|
|
LEFT JOIN tgrupo tg ON tnsg.id_group = tg.id_grupo',
|
2019-02-05 13:36:16 +01:00
|
|
|
$filter,
|
2019-02-06 12:51:02 +01:00
|
|
|
array_merge($fields, ['IFNULL(tg.nombre, "All") AS name'])
|
2019-02-01 13:46:58 +01:00
|
|
|
);
|
2019-02-05 13:36:16 +01:00
|
|
|
|
2019-02-01 13:46:58 +01:00
|
|
|
// If fails or no one is selected, return empty array
|
2019-02-06 12:51:02 +01:00
|
|
|
if ($groups === false) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:36:16 +01:00
|
|
|
return $groups;
|
2019-02-01 13:46:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-04 10:41:35 +01:00
|
|
|
/**
|
|
|
|
* Delete a set of groups from notification source
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param integer $source_id Source id.
|
|
|
|
* @param array $groups Id of groups to be deleted.
|
2019-02-04 10:41:35 +01:00
|
|
|
*
|
2019-02-06 12:51:02 +01:00
|
|
|
* @return boolean True if success. False otherwise.
|
2019-02-04 10:41:35 +01:00
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_remove_group_from_source($source_id, $groups)
|
|
|
|
{
|
2019-02-04 10:41:35 +01:00
|
|
|
// Source id is mandatory
|
2019-02-06 12:51:02 +01:00
|
|
|
if (!isset($source_id)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-04 10:41:35 +01:00
|
|
|
|
|
|
|
// Delete from database
|
2019-02-06 12:51:02 +01:00
|
|
|
return db_process_sql_delete(
|
2019-02-04 10:41:35 +01:00
|
|
|
'tnotification_source_group',
|
2019-02-06 12:51:02 +01:00
|
|
|
[
|
|
|
|
'id_group' => $groups,
|
|
|
|
'id_source' => $source_id,
|
|
|
|
]
|
2019-02-04 10:41:35 +01:00
|
|
|
) !== false;
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 18:14:10 +01:00
|
|
|
/**
|
|
|
|
* Delete a set of users from notification source
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param integer $source_id Source id.
|
|
|
|
* @param array $users Id of users to be deleted.
|
2019-02-05 18:14:10 +01:00
|
|
|
*
|
2019-02-06 12:51:02 +01:00
|
|
|
* @return boolean True if success. False otherwise.
|
2019-02-05 18:14:10 +01:00
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_remove_users_from_source($source_id, $users)
|
|
|
|
{
|
2019-02-05 18:14:10 +01:00
|
|
|
// Source id is mandatory
|
2019-02-06 12:51:02 +01:00
|
|
|
if (!isset($source_id)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-05 18:14:10 +01:00
|
|
|
|
|
|
|
// Delete from database
|
2019-02-06 12:51:02 +01:00
|
|
|
return db_process_sql_delete(
|
2019-02-05 18:14:10 +01:00
|
|
|
'tnotification_source_user',
|
2019-02-06 12:51:02 +01:00
|
|
|
[
|
|
|
|
'id_user' => $users,
|
|
|
|
'id_source' => $source_id,
|
|
|
|
]
|
2019-02-05 18:14:10 +01:00
|
|
|
) !== false;
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-04 10:41:35 +01:00
|
|
|
/**
|
|
|
|
* Insert a set of groups to notification source
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param integer $source_id Source id.
|
|
|
|
* @param array $groups Id of groups to be deleted.
|
2019-02-04 10:41:35 +01:00
|
|
|
*
|
2019-02-06 12:51:02 +01:00
|
|
|
* @return boolean True if success. False otherwise.
|
2019-02-04 10:41:35 +01:00
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_add_group_to_source($source_id, $groups)
|
|
|
|
{
|
2019-02-07 17:53:09 +01:00
|
|
|
// Source id is mandatory.
|
2019-02-06 12:51:02 +01:00
|
|
|
if (!isset($source_id)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-04 10:41:35 +01:00
|
|
|
|
2019-02-07 17:53:09 +01:00
|
|
|
// Insert into database all groups passed.
|
2019-02-04 10:41:35 +01:00
|
|
|
$res = true;
|
|
|
|
foreach ($groups as $group) {
|
2019-02-07 18:04:14 +01:00
|
|
|
if (!isset($group)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 17:40:44 +01:00
|
|
|
$res = $res && db_process_sql_insert(
|
2019-02-04 10:41:35 +01:00
|
|
|
'tnotification_source_group',
|
2019-02-06 12:51:02 +01:00
|
|
|
[
|
|
|
|
'id_group' => $group,
|
|
|
|
'id_source' => $source_id,
|
|
|
|
]
|
|
|
|
) !== false;
|
2019-02-04 10:41:35 +01:00
|
|
|
}
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-04 10:41:35 +01:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 17:40:44 +01:00
|
|
|
/**
|
|
|
|
* Insert a set of users to notification source
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param integer $source_id Source id.
|
|
|
|
* @param array $users Id of users to be deleted.
|
2019-02-05 17:40:44 +01:00
|
|
|
*
|
2019-02-06 12:51:02 +01:00
|
|
|
* @return boolean True if success. False otherwise.
|
2019-02-05 17:40:44 +01:00
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_add_users_to_source($source_id, $users)
|
|
|
|
{
|
2019-02-05 17:40:44 +01:00
|
|
|
// Source id is mandatory
|
2019-02-06 12:51:02 +01:00
|
|
|
if (!isset($source_id)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-05 17:40:44 +01:00
|
|
|
|
|
|
|
// Insert into database all groups passed
|
|
|
|
$res = true;
|
2019-02-06 18:20:26 +01:00
|
|
|
$also_mail = db_get_value(
|
|
|
|
'also_mail',
|
|
|
|
'tnotification_source',
|
|
|
|
'id',
|
|
|
|
$source_id
|
|
|
|
);
|
2019-02-05 17:40:44 +01:00
|
|
|
foreach ($users as $user) {
|
2019-02-06 12:51:02 +01:00
|
|
|
if (empty($user)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:40:44 +01:00
|
|
|
$res = $res && db_process_sql_insert(
|
|
|
|
'tnotification_source_user',
|
2019-02-06 12:51:02 +01:00
|
|
|
[
|
|
|
|
'id_user' => $user,
|
|
|
|
'id_source' => $source_id,
|
2019-02-06 18:20:26 +01:00
|
|
|
'enabled' => 1,
|
|
|
|
'also_mail' => (int) $also_mail,
|
2019-02-06 12:51:02 +01:00
|
|
|
]
|
|
|
|
) !== false;
|
2019-02-05 17:40:44 +01:00
|
|
|
}
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 17:40:44 +01:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 13:36:16 +01:00
|
|
|
/**
|
|
|
|
* Get the groups that not own to a source and, for that reason, they can be
|
|
|
|
* added to the source.
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param integer $source_id Source id.
|
|
|
|
*
|
2019-02-05 13:36:16 +01:00
|
|
|
* @return array Indexed by id group all selectable groups.
|
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_get_group_source_not_configured($source_id)
|
|
|
|
{
|
2019-02-05 13:36:16 +01:00
|
|
|
$groups_selected = notifications_get_group_sources_for_select($source_id);
|
2019-02-06 12:51:02 +01:00
|
|
|
$all_groups = users_get_groups_for_select(false, 'AR', false, true, $groups_selected);
|
2019-02-05 13:36:16 +01:00
|
|
|
return array_diff($all_groups, $groups_selected);
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 13:36:16 +01:00
|
|
|
/**
|
|
|
|
* Get the users that not own to a source and, for that reason, they can be
|
|
|
|
* added to the source.
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param integer $source_id Source id.
|
|
|
|
*
|
2019-02-05 13:36:16 +01:00
|
|
|
* @return array Indexed by id user, all selectable users.
|
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_get_user_source_not_configured($source_id)
|
|
|
|
{
|
2019-02-05 13:36:16 +01:00
|
|
|
$users_selected = array_keys(notifications_get_user_sources_for_select($source_id));
|
|
|
|
$users = get_users(
|
|
|
|
'id_user',
|
2019-02-06 12:51:02 +01:00
|
|
|
['!id_user' => $users_selected],
|
|
|
|
['id_user']
|
2019-02-05 13:36:16 +01:00
|
|
|
);
|
|
|
|
return index_array($users, 'id_user', 'id_user');
|
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-07 12:57:51 +01:00
|
|
|
/**
|
|
|
|
* Build a data struct to handle the value of a label
|
|
|
|
*
|
|
|
|
* @param mixed $status Status value.
|
|
|
|
* @param mixed $enabled Enabled value.
|
|
|
|
*
|
|
|
|
* @return array with status (1|0) and enabled (1|0)
|
|
|
|
*/
|
2019-02-06 18:20:26 +01:00
|
|
|
function notifications_build_user_enable_return($status, $enabled)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'status' => ((bool) $status === true) ? 1 : 0,
|
|
|
|
'enabled' => ((bool) $enabled === true) ? 1 : 0,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-07 12:57:51 +01:00
|
|
|
/**
|
|
|
|
* Get user label (enabled, also_mail...) status.
|
|
|
|
*
|
|
|
|
* @param integer $source Id of notification source.
|
|
|
|
* @param string $user User id.
|
|
|
|
* @param string $label Label id (enabled, also_email...).
|
|
|
|
*
|
|
|
|
* @return array Return of notifications_build_user_enable_return.
|
|
|
|
*/
|
2019-02-06 18:20:26 +01:00
|
|
|
function notifications_get_user_label_status($source, $user, $label)
|
|
|
|
{
|
|
|
|
// If not enabled, it cannot be modificable.
|
|
|
|
if (!$source['enabled'] || !$source[$label]) {
|
|
|
|
return notifications_build_user_enable_return(false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// See at first for direct reference.
|
|
|
|
$user_source = notifications_get_user_sources(
|
|
|
|
[
|
|
|
|
'id_source' => $source['id'],
|
|
|
|
'id_user' => $user,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
if (!empty($user_source)) {
|
|
|
|
return notifications_build_user_enable_return(
|
|
|
|
isset($user_source[0][$label]) ? $user_source[0][$label] : false,
|
|
|
|
$source['user_editable']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$common_groups = array_intersect(
|
|
|
|
array_keys(users_get_groups($user)),
|
|
|
|
array_keys(
|
|
|
|
notifications_get_group_sources_for_select($source['id'])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
// No group found, return no permissions.
|
|
|
|
$value = empty($common_groups) ? false : $source[$label];
|
|
|
|
return notifications_build_user_enable_return($value, false);
|
|
|
|
}
|
|
|
|
|
2019-02-07 12:57:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the status to a single label on config of users notifications.
|
|
|
|
*
|
|
|
|
* @param integer $source Id of notification source.
|
|
|
|
* @param string $user User id.
|
|
|
|
* @param string $label Label id (enabled, also_email...).
|
|
|
|
* @param mixed $value Numeric value: 1 or 0.
|
|
|
|
*
|
|
|
|
* @return boolean True if success.
|
|
|
|
*/
|
|
|
|
function notifications_set_user_label_status($source, $user, $label, $value)
|
|
|
|
{
|
2019-02-07 12:22:50 +01:00
|
|
|
$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,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-06 18:20:26 +01:00
|
|
|
|
2019-01-31 15:16:56 +01:00
|
|
|
/**
|
2019-02-07 12:57:51 +01:00
|
|
|
* Print the notification ball to see unread messages.
|
2019-01-31 15:16:56 +01:00
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @return string with HTML code of notification ball.
|
2019-01-31 15:16:56 +01:00
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_print_ball()
|
|
|
|
{
|
2019-01-31 15:16:56 +01:00
|
|
|
$num_notifications = messages_get_count();
|
2019-02-06 12:51:02 +01:00
|
|
|
$class_status = $num_notifications == 0 ? 'notification-ball-no-messages' : 'notification-ball-new-messages';
|
|
|
|
return "<div class='notification-ball $class_status' id='notification-ball-header'>
|
2019-01-31 15:16:56 +01:00
|
|
|
$num_notifications
|
|
|
|
</div>";
|
|
|
|
}
|
2019-01-31 19:13:51 +01:00
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-01-31 19:13:51 +01:00
|
|
|
/**
|
|
|
|
* Print notification configuration global
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param array $source Notification source data.
|
2019-01-31 19:13:51 +01:00
|
|
|
*
|
|
|
|
* @return string with HTML of source configuration
|
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_print_global_source_configuration($source)
|
|
|
|
{
|
2019-02-07 17:53:09 +01:00
|
|
|
// Get some values to generate the title.
|
2019-02-06 12:51:02 +01:00
|
|
|
$switch_values = [
|
2019-02-07 18:04:14 +01:00
|
|
|
'name' => 'enable-'.$source['id'],
|
2019-02-06 12:51:02 +01:00
|
|
|
'value' => $source['enabled'],
|
2019-02-07 17:53:09 +01:00
|
|
|
'id' => 'nt-'.$source['id'].'-enabled',
|
|
|
|
'class' => 'elem-clickable',
|
2019-02-06 12:51:02 +01:00
|
|
|
];
|
2019-02-01 14:48:52 +01:00
|
|
|
|
|
|
|
// Search if group all is set and handle that situation
|
|
|
|
$source_groups = notifications_get_group_sources_for_select($source['id']);
|
2019-02-06 12:51:02 +01:00
|
|
|
$is_group_all = isset($source_groups['0']);
|
|
|
|
if ($is_group_all) {
|
|
|
|
unset($source_groups['0']);
|
|
|
|
}
|
2019-02-01 14:48:52 +01:00
|
|
|
|
2019-01-31 19:13:51 +01:00
|
|
|
// Generate the title
|
|
|
|
$html_title = "<div class='global-config-notification-title'>";
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_title .= html_print_switch($switch_values);
|
|
|
|
$html_title .= "<h2>{$source['description']}</h2>";
|
|
|
|
$html_title .= '</div>';
|
2019-01-31 19:13:51 +01:00
|
|
|
|
2019-02-01 13:46:58 +01:00
|
|
|
// Generate the html for title
|
|
|
|
$html_selectors = "<div class='global-config-notification-selectors'>";
|
2019-02-07 18:04:14 +01:00
|
|
|
$html_selectors .= notifications_print_source_select_box(notifications_get_user_sources_for_select($source['id']), 'users', $source['id'], $is_group_all);
|
|
|
|
$html_selectors .= notifications_print_source_select_box($source_groups, 'groups', $source['id'], $is_group_all);
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_selectors .= '</div>';
|
2019-01-31 19:43:13 +01:00
|
|
|
// Generate the checkboxes and time select
|
|
|
|
$html_checkboxes = "<div class='global-config-notification-checkboxes'>";
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_checkboxes .= ' <span>';
|
2019-02-07 18:04:14 +01:00
|
|
|
$html_checkboxes .= html_print_checkbox_extended('all-'.$source['id'], 1, $is_group_all, false, '', 'class= "elem-clickable"', true, 'id="nt-'.$source['id'].'-all_users"');
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_checkboxes .= __('Notify all users');
|
|
|
|
$html_checkboxes .= ' </span><br><span>';
|
2019-02-07 18:04:14 +01:00
|
|
|
$html_checkboxes .= html_print_checkbox_extended('mail-'.$source['id'], 1, $source['also_mail'], false, '', 'class= "elem-clickable"', true, 'id="nt-'.$source['id'].'-also_mail"');
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_checkboxes .= __('Also email users with notification content');
|
|
|
|
$html_checkboxes .= ' </span><br><span>';
|
2019-02-07 18:04:14 +01:00
|
|
|
$html_checkboxes .= html_print_checkbox_extended('user-'.$source['id'], 1, $source['user_editable'], false, '', 'class= "elem-clickable"', true, 'id="nt-'.$source['id'].'-user_editable"');
|
2019-02-06 18:20:26 +01:00
|
|
|
$html_checkboxes .= __('Users can modify notification preferences');
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_checkboxes .= ' </span>';
|
|
|
|
$html_checkboxes .= '</div>';
|
2019-01-31 19:43:13 +01:00
|
|
|
|
2019-01-31 20:11:43 +01:00
|
|
|
// Generate the select with the time
|
|
|
|
$html_select_pospone = __('Users can postpone notifications up to');
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_select_pospone .= html_print_select(
|
|
|
|
[
|
|
|
|
SECONDS_5MINUTES => __('5 minutes'),
|
|
|
|
SECONDS_15MINUTES => __('15 minutes'),
|
|
|
|
SECONDS_12HOURS => __('12 hours'),
|
|
|
|
SECONDS_1DAY => __('1 day'),
|
|
|
|
SECONDS_1WEEK => __('1 week'),
|
|
|
|
SECONDS_15DAYS => __('15 days'),
|
|
|
|
SECONDS_1MONTH => __('1 month'),
|
|
|
|
NOTIFICATIONS_POSTPONE_FOREVER => __('forever'),
|
|
|
|
],
|
2019-02-07 17:53:09 +01:00
|
|
|
'nt-'.$source['id'].'-max_postpone_time',
|
2019-01-31 20:11:43 +01:00
|
|
|
$source['max_postpone_time'],
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
0,
|
2019-02-07 17:53:09 +01:00
|
|
|
true,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
'elem-changeable'
|
2019-01-31 20:11:43 +01:00
|
|
|
);
|
|
|
|
|
2019-01-31 19:13:51 +01:00
|
|
|
// Return all html
|
2019-02-06 12:51:02 +01:00
|
|
|
return $html_title.$html_selectors.$html_checkboxes.$html_select_pospone;
|
2019-02-01 13:46:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-01 13:53:22 +01:00
|
|
|
/**
|
|
|
|
* Print select boxes of notified users or groups
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param array $info_selec All info required for build the selector.
|
|
|
|
* @param string $id One of users|groups.
|
|
|
|
* @param string $source_id Id of source.
|
|
|
|
* @param boolean $disabled Disable the selectors.
|
2019-02-01 13:53:22 +01:00
|
|
|
*
|
|
|
|
* @return string HTML with the generated selector
|
|
|
|
*/
|
2019-02-07 12:57:51 +01:00
|
|
|
function notifications_print_source_select_box(
|
|
|
|
$info_selec,
|
|
|
|
$id,
|
|
|
|
$source_id,
|
|
|
|
$disabled
|
|
|
|
) {
|
2019-02-06 12:51:02 +01:00
|
|
|
$title = $id == 'users' ? __('Notified users') : __('Notified groups');
|
|
|
|
$add_title = $id == 'users' ? __('Add users') : __('Add groups');
|
|
|
|
$delete_title = $id == 'users' ? __('Delete users') : __('Delete groups');
|
2019-02-01 13:46:58 +01:00
|
|
|
|
|
|
|
// Generate the HTML
|
|
|
|
$html_select = "<div class='global-config-notification-single-selector'>";
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_select .= ' <div>';
|
2019-02-01 13:46:58 +01:00
|
|
|
$html_select .= " <h4>$title</h4>";
|
2019-02-01 13:53:22 +01:00
|
|
|
// Put a true if empty sources to avoid to sow the 'None' value
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_select .= html_print_select(empty($info_selec) ? true : $info_selec, "multi-{$id}-{$source_id}[]", 0, false, '', '', true, true, true, '', $disabled);
|
|
|
|
$html_select .= ' </div>';
|
2019-02-01 13:46:58 +01:00
|
|
|
$html_select .= " <div class='global-notifications-icons'>";
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_select .= html_print_image('images/input_add.png', true, ['title' => $add_title, 'onclick' => "add_source_dialog('$id', '$source_id')"]);
|
|
|
|
$html_select .= html_print_image('images/input_delete.png', true, ['title' => $delete_title, 'onclick' => "remove_source_elements('$id', '$source_id')"]);
|
|
|
|
$html_select .= ' </div>';
|
|
|
|
$html_select .= '</div>';
|
2019-02-01 13:46:58 +01:00
|
|
|
return $html_select;
|
2019-01-31 19:13:51 +01:00
|
|
|
}
|
2019-02-05 13:36:16 +01:00
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 13:36:16 +01:00
|
|
|
/**
|
|
|
|
* Print the select with right and left arrows to select new sources
|
|
|
|
* (groups or users).
|
|
|
|
*
|
2019-02-07 12:57:51 +01:00
|
|
|
* @param array $info_selec Array with source info.
|
|
|
|
* @param string $users One of users|groups.
|
|
|
|
* @param source $source_id Source id.
|
|
|
|
*
|
2019-02-05 13:36:16 +01:00
|
|
|
* @return string HTML with the select code.
|
|
|
|
*/
|
2019-02-06 12:51:02 +01:00
|
|
|
function notifications_print_two_ways_select($info_selec, $users, $source_id)
|
|
|
|
{
|
2019-02-05 17:40:44 +01:00
|
|
|
$html_select = "<div class='global_config_notifications_dialog_add'>";
|
2019-02-06 12:51:02 +01:00
|
|
|
$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, ['title' => $add_title, 'onclick' => "notifications_modify_two_ways_element('$users', '$source_id', 'add')"]);
|
|
|
|
$html_select .= html_print_image('images/darrowleft.png', true, ['title' => $add_title, 'onclick' => "notifications_modify_two_ways_element('$users', '$source_id', 'remove')"]);
|
|
|
|
$html_select .= '</div>';
|
2019-02-05 13:36:16 +01:00
|
|
|
$html_select .= html_print_select(true, "selected-multi-{$users}-{$source_id}[]", 0, false, '', '', true, true, true, '');
|
2019-02-06 12:51:02 +01:00
|
|
|
$html_select .= '</div>';
|
2019-02-05 17:40:44 +01:00
|
|
|
$html_select .= html_print_button(__('Add'), 'Add', false, "notifications_add_source_element_to_database('$users', '$source_id')", "class='sub add'", true);
|
2019-02-05 13:36:16 +01:00
|
|
|
|
|
|
|
return $html_select;
|
|
|
|
}
|
2019-02-06 18:20:26 +01:00
|
|
|
|
|
|
|
|
2019-02-07 12:57:51 +01:00
|
|
|
/**
|
|
|
|
* Print a label status represented by a switch
|
|
|
|
*
|
|
|
|
* @param integer $source Source id.
|
|
|
|
* @param string $user User id.
|
|
|
|
* @param string $label Label (enabled, also_mail...).
|
|
|
|
*
|
|
|
|
* @return string With HTML code
|
|
|
|
*/
|
2019-02-06 18:20:26 +01:00
|
|
|
function notifications_print_user_switch($source, $user, $label)
|
|
|
|
{
|
|
|
|
$status = notifications_get_user_label_status($source, $user, $label);
|
|
|
|
return html_print_switch(
|
|
|
|
[
|
|
|
|
'name' => $label,
|
|
|
|
'value' => $status['status'],
|
|
|
|
'disabled' => !$status['enabled'],
|
2019-02-07 12:22:50 +01:00
|
|
|
'class' => 'notifications-user-label_individual',
|
|
|
|
'id' => 'notifications-user-'.$source['id'].'-label-'.$label,
|
2019-02-06 18:20:26 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|