Parcourir la source

- dodanie wyświetlenia okienka z formularzem do zapisu danych wczesniej zparsowanego pliku CSV
- przekazanie danych do zapisania(należy obsłużyć)

dariusz.andryskowski il y a 8 ans
Parent
commit
fd8f877934
2 fichiers modifiés avec 106 ajouts et 75 suppressions
  1. 31 5
      tools/Bocian.php
  2. 75 70
      tools/Bocian.php.view.js

+ 31 - 5
tools/Bocian.php

@@ -34,6 +34,7 @@ class Bocian extends RouteToolBase {
 			'VIEW_KONTRAHENCI' => $this->defaultViewFormBiAudit([], 'KONTRAHENCI'),
 			'VIEW_PRACOWNICY' => $this->defaultViewFormBiAudit([], 'PRACOWNICY'),
 			'URL_PARSE_CSV_FILE_AJAX' => $this->getLink('parseCsvFileAjax'),
+			'URL_SAVE_FORM_CSV_FILE_AJAX' => $this->getLink('saveFormCsvFileAjax'),
 
 		]);
 
@@ -41,10 +42,16 @@ class Bocian extends RouteToolBase {
 		UI::dol();
 	}
 
+/**
+ * Funkcja ajax do odebrania danych z przesłanego pliku CSV do parsowania
+ */
 	public function parseCsvFileAjaxAction() {
 		Response::sendTryCatchJson(array($this, 'parseCsvFileAjax')); // , $args = 'JSON_FROM_REQUEST_BODY');
 	}
 
