<?php
#FB Facepile for CMS - CMS Made Simple
#(c)2011 by Oliver Seddon (oliver@threefold.co.uk)
#This project's homepage is: http://dev.cmsmadesimple.org/projects/fbfacepile
#
#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 fbfacepile {
	var $facepile_domain;
	var $facepile_scheme;
	var $facepile_action;
	var $facepile_id;
	var $facepile_maxrows;
	var $facepile_size;
	var $facepile_width;

	function __constructor($facepile_domain = '', $facepile_scheme = 'light', $facepile_action = 'like', $facepile_id = '', $facepile_maxrows = '1', $facepile_size = 'medium', $facepile_width = '300'){	
		$this->fbfacepile($facepile_domain, $facepile_scheme, $facepile_action, $facepile_id, $facepile_maxrows, $facepile_size, $facepile_width);
	}

	function fbfacepile($facepile_domain, $facepile_scheme, $facepile_action, $facepile_id, $facepile_maxrows, $facepile_size, $facepile_width) {
		$this->facepile_domain = $facepile_domain;
		$this->facepile_scheme = $facepile_scheme;
		$this->facepile_action = $facepile_action;
		$this->facepile_id = $facepile_id;
		$this->facepile_maxrows = $facepile_maxrows;
		$this->facepile_size = $facepile_size;
		$this->facepile_width = $facepile_width;

		if(strlen($facepile_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-post" data-href="'. $facepile_domain .'" data-colorscheme="'. $facepile_scheme .'" data-action="'. $facepile_action .'" data-layout="'. $facepile_layout .'" data-show-faces="'. $facepile_faces .'" data-width="'. $facepile_width .'" data-width="'. $facepile_width .'"></div>';
		}
	}

	function showDebugInfo() {
		echo "<ul><li><strong>facepile_domain:</strong> $this->facepile_domain</li>\n";
		echo "<ul><li><strong>facepile_scheme:</strong> $this->facepile_scheme</li>\n";
		echo "<ul><li><strong>facepile_action:</strong> $this->facepile_action</li>\n";
		echo "<ul><li><strong>facepile_id:</strong> $this->facepile_id</li>\n";
		echo "<ul><li><strong>facepile_maxrows:</strong> $this->facepile_maxrows</li>\n";
		echo "<ul><li><strong>facepile_size:</strong> $this->facepile_size</li>\n";
		echo "<ul><li><strong>facepile_width:</strong> $this->facepile_width</li>\n";
		echo "</ul>\n";
	}
}


function smarty_cms_function_fbfacepile($params, &$smarty) {
	$gCms = cmsms();
	$config =& $gCms->GetConfig();
	
	$debug = isset($params['debug']) ? $params['debug']:'false';
	$facepile_domain = isset($params['facepile_domain']) ? $params['facepile_domain']:'';
	$facepile_scheme = isset($params['facepile_scheme']) ? $params['facepile_scheme']:'';
	$facepile_action = isset($params['facepile_action']) ? $params['facepile_action']:'';
	$facepile_id = isset($params['facepile_id']) ? $params['facepile_id']:'';
	$facepile_maxrows = isset($params['facepile_maxrows']) ? $params['facepile_maxrows']:'';
	$facepile_size = isset($params['facepile_size']) ? $params['facepile_size']:'';
	$facepile_width = isset($params['facepile_width']) ? $params['facepile_width']:'';
	
	$fbfacepile = new fbfacepile($facepile_domain, $facepile_scheme, $facepile_action, $facepile_id, $facepile_maxrows, $facepile_size, $facepile_width);
	
	if($debug != 'false') {
		$fbfacepile->showDebuginfo();
	}
}


function smarty_cms_help_function_fbfacepile() {
?>
<h3>What does this do?</h3>
<p>The Facepile plugin displays the Facebook profile photos of people who have connected with your Facebook page or app.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template/page like <code>{fbfacepile facepile_domain='https://www.facebook.com/FacebookDevelopers'}</code>.</p>
<h3>What parameters does it take?</h3>
<ul>
<li><tt>facepile_domain='https://www.facebook.com/FacebookDevelopers'</tt> - The Facebook.com profile URL of the user to follow.</li>
<li><em>(optional)</em> <tt>facepile_scheme='light'</tt> - The color scheme used by the plugin. Can be "light" or "dark". Default is "light".</li>
<li><em>(optional)</em> <tt>facepile_action='like'</tt> - An Open Graph action type, such as like or customapp:run. Default is "like".</li>
<li><em>(optional)</em> <tt>facepile_id=''</tt> - Specify this if you want the plugin to display users who have connected to your site using Facebook Login. If you are using the JavaScript SDK, this will be automatically specified.</li>
<li><em>(optional)</em> <tt>facepile_maxrows='1'</tt> - The maximum number of rows of faces to display. Note that if there are fewer faces to display than rows specified, the plugin will pick the next lowest number of rows. Default is "1".</li>
<li><em>(optional)</em> <tt>facepile_size='medium'</tt> - Controls the size of the photos shown in the plugin. Can be "small", "medium" or "large". Default is "medium".</li>
<li><em>(optional)</em> <tt>facepile_width=''</tt> - The width of the plugin. The layout you choose affects the minimum and default widths you can use.</li>
<li><em>(optional)</em> <tt>debug='false'</tt> - Enable debug mode. Default is "false".</li>
</ul>

<?php
}

function smarty_cms_about_function_fbfacepile() {
?>
	<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
}
?>