From 58f129799b33de675d96f1ea6e022a41a715e12e Mon Sep 17 00:00:00 2001 From: Cameron Date: Wed, 24 Feb 2021 15:26:01 -0800 Subject: [PATCH] New feature: Previous/Next News item navigation. --- .../shortcodes/batch/news_shortcodes.php | 115 +++++++++++++++--- .../news/templates/news_view_template.php | 20 +-- 2 files changed, 113 insertions(+), 22 deletions(-) diff --git a/e107_core/shortcodes/batch/news_shortcodes.php b/e107_core/shortcodes/batch/news_shortcodes.php index d91fcfb4d..a2b01b97c 100644 --- a/e107_core/shortcodes/batch/news_shortcodes.php +++ b/e107_core/shortcodes/batch/news_shortcodes.php @@ -192,20 +192,7 @@ class news_shortcodes extends e_shortcode */ function sc_newsnavlink($parm=null) //TODO add more options. { - $url = e107::getUrl()->create('news/list/items'); // default for now. - - if(varset($parm['list']) == 'all') // A list of all items - usually headings and thumbnails - { - $url = e107::getUrl()->create('news/list/all'); - } - elseif(varset($parm['list']) == 'category') - { - $url = e107::getUrl()->create('news/list/short', $this->news_item); //default for now. - } - elseif(varset($parm['items']) == 'category') - { - $url = e107::getUrl()->create('news/list/category', $this->news_item); - } + $url = $this->sc_news_nav_url($parm); $caption = vartrue($parm['text'], LAN_BACK); @@ -223,6 +210,27 @@ class news_shortcodes extends e_shortcode + public function sc_news_nav_url($parm) + { + $url = e107::getUrl()->create('news/list/items'); // default for now. + + if(varset($parm['list']) == 'all') // A list of all items - usually headings and thumbnails + { + $url = e107::getUrl()->create('news/list/all'); + } + elseif(varset($parm['list']) == 'category') + { + $url = e107::getUrl()->create('news/list/short', $this->news_item); //default for now. + } + elseif(varset($parm['items']) == 'category') + { + $url = e107::getUrl()->create('news/list/category', $this->news_item); + } + + return $url; + + } + function sc_newsheader($parm=null) { @@ -1186,5 +1194,82 @@ class news_shortcodes extends e_shortcode } + /** + * @example {NEWS_NAV_NEXT} + * @return string|null + */ + function sc_news_nav_next() + { + return $this->parseNavTemplate('next'); + } + + /** + * {NEWS_NAV_PREVIOUS} + * @return string|null + */ + function sc_news_nav_previous() + { + return $this->parseNavTemplate('previous'); + } + + /** + * {NEWS_NAV_PREVIOUS} + * @return string|null + */ + function sc_news_nav_current() + { + $template = e107::getTemplate('news', 'news_view', 'nav'); + return e107::getParser()->parseTemplate($template['current'], true, $this); + } + + /** + * Retrieves the data from the previous/next news record and renders the corresponding template. + * @param string $type next|previous + * @return string|null + */ + private function parseNavTemplate($type) + { + if(!$data = $this->getNavQuery($type)) + { + return null; + } + + $template = e107::getTemplate('news', 'news_view', 'nav'); + $orig = $this->getScVar('news_item'); + $this->setScVar('news_item', $data); + $text = e107::getParser()->parseTemplate($template[$type], true, $this); + $this->setScVar('news_item', $orig); + + return $text; + } + + /** + * Retrieve table data from the previous or next news item. + * @param string $type next/previous + * @return array|string + */ + private function getNavQuery($type) + { + $nobody_regexp = "'(^|,)(".str_replace(",", "|", e_UC_NOBODY).")(,|$)'"; + + $var = $this->getScVar('news_item'); + + $dir = ($type === 'next') ? '>=' : '<='; + $sort = ($type === 'next') ? 'ASC' : 'DESC'; + + $query = " + SELECT SQL_CALC_FOUND_ROWS n.*, u.user_id, u.user_name, u.user_customtitle, u.user_image, nc.category_id, nc.category_name, nc.category_sef, nc.category_icon, + nc.category_meta_keywords, nc.category_meta_description, nc.category_template + FROM #news AS n + LEFT JOIN #user AS u ON n.news_author = u.user_id + LEFT JOIN #news_category AS nc ON n.news_category = nc.category_id + WHERE n.news_class REGEXP '".e_CLASS_REGEXP."' AND NOT (n.news_class REGEXP ".$nobody_regexp.") + AND n.news_start < ".time()." AND (n.news_end=0 || n.news_end>".time().") + AND (FIND_IN_SET('0', n.news_render_type) OR FIND_IN_SET(1, n.news_render_type)) + AND n.news_datestamp ".$dir . (int) $var['news_datestamp']. " AND n.news_id != ".(int) $var['news_id']." ORDER by n.news_datestamp ".$sort." LIMIT 1"; + + return e107::getDb()->retrieve($query); + } + } -?> + diff --git a/e107_plugins/news/templates/news_view_template.php b/e107_plugins/news/templates/news_view_template.php index ca1148d3e..3515ebcfb 100644 --- a/e107_plugins/news/templates/news_view_template.php +++ b/e107_plugins/news/templates/news_view_template.php @@ -14,13 +14,13 @@ $NEWS_VIEW_INFO = array( 'default' => array('title' => LAN_DEFAULT, 'description' => 'unused'), - 'videos' => array('title' => "Videos (experimental)", 'description' => 'unused'), + 'videos' => array('title' => "Videos (experimental)", 'description' => 'unused'), ); // Default $NEWS_VIEW_WRAPPER['default']['item']['NEWSIMAGE: item=1'] = '{---}'; - +$NEWS_VIEW_WRAPPER['default']['item']['NEWSRELATED'] = '
{---}
'; $NEWS_VIEW_TEMPLATE['default']['caption'] = '{NEWS_TITLE}'; // null; // add a value to user tablerender() $NEWS_VIEW_TEMPLATE['default']['item'] = ' @@ -77,10 +77,13 @@ $NEWS_VIEW_TEMPLATE['default']['item'] = ' -
{NEWSRELATED} -
- {NEWSNAVLINK} + + '; @@ -103,7 +106,10 @@ $NEWS_VIEW_TEMPLATE['default']['item'] = ' // Videos -$NEWS_VIEW_TEMPLATE['videos']['item'] = '
Empty news_view_template.php (videos) - have ideas? let us know.
'; - + $NEWS_VIEW_TEMPLATE['videos']['item'] = '
Empty news_view_template.php (videos) - have ideas? let us know.
'; +// Navigation/Pagination +$NEWS_VIEW_TEMPLATE['nav']['previous'] = '{GLYPH=fa-chevron-left}{NEWS_TITLE} {NEWS_ID}'; +$NEWS_VIEW_TEMPLATE['nav']['current'] = '{LAN=BACK}'; +$NEWS_VIEW_TEMPLATE['nav']['next'] = '{NEWS_ID} {NEWS_TITLE}{GLYPH=fa-chevron-right} ';