浏览代码

TableAjax add Export

Piotr Labudda 11 年之前
父节点
当前提交
230ddde2ef
共有 4 个文件被更改,包括 287 次插入8 次删除
  1. 1 1
      SE/VERSION
  2. 40 7
      SE/se-lib/TableAcl.php
  3. 106 0
      SE/se-lib/TableAjax.php
  4. 140 0
      SE/superedit-VIEWTABLE_AJAX_EXPORT.php

+ 1 - 1
SE/VERSION

@@ -1 +1 @@
-3.9.9
+3.9.9-1

+ 40 - 7
SE/se-lib/TableAcl.php

@@ -740,6 +740,18 @@ class TableAcl {
 		return $cols;
 	}
 
+	public function getExportFieldList() {
+		$cols = array();
+		$realFlds = $this->getRealFieldList();
+		foreach ($realFlds as $vFieldName) {
+			$fldId = $this->getFieldIdByName($vFieldName);
+			if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
+				$cols[] = $vFieldName;
+			}
+		}
+		return $cols;
+	}
+
 	/**
 	 * List of real fields in database.
 	 */
@@ -847,17 +859,27 @@ class TableAcl {
 		return $arr;
 	}
 
+	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() {
-		Lib::loadClass('Data_Source');
-		$dataSource = new Data_Source($this->getDB());
-		$dataSource->setTable($this->getName());
 		$realFieldList = $this->getRealFieldList();
-		$dataSource->setCols($realFieldList);
-		$dataSource->setColTypes($this->getTypes());
-		$dataSource->setVirtualCols($this->getVirtualFieldList());
+		$dataSource = $this->_getDataSource($realFieldList);
+
 		$dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
 		$dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
-		$dataSource->setAccessFltrAllowed(!$this->hasSuperAccessPerms());
 
 		$adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
 		foreach ($adminFields as $vAdmFld) {
@@ -868,4 +890,15 @@ class TableAcl {
 		return $dataSource;
 	}
 
+	private function _getDataSource($cols) {
+		Lib::loadClass('Data_Source');
+		$dataSource = new Data_Source($this->getDB());
+		$dataSource->setTable($this->getName());
+		$dataSource->setCols($cols);
+		$dataSource->setColTypes($this->getTypes());
+		$dataSource->setVirtualCols($this->getVirtualFieldList());
+		$dataSource->setAccessFltrAllowed(!$this->hasSuperAccessPerms());
+		return $dataSource;
+	}
+
 }

+ 106 - 0
SE/se-lib/TableAjax.php

@@ -196,6 +196,11 @@ class TableAjax extends ViewAjax {
 		return $rowFunctions;
 	}
 
+	private function _showExportFieldsJson() {
+		$exportFields = $this->_acl->getExportFieldList();
+		return $exportFields;
+	}
+
 	private function getProcesInitSelected() {
 		$userAcl = User::getAcl();
 		return $userAcl->getPermsProcesId();
@@ -549,6 +554,7 @@ class TableAjax extends ViewAjax {
 			filtersClean: false,
 			router: false,
 			longDesc: false,
+			exportFields: [],
 			mapEditor: false,	// mapEditor visible or not
 			mapEditorContainer: 'window' // 'window' or 'dock'
 		};
@@ -583,6 +589,7 @@ class TableAjax extends ViewAjax {
 		var _uniqueCol; // reference to column with the unique property
 		var _uniqueCols = {}; // array with checked rows
 		var _checkToggleChecked = false; // check-all toggle state
+		var _exportFieldsSelect = {};
 
 		var _tableWidth;
 
@@ -609,6 +616,10 @@ class TableAjax extends ViewAjax {
 				});
 			}
 
+			$.each(priv.options.exportFields, function(ind, col) {
+				_exportFieldsSelect[col] = true;
+			});
+
 			//try call webservice for data
 			priv.update(priv.options.router);
 
@@ -1289,6 +1300,29 @@ class TableAjax extends ViewAjax {
 						});
 					}
 				}
