<?php

/**
 * Load region copy into template variables
 *
 * 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;
$db = clearFusionCMS::getDb();

if(isset($region) && ($regionInfo = $db->fetchRow('SELECT * FROM cf_property_region_' . clearFusionCMS::getSite() . ' WHERE regionid=?', $region))) {

	$itemTpl = isset($itemTpl) ? $tplEngine->getChunk($itemTpl) : '<section class="regionInfo"><h2>[[+rgnTitle]]</h2>[[+rgnContent]]</section>';

	$params = array();
	$sql = 'SELECT rgnfldid,fieldname,snippet FROM cf_property_rgnflds_' . clearFusionCMS::getSite();
	if(isset($id)) {
		$params = preg_split('/\s*,\s*/', $id);
		$sql .= ' WHERE rgnfldid IN(?' . str_repeat(',?', count($params) - 1) . ')';
	}
	$sql .= ' ORDER BY disporder';

	$flds = $db->fetchAll($sql, $params);
	foreach($flds as $f) {
		if(!empty($regionInfo["content$f[rgnfldid]"])) {
			echo $tplEngine->parseString($itemTpl, array(
				'rgnTitle' => $f['fieldname'],
				'rgnContent' => cfContentFilter::apply($f['snippet'], $regionInfo["content$f[rgnfldid]"])
			));
		}
	}
}
