Previous/Next Post links

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
Trench
Regular
Posts: 85
Joined: Fri Nov 05, 2004 11:38 am

Previous/Next Post links

Post by Trench »

Is there a way to have Previous Post/Next Post links in the entries?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Previous/Next Post links

Post by garvinhicking »

Well, actually yes - you could do that with a plugin in 0.7 already. I'll post you one tomorrow :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Trench
Regular
Posts: 85
Joined: Fri Nov 05, 2004 11:38 am

Post by Trench »

Thank you. :D
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Please try this plugin:

Code: Select all

<?php # $Id: serendipity_event_entrypaging.php,v 1.1 2004/11/05 10:49:31 garvinhicking Exp $

switch ($serendipity['lang']) {
    case 'de':
        @define('PLUGIN_ENTRYPAGING_NAME', 'Nächster/Voriger Artikel');
        @define('PLUGIN_ENTRYPAGING_BLAHBLAH', 'Ermöglicht bei Betrachten eines Eintrages vor und zurück zu blättern');
        break;

    case 'en':
    default:
        @define('PLUGIN_ENTRYPAGING_NAME', 'Links to next/previous entry');
        @define('PLUGIN_ENTRYPAGING_BLAHBLAH', 'Allows viewing the next/previous entry when viewing');
        break;
}

class serendipity_event_entrypaging extends serendipity_event
{
    var $found_images = array();

    function introspect(&$propbag)
    {
        global $serendipity;

        $propbag->add('name',          PLUGIN_ENTRYPAGING_NAME);
        $propbag->add('description',   PLUGIN_ENTRYPAGING_BLAHBLAH);
        $propbag->add('stackable',     false);
        $propbag->add('author',        'Garvin Hicking');
        $propbag->add('version',       '1.0');
        $propbag->add('event_hooks',   array('entry_display' => true, 'css' => true, 'entries_header' => true));
        $propbag->add('scrambles_true_content', true);
        $propbag->add('configuration', array('extension'));
    }

    function introspect_config_item($name, &$propbag)
    {
        switch($name) {
            case 'extension':
                $propbag->add('type', 'string');
                $propbag->add('name', PLUGIN_ENTRYPAGING_EXTENSION);
                $propbag->add('description', PLUGIN_ENTRYPAGING_EXTENSION_BLAHBLAH);
                $propbag->add('default', 'jpg');
                break;

            default:
                    return false;
        }
        return true;
    }

    function generate_content(&$title)
    {
        $title       = PLUGIN_ENTRYPAGING_NAME;
    }

    function makeLink($resultset) {
        if (is_array($resultset) && is_numeric($resultset[0]['id'])) {
            $link = '<a href="' . serendipity_archiveURL($resultset[0]['id'], $resultset[0]['title']) . '">' . htmlspecialchars($resultset[0]['title']) . '</a>';
            return $link;
        }

        return false;
    }

    function showPaging($id = false) {
        global $serendipity;

        if (!$id) {
            return false;
        }

        $links = array();
        $cond = array();
        $cond['and'] = " AND e.isdraft = 'false' AND e.timestamp <= " . time();
        serendipity_plugin_api::hook_event('frontend_fetchentry', $cond);

        $querystring = "SELECT
                                e.id, e.title
                          FROM
                                {$serendipity['dbPrefix']}entries e
                                {$cond['joins']}
                         WHERE
                                e.id %s " . (int) $id . "
                                {$cond['and']}
                        ORDER BY e.id %s
                        LIMIT  1";

        $prevID = serendipity_db_query(sprintf($querystring, '<', 'DESC'));
        $nextID = serendipity_db_query(sprintf($querystring, '>', 'ASC'));

        if ($link = $this->makeLink($prevID)) {
            $links[] = '< ' . $link;
        }

        if ($link = $this->makeLink($nextID)) {
            $links[] = $link . ' >';
        }

        echo '<div class="serendipity_entrypaging">' . implode(' | ', $links) . '</div>';
    }

    function event_hook($event, &$bag, &$eventData, $addData = null) {
        global $serendipity;

        $hooks = &$bag->get('event_hooks');

        if (isset($hooks[$event])) {
            switch($event) {
                case 'css':
                    if (stristr('.serendipity_entrypaging', $addData)) {
                        // class exists in CSS, so a user has customized it and we don't need default
                        return true;
                    }
?>

.serendipity_entrypaging {
    text-align: center;
    margin-left: auto;
    margin-right: auto;
    border: 0px;
    display: block;
}
<?php
                    return true;
                    break;

                case 'entry_display':
                    if ($bag->get('scrambles_true_content') && is_array($addData) && isset($addData['no_scramble'])) {
                        return true;
                    }

                    if (isset($serendipity['GET']['id']) && is_numeric($serendipity['GET']['id']) && version_compare($serendipity['version'], '0.7', '<=')) {
                        echo $this->showPaging($serendipity['GET']['id']);
                    }

                    return true;
                    break;

                case 'entries_header':
                    // This hook is only available since 0.8
                    if (isset($addData) && is_numeric($addData)) {
                        // single entry case
                        echo $this->showPaging($addData);
                    }
                    return true;
                    break;

                default:
                    return false;
            }
        } else {
            return false;
        }
    }
}

/* vim: set sts=4 ts=4 expandtab : */
?>
It's also in our CVS module 'additional_plugins'. But since SF.net lags about 24 hours, it will show up there tomorrow.
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Trench
Regular
Posts: 85
Joined: Fri Nov 05, 2004 11:38 am

Post by Trench »

This worked perfectly. I have never gotten a plug in to work so easily.
Trench
Regular
Posts: 85
Joined: Fri Nov 05, 2004 11:38 am

Post by Trench »

Garvin, this doesn't seem to be working in v0.7.1

:(
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Thanks, the version check was wrong indeed - just change the '<=' to '<' and instead '0.7' use '0.8' - I also committed this to CVS just now.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply