<?php

/**
 * Snippet to draw 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 - 2013 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

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

// Deal with parameters
$root = isset($root) ? $root : '';
$perPage = isset($perPage) ? $perPage : 10;
$words = isset($words) ? $words : 30;
$summary = isset($summary) ? $summary : null;
$resultTpl = isset($resultTpl) ? $tplEngine->getChunk($resultTpl) : '<li><a href="[[+link]]">[[+title]]</a><p>[[+summary]]</p></li>';
$wrapperTpl = isset($wrapperTpl) ? $tplEngine->getChunk($wrapperTpl) : '<ul>[[+results]]</ul>';
$containerTpl = isset($containerTpl) ? $tplEngine->getChunk($containerTpl) : '<p>' . __('[[+numResults]] results found for tag: "[[+tag]]"', 'search') . '</p>[[+results]][[+pagination]]';

// Get the route object
$route = $controller->content->getContainerRoute($controller->pageData['id'], ':tag/:page', array('page' => '0'), array('tag' => '[a-z0-9 _-]+', 'page' => '\d+'));

// Match the route
if($params = $route->match(clearFusionCMS::getRequest()->getSegments())) {

	// Get the documents for the tag
	$tags = new flSearchTags(clearFusionCMS::getDb(), 'cf_search_' . clearFusionCMS::getSite());
	$results = $tags->find(
		$params['tag'],
		$root,
		true,
		true,
		array(),
		$params['page'] * $perPage,
		$perPage
	);

	// If nothing for the tag then 404
	if(!$results->total())
		throw new flExceptionNotFound();

	// 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']
				));
			}
		}

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

	$pagination = new flWidgetPagination($params['page'], $results->total(), $perPage);
	$pagination->setRoute($route, array('tag' => $params['tag']), 'page');

	// If outside page bounds
	if($pagination->getPage() > $pagination->getNumPages() || $pagination->getPage() < 0)
		clearFusionCMS::getResponse()->redirectTo($route->assemble(array('tag' => $params['tag']), true));

	// Output the search results
	echo $tplEngine->parseString($containerTpl, array(
		'numResults' => (int)$results->total(),
		'tag' => fusionLib::htmlChars($params['tag']),
		'results' => $html,
		'pagination' => $pagination->render()
	));
}
else {
	$route = $controller->content->getContainerRoute($controller->pageData['id'], '');
	if(!$route->match(clearFusionCMS::getRequest()->getSegments()))
		throw new flExceptionNotFound();
}
