<?php

////////////////////////////////////////////////////
//
// Version 0.1
// Author: Eduardo Martinez (edmartgt@yahoo.com)
// Website: http://www.digitalfrontend.com
//
////////////////////////////////////////////////////

function smarty_cms_function_FreshContent($params, &$smarty)
{
	global $gCms;
	$db =& $gCms->GetDb();
	
	// The alias of the page to check
	$the_alias = isset($params['alias']) ? $params['alias'] : ""; 

  // Max number of days for a page to be considered "fresh"
	$the_days = isset($params['days']) ? $params['days'] : "";

	// HTML that will be output if page is at least x days "fresh"
	$the_output = isset($params['output']) ? $params['output'] : "";

	$query = "SELECT modified_date FROM ".cms_db_prefix()."content WHERE content_alias=?";
	$row = $db->GetRow($query, array($the_alias));
	
	if($row)
	{
		 $last_mod = strtotime($row['modified_date']);

		 $today = time();

		 $date_diff = floor(($today - $last_mod)/86400);
		
		 if ($date_diff < $the_days)
		 {
				return $the_output;
		 }
	}
	
	else
	{
		return "Page content not found, check the alias.";
	}
}

function smarty_cms_help_function_FreshContent() 
{?>
	<h3>What does this do?</h3>
	<p>This plugin checks the 'freshness' of a content page, it's done by using the amount of days provided in the call</p>
	<h3>Parameters</h3>
	<p><em>alias</em> - The alias of the content page that you want to check the "freshness" for<br />
	<em>days</em> - The max number of days that a page is considered new<br />
	<em>output</em> - The HTML that you want to output if the page being checked, is new</p>
	<h3>Usage</h3>
	<strong>Example 1 (Basic):</strong>
	<pre>{FreshContent alias='home' days='10' output='new!'}</pre>
	This will check if the page with the 'home' alias is 10 days or newer and output the word "new!" if it is.
	<br /><br />
	<strong>Example 2 (Menu Manager):</strong>
	<pre>{foreach from=$nodelist item=node}
...
&lt;li&gt;{FreshContent alias=$node->alias days='5' output='new!'}&lt;/li&gt;
...
{/foreach}</pre>
	This example is for using it in a Menu Manager template, and will check if the current node with the 'home' alias is 10 days or newer and output the word "new!" if it is.
	<br /><br />
	<strong>Example 3 (Page Templates):</strong><br />
	<pre>{FreshContent alias=$page_alias days='5' output='&lt;div class="notice"&gt;&lt;img src="star.gif" /&gt;This page is new!&lt;/div&gt;'}</pre>
	<br />
	When using this within a Page Template, it will automatically echo the output html (star image with text next to it) if the page is 5days old or newer.
	<br /><br />
	As you can see this is a very simple yet flexible plugin. Feel free to send your comments to: <a href="mailto:edmartgt@yahoo.com">edmartgt@yahoo.com</a>.
<?php }

function smarty_cms_about_function_FreshContent() 
{?>
	<p><strong>Author: </strong>Eduardo Martinez<br />
	<strong>Version: </strong>0.1<br />
	<strong>Change History: </strong><br />
	<ul>
		<li>0.1 - Initial release</li>
	</ul>
	</p>
<?php } 
?>



