Page 1 of 1

Plugin: In former times

Posted: Sat Sep 17, 2005 4:18 am
by Rembrandt
Hi!

I've written a little plugin called "In former times". It is a sidebar plugin, which displays links to entries you have published exactly one (two, three, upto 10) year(s) ago. Nothing special, but maybe you find it usefull (like me 8) ).

Here comes the code:

Code: Select all

<?php # serendipity_plugin_formertimes.php 2005-09-10 Rembrandt $

// Probe for a language include with constants. Still include defines later on, if some constants were missing
$probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
if (file_exists($probelang)) {
    include $probelang;
}

@define('PLUGIN_FORMERTIMES_NAME', 'In former times');
@define('PLUGIN_FORMERTIMES_BLAHBLAH', 'Shows links to entries posted at the same date one, two, three or upto ten years ago.');
@define('PLUGIN_FORMERTIMES_NEWWIN', 'Open entries in new window?');
@define('PLUGIN_FORMERTIMES_NEWWIN_BLAHBLAH', 'Should the entries be shown in a new window? (Default: Current window)');
@define('PLUGIN_FORMERTIMES_YEARS', 'Years');
@define('PLUGIN_FORMERTIMES_YEARS_BLAHBLAH', 'How many years backwards?');
@define('PLUGIN_FORMERTIMES_YEAR_SING', 'year');
@define('PLUGIN_FORMERTIMES_YEAR_PLUR', 'years');
@define('PLUGIN_FORMERTIMES_YEAR_OUT','Today %s %s ago'); //Number .. Year
@define('PLUGIN_FORMERTIMES_YEAR_1','one');
@define('PLUGIN_FORMERTIMES_YEAR_2','two');
@define('PLUGIN_FORMERTIMES_YEAR_3','three');
@define('PLUGIN_FORMERTIMES_YEAR_4','four');
@define('PLUGIN_FORMERTIMES_YEAR_5','five');
@define('PLUGIN_FORMERTIMES_YEAR_6','six');
@define('PLUGIN_FORMERTIMES_YEAR_7','seven');
@define('PLUGIN_FORMERTIMES_YEAR_8','eight');
@define('PLUGIN_FORMERTIMES_YEAR_9','nine');
@define('PLUGIN_FORMERTIMES_YEAR_10','ten');

class serendipity_plugin_formertimes extends serendipity_plugin
{
    var $title = PLUGIN_FORMERTIMES_NAME;

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

        $this->title = $this->get_config('title', $this->title);

        $propbag->add('name',          PLUGIN_FORMERTIMES_NAME);
        $propbag->add('description',   PLUGIN_FORMERTIMES_BLAHBLAH);
        $propbag->add('stackable',     false);
        $propbag->add('author',        'Sebastian "Rembrandt" Ruhs');
        $propbag->add('version',       '0.1');
        $propbag->add('requirements',  array(
            'serendipity' => '0.8',
            'smarty'      => '2.6.7',
            'php'         => '4.1.0'
        ));
        $propbag->add('groups', array('FRONTEND_VIEWS'));
        $propbag->add('configuration', array('title', 'newwin', 'years'));
    }
    function introspect_config_item($name, &$propbag)
    {
        switch($name) {
            case 'title':
                $propbag->add('type', 'string');
                $propbag->add('name', TITLE);
                $propbag->add('description', TITLE);
                $propbag->add('default', '');
                break;

            case 'newwin':
                $propbag->add('type', 'boolean');
                $propbag->add('name', PLUGIN_FORMERTIMES_NEWWIN);
                $propbag->add('description', PLUGIN_FORMERTIMES_NEWWIN_BLAHBLAH);
                $propbag->add('default', 'false');
                break;

            case 'years':
                $propbag->add('type', 'select');
                $propbag->add('name', PLUGIN_FORMERTIMES_YEARS);
                $propbag->add('description', PLUGIN_FORMERTIMES_YEARS_BLAHBLAH);
                $propbag->add('select_values', array(1,2,3,4,5,6,7,8,9,10));
                $propbag->add('default',2);
                break;

            default:
                    return false;
        }
        return true;
    }

    function generate_content(&$title)
    {
        global $serendipity;

        $title = $this->get_config('title', $this->title);

        $y_array = array(PLUGIN_FORMERTIMES_YEAR_1,PLUGIN_FORMERTIMES_YEAR_2,PLUGIN_FORMERTIMES_YEAR_3,PLUGIN_FORMERTIMES_YEAR_4,
                         PLUGIN_FORMERTIMES_YEAR_5,PLUGIN_FORMERTIMES_YEAR_6,PLUGIN_FORMERTIMES_YEAR_7,PLUGIN_FORMERTIMES_YEAR_8,
                         PLUGIN_FORMERTIMES_YEAR_9,PLUGIN_FORMERTIMES_YEAR_10);

        $now = mktime(0, 0, 0, date('m'), date('d'), date('Y')-1);

        $max_y = $this->get_config('years',4)+1;

        for($y=1; $y<=$max_y; $y++) {
            $linkStamp = date('Y/m/d', $now);
            $now_title = serendipity_formatTime("%Y, %B %e.", $now, false);
            $now = mktime(0, 0, 0, date('m', $now), date('d', $now), date('Y', $now)-1);

            $link = serendipity_rewriteURL(PATH_ARCHIVES . '/' . $linkStamp . '.html', 'serendipityHTTPPath');

            $ys=$y_array[$y-1];

            if ($y==1) { //singular or plural
                $y_title=sprintf(PLUGIN_FORMERTIMES_YEAR_OUT,$ys,PLUGIN_FORMERTIMES_YEAR_SING);
            } else {
                $y_title=sprintf(PLUGIN_FORMERTIMES_YEAR_OUT,$ys,PLUGIN_FORMERTIMES_YEAR_PLUR);
            }

            echo '<a href="' . $link . '" title="' . $now_title . '">' . $y_title . '</a><br />' . "\n";
        }
    }
}

/* vim: set sts=4 ts=4 expandtab : */
?>
You can download a .tgz-file here (including language files for german+UTF8). Just extract it contents in your plugin folder. That' all. ;)

Comments, reviews and suggestions are welcome!

so long,
Rembrandt

Re: Plugin: In former times

Posted: Mon Sep 19, 2005 3:41 pm
by garvinhicking
What exactly did you miss in our bundled "serendipity_plugin_history" plugin? To me it does about the same but being more configurable. :)

Thanks though for getting involved in serendipity development! :)

Regards,
Garvin

Posted: Mon Sep 19, 2005 4:34 pm
by Rembrandt
Hi!

I don't think, that my plugin and the "serendipity_plugin_history" do the same. For example, I was not able to configure the bundled one, to show me the links to entries I published one, two, three years ago.

Maybe I'm blind? ;)

Posted: Mon Sep 19, 2005 4:40 pm
by garvinhicking
Dang, now I understand it. I'm sorry. :)

But maybe you'd like to couple your plugin with the history plugin instead?

Regards,
Garvin

Posted: Mon Sep 19, 2005 4:47 pm
by Rembrandt
No problem.

At the moment I'm working on version 0.2, with a few improvements. Afther finishing that, I could try to merge both plugins. (Did I mention that I hate php? ;))