<?php if(!defined('master_script')) die();

/**
 * Default settings
 *
 * @category   Module (tag-cloud)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-06-06
 */
	
	global $page_info, $tags, $target, $scale, $tagged_pages;

	if(!isset($module_settings["tag-cloud"]["target page"])) $module_settings["tag-cloud"]["target page"] = '';
	if(!isset($module_settings["tag-cloud"]["hide tags"])) $module_settings["tag-cloud"]["hide tags"] = 0;
	if(!isset($module_settings["tag-cloud"]["sort method"])) $module_settings["tag-cloud"]["sort method"] = 'alphabetical';
	if(!isset($module_settings["tag-cloud"]["sort ascending"])) $module_settings["tag-cloud"]["sort ascending"] = 1;
	if(!isset($module_settings["tag-cloud"]["number of tags"])) $module_settings["tag-cloud"]["number of tags"] = 0;

/**
 * Languages
 *
 * @category   Module (tag-cloud)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-07-28
 */

	$modules['tag-cloud']['key'] = 'tag-cloud';
	include_txt($modules['tag-cloud']['key'], $system_data['folder']['modules'] . $modules['tag-cloud']['key'] . '/languages/');
	include_txt($modules['tag-cloud']['key'], $system_data['folder']['settings'] . 'modules/' . $modules['tag-cloud']['key'] . '/languages/');

/**
 * Create tags array and get tagged pages
 *
 * @category   Module (tag-cloud)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-07
 */
 	
 	// Decode $_GET['tag']
 	if(isset($_GET['tag'])) $_GET['tag'] = urldecode($_GET['tag']);
 	
 	// Defaults
 	if($module_settings["tag-cloud"]["target page"] == '') $module_settings["tag-cloud"]["target page"] = $page_info->alias;
 	$target = (isset($_GET['tag']) ? '' : $system_data['frontend']['href attribute']) . $module_settings["tag-cloud"]["target page"] . '&amp;tag=';
 	
	// Get tag database
	$database_tags_file = $database_tags_folder = $system_data['folder']['database'] . 'tags/catalogue.xml';
    $database_tags_xml = is_file($database_tags_file) ? simplexml_load_file($database_tags_file) : simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><tags></tags>');
	
	// Get tagged pages
	if(isset($_GET['tag'])) {
		$tagged_pages = array();
		
		foreach($database_tags_xml->tag as $tag_entry) {
			if(strtolower(strval($tag_entry->name)) == strtolower($_GET['tag'])) {
				foreach($tag_entry->link as $link_entry) {
					$page_info_xml = get_page_info($link_entry);
					$tagged_pages[strval($link_entry)]['href'] = $system_data['frontend']['href attribute'] . $page_info_xml->alias;
					$tagged_pages[strval($link_entry)]['title'] = $page_info_xml->title;
					$tagged_pages[strval($link_entry)]['description'] = $page_info_xml->description;
					
					$page_tags = explode(', ', $page_info_xml->tags);
					
					for($i = 0; $i < count($page_tags); $i++)
						$page_tags[$i] = '<a href="' . $system_data['frontend']['href attribute'] . $target . urlencode($page_tags[$i]) . '">' . $page_tags[$i] . '</a>';
					
					$tagged_pages[strval($link_entry)]['tags'] = implode(', ', $page_tags);
				}
				break;
			}
		}
	}
	
	// Get tag array
	else {
		$tags = array();
		$scale = array();
		
		foreach($database_tags_xml->tag as $tag_entry) {
			$links = count($tag_entry->link);
			$tags[strval($tag_entry->name)] = $links;
			$scale[$links] = $links;
		}
		
		krsort($scale, SORT_NUMERIC);
		$max = reset($scale);
		
		foreach($scale as $scale_entry) {
			$scale[$scale_entry] = '01';
			if($scale_entry >= $max / 5) $scale[$scale_entry] = '02';
			if($scale_entry >= $max / 4) $scale[$scale_entry] = '03';
			if($scale_entry >= $max / 3) $scale[$scale_entry] = '04';
			if($scale_entry >= $max / 2) $scale[$scale_entry] = '05';
			if($scale_entry >= $max / 1.8) $scale[$scale_entry] = '06';
			if($scale_entry >= $max / 1.6) $scale[$scale_entry] = '07';
			if($scale_entry >= $max / 1.4) $scale[$scale_entry] = '08';
			if($scale_entry >= $max / 1.2) $scale[$scale_entry] = '09';
			if($scale_entry >= $max) $scale[$scale_entry] = '10';
		}
		
		// Sort tag array
		if($module_settings["tag-cloud"]["sort method"] == 'alphabetical')
			($module_settings["tag-cloud"]["sort ascending"] == 1) ? ksort($tags, SORT_STRING) : krsort($tags, SORT_STRING);
		
		if($module_settings["tag-cloud"]["sort method"] == 'emphasis')
			($module_settings["tag-cloud"]["sort ascending"] == 1) ? asort($tags, SORT_NUMERIC) : arsort($tags, SORT_NUMERIC);
		
		if($module_settings["tag-cloud"]["number of tags"] > 0 && $module_settings["tag-cloud"]["number of tags"] < count($tags))
			$tags = array_slice($tags, 0, $module_settings["tag-cloud"]["number of tags"], true);
	}

/**
 * Include module frontend template
 *
 * @category   Module (tag-cloud)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-05-17
 */
	
	include_template('tag-cloud');

?>