Notifications for TorrentTrader v3 (AJAX/jQuery)
Requires: TTv3 (mysqli)
Using independent mysqli code for better control.


Import SQL
Create ajax_user_notifications.php & add to root
Open header.php and add
Add to themes/default/theme.css
Open /backend/functions.php and replace entire loggedinonly() function with
To send a notfication, add to /backend/functions.php
And use like this in pages/areas where another user is concerned (like cleanup, login, account updates, announcement, etc)
Requires: TTv3 (mysqli)
Using independent mysqli code for better control.


Import SQL
CREATE TABLE `notifications` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NULL DEFAULT NULL,
`message` VARCHAR(255) NULL DEFAULT NULL COLLATE 'latin1_swedish_ci',
`added` DATETIME NULL DEFAULT NULL,
`read` ENUM('yes','no') NULL DEFAULT 'no' COLLATE 'latin1_swedish_ci',
`title` VARCHAR(255) NULL DEFAULT NULL COLLATE 'latin1_swedish_ci',
PRIMARY KEY (`id`) USING BTREE
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
Create ajax_user_notifications.php & add to root
<?php
// TTv3 AJAX User Notifications
require_once 'backend/functions.php';
dbconn();
$db = new mysqli($mysql_host, $mysql_user, $mysql_pass, $mysql_db);
if ($db->connect_errno) {
die(json_encode(array(
'error' => $db->connect_error
)));
}
if (loggedinonly() !== false) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['id']) && intval($_POST['id']) > 0) {
if (isset($_POST['viewed']) && $_POST['viewed'] !== false) {
$read = $db->prepare("UPDATE notifications SET `read` = 'yes' WHERE id = ? AND user_id = ?") or die('SQL Query Error');
$read->bind_param('ii', intval($_POST['id']), $CURUSER['id']);
$read->execute();
if ($read) {
echo json_encode(array(
'read' => true
));
}
} else {
$delnotif = $db->prepare('DELETE FROM notifications WHERE id = ? AND user_id = ?') or die('SQL Query Error');
$delnotif->bind_param('ii', intval($_POST['id']), $CURUSER['id']);
$delnotif->execute();
if ($delnotif) {
echo json_encode(array(
'delete' => true
));
}
}
}
} else {
$notif = $db->prepare('SELECT id, user_id AS uid, message, added, `read`, title FROM notifications WHERE user_id = ?') or die('SQL Query Error');
$notif->bind_param('i', $CURUSER['id']);
$notif->execute();
$result = $notif->get_result();
$notification = array();
while ($row = $result->fetch_assoc()) {
$data['id'] = intval($row['id']);
$data['message'] = trim($row['message']);
$data['total'] = count($row);
$data['added'] = $row['added'];
$data['read'] = $row['read'];
$data['title'] = $row['title'];
array_push($notification, $data);
}
echo json_encode($notification, JSON_UNESCAPED_SLASHES);
$notif->free_result();
$db->close();
}
}
Open header.php and add
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script>
function truncateString(str, num) {
return (str.length > num) ? str.slice(0, num) + '...' : str;
}
function getSelected() {
return (window.getSelection) ? window.getSelection().toString() : document.getSelection().toString();
}
function closeNotificationDrop() {
$(document).on('click', '*', function (e) {
if (!$(e.target).is('.notification--bell')
&& !$(e.target).is('.notification--num')
&& !$(e.target).is('span#rate-notif-feature')
&& !$(e.target).is('div#back-notif-list')
&& !$(e.target).is('span#show-all-notif')
&& !$(e.target).is('#notif_container')
&& !$(e.target).is('#vote-actions')
&& !$(e.target).is('span#clean-all-notif')) {
$('#notif_container').hide();
//$('.notif').removeAttr('data-id');
}
});
}
// Get notification count
function fetchNotificationCount() {
$(function () {});
let total = 0;
const delay = 2000;
$('.notification--bell').css('animation-play-state', 'paused');
$('.notification--bellClapper').css('animation-play-state', 'paused');
if (!$.trim($('.notification--num').html()).length || parseInt($('.notification--num').text()) === total) {
$('.notification--bell').css('fill', '#D3D3D3');
$('.notification--bellClapper').css('fill', '#D3D3D3');
}
$.ajax({
url: 'ajax_user_notifications.php',
method: 'GET',
dataType: 'JSON',
success: function (data) {
$('.notification--bell').css('fill', '#FFF');
$('.notification--bellClapper').css('fill', '#FFF');
let html_to_append = '';
$.each(data, function (i, item) {
total = item.total;
});
if (total > 0) {
$('.notification--num').html(total);
if (!($('.notif').is('[data-id]'))) {
$('.notification--bell').css('animation-play-state', 'running');
$('.notification--bellClapper').css('animation-play-state', 'running');
}
if (parseInt($('.notification--num').text()) > 0) {
setTimeout(function() {
$('.notification--bell').css('animation-play-state', 'paused');
$('.notification--bellClapper').css('animation-play-state', 'paused');
}, delay);
}
}
},
error: function(errorThrown) {
$('.notification--num').html(total);
}
});
}
fetchNotificationCount();
setInterval(function () {
fetchNotificationCount()
}, 15000);
/* Get notifications */
$(document).ready(function() {
$('.notification--bell, .notification--num').click(function () {
const request = 'ajax_user_notifications.php';
$(function () {});
$.ajax({
url: request,
method: 'GET',
dataType: 'JSON',
success: function (data) {
$('#notif_container').css('box-shadow', '0 2px 5px 0 rgba(0, 0, 0, .16), 0 2px 10px 0 rgba(0, 0, 0, .12)');
if ($('.notif').children('div').length > 0) {
$('#notif_container').toggle('fade');
}
var html_to_append = '';
var morelink = false;
var effect = 'drop';
$.each(data, function (i, item) {
let read = (item.read === 'yes') ? '· read' : '';
html_to_append += '<div class="notif" notif-read="' + item.read + '" data-id="' + item.id + '" style="background:#d2d2d2;cursor:pointer;padding:0;height:70px;margin-bottom:17px;"><div style="display:inline;float:left;padding:8px 10px"><a href="/user/?id=' + item.user_id + '"><span class="material-icons" style="color:orange;vertical-align:middle;padding-right:5px;font-size:45px;margin-top:-11px">notifications</span></a></div> <div style="float:left;display:inline;padding-top:0px;font-size:15px">' + truncateString(item.message, 40) + '</div><div class="added-' + item.id + '" style="display:inline;float:left"><small>' + item.title + ' · ' + jQuery.timeago(item.added) + ' ago ' + read + '</small></div><div style="clear:both"></div></div>';
if (i >= 5) {
morelink = true;
return false;
}
});
$('#notif_container').html(html_to_append).css('position', 'absolute').css('left', '0').css('top', '27px').css('width', '320px').css('padding', '0px 17px 17px 17px').css('border-bottom-left-radius', '5px').css('border-bottom-right-radius', '5px');
if (morelink === true) {
$('#notif_container').append('<span id="show-all-notif" style="cursor:pointer;user-select:none;border-radius:4px;font-size:15px;padding:2px 8px;text-align:center;background:#d2d2d2">+ ' + (data.length - 5) + ' hidden</span> <span style="float:right"><span style="padding-left:5px">·</span> <span class="material-icons" style="vertical-align:middle;font-size:17px">thumb_up</span> <span id="rate-notif-feature" style="font-size:15px;cursor:pointer">Rate</span></span><span style="float:right"><span class="material-icons" style="vertical-align:middle">delete_sweep</span> <span id="clean-all-notif" style="font-size:15px;cursor:pointer">Clean up</span></span>');
}
$('#rate-notif-feature').on('click', function () {
$('.notif').children('div').remove();
$('#notif_container').html('<div style="padding-top:8px"><div style="display:inline;float:left;margin-top:8px;color:#F0494F"><span class="material-icons" style="font-size:27px">science</span></div><div style="display:inline;float:left;padding-left:20px"><span>This is a beta feature. Let us know!</span></div><div style="clear:both"></div></div>');
$('#notif_container').append('<div style="text-align:center" id="vote-actions"><span id="vote_up" user-id="<?=$CURUSER['id']?>" class="material-icons" style="font-size:27px;vertical-align:top">thumb_up</span><span style="padding:0 20px"></span><span id="vote_down" user-id="<?=$CURUSER['id']?>" class="material-icons" style="font-size:27px;vertical-align:top">thumb_down</span></div>');
$('#notif_container').append('<div id="back-notif-list" style="margin-top:-30px;text-decoration:underline;cursor:pointer">back</div>');
$('#back-notif-list').on('click', function () {
$('#notif_container').html(html_to_append);
});
// Handle voting actions
$('#vote_up').on('click', function () {
return false;
});
return false;
});
$('#clean-all-notif').on('click', function () {
let count = parseInt($('.notif').children('div').length);
if (Array.isArray(results)) {
$.each(results[1], function (i, item) {
$.ajax({
url: request,
method: 'POST',
data: {
id: item.id
},
dataType: 'JSON',
success: function (data) {
if (data.delete === true) {
$('.notif').children('div').remove();
if ($('.notif').children('div').length === 0) {
$('.notification--num').text('0');
$('.notification--bell').css('fill', '#D3D3D3');
$('.notification--bellClapper').css('fill', '#D3D3D3');
}
}
}
});
});
$('#notif_container').html('No more notifications.').delay(1000).fadeIn().delay(1000).toggle(effect);
} else {
$('#notif_container').html('Unable to delete notifications.');
return false;
}
});
$('#show-all-notif').on('click', function () {
$('#notif_container').html(html_to_append);
});
$('.notif').each(function (index) {
var notif = $(this);
const notification_id = notif.attr('data-id');
notif.hover(function () {
$.ajax({
url: request,
method: 'POST',
data: {
id: notification_id,
viewed: true
},
dataType: 'JSON',
success: function (data) {
if (data.read === true) {
if ($('.added-' + notification_id).children('small:contains("read")').length === 0) {
$('.added-' + notification_id).children('small').append(' · read');
}
}
}
});
notif.css('background', '#C0C0C0');
}, function () {
notif.css('background', '#D2D2D2');
});
notif.on('click', function () {
var sel = getSelected().toString();
if (sel === '') {
$.ajax({
url: request,
method: 'POST',
data: {
id: notification_id
},
dataType: 'JSON',
success: function (data) {
if (data.delete === true) {
$('div[data-id=' + notification_id + ']').text('Notification deleted.').css('text-align', 'center').delay(1000).fadeIn().delay(1000).toggle(effect);
let counter = parseInt($('.notification--num').text());
$('.notification--num').text((counter - 1));
if ($('.notif').children('div').length === 0) {
$('#notif_container').html('No more notifications.');
}
}
}
});
return false;
}
});
});
},
error: function(errorThrown) {
$('#notif_container')
.css('position', 'absolute')
.css('left', '0')
.css('top', '40px')
.css('width', '320px')
.css('padding', '0 17px')
.css('border-bottom-left-radius', '5px')
.css('border-bottom-right-radius', '5px');
$('#notif_container').html('No notifications yet :(');
}
});
closeNotificationDrop();
});
});
</script>
Add to themes/default/theme.css
/* Notifications */
#notif_container {
position: relative;
z-index: 9999999;
background: #FFF;
height: auto;
}
#notif_container div {
margin-top:15px
}
.notif-main {
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
position: relative;
width: 1.563rem;
}
.notif-main .notification {
position: relative;
width: 3em;
height: 3em;
}
.notif-main .notification svg {
width: 3em;
height: 3em;
}
.notif-main .notification svg > path {
fill: #FFFFFF;
}
.notif-main .notification--bell {
animation: bell 2.2s linear infinite;
transform-origin: 50% 0%;
}
.notif-main .notification--bellClapper {
animation: bellClapper 2.2s 0.1s linear infinite;
}
.notif-main .notification--num {
position: absolute;
top: -5%;
left: 50%;
line-height: 0.375rem;
font-size: 0.75rem;
border-radius: 50%;
width: 0.5357142857em;
height: 0.5357142857em;
background-color: #FF4C13;
border: 5px solid #FF4C13;
color: #FFFFFF;
text-align: center;
animation: notification 2.2s linear;
}
@keyframes bell {
0%, 25%, 75%, 100% {
transform: rotate(0deg);
}
40% {
transform: rotate(10deg);
}
45% {
transform: rotate(-10deg);
}
55% {
transform: rotate(8deg);
}
60% {
transform: rotate(-8deg);
}
}
@keyframes bellClapper {
0%, 25%, 75%, 100% {
transform: translateX(0);
}
40% {
transform: translateX(-0.15em);
}
45% {
transform: translateX(0.15em);
}
55% {
transform: translateX(-0.1em);
}
60% {
transform: translateX(0.1em);
}
}
@keyframes notification {
0%, 25%, 75%, 100% {
opacity: 1;
}
30%, 70% {
opacity: 0;
}
}
Open /backend/functions.php and replace entire loggedinonly() function with
function loggedinonly()
{
global $CURUSER;
if (!$CURUSER) {
header("Refresh: 0; url=account-login.php?returnto=" . urlencode($_SERVER["REQUEST_URI"]));
exit();
}
return true;
}
To send a notfication, add to /backend/functions.php
function sendNotif($userid, $message, $title = '')
{
$notif = SQL_Query_exec("INSERT INTO `notifications` (`user_id`, `message`, `added`, `read`, `title`) VALUES (" . $userid . ", " . $message . ", NOW(), 'no', " . $title . ")");
if ($notif)
return true;
return false;
}
And use like this in pages/areas where another user is concerned (like cleanup, login, account updates, announcement, etc)
sendNotif($CURUSER['id'], 'Welcome to site', 'New User');
Last edited by STorrents2020 on 2022-07-02 20:01:14

