<?php
#FB Feed for CMS - CMS Made Simple
#(c)2011 by Oliver Seddon (oliver@threefold.co.uk)
#This project's homepage is: http://dev.cmsmadesimple.org/projects/fbfeed
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

class fbfeed {
	var $feed_domain;
	var $feed_action;
	var $feed_width;
	var $feed_height;
	var $feed_age;
	var $feed_scheme;
	var $feed_header;
	var $feed_id;
	var $feed_filter;
	var $feed_linktarget;
	var $feed_recommendations;
	var $feed_ref;
	  
	function __constructor($feed_domain = '', $feed_action = 'likes, recommends', $feed_width = '300', $feed_height = '300', $feed_age = '0', $feed_scheme = 'light', $feed_header = 'true', $feed_id = 'true', $feed_filter = 'true', $feed_linktarget = 'true', $feed_recommendations = 'true', $feed_ref = 'true'){	
		$this->fbfeed($feed_domain, $feed_action, $feed_width, $feed_height, $feed_age, $feed_scheme, $feed_header, $feed_id, $feed_filter, $feed_linktarget, $feed_recommendations, $feed_ref);
	}
	 
	function fbfeed($feed_domain, $feed_action, $feed_width, $feed_height, $feed_age, $feed_scheme, $feed_header, $feed_id, $feed_filter, $feed_linktarget, $feed_recommendations, $feed_ref) {
		$this->feed_domain = $feed_domain;
		$this->feed_action = $feed_action;
		$this->feed_width = $feed_width;
		$this->feed_height = $feed_height;
		$this->feed_age = $feed_age;
		$this->feed_scheme = $feed_scheme;
		$this->feed_header = $feed_header;
		$this->feed_id = $feed_id;
		$this->feed_filter = $feed_filter;
		$this->feed_linktarget = $feed_linktarget;
		$this->feed_recommendations = $feed_recommendations;
		$this->feed_ref = $feed_ref;
	
		if(strlen($feed_domain) == '0') {
			// domain not specified
			$this->showError(1);
			return false;
		} else {
			echo '<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_GB/sdk.js#xfbml=1&version=v2.0";
					fjs.parentNode.insertBefore(js, fjs);
				}(document, \'script\', \'facebook-jssdk\'));
			</script>

			<div class="fb-activity" data-site="'. $feed_domain .'" data-action="'. $feed_action .'" data-width="'. $feed_width .'" data-height="'. $feed_height .'" data-max-age="'. $feed_age .'" data-colorscheme="'. $feed_scheme .'" data-header="'. $feed_header .'" data-app-id="'. $feed_id .'" data-filter="'. $feed_filter .'" data-linktarget="'. $feed_linktarget .'" data-recommendations="'. $feed_recommendations .'" data-ref="'. $feed_ref .'"></div>';
		}
	}
	
	function showDebugInfo() {
		echo "<ul><li><strong>feed_domain:</strong> $this->feed_domain</li>\n";
		echo "<li><strong>feed_action:</strong> $this->feed_action</li>\n";
		echo "<li><strong>feed_width:</strong> $this->feed_width</li>\n";
		echo "<li><strong>feed_height:</strong> $this->feed_height</li>\n";
		echo "<li><strong>feed_age:</strong> $this->feed_age</li>\n";
		echo "<li><strong>feed_scheme:</strong> $this->feed_scheme</li>\n";
		echo "<li><strong>feed_header:</strong> $this->feed_header</li>\n";
		echo "<li><strong>feed_id:</strong> $this->feed_id</li>\n";
		echo "<li><strong>feed_filter:</strong> $this->feed_filter</li>\n";
		echo "<li><strong>feed_linktarget:</strong> $this->feed_linktarget</li>\n";
		echo "<li><strong>feed_recommendations:</strong> $this->feed_recommendations</li>\n";
		echo "<li><strong>feed_ref:</strong> $this->feed_ref</li>\n";
		echo "</ul>\n";
	}
}



