Sfoglia il codice sorgente

TableAjax fix json response for saving geom

Piotr Labudda 11 anni fa
parent
commit
1c995f7fe8
1 ha cambiato i file con 62 aggiunte e 48 eliminazioni
  1. 62 48
      SE/se-lib/TableAjax.php

+ 62 - 48
SE/se-lib/TableAjax.php

@@ -1001,7 +1001,6 @@ class TableAjax extends ViewAjax {
 													var notify = {};
 													notify.type = (data && data.type)? data.type : '';
 													notify.msg = (data && data.msg)? data.msg : '';
-													console.log('TODO: data:', data, 'notify', notify);
 													switch (notify.type) {
 														case 'success':
 															if (!notify.msg) notify.msg = 'Usunięto obiekt z mapy dla rekordu ' + selectedRecordId;
@@ -1017,7 +1016,8 @@ class TableAjax extends ViewAjax {
 															if (!notify.msg) notify.msg = 'Wystąpiły błędy';
 															break;
 														default:
-															notify.msg = 'TODO: response...';
+															notify.msg = 'Nieznany błąd';
+															if (data && data.errorCode) notify.msg += ' ' + data.errorCode;
 															notify.type = '';
 													}
 													jQuery.notify(notify.msg, notify.type);
@@ -1045,7 +1045,6 @@ class TableAjax extends ViewAjax {
 														notifyAjaxCallback(jqXHR.responseJSON);
 													}
 													else {
-														console.log('TODO: jqXHR', jqXHR);
 														var txt = jqXHR.responseText || 'Wystąpiły błędy';
 														if (jqXHR.status == 404) {
 															jQuery.notify(jqXHR.responseText, 'error');
@@ -1708,22 +1707,54 @@ class TableAjax extends ViewAjax {
 						return;
 					}
 
+					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 = 'Aktualizacja danych dla rekordu ' + selectedRecordId;
+								break;
+							case 'info':
+								if (!notify.msg) notify.msg = 'Nie wprowadzono żadnych zmian';
+								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: {polygon: selectedFeatureExtent},
+						dataType: 'json',
 						type: "POST",
 						url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=THE_GEOM_SAVE&ID=' + selectedRecordId
 					})
 					.done(function(data, textStatus, jqXHR){
-						jQuery.notify('Aktualizacja danych dla rekordu ' + selectedRecordId, 'success');
+						notifyAjaxCallback(data);
+						//jQuery.notify('Aktualizacja danych dla rekordu ' + selectedRecordId, 'success');
 						publ.loadPage(0);// TODO: reload table data
 					})
 					.fail(function(jqXHR){// jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
-						var txt = jqXHR.responseText || 'Error';
-						if (jqXHR.status == 404) {
-							jQuery.notify(jqXHR.responseText, 'error');
-						} else {
-							jQuery.notify(jqXHR.responseText, 'warn');
+						if (jqXHR.responseJSON) {
+							notifyAjaxCallback(jqXHR.responseJSON);
+						}
+						else {
+							var txt = jqXHR.responseText || 'Wystąpiły błędy';
+							if (jqXHR.status == 404) {
+								jQuery.notify(jqXHR.responseText, 'error');
+							} else {
+								jQuery.notify(jqXHR.responseText, 'warn');
+							}
 						}
 					});
 
@@ -2724,7 +2755,7 @@ function hidePopover() {
 				break;
 			}
 			case 'THE_GEOM_SAVE': {
-				$this->sendTheGeomSave($_REQUEST);
+				$this->sendAjaxResponseJson('sendTheGeomSave', $_REQUEST);
 				break;
 			}
 			case 'THE_GEOM_REMOVE': {
@@ -4671,62 +4702,45 @@ jQuery(document).ready(function(){
 		UserProfile::save();
 	}
 
-	private function sendTheGeomSave($args) {// ajax task 'THE_GEOM_SAVE'
+	private function sendTheGeomSave($args) {
 		$id = V::get('ID', 0, $args, 'int');
 		$polygon = V::get('polygon', 0, $args);
 		$geomFieldName = 'the_geom';
-
 		if ($id <= 0) {
-			header('HTTP/1.0 404 Not Found');
-			echo "Wrong param ID";
-			exit;
+			throw new HttpException("Wrong param ID", 404);
 		}
 		// TODO: validate polygon - ex.: POLYGON((2072030.2315435 7234115.910678,2072029.4815435 7234093.660678,2072115.2315435 7234091.160678,2072115.4815435 7234113.660678,2072115.2315435 7234113.660678,2072094.2315435 7234113.910678,2072030.2315435 7234115.910678)))
 
-		$tblName = $this->_acl->getName();
-
-		$db = DB::getDB();
-		$record = $db->get_by_id($tblName, $id);
+		$record = $this->_dataSource->getItem($id);
 		if (!$this->_acl->canWriteRecord($record) && !$this->_acl->hasPermSuperWrite()) {
-			header('HTTP/1.0 403 Forbidden');
-			echo "Brak dostępu do rekordu";
-			exit;
+			throw new HttpException("Brak dostępu do rekordu", 403);
 		}
 
 		$theGeomFieldId = $this->_acl->getFieldIdByName($geomFieldName);
 		if (!$this->_acl->isAllowed($theGeomFieldId, 'W', $record)) {
-			header('HTTP/1.0 403 Forbidden');
-			echo "Brak dostępu do zapisu dla pola {$geomFieldName}";
-			exit;
+			throw new HttpException("Brak dostępu do zapisu dla pola {$geomFieldName}", 403);
 		}
 
-		$sqlObj = new stdClass();
-		$sqlObj->{$geomFieldName} = "GeomFromText('{$polygon}')";
-		$sqlObj->ID = $id;
-		if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sqlObj);echo'</pre>';}
+		$itemPath = new stdClass();
+		$itemPath->{$geomFieldName} = "GeomFromText('{$polygon}')";
+		$itemPath->ID = $id;
 
-		$ret = $db->UPDATE_OBJ($tblName, $sqlObj);
+		$affected = $this->_dataSource->updateItem($itemPath);
 
-		if ($ret > 0) {
-			echo '<div class="alert alert-success">';
-				echo "Rekord zapisany pomyślnie";//"Record saved successfully";
-			echo '</div>';
-		} else if ($ret == 0) {
-			echo '<div class="alert alert-info">';
-				echo "Nie wprowadzono żadnych zmian";
-			echo '</div>';
+		$response = new stdClass();
+		if ($affected > 0) {
+			$response->type = 'success';
+			$response->msg = "Rekord zapisany pomyślnie";//"Record saved successfully";
+		} else if ($affected == 0) {
+			$response->type = 'info';
+			$response->msg = "Nie wprowadzono żadnych zmian";
 		} else {
-			header('HTTP/1.0 404 Not Found');
-			echo '<div class="alert alert-error">';
-				echo '<h4>' . "Wystąpiły błędy!" . '</h4>';
-				if ($db->has_errors()) {
-					$errors = $db->get_errors();
-					echo implode('<br>', $errors);
-				}
-			echo '</div>';
+			$response->type = 'error';
+			$response->msg = "Wystąpiły błędy!";
+			$response->errors = $this->_dataSource->getDbErrors();
 		}
-
-		exit;
+		$response->record = $this->_dataSource->getItem($id);
+		return $response;
 	}
 
 	private function sendAjaxResponseJson($method, $args) {