Explorar o código

mved Export to ViewTableAjax

Piotr Labudda %!s(int64=9) %!d(string=hai) anos
pai
achega
0ae48bab69

+ 96 - 0
SE/se-lib/Route/ViewTableAjax.php

@@ -308,6 +308,102 @@ class Route_ViewTableAjax extends RouteBase {
 		return $response;
 	}
 
+	/**
+	 * @param $_GET['namespace'] = AclNamespace
+	 * @param $_GET['format'] = 'csv' | 'html'
+	 * @param $_GET['flds'] = csv - coma separated field names
+	 * @param $_GET['sortCol'] = FieldName
+	 * @param $_GET['sortDir'] = SortDir ('desc' | 'asc')
+	 * @param $_GET['f_{$fieldName}'] = filter
+	 * @param $_GET['sf_{$fieldName}'] = force filter
+	 */
+	public function exportAction() {
+		$args = $_GET;
+		$namespace = V::get('namespace', '', $args, 'word');
+		if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
+		$acl = Core_AclHelper::getAclByNamespace($namespace);
+
+		$exportLimit = 10000;
+		$params = array();
+		$params['limit'] = $exportLimit;
+		// $params['limitstart'] = 0;
+		$params['order_by'] = V::get('sortCol', '', $args);
+		$params['order_dir'] = V::get('sortDir', '', $args);
+		$params['cols'] = array($acl->getPrimaryKeyField());
+		$toExportFields = explode(',', V::get('flds', '', $_GET));
+		if (empty($toExportFields)) throw new Exception("Nie wybrano żandych pól do exportu.");
+		$allowedExportFieldList = Core_AclHelper::getExportFieldList($acl);
+		foreach ($toExportFields as $fieldName) {
+			if ($fieldName == $acl->getPrimaryKeyField()) continue;
+			if (!in_array($fieldName, $allowedExportFieldList)) throw new Exception("Brak uprawnień do exportu pola '{$fieldName}'");
+			$params['cols'][] = $fieldName;
+		}
+
+		$labels = array();
+		foreach ($toExportFields as $fieldName) {
+			$labels[ $fieldName ] = $acl->getFieldLabel($fieldName);
+		}
+
+		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;
+			}
+		}
+		$total = $acl->getTotal($params);
+		// if ($total > $exportLimit) $params['limit'] = $exportLimit;
+		$items = $acl->getItems($params);
+
+		$format = V::get('format', 'html', $_GET);
+		if ('html' == $format) {
+			UI::gora();
+			UI::startTag('table', ['class'=>'table table-bordered table-hover']);
+				UI::startTag('thead');
+					UI::startTag('tr');
+					foreach ($labels as $fldName => $label) {
+						UI::tag('th', [], $label);
+					}
+					UI::endTag('tr');
+				UI::endTag('thead');
+				UI::startTag('tbody');
+				foreach ($items as $item) :
+					UI::startTag('tr');
+					foreach ($labels as $fldName => $label) :
+						UI::tag('td', [], $item->{$fldName});
+					endforeach;
+					UI::endTag('tr');
+				endforeach;
+				UI::endTag('tbody');
+			UI::endTag('table');
+			UI::dol();
+		}
+		else if ('csv' == $format) {
+			$csvFileName = "Tabela-" . $acl->getName() . "-" . date("Y-m-d_H_s");
+			header('Content-Type: text/csv; charset=utf-8');
+			header("Content-Disposition: attachment; filename={$csvFileName}.csv");
+			$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 {
+			die("Nieobsługiwany format danych.");
+		}
+	}
+
 	public function loadDataAjaxAction() {
 		$namespace = V::get('namespace', '', $_REQUEST, 'word');
 		if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);

+ 0 - 15
SE/se-lib/TableAcl.php

@@ -1548,21 +1548,6 @@ class TableAcl extends Core_AclBase {
 		return $itemCopy;
 	}
 
-	public function getExportDataSource($cols = array()) {
-		$exportFieldList = $this->getExportFieldList();
-		if (!empty($cols)) {
-			$fltrExportFlds = array();
-			foreach ($exportFieldList as $fldName) {
-				if (in_array($fldName, $cols)) {
-					$fltrExportFlds[] = $fldName;
-				}
-			}
-			$exportFieldList = $fltrExportFlds;
-		}
-		$dataSource = $this->_getDataSource($exportFieldList);
-		return $dataSource;
-	}
-
 	public function getDataSource() {
 		$realFieldList = $this->getRealFieldList();
 		$dataSource = $this->_getDataSource($realFieldList);

+ 3 - 12
SE/se-lib/TableAjax.php

@@ -213,14 +213,6 @@ class TableAjax extends ViewAjax {
 		return $rowFunctions;
 	}
 
-	private function _showExportFieldsJson() {
-		if (method_exists($this->_acl, 'getExportFieldList')) {
-			$exportFields = $this->_acl->getExportFieldList();
-			return $exportFields;
-		}
-		return null;
-	}
-
 	private function getProcesInitSelected() {
 		$userAcl = User::getAcl();
 		return $userAcl->getPermsFiltrProcesId();
@@ -1995,8 +1987,7 @@ var p5UI_TableAjax_generateFunctionNode = function(funObj, rowPK, props) {
 				}
 			}
 
-			var exportUrl = 'index.php?MENU_INIT=VIEWTABLE_AJAX_EXPORT&ZASOB_ID=<?php echo $this->_zasobID; ?>';
-			if ('csv' == format) exportUrl += '&HEADER_NOT_INIT=YES';
+			var exportUrl = 'index.php?_route=ViewTableAjax&_task=export&namespace=<?= $acl->getNamespace(); ?>';
 			exportUrl += '&format=' + format;
 			exportUrl += '&flds=' + exportFields.join(',');
 			exportUrl += '&sortCol=' + (_state.filters.currSortCol || '');
@@ -3274,7 +3265,7 @@ var p5UI_TableAjax_generateFunctionNode = function(funObj, rowPK, props) {
 	$pageSizes = $this->_pageSizes;
 	$pageSize = $this->_pageSize;
 	$rowFunctions = $this->_rowFunctions;//$this->_showRowFunctionsJson();
-	$exportFields = $this->_showExportFieldsJson();
+	$exportFields = Core_AclHelper::getExportFieldList($acl);
 ?>
 		<script>
 function TableAjax__HIST_Route(args) {
@@ -3489,7 +3480,7 @@ jQuery(document).ready(function(){
 		rowFunctions: <?php echo json_encode($rowFunctions); ?>,
 		exportFields: <?php echo json_encode($exportFields); ?>,
 		specialFilterFunctions: <?php
-			$fltrs = (method_exists($this->_acl, 'getSpecialFilters')) ? $this->_acl->getSpecialFilters() : null;
+			$fltrs = (method_exists($acl, 'getSpecialFilters')) ? $acl->getSpecialFilters() : null;
 			if (!empty($fltrs)) {
 				echo json_encode($fltrs);
 			} else {

+ 0 - 144
SE/superedit-VIEWTABLE_AJAX_EXPORT.php

@@ -1,144 +0,0 @@
-<?php
-
-
-function VIEWTABLE_AJAX_EXPORT() {
-
-	$exportLimit = 10000;
-
-	$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) {
-		$label = $tblAcl->getFieldLabel($fldName);
-		$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) {
-
-		$tblName = $tblAcl->getName();
-		$exportDate = date("Y-m-d_H_s");
-		$csvFileName = "Tabela-{$tblName}-{$exportDate}";
-		header('Content-Type: text/csv; charset=utf-8');
-		header("Content-Disposition: attachment; filename={$csvFileName}.csv");
-		//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();
-}