$usuario_origen, 'subject' => $subject, 'mensaje' => $mensaje, 'id_source' => get_notification_source_id('message'), 'timestamp' => get_system_time(), ] ); // Update URL // Update targets. if ($message_id !== false) { $ret = message_set_targets( $message_id, $target_users, $target_groups ); if ($ret === false) { // Failed to deliver messages. Erase message and show error. db_process_sql_delete( 'tmensajes', ['id_mensaje' => $message_id] ); return false; } } if ($return === false) { return false; } else { return true; } } /** * Deletes a private message * * @param integer $id_message Message to be deleted. * * @return boolean true when deleted, false in case of error */ function messages_delete_message(int $id_message) { global $config; // Check if user has grants to access the message. if (check_notification_readable($id_message) === false) { return false; } $utimestamp = time(); $ret = db_process_sql_update( 'tnotification_user', ['utimestamp_erased' => $utimestamp], [ 'id_mensaje' => $id_message, 'id_user' => $config['id_user'], ] ); if ($ret === 0) { // No previous updates. // Message available to user due group assignment. $ret = db_process_sql_insert( 'tnotification_user', [ 'id_mensaje' => $id_message, 'id_user' => $config['id_user'], 'utimestamp_erased' => $utimestamp, ] ); // Quick fix. Insertions returns 0. if ($ret !== false) { $ret = 1; } } return (bool) $ret; } /** * Marks a private message as read/unread * * @param integer $message_id The message to modify. * @param boolean $read To set unread pass 0, false or empty value. * * @return boolean true when marked, false in case of error */ function messages_process_read( int $message_id, bool $read=true ) { global $config; // Check if user has grants to read the message. if (check_notification_readable($message_id) === false) { return false; } if (empty($read)) { // Mark as unread. $utimestamp = null; } else { // Mark as read. $utimestamp = time(); } $ret = db_process_sql_update( 'tnotification_user', ['utimestamp_read' => $utimestamp], [ 'id_mensaje' => $message_id, 'id_user' => $config['id_user'], 'utimestamp_read' => null, ] ); if ($ret === 0) { // No previous updates. // Message available to user due group assignment. $ret = db_process_sql_insert( 'tnotification_user', [ 'id_mensaje' => $message_id, 'id_user' => $config['id_user'], 'utimestamp_read' => $utimestamp, ] ); } return (bool) $ret; } /** * Gets a private message * * This function abstracts the database backend so it can simply be * replaced with another system * * @param integer $message_id Message to be retrieved. * * @return mixed False if it doesn't exist or a filled array otherwise */ function messages_get_message(int $message_id) { global $config; // Check if user has grants to read the message. if (check_notification_readable($message_id) === false) { return false; } $sql = sprintf( 'SELECT *, nu.utimestamp_read > 0 as "read" FROM tmensajes tm LEFT JOIN tnotification_user nu ON nu.id_mensaje = tm.id_mensaje WHERE tm.id_mensaje=%d', $message_id ); $row = db_get_row_sql($sql); if (empty($row)) { return false; } $row['id_usuario_destino'] = $config['id_user']; return $row; } /** * Gets a sent message * * This function abstracts the database backend so it can simply be * replaced with another system * * @param integer $message_id Message to be retrieved. * * @return mixed False if it doesn't exist or a filled array otherwise */ function messages_get_message_sent(int $message_id) { global $config; $sql = sprintf( "SELECT id_usuario_origen, subject, mensaje, timestamp FROM tmensajes WHERE id_usuario_origen='%s' AND id_mensaje=%d", $config['id_user'], $message_id ); $row = db_get_row_sql($sql); if (empty($row)) { return false; } $targets = get_notification_targets($message_id); $row['id_usuario_destino'] = implode( ',', $targets['users'] ).','.implode( ',', $targets['groups'] ); return $row; } /** * Counts private messages * * @param string $user Target user. * @param boolean $incl_read Whether or not to include read messages. * * @return integer The number of messages this user has */ function messages_get_count( string $user='', bool $incl_read=false ) { if (empty($user)) { global $config; $user = $config['id_user']; } if (!empty($incl_read)) { // Do not filter. $read = ''; } else { // Retrieve only unread messages. $read = 'where t.read is null'; } $sql = sprintf( 'SELECT count(*) FROM ( SELECT tm.*, utimestamp_read > 0 as "read" FROM tmensajes tm LEFT JOIN tnotification_user nu ON tm.id_mensaje=nu.id_mensaje 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) ) t %s', $user, $user, $read ); return (int) db_get_sql($sql); } /** * Counts messages sent. * * @param string $user Target user. * * @return integer The number of messages this user has sent */ function messages_get_count_sent(string $user='') { if (empty($user)) { global $config; $user = $config['id_user']; } $sql = sprintf( "SELECT COUNT(*) FROM tmensajes WHERE id_usuario_origen='%s'", $user ); return (int) db_get_sql($sql); } /** * Get message overview in array * * @param string $order How to order them valid: * (status (default), subject, timestamp, sender). * @param string $order_dir Direction of order * (ASC = Ascending, DESC = Descending). * @param boolean $incl_read Include read messages in return. * * @return integer The number of messages this user has */ function messages_get_overview( string $order='status', string $order_dir='ASC', bool $incl_read=true ) { global $config; switch ($order) { case 'timestamp':{ } case 'sender':{ } case 'subject':{ } break; case 'status': default: $order = 'estado, timestamp'; break; } if ($order_dir != 'ASC') { $order .= ' DESC'; } if (!empty($incl_read)) { // Do not filter. $read = ''; } else { // Retrieve only unread messages. $read = 'where t.read is null'; } $sql = sprintf( 'SELECT * FROM ( SELECT tm.*, utimestamp_read > 0 as "read" FROM tmensajes tm LEFT JOIN tnotification_user nu ON tm.id_mensaje=nu.id_mensaje 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) ) t %s ORDER BY %s', $config['id_user'], $config['id_user'], $read, $order ); return db_get_all_rows_sql($sql); } /** * Get sent message overview in array * * @param string $order How to order them valid: * (status (default), subject, timestamp, sender). * @param string $order_dir Direction of order * (ASC = Ascending, DESC = Descending). * * @return integer The number of messages this user has */ function messages_get_overview_sent( string $order='timestamp', string $order_dir='ASC' ) { global $config; switch ($order) { case 'timestamp':{ } case 'sender':{ } case 'subject':{ } break; case 'status': default: $order = 'estado, timestamp'; break; } if ($order_dir != 'ASC') { $order .= ' DESC'; } return db_get_all_rows_field_filter( 'tmensajes', 'id_usuario_origen', $config['id_user'], $order ); }