# Album Plugin:	captions/paypal/simple
# For info:	'album -plugin_info captions/paypal/simple'
# For usage:	'album -plugin_usage captions/paypal/simple'
use strict;

sub start_plugin {
  my ($opt,$plugin,$path) = @_;

  # Setup my hooks
  album::hook($opt,'modify_caption',\&modify_caption);
  album::add_option(1,'paypal_email',$album::OPTION_STR, usage=>"Your paypal email address");
  album::add_option(1,'paypal_price',$album::OPTION_STR, usage=>"Price of images");

  return {
    author => 'David Ljung Madison',
    href => 'http://MarginalHacks.com/',
    version => '1.0',
    description =>
"Paypal linker.
Adds simple paypal links to each image.

You need to set the email with the -paypal_email <email> option.
You can also set a default price with the -paypal_price <price> option,
or else specify a price in the beginning of each caption, such as:

sunset.jpg	Beautiful Sunset	$21.50 Here is the caption
",
  };
}

sub modify_caption {
  my ($opt, $data, $hookname, $dir, $pic, $cap) = @_;

  # Make sure we have the options
  album::fatal($opt,"Need to specify paypal email address with -paypal_email")
    unless $opt->{paypal_email};

  # Figure out the price
  my $price = $opt->{paypal_price};
  $price = $1 if $cap =~ s/^\s*\$(\d+(\.\d+)?)(\s+|$)//;
  return $cap unless $price;

  # Item number (don't know how paypal uses this..
  my $num = ($pic =~ /(\d+)/) ? $1 : 1;

  # Do we need to ensure regen of the HTML?
  album::new_html($opt,$data,$pic)
    if album::option_changed($opt,'paypal_price');

  # The new caption with the paypal button
  $cap . <<PAYPAL;
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="$opt->{paypal_email}">
<input type="hidden" name="item_name" value="$pic">
<input type="hidden" name="item_number" value="$num">
<input type="hidden" name="amount" value="$price">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
PAYPAL
}

# Plugins always end with:
1;
