<?php


function VIEWTABLE_AJAX_EXPORT() {

	$exportLimit = 1000;

	$zasobID = V::get('ZASOB_ID', 0, $_GET, 'int');
	if ($zasobID <= 0) {
		echo 'Wrong param ZASOB_ID';
		return;
	}

	Lib::loadClass('ProcesHelper');

	$zasobObj = ProcesHelper::getZasobTableInfo($zasobID);
	if (!$zasobObj) {
		echo "Zasob TABELA ID={$zasobID} nie istnieje";
		return;
	}

	$DBG = ('1' == V::get('DBG_EDS', '', $_GET));

	//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">zasobObj (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($zasobObj);echo'</pre>';

	$userAcl = User::getAcl();
	$userAcl->fetchGroups();
	//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;display:none;">$userAcl (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($userAcl);echo'</pre>';

	if (!$userAcl->hasTableAcl($zasobObj->ID)) {
		die("Brak uprawnień do tabeli ID={$zasobObj->ID}");
	}

	$tblAcl = $userAcl->getTableAcl($zasobObj->ID);
	//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tblAcl (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tblAcl);echo'</pre>';

	$forceTblAclInit = ('1' == V::get('_force', '', $_GET));
	$tblAcl->init($forceTblAclInit);

	if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">post (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_POST);echo'</pre>';}
	if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">get (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_GET);echo'</pre>';}

	$exportFlds = V::get('flds', '', $_GET);
	$exportFldList = explode(',', $exportFlds);
	if (!$exportFlds || 0 == count($exportFldList)) {
		echo "Nie wybrano żandych pól do exportu.";
		return;
	}

	$dataSource = $tblAcl->getExportDataSource($exportFldList);
	if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">dataSource (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($dataSource);echo'</pre>';}

	$args = $_GET;
	$currSortCol = V::get('sortCol', '', $args);
	$currSortFlip = V::get('sortDir', '', $args);

	$params = array();
	$params['limit'] = $exportLimit;
//	$params['limitstart'] = 0;
	$params['order_by'] = ($currSortCol)? $currSortCol : '';
	$params['order_dir'] = $currSortFlip;

	foreach ($args as $k => $v) {
		if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && strlen($v) > 0) {// filter prefix
			$params[$k] = $v;
		}
		else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && strlen($v) > 0) {// special filter prefix
			$params[$k] = $v;
		}
	}

	if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">params (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($params);echo'</pre>';}
	$total = $dataSource->getTotal($params);
	if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">total (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($total);echo'</pre>';}

	if ($total > $exportLimit) {
		$params['limit'] = $exportLimit;
	}

	$items = $dataSource->getItems($params);
	if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">items (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($items);echo'</pre>';}

	$flds = reset($items);
	$flds = get_object_vars($flds);
	$flds = array_keys($flds);
	$labels = array();
	foreach ($flds as $fldName) {
		$fldId = $tblAcl->getFieldIdByName($fldName);
		$label = $tblAcl->getFieldLabel($fldId);
		$labels[$fldName] = ($label)? $label : $fldName;
	}

	$format = V::get('format', 'html', $_GET);
	if ('html' == $format) {
		?>
		<table class="table table-bordered">
			<thead>
				<tr>
					<?php foreach ($labels as $fldName => $label) : ?>
						<th><?php echo $label; ?></th>
					<?php endforeach; ?>
				</tr>
			</thead>
			<tbody>
			<?php foreach ($items as $item) : ?>
				<tr>
					<?php foreach ($labels as $fldName => $label) : ?>
						<td><?php echo $item->{$fldName}; ?></td>
					<?php endforeach; ?>
				</tr>
			<?php endforeach; ?>
			</tbody>
		</table>
		<?php
	}
	else if ('csv' == $format) {

		header('Content-Type: text/plain; charset=UTF-8');
		$csvSeparator = ';';

		$labelsLine = array();
		foreach ($labels as $fldName => $label) {
			$labelsLine[] = '"' . addslashes($label) . '"';
		}
		echo implode($csvSeparator, $labelsLine) . "\n";

		foreach ($items as $item) {
			$itemLine = array();
			foreach ($labels as $fldName => $label) {
				$itemLine[] = '"' . addslashes($item->{$fldName}) . '"';
			}
			echo implode($csvSeparator, $itemLine) . "\n";
		}
	}
	else {
		echo "Nieobsługiwany format danych.";
	}

	die();
}
