<?php

/**
 * Snippet to draw all the documents associated with a tag.
 *
 * 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 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

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

// Deal with parameters
$root = isset($root) ? $root : '';
$resultTpl = isset($resultTpl) ? $tplEngine->getChunk($resultTpl) : '<li><a href="[[+link]]">[[+title]]</a></li>';
$wrapperTpl = isset($wrapperTpl) ? $tplEngine->getChunk($wrapperTpl) : '<ul class="taggeddocs">[[+results]]</ul>';
$words = isset($words) ? $words : 30;
$summary = isset($summary) ? $summary : null;

if(isset($tag)) {

	// Get the documents for the tag
	$tags = new flSearchTags(clearFusionCMS::getDb(), 'cf_search_' . clearFusionCMS::getSite());
	$results = $tags->find(
		$tag,
		$root,
		true,
		true,
		array(),
		0,
		isset($limit) ? $limit : 0
	);

	// Build the search results
	$html = '';
	if(count($results)) {
		foreach($results as $result) {
			if(($helper = cfSearchHelper::getHelper($result['root'])) && ($sum = $helper->getSearchSummary($result['node'], $summary, $words))) {
				$html .= $tplEngine->parseString($resultTpl, array(
					'link' => $sum['uri'],
					'title' => $sum['title'],
					'summary' => $sum['summary'],
					'img' => isset($sum['img']) ? $sum['img'] : '',
					'img_t' => isset($sum['img_t']) ? $sum['img_t'] : '',
					'img_s' => isset($sum['img_s']) ? $sum['img_s'] : '',
					'img_m' => isset($sum['img_m']) ? $sum['img_m'] : '',
					'img_l' => isset($sum['img_l']) ? $sum['img_l'] : ''
				));
			}
		}

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