+
+				//create export dropdown
+				if (priv.options.exportFields.length) {
+					var div = $('<div class="btn-group dropup pagesize"></div>').appendTo(footToolbar);
+					var btn = $('<button class="btn btn-small dropdown-toggle" data-toggle="dropdown" href="#">Export&nbsp;</button>').appendTo(div);
+					var span = $('<span class="caret"></span>').appendTo(btn);
+					var ul = $('<ul class="dropdown-menu">').appendTo(div);
+
+					$.each(_data.cols, function (col, props) {
+						if (-1 !== priv.options.exportFields.indexOf(col)) {
+							var li = $('<li></li>').appendTo(ul);
+							$('<input {0} type="checkbox" title="{1}" value="{1}" >&nbsp;{2}</input>'.f((_exportFieldsSelect[col])? 'checked' : '', col, props.friendly || col)).appendTo(li);
+							li.on('click', 'input', priv.exportFieldChanged);
+						}
+					});
+
+					var li = $('<li></li>').appendTo(ul);
+					$('<a href="index.php" target="_blank" class=""><i class="icon-share"></i>Export do HTML</a>').appendTo(li);
+					li.on('click', 'a', priv.exportToHTML);
+					var li = $('<li></li>').appendTo(ul);
+					$('<a href="index.php" target="_blank" class=""><i class="icon-share"></i>Export do CSV</a>').appendTo(li);
+					li.on('click', 'a', priv.exportToCSV);
+				}
 			}
 
 			var stickyCol1Width = 55;
@@ -1375,6 +1409,76 @@ class TableAjax extends ViewAjax {
 
 		};
 
+		priv.exportFieldChanged = function (e) {
+
+			e.stopPropagation();
+
+			var column = $(this).val();
+
+			_exportFieldsSelect[column] = !_exportFieldsSelect[column];
+		};
+
+		priv.exportToHTML = function (e) {
+			priv.exportData('html', $(this), e);
+		};
+		priv.exportToCSV = function (e) {
+			priv.exportData('csv', $(this), e);
+		};
+		priv.exportData = function (format, node, e) {
+			var exportFields = [];
+			$.each(_exportFieldsSelect, function (col, selected) {
+				if (selected) {
+					exportFields.push(col);
+				}
+			});
+
+			if (!exportFields.length) {
+				alert('Nie wybrano żadnych pól do eksportu.');
+				return false;
+			}
+
+			var limit = 1000;
+			if (!_data.total || _data.total <= 0) {
+				alert('Brak rekordów do eksportu.');
+				return false;
+			}
+			if (_data.total > limit) {
+				if (!confirm('Za dużo rekordów. Wyeksportowane zostaną tylko pierwsze ' + limit + ' z ' + _data.total + ' rekordów.')) {
+					return false;
+				}
+			}
+
+			var exportUrl = 'index.php?MENU_INIT=VIEWTABLE_AJAX_EXPORT&ZASOB_ID=<?php echo $this->_zasobID; ?>';
+			if ('csv' == format) exportUrl += '&HEADER_NOT_INIT=YES';
+			exportUrl += '&format=' + format;
+			exportUrl += '&flds=' + exportFields.join(',');
+			exportUrl += '&sortCol=' + (_currSortCol || '');
+			exportUrl += '&sortDir=' + (_currSortFlip ? "desc" : "asc");
+
+			if (Object.keys(_filterCols).length > 0) {
+				$.each(_filterCols, function (col, colProps) {
+					if (colProps.filter && colProps.filter.length > 0) {
+						exportUrl += '&f_' + col + '=' + colProps.filter;
+					}
+				});
+			}
+
+			// specialFilters
+			$.each(_specialFilters, function(groupName, btnValue) {
+				if (btnValue.length > 0) {
+					exportUrl += '&sf_' + groupName + '=' + btnValue;
+				}
+			});
+
+			if (priv.options.forceFilterInit) {
+				$.map(priv.options.forceFilterInit, function (fltrProps, fltr) {
+					exportUrl += '&sf_' + fltr + '=' + fltrProps;
+				});
+			}
+
+			node.attr('href', exportUrl);
+		};
+
 		/*
 		 calls the webservice(if defined).
 		 used only inside priv.init
@@ -2354,6 +2458,7 @@ class TableAjax extends ViewAjax {
 	$forceFilterInit = $this->_forceFilterInit;
 	$pageSizes = $this->_pageSizes;
 	$rowFunctions = $this->_showRowFunctionsJson();
+	$exportFields = $this->_showExportFieldsJson();
 ?>
 		<script>
 jQuery(document).ready(function(){
@@ -2396,6 +2501,7 @@ jQuery(document).ready(function(){
 			}
 		},
 		rowFunctions: <?php echo json_encode($rowFunctions); ?>,
+		exportFields: <?php echo json_encode($exportFields); ?>,
 		specialFilterFunctions: <?php
 			$fltrs = $this->_dataSource->getSpecialFilters();
 			if (!empty($fltrs)) {

+ 140 - 0
SE/superedit-VIEWTABLE_AJAX_EXPORT.php

@@ -0,0 +1,140 @@
+<?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'] = 0;
+//	$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();
+}