<?php

/**
 * Snippet to display comments.
 *
 * 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\comments
 * @copyright Copyright (c) 2013 - 2014 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

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

$module = isset($module) ? $module : $controller->pageData['module'];
$id = isset($id) ? $id : $controller->pageData['contentid'];
$ratings = isset($ratings) && $ratings;

$wrapperTpl = $tplEngine->getChunk(isset($wrapperTpl) ? $wrapperTpl : 'comments.wrapper');
$commentTpl = $tplEngine->getChunk(isset($commentTpl) ? $commentTpl : ($ratings ? 'comments.review' : 'comments.comment'));

// Get the comments and display them
$results = clearFusionCMS::getDb()->query('SELECT id,commentdate,rating,author,content FROM cf_comments_' . clearFusionCMS::getSite() . ' WHERE published && modulename=? && contentid=? ORDER BY commentdate DESC' . (isset($limit) ? " LIMIT $limit" : ''), $module, $id);
if($results->numRows()) {
	$html = '';
	while($row = $results->fetch()) {
		$date = new flDate($row['commentdate'] . ' UTC', clearFusionCMS::getSysTimezone());
		$html .= $tplEngine->parseString($commentTpl, array(
			'author' => $row['author'],
			'rating' => $ratings ? '<img src="system/modules/comments/images/stars' . $row['rating'] . '.png" alt="' . ($row['rating'] ? sprintf( __('Rated %d out of 5', 'commentsBackend'), $row['rating']) : __('Not Rated', 'commentsBackend')) . '" />' : '',
			'ratingValue' => $ratings ? $row['rating'] : '',
			'date' => $date->toString(),
			'dateYMD' => $date->toString('%Y-%m-%d'),
			'comment' => nl2br(fusionLib::htmlChars($row['content']))
		));
	}

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