<?php

/**
 * Snippet to generate a list of property listings.
 *
 * 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;

$show = isset($show) ? $show : null;
$_limit = isset($limit) ? $limit : 10;
$abs = isset($abs) ? $abs : false;

$size = isset($size) && preg_match('/^[tsmlo]$/', $size) ? $size : 's';
$wrapperCSS = isset($wrapperCSS) ? $wrapperCSS : 'propertyList';
$itemCSS = isset($itemCSS) ? $itemCSS : '';
$wrapperTpl = $tplEngine->getChunk(isset($wrapperTpl) ? $wrapperTpl : 'property.listWrap');
$itemTpl = $tplEngine->getChunk(isset($itemTpl) ? $itemTpl : 'property.listItem');

$html = '';
$cache = array();
$cfg = clearFusionCMS::getSiteConfig('cfProperty');

if(!isset($category)) {
	$category = array();
	if(preg_match('/^property\.(buy|rent)\.\d+$/', $controller->pageData['container'], $category)) {
		$category = $category[1];
		$region = $controller->pageData['container_extra'];
	}
	else
		$category = '';
}

// Get the properties depending on what's being shown
switch(isset($show) ? $show : 'default') {
	case 'latest':
		if(isset($region))
			$results = clearFusionCMS::getDb()->query('SELECT * FROM cf_property_' . clearFusionCMS::getSite() . ' WHERE category=? AND regionid=? AND published ORDER BY addedat DESC LIMIT ' . $_limit, $category, $region);
		else
			$results = clearFusionCMS::getDb()->query('SELECT * FROM cf_property_' . clearFusionCMS::getSite() . ' WHERE category=? AND published  ORDER BY addedat DESC LIMIT ' . $_limit, $category);
		break;

	default:
		if(!isset($region))
			$region = $controller->pageData['container_extra'];

		$results = clearFusionCMS::getDb()->query('SELECT * FROM cf_property_' . clearFusionCMS::getSite() . ' WHERE regionid=? && category=? AND published' . (isset($limit) ? ' LIMIT ' . $limit : ''), $region, $category);
		break;

}

// Draw the results
while ($row = $results->fetch()) {

	if(!isset($cache[$row['regionid']])) {
		// Build the routes
		$container = $controller->content->getContainer('property.' . $category . '.' . $row['regionid']);

		$cache[$row['regionid']] = $routes = array(
			'page' => $controller->content->getContainerRoute($container, ':slug'),
			'.jpg' => $controller->content->getContainerRoute($container, 'images/:size/:slug', array(), array(), '.jpg'),
			'.png' => $controller->content->getContainerRoute($container, 'images/:size/:slug', array(), array(), '.png'),
			'.gif' => $controller->content->getContainerRoute($container, 'images/:size/:slug', array(), array(), '.gif')
		);
	}
	else {
		$routes = $cache[$row['regionid']];
	}

	if($routes['page']) {
		$pageURI = $routes['page']->assemble(array('slug' => $row['slug']), $abs);
		$imgURI = empty($row['fileext']) ? '' : $routes[$row['fileext']]->assemble(array('size' => $size, 'slug' => $row['slug']), $abs);

		$title = $row['street1'];
		if(!empty($row['street2']))
			$title .= ', ' . $row['street2'];
		if(!empty($row['city']))
			$title .= ', ' . $row['city'];
		if(!empty($row['state']))
			$title .= ', ' . $row['state'];
		if(!empty($row['postcode']))
			$title .= ', ' . $row['postcode'];

		$period = '';
		if($row['category'] == 'rent') {
			if($row['price_period'] == 'per_week')
				$period = __(' p/w', 'property');
			else if($row['price_period'] == 'per_month')
				$period = __(' p/m', 'property');
		}

		$d = array(
			'class'       => $itemCSS,
			'imgsrc'      => $imgURI,
			'imgalt'      => $title,
			'link'        => $pageURI,
			'title'       => $row['title'],
			'address'     => $title,
			'description' => $row['shortdesc'],
			'price'       => $cfg->curleft . number_format($row['price'], $cfg->numdecimals, $cfg->decpoint, $cfg->thoussep) . $cfg->curright . $period,
			'sold'        => $row['sold'],
			'soldLabel'   => $row['category'] == 'buy' ? __('Sold', 'cfProperty') : __('Rented', 'cfProperty')
		);

		$html .= $tplEngine->parseString($itemTpl, $d);
	}
}

// Wrap the product list and draw
echo $tplEngine->parseString($wrapperTpl, array(
	'properties' => $html,
	'class' => "$wrapperCSS $size"
));
