Prev / Next for CGBlog

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
User avatar
Gregor
Power Poster
Power Poster
Posts: 1872
Joined: Thu Mar 23, 2006 9:25 am
Location: The Netherlands

Prev / Next for CGBlog

Post by Gregor »

After intensive puzzling, trying, testing, re-trying, I think I made the UDT prev_next working for cgblog.

Two things I did to get it working.

First the create a UDT named prev_next (actually changed the code used with news in another topic):

Code: Select all

if (!function_exists('MyGetModuleInstance'))
{
        function &MyGetModuleInstance($module)
        {
                global $gCms;

                if (isset($gCms->modules[$module]) &&
                        $gCms->modules[$module]['installed'] == true &&
                        $gCms->modules[$module]['active'] == true)
                {
                        return $gCms->modules[$module]['object'];
                }
                // Fix only variable references should be returned by reference
                $tmp = FALSE;
                return $tmp;
        }
}

$gCms = cmsms();
$db = cmsms()->GetDb();    // changed this line.
$cgblog = cms_utils::get_module('CGBlog');  // changed this line.

// call from cgblog detail_template: {prev_next pageid=$page_id currid=$entry->id currdate=$entry->postdate}
$pageid = ($_REQUEST['pageid']) ? $_REQUEST['pageid'] : $params['pageid'];
$currid = ($_REQUEST['currid']) ? $_REQUEST['currid'] : $params['currid'];  // added this line for testing purposes.
$currdate = $params['currdate'];
if(!$currdate) return;

// call from cgblog detail_template: {prev_next pageid=$page_id currid=$entry->id currdate=$entry->postdate}

// For performance, added an index over two columns on ".cms_db_prefix()."module_cgblog_blog_categories

// get all CGBlog articles sorted by ascending date
$query_next = "
SELECT cgblog_id, cgblog_title
FROM ".cms_db_prefix()."module_cgblog
WHERE cgblog_date > ?
AND status = 'published'
AND (end_time >= CURRENT_DATE OR end_time IS NULL) 
AND cgblog_id IN
   ((SELECT blog_id
   FROM ".cms_db_prefix()."module_cgblog_blog_categories
   WHERE category_id IN
     (SELECT category_id 
     FROM ".cms_db_prefix()."module_cgblog_blog_categories
     WHERE blog_id = ".$currid.")))
ORDER BY cgblog_date
ASC LIMIT 1";

$result_next =& $db->GetRow($query_next,array($currdate)); 

$query_prev = "
SELECT cgblog_id, cgblog_title
FROM ".cms_db_prefix()."module_cgblog
WHERE cgblog_date < ?
AND status = 'published'
AND (end_time >= CURRENT_DATE OR end_time IS NULL) 
AND cgblog_id IN
   ((SELECT blog_id
   FROM ".cms_db_prefix()."module_cgblog_blog_categories
   WHERE category_id IN
     (SELECT category_id 
     FROM ".cms_db_prefix()."module_cgblog_blog_categories
     WHERE blog_id = ".$currid.")))
ORDER BY cgblog_date
DESC LIMIT 1";

// end of get all CGBlog items 

$result_prev = &$db->GetRow($query_prev,array($currdate));

if($result_next['cgblog_id']) {
$aliased_title = munge_string_to_url($result_next['cgblog_title']);
$prettyurl = 'logboek/' . $result_next["cgblog_id"] .'/'.$pageid."/$aliased_title";
$next_uri = $cgblog->CreateLink('cntnt01', 'detail', $pageid, '', array('articleid' => $result_next["cgblog_id"]) ,'', true, false, '', true, $prettyurl);
}else{
$next_uri = "";
}


if($result_prev['cgblog_id']) {
$aliased_title = munge_string_to_url($result_prev['cgblog_title']);
$prettyurl = 'logboek/' . $result_prev["cgblog_id"] .'/'.$pageid."/$aliased_title";
$prev_uri = $cgblog->CreateLink('cntnt02', 'detail', $pageid, '', array('articleid' => $result_prev["cgblog_id"]) ,'', true, false, '', true, $prettyurl);
}else{
$prev_uri = "";
}

$smarty->assign('cgblog_next_url', $next_uri);
$smarty->assign('cgblog_next_text', $cgblog->lang("next"));
$smarty->assign('cgblog_prev_url', $prev_uri);
$smarty->assign('cgblog_prev_text', $cgblog->lang("prev"));
This query is quit heavy for the database because of the three nested select-statements. To reach an acceptable performance, I created an extra index over both columns on
cms_module_cgblog_blog_categories
type of index is 'Primary'. This helped to reduce the execution time from 15sec to far less than a second.

A working example can be found at my site http://www.uisge-beatha.eu/logboek/270/ ... trijd.html

If one sees any improvements or suggestions, please let me know.

Gregor
arnoud
Power Poster
Power Poster
Posts: 1399
Joined: Sun Apr 19, 2009 9:33 am

Re: Prev / Next for CGBlog

Post by arnoud »

----------
Last edited by arnoud on Tue Mar 12, 2013 5:55 pm, edited 1 time in total.
User avatar
Gregor
Power Poster
Power Poster
Posts: 1872
Joined: Thu Mar 23, 2006 9:25 am
Location: The Netherlands

Re: Prev / Next for CGBlog

Post by Gregor »

Thanks Arnoud. Removed it. It was there ever since :)

Gregor
User avatar
manuel
Power Poster
Power Poster
Posts: 350
Joined: Fri Nov 30, 2007 9:15 am

Re: Prev / Next for CGBlog

Post by manuel »

Here is my solution for adding prev/next links in the CGBlog detail pages.
You should be able to easily change this to work for the news module...

http://www.i-do-this.com/blog/72/Prev-N ... -in-CGBlog

Greetings,
Manuel
Do you like your open source cms? Buy from the CMSMS partners || Donate
chrismarie

Re: Prev / Next for CGBlog

Post by chrismarie »

Gregor, thanks for the update on that UDT, and Manuel, your solution is also a winner! Thanks!
Post Reply

Return to “Tips and Tricks”