function smarty_cms_function_fbfeed($params, &$smarty) {
	$gCms = cmsms();
	$config =& $gCms->GetConfig();
	
	$debug = isset($params['debug']) ? $params['debug']:'false';
	$feed_domain = isset($params['feed_domain']) ? $params['feed_domain']:'';
	$feed_action = isset($params['feed_action']) ? $params['feed_action']:'likes, recommends';
	$feed_width = isset($params['feed_width']) ? $params['feed_width']:'300';
	$feed_height = isset($params['feed_height']) ? $params['feed_height']:'300';
	$feed_age = isset($params['feed_age']) ? $params['feed_age']:'0';
	$feed_scheme = isset($params['feed_scheme']) ? $params['feed_scheme']:'light';
	$feed_header = isset($params['feed_header']) ? $params['feed_header']:'true';
	$feed_id = isset($params['feed_id']) ? $params['feed_id']:'true';
	$feed_filter = isset($params['feed_filter']) ? $params['feed_filter']:'true';
	$feed_linktarget = isset($params['feed_linktarget']) ? $params['feed_linktarget']:'true';
	$feed_recommendations = isset($params['feed_recommendations']) ? $params['feed_recommendations']:'true';
	$feed_ref = isset($params['feed_ref']) ? $params['feed_ref']:'true';
	
	$fbfeed = new fbfeed($feed_domain, $feed_action, $feed_width, $feed_height, $feed_age, $feed_colour, $feed_header, $feed_id, $feed_filter, $feed_linktarget, $feed_recommendations, $feed_ref);
	
	if($debug != 'false') {
		$fbfeed->showDebuginfo();
	}
}


function smarty_cms_help_function_fbfeed() {
?>
<h3>What does this do?</h3>
<p>The Activity feed displays the most interesting, recent activity taking place on your site, using actions (such as likes) by your friends and other people.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template/page like <code>{fbfeed feed_domain='developers.facebook.com'}</code>.</p>
<h3>What parameters does it take?</h3>
<ul>
<li><tt>feed_domain='developers.facebook.com'</tt> - The domain you want to show activity for.</li>
<li><em>(optional)</em> <tt>feed_action='likes, recommends'</tt> - A comma seperatedlist of actions to show activities for, options are . Default is "likes, recommends".</li>
<li><em>(optional)</em> <tt>feed_width='300'</tt> - The pixel width of the plugin. Default is "300".</li>
<li><em>(optional)</em> <tt>feed_height='300'</tt> - The pixel height of the plugin. Default is "300".</li>
<li><em>(optional)</em> <tt>feed_age='0'</tt> - The oldest an item can be in days. Maximum is 180. Default is "0".</li>
<li><em>(optional)</em> <tt>feed_scheme='light'</tt> - The colour scheme used by the plugin. Can be "light" or "dark". Default is "light".</li>
<li><em>(optional)</em> <tt>feed_header='light'</tt> - Show the Recent Activity header above the feed. Can be "true" or "false". Default is "true".</li>
<li><em>(optional)</em> <tt>feed_id=''</tt> - Display all actions associated with this app ID. This is usually inferred from the app ID you use to initiate the [JavaScript SDK].</li>
<li><em>(optional)</em> <tt>feed_filter=''</tt> - Allows you to filter which URLs are shown in the plugin. For example, if the site parameter is set to 'www.example.com' and the filter parameter was set to '/section1/section2' then only pages which matched 'http://www.example.com/section1/section2/*' would be included in the activity feed section of this plugin. This filter does not apply to any recommendations which may appear in this plugin (see recommendations setting).</li>
<li><em>(optional)</em> <tt>feed_linktarget='_blank'</tt> - Determines what happens when people click on the links in the feed. Can be any of the standard HTML target values. Default is "_blank".</li>
<li><em>(optional)</em> <tt>feed_recommendations='false'</tt> - Specifies whether to always show recommendations (Articles liked by a high amount of people) in the bottom half of the feed. Can be "true" or "false". Default is "false".</li>
<li><em>(optional)</em> <tt>feed_ref=''</tt> - A label for tracking referrals which must be less than 50 characters and can contain alphanumeric characters and some punctuation (currently +/=-.:_).</li>
<li><em>(optional)</em> <tt>debug='false'</tt> - Enable debug mode. Default is false.</li>
</ul>

<?php
}

function smarty_cms_about_function_fbfeed() {
?>
	<p><strong>Author:</strong> Oliver Seddon <a href=\"mailto:oliver@threefold.co.uk\">oliver@threefold.co.uk</a><br /><br /></p>
    <p>Version 1.0<br /><br /></p>
    <p>Change History:<br />
    1.0 - Alpha version released.</p>
<?php
}
?>