1
1
Fork 0

notifications - add docu & pagination

This commit is contained in:
rabuzarus 2016-08-01 23:04:41 +02:00
parent 7a4be1cead
commit bcee356f66
4 changed files with 151 additions and 30 deletions

View file

@ -183,7 +183,20 @@ class NotificationsManager {
return $tabs; return $tabs;
} }
public function format($notifs, $ident = "") { /**
* @brief Format the notification query in an usable array
*
* @param array $notifs The array from the db query
* @param string $ident The notifications identifier (e.g. network)
* @return array
* string 'label' => The type of the notification
* string 'link' => URL to the source
* string 'image' => The avatar image
* string 'text' => The notification text
* string 'when' => Relative date of the notification
* bool 'seen' => Is the notification marked as "seen"
*/
private function format($notifs, $ident = "") {
$notif = array(); $notif = array();
$arr = array(); $arr = array();
@ -191,13 +204,17 @@ class NotificationsManager {
if (dbm::is_result($notifs)) { if (dbm::is_result($notifs)) {
foreach ($notifs as $it) { foreach ($notifs as $it) {
// Because we use different db tables for the notification query
// we have sometimes $it['unseen'] and sometimes $it['seen].
// So we will have to transform $it['unseen']
if($it['unseen']) if($it['unseen'])
$it['seen'] = ($it['unseen'] > 0 ? false : true); $it['seen'] = ($it['unseen'] > 0 ? false : true);
// Depending on the identifier of the notification we need to use different defaults
switch ($ident) { switch ($ident) {
case 'system': case 'system':
$default_item_label = 'notify'; $default_item_label = 'notify';
$default_item_link = app::get_baseurl(true).'/notify/view/'. $it['id']; $default_item_link = $this->a->get_baseurl(true).'/notify/view/'. $it['id'];
$default_item_image = proxy_url($it['photo'], false, PROXY_SIZE_MICRO); $default_item_image = proxy_url($it['photo'], false, PROXY_SIZE_MICRO);
$default_item_text = strip_tags(bbcode($it['msg'])); $default_item_text = strip_tags(bbcode($it['msg']));
$default_item_when = relative_date($it['date']); $default_item_when = relative_date($it['date']);
@ -206,7 +223,7 @@ class NotificationsManager {
case 'home': case 'home':
$default_item_label = 'comment'; $default_item_label = 'comment';
$default_item_link = app::get_baseurl(true).'/display/'.$it['pguid']; $default_item_link = $this->a->get_baseurl(true).'/display/'.$it['pguid'];
$default_item_image = proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO); $default_item_image = proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO);
$default_item_text = sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']); $default_item_text = sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']);
$default_item_when = relative_date($it['created']); $default_item_when = relative_date($it['created']);
@ -215,7 +232,7 @@ class NotificationsManager {
default: default:
$default_item_label = (($it['id'] == $it['parent']) ? 'post' : 'comment'); $default_item_label = (($it['id'] == $it['parent']) ? 'post' : 'comment');
$default_item_link = app::get_baseurl(true).'/display/'.$it['pguid']; $default_item_link = $this->a->get_baseurl(true).'/display/'.$it['pguid'];
$default_item_image = proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO); $default_item_image = proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO);
$default_item_text = (($it['id'] == $it['parent']) $default_item_text = (($it['id'] == $it['parent'])
? sprintf( t("%s created a new post"), $it['author-name']) ? sprintf( t("%s created a new post"), $it['author-name'])
@ -225,12 +242,13 @@ class NotificationsManager {
} }
// Transform the different types of notification in an usable array
switch($it['verb']){ switch($it['verb']){
case ACTIVITY_LIKE: case ACTIVITY_LIKE:
$notif = array( $notif = array(
//'$item_link' => $a->get_baseurl(true).'/display/'.$a->user['nickname']."/".$it['parent'], //'$item_link' => $a->get_baseurl(true).'/display/'.$a->user['nickname']."/".$it['parent'],
'label' => 'like', 'label' => 'like',
'link' => app::get_baseurl(true).'/display/'.$it['pguid'], 'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
'$image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO), '$image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
'text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']), 'text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
'when' => relative_date($it['created']), 'when' => relative_date($it['created']),
@ -242,7 +260,7 @@ class NotificationsManager {
$notif = array( $notif = array(
//'$item_link' => $a->get_baseurl(true).'/display/'.$a->user['nickname']."/".$it['parent'], //'$item_link' => $a->get_baseurl(true).'/display/'.$a->user['nickname']."/".$it['parent'],
'label' => 'dislike', 'label' => 'dislike',
'link' => app::get_baseurl(true).'/display/'.$it['pguid'], 'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO), 'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
'text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']), 'text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
'when' => relative_date($it['created']), 'when' => relative_date($it['created']),
@ -258,7 +276,7 @@ class NotificationsManager {
$notif = array( $notif = array(
//'$item_link' => $a->get_baseurl(true).'/display/'.$a->user['nickname']."/".$it['parent'], //'$item_link' => $a->get_baseurl(true).'/display/'.$a->user['nickname']."/".$it['parent'],
'label' => 'friend', 'label' => 'friend',
'link' => app::get_baseurl(true).'/display/'.$it['pguid'], 'link' => $this->a->get_baseurl(true).'/display/'.$it['pguid'],
'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO), 'image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO),
'text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']), 'text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
'when' => relative_date($it['created']), 'when' => relative_date($it['created']),
@ -286,6 +304,13 @@ class NotificationsManager {
} }
/**
* @brief Total number of network notifications
* @param int $seen
* If 0 only include notifications into the query
* which arn't marked as "seen" into
* @return int Number of network notifications
*/
private function networkTotal($seen = 0) { private function networkTotal($seen = 0) {
if($seen === 0) if($seen === 0)
$sql_seen = " AND `item`.`unseen` = 1 "; $sql_seen = " AND `item`.`unseen` = 1 ";
@ -304,7 +329,18 @@ class NotificationsManager {
return 0; return 0;
} }
public function networkNotifs($seen = 0) { /**
* @brief Get network notifications
*
* @param type $seen
* If 0 only include notifications into the query
* which arn't marked as "seen" into
* @param int $start Start the query at this point
* @param int $limit Maximum number of query results
*
* @return array Query output
*/
public function networkNotifs($seen = 0, $start = 0, $limit = 20) {
$ident = 'network'; $ident = 'network';
$total = $this->networkTotal($seen); $total = $this->networkTotal($seen);
@ -319,8 +355,10 @@ class NotificationsManager {
WHERE `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND WHERE `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND
`item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0
$sql_seen $sql_seen
ORDER BY `item`.`created` DESC", ORDER BY `item`.`created` DESC LIMIT %d, %d ",
intval(local_user()) intval(local_user()),
intval($start),
intval($limit)
); );
if(dbm::is_result($r)) { if(dbm::is_result($r)) {
@ -335,6 +373,13 @@ class NotificationsManager {
} }
} }
/**
* @brief Total number of system notifications
* @param int $seen
* If 0 only include notifications into the query
* which arn't marked as "seen" into
* @return int Number of system notifications
*/
private function systemTotal($seen = 0) { private function systemTotal($seen = 0) {
if($seen === 0) if($seen === 0)
$sql_seen = " AND `seen` = 0 "; $sql_seen = " AND `seen` = 0 ";
@ -349,15 +394,29 @@ class NotificationsManager {
return 0; return 0;
} }
public function systemNotifs($seen = 0) { /**
* @brief Get system notifications
*
* @param type $seen
* If 0 only include notifications into the query
* which arn't marked as "seen" into
* @param int $start Start the query at this point
* @param int $limit Maximum number of query results
*
* @return array Query output
*/
public function systemNotifs($seen = 0, $start = 0, $limit = 20) {
$ident = 'system'; $ident = 'system';
$total = $this->systemTotal($seen); $total = $this->systemTotal($seen);
if($seen === 0) if($seen === 0)
$sql_seen = " AND `seen` = 0 "; $sql_seen = " AND `seen` = 0 ";
$r = q("SELECT * FROM `notify` WHERE `uid` = %d $sql_seen ORDER BY `date` DESC", $r = q("SELECT `id`, `photo`, `msg`, `date`, `seen` FROM `notify`
intval(local_user()) WHERE `uid` = %d $sql_seen ORDER BY `date` DESC LIMIT %d, %d ",
intval(local_user()),
intval($start),
intval($limit)
); );
if(dbm::is_result($r)) { if(dbm::is_result($r)) {
@ -372,8 +431,13 @@ class NotificationsManager {
} }
} }
/**
* @brief Addional SQL query string for the personal notifications
*
* @return string The additional sql query
*/
private function _personal_sql_extra() { private function _personal_sql_extra() {
$myurl = app::get_baseurl(true) . '/profile/'. $this->a->user['nickname']; $myurl = $this->a->get_baseurl(true) . '/profile/'. $this->a->user['nickname'];
$myurl = substr($myurl,strpos($myurl,'://')+3); $myurl = substr($myurl,strpos($myurl,'://')+3);
$myurl = str_replace(array('www.','.'),array('','\\.'),$myurl); $myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
$diasp_url = str_replace('/profile/','/u/',$myurl); $diasp_url = str_replace('/profile/','/u/',$myurl);
@ -386,6 +450,13 @@ class NotificationsManager {
return $sql_extra; return $sql_extra;
} }
/**
* @brief Total number of personal notifications
* @param int $seen
* If 0 only include notifications into the query
* which arn't marked as "seen" into
* @return int Number of personal notifications
*/
private function personalTotal($seen = 0) { private function personalTotal($seen = 0) {
$sql_extra .= $this->_personal_sql_extra(); $sql_extra .= $this->_personal_sql_extra();
@ -406,7 +477,19 @@ class NotificationsManager {
return 0; return 0;
} }
public function personalNotifs($seen = 0) {
/**
* @brief Get personal notifications
*
* @param type $seen
* If 0 only include notifications into the query
* which arn't marked as "seen" into
* @param int $start Start the query at this point
* @param int $limit Maximum number of query results
*
* @return array Query output
*/
public function personalNotifs($seen = 0, $start = 0, $limit = 20) {
$ident = 'personal'; $ident = 'personal';
$total = 0; $total = 0;
$sql_extra .= $this->_personal_sql_extra(); $sql_extra .= $this->_personal_sql_extra();
@ -422,8 +505,10 @@ class NotificationsManager {
$sql_extra $sql_extra
$sql_seen $sql_seen
AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0
ORDER BY `item`.`created` DESC" , ORDER BY `item`.`created` DESC LIMIT %d, %d " ,
intval(local_user()) intval(local_user()),
intval($start),
intval($limit)
); );
if(dbm::is_result($r)) { if(dbm::is_result($r)) {
@ -438,24 +523,42 @@ class NotificationsManager {
} }
} }
/**
* @brief Total number of home notifications
* @param int $seen
* If 0 only include notifications into the query
* which arn't marked as "seen" into
* @return int Number of home notifications
*/
private function homeTotal($seen = 0) { private function homeTotal($seen = 0) {
if($seen === 0) if($seen === 0)
$sql_seen = " AND `item`.`unseen` = 1 "; $sql_seen = " AND `item`.`unseen` = 1 ";
$r = q("SELECT COUNT(*) AS `total` FROM `item` $r = q("SELECT COUNT(*) AS `total` FROM `item`
WHERE ``item`.`visible` = 1 AND WHERE `item`.`visible` = 1 AND
`item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1
$sql_seen", $sql_seen",
intval(local_user()) intval(local_user())
); );
if(dbm::is_result($r)) if(dbm::is_result($r))
return $r['total']; return $r[0]['total'];
return 0; return 0;
} }
public function homeNotifs($seen = 0) { /**
* @brief Get home notifications
*
* @param type $seen
* If 0 only include notifications into the query
* which arn't marked as "seen" into
* @param int $start Start the query at this point
* @param int $limit Maximum number of query results
*
* @return array Query output
*/
public function homeNotifs($seen = 0, $start = 0, $limit = 20) {
$ident = 'home'; $ident = 'home';
$total = $this->homeTotal($seen); $total = $this->homeTotal($seen);
@ -471,8 +574,10 @@ class NotificationsManager {
WHERE `item`.`visible` = 1 AND WHERE `item`.`visible` = 1 AND
`item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1
$sql_seen $sql_seen
ORDER BY `item`.`created` DESC", ORDER BY `item`.`created` DESC LIMIT %d, %d ",
intval(local_user()) intval(local_user()),
intval($start),
intval($limit)
); );
if(dbm::is_result($r)) { if(dbm::is_result($r)) {

View file

@ -67,7 +67,9 @@ function notifications_content(&$a) {
return; return;
} }
$page = (x($_REQUEST,'page') ? $_REQUEST['page'] : 1);
$show = (x($_REQUEST,'show') ? $_REQUEST['show'] : 0); $show = (x($_REQUEST,'show') ? $_REQUEST['show'] : 0);
nav_set_selected('notifications'); nav_set_selected('notifications');
$json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false); $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
@ -78,6 +80,8 @@ function notifications_content(&$a) {
// get the nav tabs for the notification pages // get the nav tabs for the notification pages
$tabs = $nm->getTabs(); $tabs = $nm->getTabs();
$notif_content = array(); $notif_content = array();
$perpage = 20;
$startrec = ($page * $perpage) - $perpage;
if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) { if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
nav_set_selected('introductions'); nav_set_selected('introductions');
@ -90,8 +94,6 @@ function notifications_content(&$a) {
$notif_header = t('Notifications'); $notif_header = t('Notifications');
$notif_tpl = get_markup_template('notifications.tpl'); $notif_tpl = get_markup_template('notifications.tpl');
// $notif_show_lnk .= '<a href="' . ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ) . '" id="notifications-show-hide-link" >'
// . ((strlen($sql_extra)) ? t('Show Ignored Requests') : t('Hide Ignored Requests')) . '</a></div>' . "\r\n";
$notif_show_lnk = array( $notif_show_lnk = array(
'href' => ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ), 'href' => ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ),
@ -105,7 +107,7 @@ function notifications_content(&$a) {
); );
if(dbm::is_result($r)) { if(dbm::is_result($r)) {
$a->set_pager_total($r[0]['total']); $a->set_pager_total($r[0]['total']);
$a->set_pager_itemspage(20); $a->set_pager_itemspage($perpage);
} }
/// @todo Fetch contact details by "get_contact_details_by_url" instead of queries to contact, fcontact and gcontact /// @todo Fetch contact details by "get_contact_details_by_url" instead of queries to contact, fcontact and gcontact
@ -247,7 +249,7 @@ function notifications_content(&$a) {
$notif_header = t('Network Notifications'); $notif_header = t('Network Notifications');
$notif_tpl = get_markup_template('notifications.tpl'); $notif_tpl = get_markup_template('notifications.tpl');
$notifs = $nm->networkNotifs($show); $notifs = $nm->networkNotifs($show, $startrec, $perpage);
$notif_show_lnk = array( $notif_show_lnk = array(
'href' => ($show ? 'notifications/network' : 'notifications/network?show=all' ), 'href' => ($show ? 'notifications/network' : 'notifications/network?show=all' ),
@ -266,7 +268,7 @@ function notifications_content(&$a) {
$notif_header = t('System Notifications'); $notif_header = t('System Notifications');
$notif_tpl = get_markup_template('notifications.tpl'); $notif_tpl = get_markup_template('notifications.tpl');
$notifs = $nm->systemNotifs($show); $notifs = $nm->systemNotifs($show, $startrec, $perpage);
$notif_show_lnk = array( $notif_show_lnk = array(
'href' => ($show ? 'notifications/system' : 'notifications/system?show=all' ), 'href' => ($show ? 'notifications/system' : 'notifications/system?show=all' ),
@ -285,7 +287,7 @@ function notifications_content(&$a) {
$notif_header = t('Personal Notifications'); $notif_header = t('Personal Notifications');
$notif_tpl = get_markup_template('notifications.tpl'); $notif_tpl = get_markup_template('notifications.tpl');
$notifs = $nm->personalNotifs($show); $notifs = $nm->personalNotifs($show, $startrec, $perpage);
$notif_show_lnk = array( $notif_show_lnk = array(
'href' => ($show ? 'notifications/personal' : 'notifications/personal?show=all' ), 'href' => ($show ? 'notifications/personal' : 'notifications/personal?show=all' ),
@ -304,7 +306,7 @@ function notifications_content(&$a) {
$notif_header = t('Home Notifications'); $notif_header = t('Home Notifications');
$notif_tpl = get_markup_template('notifications.tpl'); $notif_tpl = get_markup_template('notifications.tpl');
$notifs = $nm->homeNotifs($show); $notifs = $nm->homeNotifs($show, $startrec, $perpage);
$notif_show_lnk = array( $notif_show_lnk = array(
'href' => ($show ? 'notifications/home' : 'notifications/home?show=all' ), 'href' => ($show ? 'notifications/home' : 'notifications/home?show=all' ),
@ -321,6 +323,14 @@ function notifications_content(&$a) {
} }
if(count($notifs['notifications']) > 0 ) { if(count($notifs['notifications']) > 0 ) {
// set the pager
$a->set_pager_total($notifs['total']);
$a->set_pager_itemspage($perpage);
// add additional informations (needed for json output)
$notifs['items_page'] = $a->pager['itemspage'];
$notifs['page'] = $a->pager['page'];
// The template files we need in different cases for formatting the content // The template files we need in different cases for formatting the content
$tpl_item_like = 'notifications_likes_item.tpl'; $tpl_item_like = 'notifications_likes_item.tpl';
$tpl_item_dislike = 'notifications_dislikes_item.tpl'; $tpl_item_dislike = 'notifications_dislikes_item.tpl';
@ -351,8 +361,8 @@ function notifications_content(&$a) {
'$notif_content' => $notif_content, '$notif_content' => $notif_content,
'$notif_nocontent' => $notif_nocontent, '$notif_nocontent' => $notif_nocontent,
'$notif_show_lnk' => $notif_show_lnk, '$notif_show_lnk' => $notif_show_lnk,
'$notif_paginate' => paginate($a)
)); ));
$o .= paginate($a);
return $o; return $o;
} }

View file

@ -19,4 +19,7 @@
{{if $notif_nocontent}} {{if $notif_nocontent}}
<div class="notif_nocontent">{{$notif_nocontent}}</div> <div class="notif_nocontent">{{$notif_nocontent}}</div>
{{/if}} {{/if}}
{{* The pager *}}
{{$notif_paginate}}
</div> </div>

View file

@ -22,4 +22,7 @@
<div class="notif_nocontent">{{$notif_nocontent}}</div> <div class="notif_nocontent">{{$notif_nocontent}}</div>
{{/if}} {{/if}}
</div> </div>
{{* The pager *}}
{{$notif_paginate}}
</div> </div>