+/**
+ * Funkcja parsuje dane wgrywanego plku csv
+ */
 public function parseCsvFileAjax() {
 
 	if (!file_exists($_FILES['file']['tmp_name'])) throw new Exception("Wybierz plik do przesłania.");
@@ -57,7 +64,7 @@ public function parseCsvFileAjax() {
 		$arrayRows = array();
 		$i = 0;
 
-		$viewFormHtml = '<form method="POST" >';
+		$viewFormHtml = '<div id="smad-window-modal" style="overflow-x: scroll;"><form method="POST" >';
 		foreach ($file as $line) {
 			 if ( $i == 0 ) {
 			 	$headCsv =  explode(';', $line);
@@ -66,17 +73,18 @@ public function parseCsvFileAjax() {
 
 			 	$arrayRows = explode(';', $line);
 
+				$viewFormHtml .= '<div style="margin: 10px 0"><span>'.$i.'. </span>';
 			 	foreach ($arrayRows as $keyData => $rowData) {
 						// sprawdzenie czy ustawiony naglówek - jeśli nie to wyświetl komunikat
-						if ($headCsv[$keyData]) throw new Exception("Brak nagłówka w kolumnie " . ($keyData + 1) . "w pliku CSV.");
-
-						$viewFormHtml .= '<input type="text" name="item["'.$i.'"]["'.$headCsv[$keyData].'"]" value="'.trim($rowData).'" />';
+						//	if ($headCsv[$keyData]) throw new Exception("Brak nagłówka w kolumnie " . ($keyData + 1) . "w pliku CSV.");
+						$viewFormHtml .= "<input type='text' name='item[$i]['".str_replace('"', '', $headCsv[$keyData])."']' value='".str_replace('"', '', $rowData )."' />";
 					}
+					$viewFormHtml .= '</div><div style="margin: 20px 0;"></div>';
 			 }
 			 $i++;
 
 		}
-		$viewFormHtml .= '</form>';
+		$viewFormHtml .= '</form></div>';
 
 		//return $viewFormHtml;
 		return [
@@ -91,6 +99,24 @@ public function parseCsvFileAjax() {
 	//	if (!isset($_FILES['fileData'])) throw new Exception("Błąd formularza ");
 	}
 
+	/**
+	 * Funkcja ajax do odebrania danych z przesłanego formularza z parsowanego CSV
+	 */
+	public function saveFormCsvFileAjaxAction() {
+		Response::sendTryCatchJson(array($this, 'saveFormCsvFileAjax'));
+	}
+
+
+/**
+ * Funkcja ajax do zapisania danych z przesłanego formularza z parsowanego CSV
+ */
+	public function saveFormCsvFileAjax() {
+
+		//todo: dane do zapisania z formularza
+	}
+
+
+
 	public function fetchEnergaRumKontrahenciPowiazaniaAjaxAction() {
 		Response::sendTryCatchJson(array($this, 'fetchEnergaRumKontrahenciPowiazaniaAjax')); // , $args = 'JSON_FROM_REQUEST_BODY');
 	}

+ 75 - 70
tools/Bocian.php.view.js

@@ -113,55 +113,33 @@ function showViewUploadFile( event, headerTitle ) {
 	event.preventDefault();
 
 	swal({
-		title: headerTitle,
-		//input: 'file',
-		html: '<div><input id="file-csv-upload" type="file" name="files[]" enctype="multipart/form-data" style="margin: 0 10% 0 auto;"></div>',
-
-	  showCancelButton: true,
-	  confirmButtonText: 'Zapisz',
-	  showLoaderOnConfirm: true,
+		title: 'Importuj plik csv',
+		input: 'file',
+		inputAttributes: {
+			'accept': 'text/csv',
+			'aria-label': 'Upload csv file'
+		},
+		confirmButtonText: 'Importuj',
 		preConfirm: function (file) {
-		  return new Promise(function (resolve, reject) {
-
-				if (!file ) reject('Proszę wybrać plik csv');
-
-				 var fileInputData = $('#file-csv-upload').prop('files')[0];
-
-		     var formData = new FormData();
-		     formData.append('file', fileInputData);
-		     console.log('uploadowany plik: ',formData);
-				 var responseViewForm = parseCsvFile( formData );
-				 console.log('dane po przeparsowaniu', responseViewForm);
-		  })
-			.then(function(response) {
-				return response.text();
-			})
-			.then(function(responseText) {
-
-				try {
-					return JSON.parse(responseText);
-				} catch (e) {
-					throw responseText;
+			return new Promise(function (resolve, reject) {
+				if (!file) {
+					reject("Proszę wybrać plik csv");
+					return
 				}
-			})
-			.then(function(result) {
+				var formData = new FormData();
+				formData.append('file', file);
+				console.log('uploadowany plik: ',formData);
+				var responseParseFile = parseCsvFile( formData );
 
-				if (result.type == 'success') {
-					p5UI__notifyAjaxCallback(result);
-					resolve(result.msg);
-				} else {
-					reject(result.msg);
-				}
+				console.log('TODO: upload file', file)
+				resolve('file imported')
 			})
-			.catch(function(error) {
-				console.log('request failed', error)
-			});
 		},
-	  allowOutsideClick: false
-	}).then(function (responseData) {
+	}).then(function (result) {
 
-	}).catch(function(error) {
-		console.log('request failed', error)
+		console.log('result', result)
+	}).catch(function (e) {
+		console.log('catch: ', e)
 	});
 
 }
@@ -182,36 +160,63 @@ function parseCsvFile( fileData ) {
 	 return response.json()
  }).then(function(json) {
 	 console.log('parsed json', json)
-	 return json;
+
+					 swal({
+								 title: 'Dane załadowane z pliku CSV',
+								 html: json.body.view,
+								 confirmButtonText: 'Zapisz',
+								 preConfirm: function (dataForm) {
+						 			return new Promise(function (resolve, reject) {
+						 				if (!dataForm) {
+						 					reject("Formularz nie zawiera danych do zapisania");
+						 					return
+						 				}
+
+										// zapisanie danych 
+						 				var responseSaveData = saveFormCsvFileAjaxAction( dataForm );
+
+						 				console.log('TODO: data save from form', dataForm)
+						 				resolve('data save from form')
+						 			})
+						 		},
+							 }).then(function (result) {
+
+	 							console.log('result', result)
+	 						}).catch(function (e) {
+	 							console.log('catch: ', e)
+	 						});
+
+	// return json;
  }).catch(function(ex) {
 	 console.log('parsing failed', ex)
- })
-
-	// window.fetch(URL_PARSE_CSV_FILE_AJAX, {
-	// 	method: 			'POST',
-	// 	credentials: 	'same-origin',
-	// 	body:  				file
-	// })
-	// .then(function(response) {
-	// 	return response.text();
-	// })
-	// .then(function(responseText) {
-	//
-	// 	console.log(responseText);
-	// })
-	// .then(function(result) {
-	// 	result
-	// 	if (result.type == 'success') {
-	// 		p5UI_notifyAjaxCallback(result);
-	// 		resolve(result.msg);
-	// 	} else {
-	// 		reject(result.msg);
-	// 	}
-	// })
-	// .catch(function(error) {
-	// 	console.log('request failed', error)
-	// });
+ });
+}
 
+/**
+ * Funkcja zapisuje dane z formularza do wgrania pliku csv
+ */
+function saveFormCsvFileAjaxAction( formData ) {
+	event.preventDefault();
+
+	fetch(URL_SAVE_FORM_CSV_FILE_AJAX, {
+	  method: 'POST',
+		credentials: 	'same-origin',
+		headers: {
+			'Content-Type': 'application/json'
+		},
+		body: JSON.stringify({
+			 formData: formData
+		})
+	})
+	.then(function(response) {
+	 return response.json()
+ }).then(function(json) {
+	 console.log('parsed json', json)
+
+	// return json;
+ }).catch(function(ex) {
+	 console.log('parsing failed', ex)
+ });
 }