<?php

/**
 * Snippet to generate a property gallery.
 *
 * 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\property
 * @copyright Copyright (c) 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

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

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

$size = isset($size) && preg_match('/^[tsmlo]$/', $size) ? $size : 't';
$lightbox = isset($lightbox) && $lightbox == '0' ? false : true;
$class = isset($class) ? $class : 'propertyGallery';
$type = isset($type) ? $type : 'normal';
$tabID = isset($tabbed) ? $tabbed . '_' : '';

// If tabbed display
if(isset($tabbed)) {
	$itemTpl = $tplEngine->getChunk('property.galleryTabPane');
	$tabItemTpl = $tplEngine->getChunk('property.galleryTabItem');
	$tabWrapperTpl = $tplEngine->getChunk('property.galleryTabWrap');
}
else {
	$wrapperTpl = isset($wrapperTpl) ? $tplEngine->getChunk($wrapperTpl) : '<ul class="[[+class]]">[[+images]]</ul>';
	$itemTpl = isset($itemTpl) ? $tplEngine->getChunk($itemTpl) : '<li><img src="[[+src]]" alt="[[+title]]" title="[[+title]]" [[+size]] /></li>';
}

$item = clearFusionCMS::getDb()->fetchRow('SELECT slug,category,regionid FROM cf_property_' . clearFusionCMS::getSite() . ' WHERE propertyid=?', $id);
if($item) {
	$slug = $item['slug'];

	// Build routes to the images
	$container = $controller->content->getContainer('property.' . $item['category'] . '.' . $item['regionid']);

	$imgGalleryRoute['.png'] = $controller->content->getContainerRoute($container, "images/:size/$slug/:galID", array(), array(), '.png');
	$imgGalleryRoute['.jpg'] = $controller->content->getContainerRoute($container, "images/:size/$slug/:galID", array(), array(), '.jpg');
	$imgGalleryRoute['.gif'] = $controller->content->getContainerRoute($container, "images/:size/$slug/:galID", array(), array(), '.gif');

	$cfg = clearFusionCMS::getSiteConfig('cfProperty');
	$result = clearFusionCMS::getDb()->query('SELECT galid,title,fileext FROM cf_property_gal_' . clearFusionCMS::getSite() . ' WHERE propertyid=? && imgtype=? ORDER BY disporder', $id, $type);
	if ($result->numRows()) {
		$imgSize = $cfg->get("{$size}crop") ? 'width="' . $cfg->get("{$size}w") . '" height="' . $cfg->get("{$size}h") . '"' : '';
		$html = '';
		$tabHTML = '';
		while ($row = $result->fetch()) {
			if(isset($tabbed)) {
				$tabHTML .= $tplEngine->parseString($tabItemTpl, array(
					'title' => $row['title'],
					'tabid' => $tabID . $row['galid']
				));
			}

			$html .= $tplEngine->parseString($itemTpl, array(
				'src'   => $imgGalleryRoute[$row['fileext']]->assemble(array('size' => $size, 'galID' => $row['galid'])),
				'size'  => $imgSize,
				'title' => $row['title'],
				'tabid' => $tabID . $row['galid']
			));
		}

		if(isset($tabbed)) {
			echo $tplEngine->parseString($tabWrapperTpl, array(
				'tabs' => $tabHTML,
				'images' => $html,
				'class' => "$class $size" . ($lightbox ? ' cfLightbox' : '')
			));
		}
		else {
			echo $tplEngine->parseString($wrapperTpl, array(
				'images' => $html,
				'class' => "$class $size" . ($lightbox ? ' cfLightbox' : '')
			));
		}

		// Add JavaScript if using lightbox
		if ($lightbox) {
			$tplEngine->getCodeBuffer()->bufferBodyJS('system/app/js/cfLightbox.js')
				->requireCore();
		}
	}
	else if(isset($onNoGallery))
		echo $onNoGallery;
}
