<?php
/*
 * ARI Data Tables Joomla! module
 *
 * @package		ARI Data Tables Joomla! module.
 * @version		1.0.0
 * @author		ARI Soft
 * @copyright	Copyright (c) 2009 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

(defined('_JEXEC') && defined('ARI_FRAMEWORK_LOADED')) or die;

AriKernel::import('SimpleTemplate.SimpleTemplate');

$caption = AriUtils::getParam($tableParams, 'caption');
if ($caption)
	$caption = AriSimpleTemplate::parse($caption, array('REQUEST' => $_REQUEST), true);

$advStyling = (bool)$tableParams['advancedStyling'];
$colClassPrefix = 'ari-tbl-col-';
$rowClassPrefix = 'ari-tbl-row-';
$tableAttrs = array(
	'id' => $id,
	'cellpadding' => intval($tableParams['padding'], 10),
	'cellspacing' => intval($tableParams['spacing'], 10),
	'border' => intval($tableParams['border'], 10),
);

if ($tableParams['cssClass'])
	$tableAttrs['class'] = $tableParams['cssClass'];
	
$rowClasses = explode(';', AriUtils::getParam($tableParams, 'rowClasses', ''));
$rowClassesCnt = count($rowClasses);
?>

<?php
	if ($data || $dtColumns):
		$columnsKeys = null;
		if ($dtColumns)
		{
			$columnsKeys = $dtColumns;
		}
		else 
		{
			$columnsKeys = array_keys($data);
			$columnsKeys = $data[$columnsKeys[0]];
			$columnsKeys = array_keys($columnsKeys);
		}
		$columnCount = count($columnsKeys);
?>
<table<?php echo AriHtmlHelper::getAttrStr($tableAttrs); ?>>
<?php
	if ($caption):
?>
	<caption><?php echo $caption; ?></caption>
<?php
	endif;
		if ((bool)$tableParams['showHeader']): 
?>
	<thead>
		<?php
			require dirname(__FILE__) . '/header.php';
		?>
	</thead>
<?php
		endif;
?>
	<tbody>
<?php
	if (!empty($data)):
	$rowIdx = 0;
	foreach ($data as $dataItem):
		$rowAttrs = array('class' => '');
		if ($rowClassesCnt)
			$rowAttrs['class'] = $rowClasses[$rowIdx % $rowClassesCnt];
			
		if ($advStyling)
			$rowAttrs['class'] .= ' ' . $rowClassPrefix . $rowIdx;
			
		if (empty($rowAttrs['class'])) 
			unset($rowAttrs['class']);
?>
	<tr<?php echo AriHtmlHelper::getAttrStr($rowAttrs); ?>>
		<?php
			$colIdx = 0;
			if (count($dataItem) < $columnCount)
			{
				for ($emptyColIdx = count($dataItem); $emptyColIdx < $columnCount; $emptyColIdx++)
				{
					$dataItem[] = '';
				}
			}

			reset($columnsKeys);
			foreach ($columnsKeys as $colKey):
				$value = $dataItem[$colKey];
				$cellTag = 'td';
				$columnSetting = AriSmartContentFormattersHelper::getColumnSettings($columnsSettings, $colKey, $colIdx);
				$colAttrs = array('class' => '');

				if (in_array($colIdx, $hiddenColumns))
				{
					++$colIdx;
					continue ;
				}
				
				if ($advStyling)
					$colAttrs['class'] .= $colClassPrefix . $colIdx;
					
				if (!empty($columnSetting['celltag']))
					$cellTag = $columnSetting['celltag'];
					
				if (!empty($columnSetting['class']))
					$colAttrs['class'] .= ' ' . $columnSetting['class'];
/*
				if (!empty($columnSetting['format']))
				{
					$value = AriSimpleTemplate::parse(
						$columnSetting['format'], 
						array(
							'value' => $value,
							'columns' => $dataItem,
							'REQUEST' => $_REQUEST
						),
						true
					);
				}
				else if (!empty($columnSetting['reclinks']))
				{
					$value = AriHtmlHelper::linkifyContent($value, $columnSetting['linktarget']);
				}
*/
				if (empty($colAttrs['class'])) 
					unset($colAttrs['class']);
		?>
		<<?php echo $cellTag; ?><?php echo AriHtmlHelper::getAttrStr($colAttrs); ?>><?php echo $value; ?></<?php echo $cellTag; ?>>
		<?php
				++$colIdx;
			endforeach;			
		?>
	</tr>
<?php
		++$rowIdx;
	endforeach;
	endif;
?>
	</tbody>
<?php
		if ((bool)$tableParams['showFooter']): 
?>
	<tfoot>
		<?php
			require dirname(__FILE__) . '/header.php';
		?>
	</tfoot>
<?php
		endif;
?>
</table>
<?php
	else:
?>
	<?php echo JText::_('LIB_ARISMARTCONTENT_HTMLTABLE_LABEL_NODATAAVAILABLE'); ?>
<?php
	endif;
?>