From e29d05ce4a2d33d03a72ac190aeab727bb0bc409 Mon Sep 17 00:00:00 2001
From: Fabrixxm <fabrix.xm@gmail.com>
Date: Wed, 24 Jun 2015 16:10:06 +0200
Subject: [PATCH] notifications order, unseen css class

ping return notfications ordered by notification id
notifications list get seen/unseen class from ping xml
global.css define 'notify-unseen'
---
 js/main.js                           | 30 ++++++++++------------------
 mod/ping.php                         | 26 ++++++++++++++++--------
 view/global.css                      |  5 ++++-
 view/theme/quattro/templates/nav.tpl |  2 +-
 view/theme/vier/templates/nav.tpl    |  2 +-
 5 files changed, 34 insertions(+), 31 deletions(-)

diff --git a/js/main.js b/js/main.js
index 23e759ef3..b812e2508 100644
--- a/js/main.js
+++ b/js/main.js
@@ -182,42 +182,32 @@
 				nnm.html(notifications_all + notifications_mark);
 				//nnm.attr('popup','true');
 
-				var notification_lastitem = localStorage.getItem("notification-lastitem");
-				var notification_first_id = 0;
-				var notification_id;
+				var notification_lastitem = parseInt(localStorage.getItem("notification-lastitem"));
+				var notification_id = 0;
 				eNotif.children("note").each(function(){
 					e = $(this);
 					text = e.text().format("<span class='contactname'>"+e.attr('name')+"</span>");
 					html = notifications_tpl.format(e.attr('href'),e.attr('photo'), text, e.attr('date'), e.attr('seen'));
 					nnm.append(html);
-					
-					notification_id = e.attr('href').match(/\d+$/)[0];
-					if (notification_lastitem!== null && notification_id!=notification_lastitem) {
-						if (notification_first_id===0) notification_first_id = notification_id;
+				});
+				$(eNotif.children("note").get().reverse()).each(function(){
+					e = $(this);
+					notification_id = parseInt(e.attr('href').match(/\d+$/)[0]);
+					if (notification_lastitem!== null && notification_id > notification_lastitem) {
 						if (getNotificationPermission()==="granted") {
-							console.log("notification", e.text().replace('&rarr; ','').format(e.attr('name')));
 							var notification = new Notification(document.title, {
 											  body: e.text().replace('&rarr; ','').format(e.attr('name')),
 											  icon: e.attr('photo'),
-											  data: e.attr('href')
 											 });
-							// close notification after 5 secs.
-							// see https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API#Closing_notifications
-							//setTimeout(notification.close.bind(notification), 5000);
-							
+							notification['url'] = e.attr('href');
 							notification.addEventListener("click", function(ev){
-								window.location = ev.target.data;
+								window.location = ev.target.url;
 							});
 						}
 					}
-					if (notification_id == notification_lastitem) {
-						if (notification_first_id===0) notification_first_id = notification_id;
-						notification_lastitem = null;
-					}
-				
 					
 				});
-				if (notification_first_id!==0) notification_lastitem = notification_first_id;
+				notification_lastitem = notification_id;
 				localStorage.setItem("notification-lastitem", notification_lastitem)
 
 				$("img[data-src]", nnm).each(function(i, el){
diff --git a/mod/ping.php b/mod/ping.php
index 97a3070d8..a5f23e1eb 100644
--- a/mod/ping.php
+++ b/mod/ping.php
@@ -1,7 +1,7 @@
 <?php
 require_once("include/datetime.php");
 require_once('include/bbcode.php');
-
+require_once("mod/proxy.php");
 
 function ping_init(&$a) {
 
@@ -164,7 +164,6 @@ function ping_init(&$a) {
 
 
 		function xmlize($href, $name, $url, $photo, $date, $seen, $message){
-			require_once("mod/proxy.php");
 			$photo = proxy_url($photo);
 
 			$message = html_entity_decode($message, ENT_COMPAT | ENT_HTML401, "UTF-8");
@@ -216,23 +215,23 @@ function ping_init(&$a) {
 
 			if ($intro>0){
 				foreach ($intros as $i) {
-					echo xmlize($a->get_baseurl().'/notifications/intros/'.$i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), 'notify-unseen', "&rarr; ".t("{0} wants to be your friend"));
+					echo xmlize($a->get_baseurl().'/notifications/intros/'.$i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), 'notify-unseen', t("{0} wants to be your friend"));
 				};
 			}
 			if ($mail>0){
 				foreach ($mails as $i) {
-					echo xmlize($a->get_baseurl().'/message/'.$i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), 'notify-unseen',"&rarr; ".t("{0} sent you a message"));
+					echo xmlize($a->get_baseurl().'/message/'.$i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), 'notify-unseen', t("{0} sent you a message"));
 				};
 			}
 			if ($register>0){
 				foreach ($regs as $i) {
-					echo xmlize($a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen', "&rarr; ".t("{0} requested registration"));
+					echo xmlize($a->get_baseurl().'/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen', t("{0} requested registration"));
 				};
 			}
 
 			if(count($z)) {
 				foreach($z as $zz) {
-					echo xmlize($a->get_baseurl() . '/notify/view/' . $zz['id'], $zz['name'],$zz['url'],$zz['photo'],relative_date($zz['date']), ($zz['seen'] ? 'notify-seen' : 'notify-unseen'), ($zz['seen'] ? '' : '&rarr; ') .strip_tags(bbcode($zz['msg'])));
+					echo xmlize($a->get_baseurl() . '/notify/view/' . $zz['id'], $zz['name'],$zz['url'],$zz['photo'],relative_date($zz['date']), ($zz['seen'] ? 'notify-seen' : 'notify-unseen'), strip_tags(bbcode($zz['msg'])));
 				}
 			}
 		}
@@ -322,7 +321,7 @@ function ping_get_notifications($uid) {
 	$offset = 0;
 	$seen = false;
 	$seensql = "NOT";
-	$order = "";
+	$order = "DESC";
 	$quit = false;
 
 	$a = get_app();
@@ -348,6 +347,7 @@ function ping_get_notifications($uid) {
 			$quit = true;
 		else
 			$offset += 50;
+			
 
 		foreach ($r AS $notification) {
 			if (is_null($notification["visible"]))
@@ -370,11 +370,21 @@ function ping_get_notifications($uid) {
 				$notification["msg"] = substr_replace($notification["msg"],"{0}",$pos,strlen($notification["name"]));
 
 			if ($notification["visible"] AND !$notification["spam"] AND
-				!$notification["deleted"] AND !is_array($result[$notification["parent"]]))
+				!$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
 				$result[$notification["parent"]] = $notification;
+			}
 		}
 
 	} while ((count($result) < 50) AND !$quit);
 
+	// sort result by $[]['id'], inversed
+	$sort_function = function($a, $b) {
+		if ($a['id'] == $b['id']) {
+			return 0;
+		}
+		return ($a['id'] < $b['id']) ? 1 : -1;
+	};
+	usort($result, $sort_function);
+	
 	return($result);
 }
diff --git a/view/global.css b/view/global.css
index 673dbcd45..9bcd30229 100644
--- a/view/global.css
+++ b/view/global.css
@@ -173,4 +173,7 @@ span.oembed, h4 {
 /* fields help text */
 .field .field_help {
     clear: left;
-}
\ No newline at end of file
+}
+
+/* notifications unseen */
+.notify-unseen { background-color: #cceeFF; }
\ No newline at end of file
diff --git a/view/theme/quattro/templates/nav.tpl b/view/theme/quattro/templates/nav.tpl
index 2c646306a..c77ed99e4 100644
--- a/view/theme/quattro/templates/nav.tpl
+++ b/view/theme/quattro/templates/nav.tpl
@@ -109,7 +109,7 @@
 
 </nav>
 <ul id="nav-notifications-template" style="display:none;" rel="template">
-	<li><a href="{0}"><img data-src="{1}">{2} <span class="notif-when">{3}</span></a></li>
+	<li class="{4}"><a href="{0}"><img data-src="{1}">{2} <span class="notif-when">{3}</span></a></li>
 </ul>
 
 <div style="position: fixed; top: 3px; left: 5px; z-index:9999">{{$langselector}}</div>
diff --git a/view/theme/vier/templates/nav.tpl b/view/theme/vier/templates/nav.tpl
index ec1be842c..4ef640080 100644
--- a/view/theme/vier/templates/nav.tpl
+++ b/view/theme/vier/templates/nav.tpl
@@ -97,7 +97,7 @@
 
 </nav>
 <ul id="nav-notifications-template" style="display:none;" rel="template">
-	<li><a href="{0}"><img data-src="{1}">{2} <span class="notif-when">{3}</span></a></li>
+	<li class="{4}"><a href="{0}"><img data-src="{1}">{2} <span class="notif-when">{3}</span></a></li>
 </ul>
 <!--
 <div class="icon-flag" style="position: fixed; bottom: 10px; left: 20px; z-index:9;">{{$langselector}}</div>