<?php
function smarty_cms_function_chirps($params, &$smarty)
{
	//Give the user some parameters
	$tag = !empty($params['tag']) ? $params['tag']: 'p';
	$tweets = !empty($params['tweets']) ? $params['tweets']: 10;
	$username = !empty($params['username']) ? $params['username']: '';

	//If the username parameter is not empty
	if (!empty($params['username'])) {

		//Get the timeline
		$timeline = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweets . '&include_rts=1');

		//Convert the timeline into an array
		$json = json_decode($timeline);
 
		//If the username exists and Twitter is not over capacity
		if ($json) {
		
			//Get the current time
			$now = time();

			//Then loop through the timeline
			foreach ($json as $tweet) {

				//Remove the username from each tweet and just get the tweet
				$output = str_replace($username . ': ', '', $tweet->text);

				//Replace the links with a valid link (this has to go first, otherwise @username will get screwed)
				$output = preg_replace('/(https{0,1}:\/\/[\w\-\.\/#?&=]*)/', '<a href="$1">$1</a>', $output);

				//Replace the @username with a valid link
				$output = preg_replace('/@(\w+)/', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $output);

				//Replace the #trend with a valid link
				$output = preg_replace('/#(\w+)/', '<a href="http://twitter.com/search?q=%23$1" target="_blank">#$1</a>', $output);

				//When it was published = now - publish date
				$when = ($now - strtotime($tweet->created_at));

				//Display when the tweet was published, the same way as Twitter does
				if ($when < 60) {
					$posted = $when . ' seconds ago';
				} elseif ($when < 3600) {
					$posted = (floor($when / 60)) . ' minutes ago';
				} elseif ($when < 7200) {
					$posted = '1 hour ago';
				} elseif ($when < 86400) {
					$posted = (floor($when / 3600)) . ' hours ago';
				} elseif ($when > 31556926) {
					$posted = date('j M y', strtotime($tweet->created_at));
				} else {
					$posted = date('j M', strtotime($tweet->created_at));
				}

				//Output for each tweet
				echo '<' . $tag . '>' . $output . ' - ' . $posted . '</' . $tag . '>' . "\n";
			}
		}

		//If the username does not exist or Twitter is over capacity, show a nice error
		else {
			echo '<p>Chirps Plugin: There was an error retrieving your Tweets. The username specified may not exist (<a href="http://twitter.com/' . $username . '">check username &raquo;</a>) or Twitter may be over capacity (<a href="http://status.twitter.com/">check status &raquo;</a>)</p>';
		}

	}

	//If the username parameter is empty, show a nice error
	else {
		echo '<p>Chirps Plugin: You have not set the username parameter.</p>';
	}

}

function smarty_cms_help_function_chirps()
{
?>
	<h3>What does this do?</h3>
	<p>Prints tweets from a specified Twitter account in their simplest form.</p>
	<h3>How do I use it?</h3>
	<p>Simply insert the tag into your page or template with the username parameter: <code>{chirps username="cmsms"}</code></p>
	<p>What parameters does it take?</h3>
	<ul>
		<li><em>(required)</em> <tt>username</tt> - specifies the Twitter account to retrieve tweets from. This is case sensitive and must match how you have it set within Twitter</li>
		<li><em>(optional)</em> <tt>tag</tt> - specifies the tag to start and end each tweet i.e div, h1, h2 (default p, do not include open or closing tags)</li>
		<li><em>(optional)</em> <tt>tweets</tt> - specifies the number of tweets to display (default 10).</li>
	</ul>
<?php
}

function smarty_cms_about_function_chirps()
{
?>
	<p>Author: chris@inctrldesign.co.uk<br /><br /></p>
	<p class="pageheader">Version: 1.0.5</p>
	<p>Change history:</p>
	<ul>
          <li>Change from RSS to JSON<br /><br /></li>
	</ul>
	<p class="pageheader">Version: 1.0.4</p>
	<p>Change history:</p>
	<ul>
          <li>Reading the RSS from the API url<br /><br /></li>
	</ul>
	<p class="pageheader">Version: 1.0.3</p>
	<p>Change history:</p>
	<ul>
          <li>Added error reporting if the username does not exist or if Twitter is over capacity<br /><br /></li>
	</ul>
	<p class="pageheader">Version: 1.0.2</p>
	<p>Change history:</p>
	<ul>
          <li>Fixes to the #trend links<br /><br /></li>
	</ul>
	<p class="pageheader">Version: 1.0.1</p>
	<p>Change history:</p>
	<ul>
          <li>Fixed the help instructions (thanks go to Camille Pansewicz for the early testing of this plugin)<br /><br /></li>
	</ul>
	<p class="pageheader">Version: 1.0.0</p>
	<p>Change history:</p>
	<ul>
          <li>Initial version</li>
	</ul>
<?php
}
?>