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

// tagging imports
require_once(WCF_DIR.'lib/util/TaggingReloadedUtil.class.php');

// seo imports
require_once(WCF_DIR.'lib/page/PublicSEORewriterTagging.class.php');

/**
 * Displays the tags for threads/boards
 *
 * @author	Torben Brodt
 * @package	de.easy-coding.wcf.taggingreloaded
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-3.0.html>
 */
class TaggingReloadedPageListener implements EventListener {
	protected static $constructed = false;

	protected $userID=0; // params
	
	// data
	protected $tags = array();
 
	protected $eventObj;
	protected $className;
	protected $rewriter;

	/**
	 * @see EventListener::execute()
	 */
	public function execute($eventObj, $className, $eventName) {
		// abort if user deactivated tagging
		if(!WCF::getUser()->getUserOption('tagging_enable'))
			return;
			
		// is style black?
		if(!self::$constructed) {
			require_once(WCF_DIR.'lib/system/style/Style.class.php');
			$style = new Style(WCF::getUser()->styleID);
			$check = !in_array($style->getVariable('page.background.color'),array('#000','#000000'));
			TaggingReloadedUtil::setBlack($check);
			self::$constructed = false;
		}
	
		$this->eventObj = $eventObj;
		$this->className = $className;
		$this->rewriter = new PublicSEORewriterTagging();

		switch ($eventName) {
			case 'readData':
				$this->readData();
				break;
			case 'readParameters':
				$this->readParameters();
				break;
			case 'assignVariables':
				$this->assignVariables();
				break;
		}
	}
	
	/**
	 * reads by user
	 */
	protected function queryTagsByUser() {
		// order by weight and cut
		$sql = "SELECT		tag,
					SUM(weight) AS weight
			FROM		wcf".WCF_N."_taggingreloaded wcf
			WHERE		userID = ".$this->userID."
			GROUP BY	tag
			ORDER BY	weight DESC
			LIMIT		50";

		return $sql;
	}
	
	/**
         * @see Page::readData()
         */
	protected function readData () {
		if($this->userID) {
			$sql = $this->queryTagsByUser();
		}
		
		// break
		if(!isset($sql)) return;

		// order by tag
		$sql = "SELECT tag,weight FROM ($sql) A ORDER BY tag ASC";
		
		// query
		$result = WCF::getDB()->sendQuery($sql);
		while ($row = WCF::getDB()->fetchArray($result)) {
			$this->tags[$row['tag']] = array(
					'weight'=> $row['weight'],
					'color'=> 0,
					'size'=> 0,
					'url' => $this->rewriter->publicParseTagURLs($row['tag'])
				);
		}

		$this->tags = TaggingReloadedUtil::beautify($this->tags);
	}

	/**
         * @see Page::readParameters()
         */
	protected function readParameters () {
		if (isset($_GET['userID'])) $this->userID = intval($_GET['userID']);
	}
	
	/**
	 * creates a new tagging category
         */
	protected function assignCategory () {
		$script = '<script type="text/javascript">
		//<![CDATA[
		if(document.getElementsByName("taggingCloud").length > 0) {
			var taggingroot = document.getElementsByName("taggingCloud")[0].parentNode;
			taggingroot.parentNode.removeChild(taggingroot.previousSibling.previousSibling);
			taggingroot.className = "";
		}
		//]]>
		</script>';
	
		$this->eventObj->categories['profile.tagging'] = array(
				'categoryName' => 'tagging',
				'categoryIconM' => RELATIVE_WCF_DIR.'icon/tagging24.png',
				'parentCategoryName' => 'profile',
				'options' => array(
					array(
					        'optionName'=> 'tagging',
					        'categoryName'=> 'profile.tagging',
					        'optionValue'=> WCF::getTPL()->fetch('taggingCloud').$script
					)
				)
			);
	}

	/**
         * @see Page::assignVariables()
         */
	protected function assignVariables () {
		if(count($this->tags) > 0) {
			WCF::getTPL()->assign('tags', $this->tags);
			$this->assignCategory();
			WCF::getTPL()->append('specialStyles', '<link rel="stylesheet" type="text/css" href="'.RELATIVE_WCF_DIR.'style/taggingreloaded.css" />');
		}
	}
}
?>
