Plugin: In former times

Creating and modifying plugins.
Post Reply
Rembrandt
Regular
Posts: 71
Joined: Mon Dec 20, 2004 5:10 pm
Location: Germany
Contact:

Plugin: In former times

Post 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
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Plugin: In former times

Post 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
# 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/
Rembrandt
Regular
Posts: 71
Joined: Mon Dec 20, 2004 5:10 pm
Location: Germany
Contact:

Post 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? ;)
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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
# 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/
Rembrandt
Regular
Posts: 71
Joined: Mon Dec 20, 2004 5:10 pm
Location: Germany
Contact:

Post 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? ;))
Post Reply