<?php

/**
 * Snippet to draw a tag cloud.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS\module\search
 * @copyright Copyright (c) 2012 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/* @var $tplEngine cfTemplateEngine */
$controller = $tplEngine->controller;

// Deal with parameters
$root = isset($root) ? $root : '';
$maxTags = isset($maxTags) ? $maxTags : 25;
$tagPage = isset($tagPage) ? $tagPage : 0;
$tagTpl = isset($tagTpl) ? $tplEngine->getChunk($tagTpl) : '<li class="cloud[[+weight]]"><a href="[[+link]]">[[+tag]]</a></li> ';
$wrapperTpl = isset($wrapperTpl) ? $tplEngine->getChunk($wrapperTpl) : '<ul class="tagcloud">[[+results]]</ul>';

// Get the cloud data
$tags = new flSearchTags(clearFusionCMS::getDb(), 'cf_search_' . clearFusionCMS::getSite());
$results = $tags->getCloud(
	$root,
	true,
	array(),
	$maxTags
);

// Draw the cloud
if(count($results)) {
	$route = $tagPage ? $controller->content->getContainerRoute($tagPage, ':page') : new flRouterRoute(':page');
	$html = '';
	foreach($results as $tag => $w) {
		$html .= $tplEngine->parseString($tagTpl, array(
			'link' => $route->assemble(array('page' => $tag)),
			'tag' => fusionLib::htmlChars($tag),
			'weight' => $w
		));
	}

	echo $tplEngine->parseString($wrapperTpl, array('results' => $html));
}

?>