# Album Plugin:	social/facebook-comments
# For info:	'album -plugin_info social/facebook-comments'
# For usage:	'album -plugin_usage social/facebook-comments'

# Version history:
# v1.01 David Ljung Madison <contactRequest at GetDave.com>
# 			+ Cleaned up code by moving it to format/opengraph
# v1.00 David Ljung Madison <contactRequest at GetDave.com>
#       + released

use strict;

my $DESCRIPTION = << 'DESCRIPTION';
Plugin: social/facebook-comments

Include facebook comments on images/albums using FB comment plugin:

	https://developers.facebook.com/docs/plugins/comments/

Evidently there isn't any way to get notified on FB if anyone leaves
comments on your album, but you can specify a user or app_id to be a
moderator, and then you can go to the moderation tool to see them:

	https://developers.facebook.com/tools/comments?view=queue

If anyone knows how to create comment boxes that have automatic
subscribers, please let me know!  (Though I can see how FB would
be worried that someone would use this for spamming).

Facebook may update/change their code, in which case this plugin
will no longer work without being updated!  If you find that the
code this plugin generates is out-of-date, make sure you have the
most current version of album and it's plugins from:

  http://MarginalHacks.com/

And if so, please contact us and let us know!

This plugin requires the use of the 'format/opengraph' plugin

DESCRIPTION

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

	# Requires 'format/opengraph' plugin
	album::get_plugin($opt,'facebook-comments','format/opengraph');

	# Setup the options
	album::add_option(1, 'user', album::OPTION_STR, usage=>"Facebook user id to moderate comments [optional]");
	album::add_option(1, 'app_id', album::OPTION_STR, usage=>"Facebook app_id to moderate comments with a FB app [optional]");
	album::add_option(1, 'width', album::OPTION_NUM, default=>650, usage=>"Comment box width");
	album::add_option(1, 'numcomments', album::OPTION_BOOL, default=>1, usage=>"Display number of comments on album pages");
	album::add_option(1, 'numposts', album::OPTION_NUM, default=>3, usage=>"Number of posts to show");
	album::add_option(1, 'colorscheme', album::OPTION_STR, usage=>"Colorscheme (either 'light' or 'dark')");

	# Setup the hooks
	album::hook($opt, 'do_album', \&setup_head);
  album::hook($opt, 'end_handle_file',\&add_facebook_info);

	return {
		author => 'David Ljung Madison',
		href => 'http://MarginalHacks.com/',
		version => '1.01',
		description => $DESCRIPTION,
	};
}

sub setup_head {
	my ($opt,$data, undef,  $dir, $album) = @_;

	# Add it to the head
	$data->{head} .= <<FACEBOOK_JS unless $data->{head} =~ /fb-root/;
    <!-- Added by plugin social/facebook-comments -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk')); </script>
		<!-- END code for plugin social/facebook-comments   -->
FACEBOOK_JS

	# Still do the album!
	return 0;
}

sub add_facebook_info {
  my ($opt, $data, $hookname, $dir, $pic, $obj, $path) = @_;

	# Calculated by the opengraph plugin (which we require)
	my $image_page_url = $obj->{opengraph_image_url};

	# Options
	my $width = album::option($opt, 'width');
	my $numcomments = album::option($opt, 'numcomments');
	my $numposts = album::option($opt, 'numposts');
	my $user = album::option($opt, 'user');
	my $app_id = album::option($opt, 'app_id');
	my $colorscheme = album::option($opt, 'colorscheme') ||
		album::option($opt, 'social/facebook-like:colorscheme');
	$colorscheme = ($colorscheme eq 'dark') ? 'dark' : 'light';

	$obj->{head} .= "\t\t<meta property=\"fb:admins\" content=\"$user\"/>" if $user && !$app_id && $obj->{head} !~ /fb:admins/;
	$obj->{head} .= "\t\t<meta property=\"fb:app_id\" content=\"$app_id\"/>" if $app_id && $obj->{head} !~ /fb:app_id/;

	# Add the Facebook div for comments
	$obj->{cap_image} .= <<COMMENTS;
<br />
<div class="fb-comments" data-href="$image_page_url" data-width="$width" data-numposts="$numposts" data-colorscheme="$colorscheme"></div>
COMMENTS

	$obj->{cap_album} .= <<NUMCOMMENTS if $numcomments;
 <i>(<fb:comments-count href="$image_page_url"></fb:comments-count> comments)</i>
NUMCOMMENTS

}

# Plugins always end with:
1;
