<?php

/**
 * Snippet to show a random testimonial.
 *
 * 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\testimonials
 * @copyright Copyright (c) 2012 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

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

if($container = $controller->content->getContainer('testimonials.archive')) {

	// Get parameters
	$words = isset($words) ? $words : 30;
	$size = isset($size) ? $size : 's';
	$testimonialTpl = isset($testimonialTpl) ? $tplEngine->getChunk($testimonialTpl) : '<blockquote class="testimonial">
[[if &subject=`[[+image]]` &operator=`!empty` &then=`<img src="[[+image]]" [[+image_size]] alt="[[+author]]" />`]]
<p>[[+testimonial]] <span><a href="[[+testimonialURI]]">[[+continueReading]]</a></span></p>
</blockquote>';

	$db = clearFusionCMS::getDb();
	$offset = $db->fetchOne('SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM cf_testimonials_' . clearFusionCMS::getSite() . ' WHERE published');
	if($data = $db->fetchRow('SELECT id,dateoftestimonial,gender,age,fileext,author,location,occupation,website,company,testimonial FROM cf_testimonials_' . clearFusionCMS::getSite() . " WHERE published LIMIT $offset, 1")) {
		$route = $controller->content->getContainerRoute($container, 'testimonial/:id');

		$cfg = clearFusionCMS::getSiteConfig('cfTestimonials');
		$src = $imgSize = '';
		if(!empty($data['fileext'])) {
			$imageRoute = $controller->content->getContainerRoute($container, 'images/:size/:id', array(), array(), $data['fileext']);
			$src = $imageRoute->assemble(array('size' => $size, 'id' => $data['id']));

			$w = $cfg->get($size . 'w');
			$h = $cfg->get($size . 'h');
			if($cfg->get($size . 'crop'))
				$imgSize = "width=\"$w\" height=\"$h\"";
		}

		echo $tplEngine->parseString($testimonialTpl, array(
			'dategiven' => new flDate($data['dateoftestimonial'] . ' UTC', clearFusionCMS::getSysTimezone()),
			'gender' => $data['gender'] == 'm' ? __('Male', 'testimonials') : __('Female', 'testimonials'),
			'age' => $data['age'],
			'author' => $data['author'],
			'location' => $data['location'],
			'occupation' => $data['occupation'],
			'company' => $data['company'],
			'website' => $data['website'],
			'testimonial' => flTextSplitWords::split(cfContentFilter::apply($cfg->get('filter', ''), $data['testimonial']), $words),
			'testimonialURI' => $route->assemble(array('id' =>$data['id'])),
			'image' => $src,
			'image_size' => $imgSize,
			'continueReading' => __('continue reading', 'testimonials')
		));
	}
}
