Location: PHPKode > projects > Mocovie web framework > webs/common/controls/twitter.php
<?php
/**
 *  Copyright (C) 2010  Kai Dorschner
 *
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 *
 * @author Kai Dorschner <the-hide@address.com>
 * @copyright Copyright 2010, Kai Dorschner
 * @license http://www.gnu.org/licenses/gpl.html GPLv3
 * @package mocovi
 * @subpackage controls
 */

/**
 * Twitter Control
 *
 * Collects Tweets from a twitter account.
 *
 * @package mocovi
 * @subpackage controls
 * @todo Twitter Connection Problems must throw Exceptions
 */
class twitter_control extends Control
{
	protected $defaultOptions = array
		( 'user'		=> ''
		, 'maxelements'	=> '6'
		);

	/**
	 * @override
	 */
	protected function program()
	{
		Timer::stop('Loading Twitter Feeds.');
		$this->adoptAttribute('user');
		Framework::required('Zend');
		Framework::load('Service/Twitter.php');
		$twitter = new Zend_Service_Twitter();
		$statuses = $twitter->status->userTimeline
		(
			array
			(
				'screen_name'	=> $this->getOption('user'),
				'count'			=> $this->getOption('maxelements')
			)
		);
		foreach($statuses as $t)
		{
			$element = $this->dom->createElement
				( 'element'
				, htmlspecialchars
					( $t->text
					, ENT_COMPAT // Default. Encodes only double quotes
					, $this->dom->encoding
					)
				);
			$this->setLinks($element);
			$element->setAttribute('image', $t->user->profile_image_url);
			$element->setAttribute('created', date(Translator::translateToken('shorttimeformat')->nodeValue, strtotime($t->created_at)));
			$this->node->appendChild($element);
		}
		Timer::stop('Twitter Feeds loaded.');
	}

	protected function setLinks(DomElement &$element)
	{
		foreach($element->childNodes as $child)
			switch($child->nodeType)
			{
				case XML_TEXT_NODE:
					if(preg_match('/(?:https?|ftp):\/\/[^\s]+/', $child->nodeValue, $matches, PREG_OFFSET_CAPTURE))
					{
						foreach($matches as $match)
						{
							$link	= $match[0];
							$offset	= $match[1];
							$before	= substr($child->nodeValue, 0, $offset);
							$after	= substr($child->nodeValue, $offset + strlen($link));
							$element->removeChild($child);
							ControlFactory::create(new DomText($before))->load($element)->run();
							$anchor = $this->dom->createElement('anchor', $link);
							$anchor->setAttribute('href', $link);
							ControlFactory::create($anchor)->load($element)->run();
							ControlFactory::create(new DomText($after))->load($element)->run();
							
						}
					}
				break;
				case XML_ELEMENT_NODE:
					$this->setLinks($child);
				break;
				default:
					throw new Exception('Type could not be determined.');
			}
	}
}
Return current item: Mocovie web framework