From bcee356f661c02dc6f4d92bd223d293638d30475 Mon Sep 17 00:00:00 2001 From: rabuzarus <> Date: Mon, 1 Aug 2016 23:04:41 +0200 Subject: [PATCH] notifications - add docu & pagination --- include/NotificationsManager.php | 149 +++++++++++++++++--- mod/notifications.php | 26 ++-- view/templates/notifications.tpl | 3 + view/theme/frio/templates/notifications.tpl | 3 + 4 files changed, 151 insertions(+), 30 deletions(-) diff --git a/include/NotificationsManager.php b/include/NotificationsManager.php index 09e9c1494e..b14e76740b 100644 --- a/include/NotificationsManager.php +++ b/include/NotificationsManager.php @@ -183,7 +183,20 @@ class NotificationsManager { 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(); $arr = array(); @@ -191,13 +204,17 @@ class NotificationsManager { if (dbm::is_result($notifs)) { 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']) $it['seen'] = ($it['unseen'] > 0 ? false : true); + // Depending on the identifier of the notification we need to use different defaults switch ($ident) { case 'system': $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_text = strip_tags(bbcode($it['msg'])); $default_item_when = relative_date($it['date']); @@ -206,7 +223,7 @@ class NotificationsManager { case 'home': $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_text = sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']); $default_item_when = relative_date($it['created']); @@ -215,7 +232,7 @@ class NotificationsManager { default: $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_text = (($it['id'] == $it['parent']) ? 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']){ case ACTIVITY_LIKE: $notif = array( //'$item_link' => $a->get_baseurl(true).'/display/'.$a->user['nickname']."/".$it['parent'], '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), 'text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']), 'when' => relative_date($it['created']), @@ -242,7 +260,7 @@ class NotificationsManager { $notif = array( //'$item_link' => $a->get_baseurl(true).'/display/'.$a->user['nickname']."/".$it['parent'], '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), 'text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']), 'when' => relative_date($it['created']), @@ -258,7 +276,7 @@ class NotificationsManager { $notif = array( //'$item_link' => $a->get_baseurl(true).'/display/'.$a->user['nickname']."/".$it['parent'], '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), 'text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']), '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) { if($seen === 0) $sql_seen = " AND `item`.`unseen` = 1 "; @@ -304,7 +329,18 @@ class NotificationsManager { 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'; $total = $this->networkTotal($seen); @@ -319,8 +355,10 @@ class NotificationsManager { WHERE `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 $sql_seen - ORDER BY `item`.`created` DESC", - intval(local_user()) + ORDER BY `item`.`created` DESC LIMIT %d, %d ", + intval(local_user()), + intval($start), + intval($limit) ); 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) { if($seen === 0) $sql_seen = " AND `seen` = 0 "; @@ -349,15 +394,29 @@ class NotificationsManager { 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'; $total = $this->systemTotal($seen); if($seen === 0) $sql_seen = " AND `seen` = 0 "; - $r = q("SELECT * FROM `notify` WHERE `uid` = %d $sql_seen ORDER BY `date` DESC", - intval(local_user()) + $r = q("SELECT `id`, `photo`, `msg`, `date`, `seen` FROM `notify` + WHERE `uid` = %d $sql_seen ORDER BY `date` DESC LIMIT %d, %d ", + intval(local_user()), + intval($start), + intval($limit) ); 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() { - $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 = str_replace(array('www.','.'),array('','\\.'),$myurl); $diasp_url = str_replace('/profile/','/u/',$myurl); @@ -386,6 +450,13 @@ class NotificationsManager { 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) { $sql_extra .= $this->_personal_sql_extra(); @@ -406,7 +477,19 @@ class NotificationsManager { 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'; $total = 0; $sql_extra .= $this->_personal_sql_extra(); @@ -422,8 +505,10 @@ class NotificationsManager { $sql_extra $sql_seen AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 - ORDER BY `item`.`created` DESC" , - intval(local_user()) + ORDER BY `item`.`created` DESC LIMIT %d, %d " , + intval(local_user()), + intval($start), + intval($limit) ); 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) { if($seen === 0) $sql_seen = " AND `item`.`unseen` = 1 "; $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 $sql_seen", intval(local_user()) ); if(dbm::is_result($r)) - return $r['total']; + return $r[0]['total']; 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'; $total = $this->homeTotal($seen); @@ -471,8 +574,10 @@ class NotificationsManager { WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 $sql_seen - ORDER BY `item`.`created` DESC", - intval(local_user()) + ORDER BY `item`.`created` DESC LIMIT %d, %d ", + intval(local_user()), + intval($start), + intval($limit) ); if(dbm::is_result($r)) { diff --git a/mod/notifications.php b/mod/notifications.php index faf7651914..cdafe07ee9 100644 --- a/mod/notifications.php +++ b/mod/notifications.php @@ -67,7 +67,9 @@ function notifications_content(&$a) { return; } + $page = (x($_REQUEST,'page') ? $_REQUEST['page'] : 1); $show = (x($_REQUEST,'show') ? $_REQUEST['show'] : 0); + nav_set_selected('notifications'); $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 $tabs = $nm->getTabs(); $notif_content = array(); + $perpage = 20; + $startrec = ($page * $perpage) - $perpage; if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) { nav_set_selected('introductions'); @@ -90,8 +94,6 @@ function notifications_content(&$a) { $notif_header = t('Notifications'); $notif_tpl = get_markup_template('notifications.tpl'); -// $notif_show_lnk .= '' -// . ((strlen($sql_extra)) ? t('Show Ignored Requests') : t('Hide Ignored Requests')) . '' . "\r\n"; $notif_show_lnk = array( 'href' => ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ), @@ -105,7 +107,7 @@ function notifications_content(&$a) { ); if(dbm::is_result($r)) { $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 @@ -247,7 +249,7 @@ function notifications_content(&$a) { $notif_header = t('Network Notifications'); $notif_tpl = get_markup_template('notifications.tpl'); - $notifs = $nm->networkNotifs($show); + $notifs = $nm->networkNotifs($show, $startrec, $perpage); $notif_show_lnk = array( 'href' => ($show ? 'notifications/network' : 'notifications/network?show=all' ), @@ -266,7 +268,7 @@ function notifications_content(&$a) { $notif_header = t('System Notifications'); $notif_tpl = get_markup_template('notifications.tpl'); - $notifs = $nm->systemNotifs($show); + $notifs = $nm->systemNotifs($show, $startrec, $perpage); $notif_show_lnk = array( 'href' => ($show ? 'notifications/system' : 'notifications/system?show=all' ), @@ -285,7 +287,7 @@ function notifications_content(&$a) { $notif_header = t('Personal Notifications'); $notif_tpl = get_markup_template('notifications.tpl'); - $notifs = $nm->personalNotifs($show); + $notifs = $nm->personalNotifs($show, $startrec, $perpage); $notif_show_lnk = array( 'href' => ($show ? 'notifications/personal' : 'notifications/personal?show=all' ), @@ -304,7 +306,7 @@ function notifications_content(&$a) { $notif_header = t('Home Notifications'); $notif_tpl = get_markup_template('notifications.tpl'); - $notifs = $nm->homeNotifs($show); + $notifs = $nm->homeNotifs($show, $startrec, $perpage); $notif_show_lnk = array( 'href' => ($show ? 'notifications/home' : 'notifications/home?show=all' ), @@ -321,6 +323,14 @@ function notifications_content(&$a) { } 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 $tpl_item_like = 'notifications_likes_item.tpl'; $tpl_item_dislike = 'notifications_dislikes_item.tpl'; @@ -351,8 +361,8 @@ function notifications_content(&$a) { '$notif_content' => $notif_content, '$notif_nocontent' => $notif_nocontent, '$notif_show_lnk' => $notif_show_lnk, + '$notif_paginate' => paginate($a) )); - $o .= paginate($a); return $o; } diff --git a/view/templates/notifications.tpl b/view/templates/notifications.tpl index 4048cfa46b..f3068226c4 100644 --- a/view/templates/notifications.tpl +++ b/view/templates/notifications.tpl @@ -19,4 +19,7 @@ {{if $notif_nocontent}}
{{$notif_nocontent}}
{{/if}} + + {{* The pager *}} + {{$notif_paginate}} diff --git a/view/theme/frio/templates/notifications.tpl b/view/theme/frio/templates/notifications.tpl index 5f131bd7ed..82f11c8be4 100644 --- a/view/theme/frio/templates/notifications.tpl +++ b/view/theme/frio/templates/notifications.tpl @@ -22,4 +22,7 @@
{{$notif_nocontent}}
{{/if}} + + {{* The pager *}} + {{$notif_paginate}}