소스 검색

updated TableAjax widget to use sync url with namespace for removeGeom

Piotr Labudda 9 년 전
부모
커밋
186f0284a0
2개의 변경된 파일92개의 추가작업 그리고 115개의 파일을 삭제
  1. 61 1
      SE/se-lib/Route/ViewTableAjax.php
  2. 31 114
      SE/se-lib/TableAjax.php

+ 61 - 1
SE/se-lib/Route/ViewTableAjax.php

@@ -102,6 +102,7 @@ class Route_ViewTableAjax extends RouteBase {
 				UI::endContainer();
 			}
 
+			$tbl->setSyncUrl(Request::getPathUri() . 'index.php?_route=ViewTableAjax&namespace=' . $acl->getNamespace());
 			$tbl->setLabel($tblLabel);
 			$tbl->setFilterInit($filterInit);
 			if (!empty($forceFilterInit)) $tbl->setForceFilterInit($forceFilterInit);
@@ -122,10 +123,19 @@ class Route_ViewTableAjax extends RouteBase {
 	public function revertFromHistAjaxAction() {
 		Response::sendTryCatchJson(array($this, 'revertFromHistAjax'));
 	}
-
 	public function revertFromHistAjax() {
 		$typeName = V::get('typeName', '', $_REQUEST, 'word');
 		if (!$typeName) throw new Exception("Wrong param typeName");
+
+		// TODO: use namespace from url
+		// $namespace = V::get('namespace', '', $_GET, 'word');
+		// if (!$namespace) {
+		// 	$typeName = V::get('typeName', '', $_GET, 'word');
+		// 	if (!$typeName) throw new Exception("Wrong param typeName");
+		// 	$namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
+		// }
+		// $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
+
 		$id = V::get('ID', '', $_REQUEST, 'word');
 		if (!$id) throw new Exception("Wrong param ID");
 		$idHist = V::get('idHist', '', $_REQUEST, 'word');
@@ -169,6 +179,56 @@ class Route_ViewTableAjax extends RouteBase {
 		throw new Exception("BUG: update field '{$fieldName}' in item[{$id}] from hist[{$idHist}]", 501);
 	}
 
+	public function removeTheGeomAjaxAction() {
+		Response::sendTryCatchJson(array($this, 'removeTheGeomAjax'), $args = 'JSON_FROM_REQUEST_BODY');
+	}
+	public function removeTheGeomAjax($args) {
+		$namespace = V::get('namespace', '', $args, 'word');
+		if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
+		$acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
+		$primaryKeyField = $acl->getPrimaryKeyField();
+		$primaryKey = V::get($primaryKeyField, 0, $args, 'int');
+		$geomFieldName = 'the_geom';
+		$response = new stdClass();
+
+		if ($primaryKey <= 0) throw new HttpException("Bad Request - Wrong param ID", 400);
+
+		$record = $acl->getItem($primaryKey);
+		if (!$record) throw new HttpException("Nie odnaleziono rekordu nr {$primaryKey}", 404);
+		if (!$acl->canWriteObjectField($geomFieldName, $record)) throw new HttpException("Brak dostępu do zapisu dla pola {$geomFieldName}", 403);
+
+		if (empty($record->{$geomFieldName})) {
+			$response->type = 'info';
+			$response->msg = "Rekord nie jest powiązany z żadnym obiektem na mapie";
+			$response->record = $record;
+			return $response;
+		}
+
+		$itemPatch = array();
+		$itemPatch[$geomFieldName] = "NULL";
+		$itemPatch[$primaryKeyField] = $primaryKey;
+
+		$response = new stdClass();
+		try {
+			$affected = $acl->updateItem($itemPatch);
+
+			if ($affected > 0) {
+				$response->type = 'success';
+				$response->msg = "Usunięto obiekt z mapy dla rekordu {$primaryKey}";// Rekord zapisany pomyślnie
+			} else if ($affected == 0) {
+				$response->type = 'info';
+				$response->msg = "Rekord nie był powiązany z żadnym obiektem na mapie";// Nie wprowadzono żadnych zmian
+			}
+			$response->record = $acl->getItem($primaryKey);
+		}
+		catch (Exception $e) {
+			$response->type = 'error';
+			$response->msg = $e->getMessage();
+		}
+
+		return $response;
+	}
+
 	/**
 	 * @param string $typeName - 'p5_default_db:TEST_PERMS'
 	 */

+ 31 - 114
SE/se-lib/TableAjax.php

@@ -24,6 +24,7 @@ class TableAjax extends ViewAjax {
 	private $_filterInit = null;
 	private $_forceFilterInit = null;
 	private $_showProcesInit = true;
+	public $syncUrl = null;
 
 	public function __construct($tblAcl) {
 		$this->_tbl = $tblAcl->getName();
@@ -49,6 +50,10 @@ class TableAjax extends ViewAjax {
 		}
 	}
 
+	public function setSyncUrl($syncUrl) {
+		$this->syncUrl = $syncUrl;
+	}
+
 	public function showProcesInit($bool = true) {
 		$this->_showProcesInit = $bool;
 	}
@@ -990,63 +995,34 @@ var p5UI_TableAjax_generateFunctionNode = function(funObj, rowPK, props) {
 										if (!recordID) return;
 
 										var selectedRecordId = recordID;
-										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 = 'Usunięto obiekt z mapy dla rekordu ' + selectedRecordId;
-													break;
-												case 'info':
-													if (!notify.msg) notify.msg = 'Rekord nie był powiązany z żadnym obiektem na mapie';
-													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: {},
-											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_REMOVE&ID=' + recordID
-										})
-										.done(function(data, textStatus, jqXHR){
-											notifyAjaxCallback(data);
-											if (priv.options.debug) console.log('data.record.the_geom:', data.record.the_geom);
-											if (data && data.record && data.record.the_geom) {
-												if (priv.options.debug) console.log('data.record.the_geom:2:', data.record.the_geom);
-												geomField.setValue(data.record.the_geom);
-											}
-											else if (data && data.record) {
-												if (priv.options.debug) console.log('data.record.the_geom:2:', data.record.the_geom);
-												geomField.setValue(data.record.the_geom);
-											}
-											_mapEditor.TableAjaxMapRefresh();
-										})
-										.fail(function(jqXHR){// jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
-											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');
+										superagent
+											.post('<?= $this->syncUrl; ?>&_hash=<?= $this->_htmlID; ?>&_task=removeTheGeomAjax')
+											.type('json') // header ĺapplication/x-www-form-urlencoded' requires type('form');
+											.send({
+												ID: recordID,
+												namespace: '<?= $acl->getNamespace(); ?>'
+											})
+											.set('Accept', 'application/json')
+											.end(function(err, res) {
+												if(priv.options.debug)console.log('DBG: res:', res, 'res.body:', res.body);
+												var payload;
+												if (err || !res.ok || 'application/json' !== res.type) {
+													payload = {type: 'warning', msg: res.body.msg || 'Wystąpiły błędy', body: res.body};
 												} else {
-													jQuery.notify(jqXHR.responseText, 'warn');
+													payload = {type: 'success', msg: res.body.msg || '', body: res.body};
+												}
+												p5UI__notifyAjaxCallback(payload);
+												var data = res.body;
+												if (data && data.record && data.record.the_geom) {
+													if (priv.options.debug) console.log('data.record.the_geom:2:', data.record.the_geom);
+													geomField.setValue(data.record.the_geom);
 												}
-											}
-										});
+												else if (data && data.record) {
+													if (priv.options.debug) console.log('data.record.the_geom:2:', data.record.the_geom);
+													geomField.setValue(data.record.the_geom);
+												}
+												_mapEditor.TableAjaxMapRefresh();
+											});
 									}
 								}
 							});
