<?php
#FB Embedded Posts for CMS - CMS Made Simple
#(c)2011 by Oliver Seddon (oliver@threefold.co.uk)
#This project's homepage is: http://dev.cmsmadesimple.org/projects/fbposts
#
#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 fbposts {
	var $posts_domain;
	var $posts_width;
	  
	function __constructor($posts_domain = '', $posts_width = '500'){	
		$this->fbposts($posts_domain, $posts_width);
	}
	 
	function fbposts($posts_domain, $posts_width) {
		$this->posts_domain = $posts_domain;
		$this->posts_width = $posts_width;
	
		if(strlen($posts_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="'. $posts_domain .'" data-width="'. $posts_width .'"></div>';
		}
	}
	
	function showDebugInfo() {
		echo "<ul><li><strong>posts_domain:</strong> $this->posts_domain</li>\n";
		echo "<ul><li><strong>posts_width:</strong> $this->posts_width</li>\n";
		echo "</ul>\n";
	}
}


function smarty_cms_function_fbposts($params, &$smarty) {
	$gCms = cmsms();
	$config =& $gCms->GetConfig();
	
	$debug = isset($params['debug']) ? $params['debug']:'false';
	$posts_domain = isset($params['posts_domain']) ? $params['posts_domain']:'';
	$posts_width = isset($params['posts_width']) ? $params['posts_width']:'';
	
	$fbposts = new fbposts($posts_domain, $posts_width);
	
	if($debug != 'false') {
		$fbposts->showDebuginfo();
	}
}


function smarty_cms_help_function_fbposts() {
?>
<h3>What does this do?</h3>
<p>Embedded Posts are a simple way to put public posts - by a Page or a person on Facebook - into the content of your web site or web page. Only public posts from Facebook Pages and profiles can be embedded.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template/page like <code>{fbposts posts_domain='https://www.facebook.com/FacebookDevelopers/posts/10151471074398553'}</code>.</p>
<h3>What parameters does it take?</h3>
<ul>
<li><tt>posts_domain='https://www.facebook.com/FacebookDevelopers/posts/10151471074398553'</tt> - The URL of the post that you want to embed.</li>
<li><em>(optional)</em> <tt>posts_width='500'</tt> - The pixel width of the post (between 350 and 750). Default is "500".</li>
<li><em>(optional)</em> <tt>debug='false'</tt> - Enable debug mode. Default is "false".</li>
</ul>

<?php
}

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