<?php
// WCF includes
require_once(WCF_DIR.'lib/system/event/EventListener.class.php');

// Utils
require_once(WBB_DIR.'lib/util/TrackbackUtil.class.php');

/**
 * Loads the trackback data for the Threadpage
 *
 * @author	Torben Brodt
 * @package	de.easy-coding.wbb.trackback
 * @license	GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
 */
class ThreadPageTrackbackListener implements EventListener {

	/**
	 * @see EventListener::execute()
	 * @param ThreadPage $eventObj
	 */
	public function execute($eventObj, $className, $eventName) {
		$postTrackbacks = array();
		$check = array_filter($eventObj->postList->posts, create_function('$a', 'return $a->hasTrackback;'));

		if(count($check) > 0) {
			// foreign trackbacks
			$sql = "SELECT		title, 
						excerpt, 
						url, 
						blog_name
				FROM		wbb".WBB_N."_trackback
				WHERE 		postID IN (".$eventObj->postList->postIDs."); ";

			$rows = WBBCore::getDB()->sendQuery($sql);
			while ($row = WBBCore::getDB()->fetchArray($rows)) {
				$postTrackbacks[$row['postID']] = $row;
			}
			
			// fetch data
			$row = TrackbackUtil::getPost($eventObj->postList->postIDs);
			WBBCore::getTPL()->assign(array(
				'postTrackbacks' => $postTrackbacks,
				'postPermalink'=> $row['url'],
				'postID'=> $row['firstPostID'],
				'postTopic'=> $row['topic'],
				'postPreview'=> $row['firstPostPreview'],
				'postUsername'=> $row['username'],
				'postTime'=> $row['time'],
				
			));
			
			WBBCore::getTPL()->append('specialStyles', WBBCore::getTPL()->fetch('postTrackback'));
		}
	}
}
?>
