#!/usr/bin/perl

# This plugin allows your page templates to include Flattr buttons, so
# Flattr buttons are automatically added on every page that uses the template.
# If you don't specify a thing ID with the flattrthing directive, it adds
# auto-submit buttons so things are created when the button is clicked the first
# time.
#
# The plugin is customizable through the following configuration options:
# - flattrthing_pagespec: allows you to specify a pagespec on which pages the
#   plugin is active. If empty, no Flattr buttons are generated at all.
# - flattrthing_defaultuser: sets the default user that owns the Flattr things
#   which are created by the auto-submit buttons.
#
# Also, the plugin adds the flattrthing directive, which allows you to customize
# the behaviour for one page. It knows the following parameters:
# - id: the Flattr thing ID (like in http://flattr.com/thing/12341234). If you
#   specify this option, a button with a thing URL is generated instead of an
#   auto-submit button. This allows you to link to things which already exist
#   by the time you create your page.
# - user: a Flattr username that owns the thing created by the generated
#   auto-submit button. This parameter has no effect when an ID is specified,
#   since in this case, the owner is already known to Flattr.
#
# This plugin sets the following template variables which your templates should
# process and generate the according buttons with the right URLs:
# - FLATTR_ENABLE: contains a truthful value if the page matches the pagespec
#   configured with flattrthing_pagespec. You can use it with TMPL_IF to
#   generate code specific to pages which have a Flattr button.
# - FLATTR_THING_ID: if a thing ID was specified with the flattrthing directive,
#   this variable contains the ID that was given in the directive.
# - FLATTR_THING_ID_SET: contains a thurthful value if FLATTR_THING_ID is set.
# - FLATTR_USER: contains the username that was specified with the flattrthing
#   directive, or if that was not the case, the username that was configured
#   with flattrthing_defaultuser in your setup file.
# - FLATTR_THING_URL: contains the full URL to the page.

package IkiWiki::Plugin::flattrthing;

use warnings;
use strict;
use IkiWiki 3.00;

sub import {
	hook(type => "getsetup", id => "flattrthing", call => \&getsetup);
	hook(type => "scan", id => "flattrthing", call => \&scan);
	hook(type => "preprocess", id => "flattrthing", call => \&preprocess);
	hook(type => "pagetemplate", id => "flattrthing", call => \&pagetemplate);
}

sub getsetup () {
	return
		plugin => {
			safe => 1,
			rebuild => undef,
			section => "widget",
		},
		flattrthing_pagespec => {
			type => "pagespec",
			example => "!*/Discussion",
			description => "Pages on which flattr button should be inserted. If empty, flattr buttons are not shown on any page.",
			link => "ikiwiki/PageSpec",
			safe => 1,
			rebuild => undef,
		},
		flattrthing_defaultuser => {
			type => "string",
			example => "!*/Discussion",
			description => "Default flattr user that owns a thing if the username is not changed with the flattrthing directive",
			safe => 1,
			rebuild => undef,
		},
}

sub scan {
	my %params = @_;
	my $page = $params{page};
	my $destpage = $params{destpage};
	my $baseurl = $config{url};
	$baseurl .= "/" unless substr($baseurl, -1) eq "/";
	if($destpage) {
		$baseurl .= $destpage;
	} else {
		$baseurl .= $page;
	}
	$pagestate{$page}{flattrthing}{url} = $baseurl;

	if(exists $config{flattrthing_defaultuser} &&
		 length $config{flattrthing_defaultuser}) {
		$pagestate{$page}{flattrthing}{user} = $config{flattrthing_defaultuser};
	} else {
		return error("Please set flattrthing_defaultuser in configuration!");
	}
}

sub preprocess {
	return "" unless @_;
	my %params = @_;

	return error("flattrthing: please specify id or user") unless ($params{id}
		or $params{user});

	my $page = $params{page};
	$pagestate{$page}{flattrthing}{id} = int($params{id}) if $params{id};
	$pagestate{$page}{flattrthing}{user} = int($params{user}) if $params{user};

	return "";
}

sub pagetemplate {
	my %params = @_;
	my $page = $params{page};
	my $template = $params{template};

	if(exists $config{flattrthing_pagespec} &&
		 length $config{flattrthing_pagespec} &&
		 exists $pagestate{$page}{flattrthing}{user} &&
		 length $pagestate{$page}{flattrthing}{user} &&
		 pagespec_match($params{page}, $config{flattrthing_pagespec})) {
		$template->param(FLATTR_ENABLED => 1);
		$template->param(
			FLATTR_THING_URL => $pagestate{$page}{flattrthing}{url},
			FLATTR_USER => $pagestate{$page}{flattrthing}{user},
			);

		if(exists $pagestate{$page}{flattrthing}{id}) {
			$template->param(
				FLATTR_THING_ID_SET => 1,
				FLATTR_THING_ID => $pagestate{$page}{flattrthing}{id},
				);
		}
	}
}

1;