@@ -4058,10 +4034,6 @@ function <?php echo $jsToogleFiltrProcesuFunctionName; ?>(n) {
 				$this->sendAjaxResponseJson('ajaxTheGeomSave', $_REQUEST);
 				break;
 			}
-			case 'THE_GEOM_REMOVE': {
-				$this->sendAjaxResponseJson('ajaxTheGeomRemove', $_REQUEST);
-				break;
-			}
 			case 'PROCES_INIT_FILTR': {
 				$this->sendAjaxResponseJson('ajaxProcesInitFiltr', $_REQUEST);
 				break;
@@ -6194,61 +6166,6 @@ jQuery(document).ready(function(){
 		exit;
 	}
 
-	private function ajaxTheGeomRemove($args) {// ajax task 'THE_GEOM_REMOVE'
-		$primaryKeyField = $this->_acl->getPrimaryKeyField();
-		$primaryKey = V::get($primaryKeyField, 0, $args, 'int');
-		$geomFieldName = 'the_geom';
-		$response = new stdClass();
-
-		if ($primaryKey <= 0) {
-			throw new HttpException("Wrong param ID", 404);
-		}
-
-		$record = $this->_acl->getItem($primaryKey);
-		if (!$record) {
-			throw new HttpException("Nie odnaleziono rekordu nr {$primaryKey}", 404);
-		}
-		if (!$this->_acl->canWriteRecord($record) && !$this->_acl->hasPermSuperWrite()) {
-			throw new HttpException("Brak dostępu do rekordu nr {$primaryKey}", 403);
-		}
-
-		$theGeomFieldId = $this->_acl->getFieldIdByName($geomFieldName);
-		if (!$this->_acl->isAllowed($theGeomFieldId, 'W', $record)) {
-			throw new HttpException("Brak dostępu do zapisu dla pola {$geomFieldName}", 403);
-		}
-
-		if (empty($record->{$geomFieldName})) {
-			$response->type = 'info';
-			$response->msg = "Rekord nie jest powiązany z żadnym obiektem na mapie";
-			$response->record = $record;
-			return $response;
-		}
-
-		$itemPatch = array();
-		$itemPatch[$geomFieldName] = "NULL";
-		$itemPatch[$primaryKeyField] = $primaryKey;
-
-		$response = new stdClass();
-		try {
-			$affected = $this->_acl->updateItem($itemPatch);
-
-			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";
-			}
-			$response->record = $this->_acl->getItem($primaryKey);
-		}
-		catch (Exception $e) {
-			$response->type = 'error';
-			$response->msg = $e->getMessage();
-		}
-
-		return $response;
-	}
-
 	private function ajaxProcesInitFiltr($args) {// ajax task 'PROCES_INIT_FILTR'
 		$response = new stdClass();