Bladeren bron

added Export csv in Windows-1250

Piotr Labudda 9 jaren geleden
bovenliggende
commit
9c7e837fce
2 gewijzigde bestanden met toevoegingen van 44 en 41 verwijderingen
  1. 38 41
      SE/se-lib/Route/ViewTableAjax.php
  2. 6 0
      SE/se-lib/TableAjax.php

+ 38 - 41
SE/se-lib/Route/ViewTableAjax.php

@@ -504,51 +504,48 @@ class Route_ViewTableAjax extends RouteBase {
 		$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', [], V::get($fldName, '', $item));
-					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) . '"';
+		switch ($format) {
+			case 'html': {
+				UI::gora();
+				echo UI::h('table', ['class'=>'table table-bordered table-hover'], [
+					UI::h('thead', [], [
+						UI::h('tr', [], array_map(function ($label) {
+							return UI::h('th', [], $label);
+						}, $labels))
+					]),
+					UI::h('tbody', [], array_map(function ($item) use($labels) {
+						return UI::h('tr', [], array_map(function ($fieldName) use ($item) {
+							return UI::h('td', [], V::get($fieldName, '', $item));
+						}, array_keys($labels)));
+					}, $items)),
+				]);
+				UI::dol();
+				exit;
 			}
-			echo implode($csvSeparator, $labelsLine) . "\n";
-
-			foreach ($items as $item) {
-				$itemLine = array();
-				foreach ($labels as $fldName => $label) {
-					$itemLine[] = '"' . addslashes(V::get($fldName, '', $item)) . '"';
+			case 'csv_cp1250':
+			case 'csv': {
+				$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 = ';';
+
+				$csvHeader = implode($csvSeparator, array_map(function ($label) use ($item) {
+					return '"' . addslashes($label) . '"';
+				}, array_values($labels)));
+				$csvRows = implode("\r\n", array_map(function ($item) use ($labels, $csvSeparator) {
+					return implode($csvSeparator, array_map(function ($fieldName) use ($item) {
+						return '"' . addslashes(V::get($fieldName, '', $item)) . '"';
+					}, array_keys($labels)));
+				}, $items));
+				switch ($format) {
+					case 'csv': echo $csvHeader . "\n" . $csvRows; exit;
+					case 'csv_cp1250': echo iconv('utf-8', 'Windows-1250//IGNORE', $csvHeader) . "\r\n" . iconv('utf-8', 'Windows-1250//IGNORE', $csvRows); exit;
+					die("Nieobsługiwane kodowanie danych csv.");
 				}
-				echo implode($csvSeparator, $itemLine) . "\n";
+				exit;
 			}
 		}
-		else {
-			die("Nieobsługiwany format danych.");
-		}
+		die("Nieobsługiwany format danych.");
 	}
 
 	public function loadDataAjaxAction() {

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

@@ -1964,6 +1964,9 @@ var p5UI_TableAjax_generateFunctionNode = function(funObj, rowPK, props) {
 				var li = $('<li></li>').appendTo(ul);
 				$('<a href="index.php" target="_blank" class=""><i class="glyphicon glyphicon-share"></i>Export do CSV</a>').appendTo(li);
 				li.on('click', 'a', priv.exportToCSV);
+				var li = $('<li></li>').appendTo(ul);
+				$('<a href="index.php" target="_blank" class=""><i class="glyphicon glyphicon-share" title="Export do pliku CSV w kodowaniu Windows-1250"></i>Export do CSV (Windows-1250)</a>').appendTo(li);
+				li.on('click', 'a', priv.exportToCSVWinCP1250);
 			} else {
 				node = $('<span class="' + nodeClass + '"></span>');
 			}
@@ -2068,6 +2071,9 @@ var p5UI_TableAjax_generateFunctionNode = function(funObj, rowPK, props) {
 		priv.exportToCSV = function(e) {
 			priv.exportData('csv', $(this), e);
 		};
+		priv.exportToCSVWinCP1250 = function(e) {
+			priv.exportData('csv_cp1250', $(this), e);
+		};
 		priv.exportData = function(format, node, e) {
 			var exportFields = [];
 			$.each(_exportFieldsSelect, function(col, selected) {