Преглед изворни кода

TableAjax fix json response for saving record

Piotr Labudda пре 11 година
родитељ
комит
373dae627a
3 измењених фајлова са 109 додато и 99 уклоњено
  1. 1 1
      SE/VERSION
  2. 7 1
      SE/se-lib/Data_Source.php
  3. 101 97
      SE/se-lib/TableAjax.php

+ 1 - 1
SE/VERSION

@@ -1 +1 @@
-3.9.2
+3.9.3

+ 7 - 1
SE/se-lib/Data_Source.php

@@ -699,7 +699,13 @@ if(V::get('DBG_DS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:au
 	public function getDbErrors() {
 		$errors = array();
 		if ($this->_db->has_errors()) {
-			$errors = $this->_db->get_errors();
+			$errorsSql = $this->_db->get_errors();
+			foreach ($errorsSql as $vErr) {
+				if ('SQL QUERY FAILED: ' == substr($vErr, 0, 18)) {
+					$vErr = substr($vErr, 18);
+				}
+				$errors[] = $vErr;
+			}
 		}
 		return $errors;
 	}

+ 101 - 97
SE/se-lib/TableAjax.php

@@ -2687,7 +2687,7 @@ function hidePopover() {
 				break;
 			}
 			case 'CREATE_SAVE': {
-				$this->sendAjaxCreateSave($_REQUEST);
+				$this->sendAjaxResponseJson('ajaxCreateSave', $_REQUEST);
 				break;
 			}
 			case 'HIST': {
@@ -3299,68 +3299,100 @@ jQuery(document).ready(function(){
 		var taskCnt = jQuery('<div id="<?php echo $this->_htmlID . '_task'; ?>" class="AjaxTableTaskCnt AjaxTable-loading"></div>').appendTo(taskCont);
 		jQuery('<div class="alert alert-error"><div style="padding:0 0 0 20px; background:url(./icon/loading.gif) no-repeat left top;"> save ... </div></div>').appendTo(taskCnt);
 
-		jQuery.ajax({
-			url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=CREATE_SAVE',
-			type: 'POST',
-			dataType: 'json',
+		function notifyAjaxCallback(data) {
+			var notify = {};
+			notify.type = (data && data.type)? data.type : '';
+			notify.msg = (data && data.msg)? data.msg : '';
+			switch (notify.type) {
+				case 'success':
+					if (!notify.msg) notify.msg = 'OK';
+					break;
+				case 'info':
+					if (!notify.msg) notify.msg = '';
+					break;
+				case 'error':
+					if (!notify.msg) notify.msg = 'Wystąpiły błędy';
+					break;
+				case 'warning':
+					notify.type = 'warn';
+					if (!notify.msg) notify.msg = 'Wystąpiły błędy';
+					break;
+				default:
+					notify.msg = 'Nieznany błąd';
+					if (data && data.errorCode) notify.msg += ' ' + data.errorCode;
+					notify.type = '';
+			}
+			jQuery.notify(notify.msg, notify.type);
+		}
+		$.ajax({
 			data: data,
+			dataType: 'json',
+			type: "POST",
 			async: true,
-			success: function(data) {
-				//console.log('success L.<?php echo __LINE__; ?> data', data);
-			},
-			error: function(xhr) {
-				//console.log('error L.<?php echo __LINE__; ?> xhr', xhr);
-			},
-			complete: function(xhr) {
-				if (xhr.responseJSON) {
-					var data = xhr.responseJSON;
+			url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=CREATE_SAVE'
+		})
+		.always(function(dataOrJqXHR){
+			console.log('dataOrJqXHR (isXHR: ', (dataOrJqXHR && 'readyState' in dataOrJqXHR), ')', dataOrJqXHR);
+			var data;
+			if (dataOrJqXHR && 'readyState' in dataOrJqXHR) {
+				if ('responseJSON' in dataOrJqXHR) {
+					data = dataOrJqXHR.responseJSON;
 				} else {
-					// TODO: error
+					data = {};
+					data.msg = jqXHR.responseText || 'Nieznany błąd';
+					if (jqXHR.status == 404) {
+						data.type = 'error';
+					} else {
+						data.type = 'warning';
+					}
 				}
+			}
+			else {
+				data = dataOrJqXHR;
+			}
 
-				taskCnt.removeClass('AjaxTable-loading');
-				taskCnt.empty();
-				if (typeof data == 'object') {
-					if (data.type == 'ERROR') {
-						var out = '<div class="container">' + 
-								'<div class="alert alert-error">' +
-									'<h4>Wystąpiły błędy!</h4>' + data.msg +
-								'</div>';
-						out += '<div class="breadcrumb">' +
-									' <a class="create-fix" href="#CREATE/' + Math.random(1).toString().substr(2) + '" class="btn btn-link btn-small"> <i class="icon-arrow-left"></i> Wróć do formularza i popraw dane</a></li>' +
-								'</div>' +
-							'</div>';
-						jQuery(out).appendTo(taskCnt);
-						var taskContLastNode = taskContLast
-							, taskCntNode = taskCnt;
-						taskCnt.find('.create-fix').click(function(){
-							taskCntNode.remove();
-							taskContLastNode.fadeIn('slow');
-							return false;
-						});
-					}
-					else if (data.type == 'SUCCESS') {
-						var msg = '';
-						if (data.id && data.id > 0) {
-							msg = 'Utworzono pomyślnie rekord: ID = ' + data.id;
-						} else if (data.msg) {
-							msg = data.msg;
-						} else {
-							msg = 'OK';
-						}
-						var out = '<div class="container">';
-						out += '<div class="alert alert-success">' + msg + '</div>';
-						out += '<div class="breadcrumb">' +
-								' <a href="#" onclick="return tableAjaxBackToTable();" class="btn btn-link btn-small"> <i class="icon-arrow-left"></i> Wróć do tabeli <?php echo $this->getLabelHtml(); ?></a>' +
-								'<span class="divider">/</span>' +
-								' <a href="#EDIT/' + data.id + '" class="btn btn-link btn-small"> <i class="icon-pencil"></i> Edytuj rekord ' + data.id + '</a>' +
-								'<span class="divider">/</span>' +
-								' <a href="#CREATE/' + Math.random(1).toString().substr(2) + '" class="btn btn-link btn-small"> <i class="icon-plus"></i> Dodaj nowy rekord</a></li>' +
-							'</div>';
-						out += '</div>';
-						jQuery(out).appendTo(taskCnt);
-					}
+			notifyAjaxCallback(data);
+
+			taskCnt.removeClass('AjaxTable-loading');
+			taskCnt.empty();
+			if (data.type == 'error' || data.type == 'warning') {
+				var out = '<div class="container">' + 
+						'<div class="alert alert-error">' +
+							'<h4>Wystąpiły błędy!</h4>' + data.msg +
+						'</div>';
+				out += '<div class="breadcrumb">' +
+							' <a class="create-fix" href="#CREATE/' + Math.random(1).toString().substr(2) + '" class="btn btn-link btn-small"> <i class="icon-arrow-left"></i> Wróć do formularza i popraw dane</a></li>' +
+						'</div>' +
+					'</div>';
+				jQuery(out).appendTo(taskCnt);
+				var taskContLastNode = taskContLast
+					, taskCntNode = taskCnt;
+				taskCnt.find('.create-fix').click(function(){
+					taskCntNode.remove();
+					taskContLastNode.fadeIn('slow');
+					return false;
+				});
+			}
+			else if (data.type == 'success') {
+				var msg = '';
+				if (data.id && data.id > 0) {
+					msg = 'Utworzono pomyślnie rekord: ID = ' + data.id;
+				} else if (data.msg) {
+					msg = data.msg;
+				} else {
+					msg = 'OK';
 				}
+				var out = '<div class="container">';
+				out += '<div class="alert alert-success">' + msg + '</div>';
+				out += '<div class="breadcrumb">' +
+						' <a href="#" onclick="return tableAjaxBackToTable();" class="btn btn-link btn-small"> <i class="icon-arrow-left"></i> Wróć do tabeli <?php echo $this->getLabelHtml(); ?></a>' +
+						'<span class="divider">/</span>' +
+						' <a href="#EDIT/' + data.id + '" class="btn btn-link btn-small"> <i class="icon-pencil"></i> Edytuj rekord ' + data.id + '</a>' +
+						'<span class="divider">/</span>' +
+						' <a href="#CREATE/' + Math.random(1).toString().substr(2) + '" class="btn btn-link btn-small"> <i class="icon-plus"></i> Dodaj nowy rekord</a></li>' +
+					'</div>';
+				out += '</div>';
+				jQuery(out).appendTo(taskCnt);
 			}
 		});
 
@@ -3372,23 +3404,11 @@ jQuery(document).ready(function(){
 		exit;
 	}
 
-	private function sendAjaxCreateSave($args) {
-		header("Content-type: application/json");
+	private function ajaxCreateSave($args) {
 		$DBG = ('1' == V::get('DBG', '', $_REQUEST));
-		sleep(1);// TODO: RMME DBG loading
 
 		if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: save ID(' . $id . ') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';}
 		if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">acl (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_acl);echo'</pre>';}
-		$dbID = $this->_acl->getDB();
-		$db = DB::getDB($dbID);
-
-		if (!$db) {
-			header('HTTP/1.0 406 Not Acceptable');
-			echo '{"type":"ERROR", "msg": "' . "Błąd połączenia z bazą danych!" . '"}';
-			exit;
-		}
-
-		$tblName = $this->_acl->getName();
 
 		$sqlObj = new stdClass();
 		$fields = $this->_acl->getFields();
@@ -3425,36 +3445,20 @@ jQuery(document).ready(function(){
 
 		$retID = $this->_dataSource->addItem($sqlObj);
 
-		$retJson = new stdClass();
-		$retJson->type = '';
-		$retJson->msg = '';
-
+		$response = new stdClass();
+		$response->type = '';
+		$response->msg = '';
 		if ($retID > 0) {
-			$retJson->type = 'SUCCESS';
-			$retJson->msg = "Utworzono pomyślnie rekord: ID = {$retID}";
-			$retJson->id = $retID;
+			$response->type = 'success';
+			$response->msg = "Utworzono pomyślnie rekord nr {$retID}";
+			$response->id = $retID;
+			$response->record = $this->_dataSource->getItem($retID);
 		} else {
-			header('HTTP/1.0 404 Not Found');
-			$retJson->type = 'ERROR';
-			$retJson->msg = "";
-			if ($db->has_errors()) {
-				$outArr = array();
-				$errorsSql = $db->get_errors();
-				foreach ($errorsSql as $vErr) {
-					if (substr($vErr, 0, 18) == 'SQL QUERY FAILED: ') {
-						$vErr = substr($vErr, 18);
-						// Duplicate entry '123456-1' for key 'P_NIP'
-						if (substr($vErr, 0, 16) == 'Duplicate entry ') {
-						}
-					}
-					$outArr[] = $vErr;
-				}
-				$retJson->msg .= implode('<br>', $outArr);
-			}
+			$response->type = 'error';
+			$response->msg = "Wystąpiły błędy!";
+			$response->errors = $this->_dataSource->getDbErrors();
 		}
-
-		echo json_encode($retJson);
-		exit;
+		return $response;
 	}
 
 	private function sendAjaxCopy($id, $args) {