Przeglądaj źródła

TableAjax: remove the_geom value from record

Piotr Labudda 11 lat temu
rodzic
commit
2c21cb7198
2 zmienionych plików z 279 dodań i 7 usunięć
  1. 15 0
      SE/se-lib/Data_Source.php
  2. 264 7
      SE/se-lib/TableAjax.php

+ 15 - 0
SE/se-lib/Data_Source.php

@@ -684,4 +684,19 @@ if(V::get('DBG_DS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:au
 		return in_array($fldName, $this->_geomFields);
 	}
 
+	public function updateItem($item) {
+		if (!$item->ID) return false;
+
+		$affected = $this->_db->UPDATE_OBJ($this->_tbl, $item);
+		return $affected;
+	}
+
+	public function getDbErrors() {
+		$errors = array();
+		if ($this->_db->has_errors()) {
+			$errors = $this->_db->get_errors();
+		}
+		return $errors;
+	}
+
 }

+ 264 - 7
SE/se-lib/TableAjax.php

@@ -346,6 +346,9 @@ class TableAjax extends ViewAjax {
 /* map */
 .AjaxTable .cell-mapfld { cursor:pointer; }
 .AjaxTable .cell-mapfld:hover { opacity:1; }
+ .AjaxTable .cell-mapfld-remove { display:none; }
+ .AjaxTable .cell-mapfld-hasValue .cell-mapfld-select { background-color:#f00; }
+ .AjaxTable .cell-mapfld-hasValue .cell-mapfld-remove { display:inline-block; }
 .AjaxTableCont .mapEditor { position:absolute; bottom:0; right:6px; width:512px; height:318px; overflow:hidden; background:#eee; border:1px solid #999; }
  .AjaxTableCont .mapEditor-panel { height:16px; padding:0 6px; background:#999; border-bottom:1px solid #eee; }
   .AjaxTableCont .mapEditor-panel a { display:block; float:right; padding:0 6px; font-weight:bold; font-size:12px; line-height:16px; color:#fff; }
@@ -387,7 +390,107 @@ class TableAjax extends ViewAjax {
 			<div id="<?php echo $this->_htmlID; ?>"></div>
 		</div>
 		<script>
-(function ($, undefined) {
+
+(function($) {
+	var TableAjaxGeomField = function() {
+		var priv = {}; //private api
+		var publ = {}; //public api
+
+		priv.options = {};
+		var defaults = {
+			recordID: '',
+			fieldValue: '',
+			hasValueClassName: 'TableAjaxGeomField-hasValue',
+			linkClassNamePrefix: 'TableAjaxGeomField',
+			linkSelectClassName: 'select',
+			linkSelectAddClassNames: '', // 'ico-map-marker'
+			linkRemoveClassName: 'remove',
+			linkRemoveAddClassNames: '', // 'ico-remove'
+			removeUrl: '',
+			onSelect: null,
+			onRemove: null,
+			debug: false
+		};
+
+		priv.init = function() {
+			priv.render();
+		};
+
+		priv.render = function() {
+			var state = priv.options;
+			var cellMapSelect = jQuery('<i title="Pokaż/utwórz obiekt na mapie"></i>');
+			cellMapSelect.addClass(state.linkClassNamePrefix + '-' + state.linkSelectClassName);
+			if (state.linkSelectAddClassNames) {
+				cellMapSelect.addClass(state.linkSelectAddClassNames);
+			}
+			cellMapSelect.on('click', {recordID: priv.options.recordID, fieldValue: priv.options.fieldValue}, function(e) {
+				if (typeof state.onSelect == "function") {
+					state.onSelect.call(this, e.data.recordID, e.data.fieldValue);
+				}
+			});
+
+			var cellMapRemove = jQuery('<i title="Usuń obiekt z mapy"></i>');
+			cellMapRemove.addClass(state.linkClassNamePrefix + '-' + state.linkRemoveClassName);
+			if (state.linkRemoveAddClassNames) {
+				cellMapRemove.addClass(state.linkRemoveAddClassNames);
+			}
+			cellMapRemove.on('click', {recordID: state.recordID, fieldValue: state.fieldValue}, function(e) {
+				if (typeof state.onRemove == "function") {
+					state.onRemove.call(this, publ, e.data.recordID, e.data.fieldValue);
+				}
+			});
+
+			var node = $(state.id);
+			node.empty();
+			node.addClass(state.linkClassNamePrefix);
+			node.removeClass(state.hasValueClassName);
+			if (state.fieldValue) {
+				node.addClass(state.hasValueClassName);
+			}
+
+			node.append(cellMapSelect);
+			node.append(cellMapRemove);
+		};
+
+		publ.setValue = function(value) {
+			console.log('TEST setValue: ', value, 'this', this);
+			priv.options.fieldValue = value;
+			priv.render();
+		};
+
+		publ.init = function(options) {
+			if (priv.options.debug) console.log('TableAjaxGeomField initialization...', options);
+			//merge supplied options with defaults
+			$.extend(priv.options, defaults, options);
+			priv.init();
+			return publ;
+		};
+		return publ;
+	};
+
+	$.fn.TableAjaxGeomFieldSetValue = function(value) {
+		return this.each(function() {
+			var tblAjaxMapGeomFld = $(this).data('TableAjaxGeomField');
+			if (tblAjaxMapGeomFld) {
+				tblAjaxMapGeomFld.setValue(value);
+			}
+		});
+		return this;
+	};
+
+	$.fn.TableAjaxGeomField = function(options) {
+		options = options || {};
+		return this.each(function() {
+			options.id = this;
+			if (!$(this).data('TableAjaxGeomField')) {
+				$(this).data('TableAjaxGeomField', new TableAjaxGeomField().init(options));
+			}
+		});
+		return this;
+	};
+}(jQuery));
+
+(function($, undefined) {
 	var TableAjax = function () {
 		var priv = {}; //private api
 		var publ = {}; //public api
@@ -874,10 +977,85 @@ class TableAjax extends ViewAjax {
 									cellCnt.html(format.f(val));
 									break;
 								case "geom":
-									cellCnt.html('<i class="cell-mapfld icon-map-marker"></i>');
-									cellCnt.on('click', {recordID: cellID, fieldValue: val}, function(e) {
-										priv.mapEditorShow();
-										_mapEditor.TableAjaxMapSelectRecord(e.data.recordID, e.data.fieldValue);
+									cellCnt.TableAjaxGeomField({
+										recordID: cellID,
+										fieldValue: val,
+										hasValueClassName: 'cell-mapfld-hasValue',
+										//removeUrl: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=THE_GEOM_REMOVE&ID=' + cellID,
+										linkClassNamePrefix: 'cell-mapfld',
+										linkSelectClassName: 'select',
+										linkSelectAddClassNames: 'ico-map-marker',
+										linkRemoveClassName: 'remove',
+										linkRemoveAddClassNames: 'ico-remove',
+										onSelect: function(recordID, geomShape) {
+											console.log('recordID:', recordID, 'geomShape:', geomShape);
+											priv.mapEditorShow();
+											_mapEditor.TableAjaxMapSelectRecord(recordID, geomShape);
+										},
+										onRemove: function(geomField, recordID, geomShape) {
+											if (confirm('Czy usunąć obiekt z mapy dla rekordu ' + recordID + '?')) {
+												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 : '';
+													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;
+															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 = 'TODO: response...';
+															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);
+													console.log('TODO: data.record.the_geom:', data.record.the_geom);
+													if (data && data.record && data.record.the_geom) {
+														console.log('TODO: data.record.the_geom:2:', data.record.the_geom);
+														geomField.setValue(data.record.the_geom);
+													}
+													else if (data && data.record) {
+														console.log('TODO: data.record.the_geom:2:', data.record.the_geom);
+														geomField.setValue(data.record.the_geom);
+													}
+												})
+												.fail(function(jqXHR){// jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
+													if (jqXHR.responseJSON) {
+														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');
+														} else {
+															jQuery.notify(jqXHR.responseText, 'warn');
+														}
+													}
+												});
+											}
+										}
 									});
 									break;
 							}
@@ -1519,6 +1697,7 @@ class TableAjax extends ViewAjax {
 			priv.options.mapEditor = true;
 			_mapEditorWrap.show();
 			_mapEditor.TableAjaxMap({
+				//debug: true,
 				wpsUrl: 'http://biuro.biall-net.pl/wps',
 				wfsUrl: 'http://biuro.biall-net.pl/wps',
 				layerName: '<?php echo $this->getLabelHtml(); ?>',
@@ -1536,8 +1715,8 @@ class TableAjax extends ViewAjax {
 						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){
-						//TODO: update table cell data; that.data("treetable").updateNodeHtmlData(id, jqXHR.responseText);
 						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';
@@ -2548,6 +2727,10 @@ function hidePopover() {
 				$this->sendTheGeomSave($_REQUEST);
 				break;
 			}
+			case 'THE_GEOM_REMOVE': {
+				$this->sendAjaxResponseJson('sendTheGeomRemove', $_REQUEST);
+				break;
+			}
 			default:
 				$this->sendAjaxData($_REQUEST);
 		}
@@ -4484,7 +4667,7 @@ jQuery(document).ready(function(){
 		UserProfile::save();
 	}
 
-	private function sendTheGeomSave($args) {
+	private function sendTheGeomSave($args) {// ajax task 'THE_GEOM_SAVE'
 		$id = V::get('ID', 0, $args, 'int');
 		$polygon = V::get('polygon', 0, $args);
 		$geomFieldName = 'the_geom';
@@ -4542,4 +4725,78 @@ jQuery(document).ready(function(){
 		exit;
 	}
 
+	private function sendAjaxResponseJson($method, $args) {
+		try {
+			$response = $this->ajaxTheGeomRemove($args);
+		}
+		catch (Exception $e) {
+			$response = new stdClass();
+			$response->type = 'error';
+			$response->msg = $e->getMessage();
+			$response->errorCode = $e->getCode();
+		}
+
+		switch ($response->errorCode) {
+			case 404: header('HTTP/1.0 404 Not Found'); break;
+			case 403: header('HTTP/1.0 403 Forbidden'); break;
+			case 4033: header('HTTP/1.0 403.3 - Write access forbidden'); break;
+			default:
+		}
+		echo json_encode($response);
+		exit;
+	}
+
+	private function ajaxTheGeomRemove($args) {// ajax task 'THE_GEOM_REMOVE'
+		$id = V::get('ID', 0, $args, 'int');
+		$geomFieldName = 'the_geom';
+		$response = new stdClass();
+
+		if ($id <= 0) {
+			throw new Exception("Wrong param ID", 404);
+		}
+
+		$tblName = $this->_acl->getName();
+
+		$record = $this->_dataSource->getItem($id);
+		if (!$record) {
+			throw new Exception("Nie odnaleziono rekordu nr {$id}", 404);
+		}
+		if (!$this->_acl->canWriteRecord($record) && !$this->_acl->hasPermSuperWrite()) {
+			throw new Exception("Brak dostępu do rekordu nr {$id}", 403);
+		}
+
+		$theGeomFieldId = $this->_acl->getFieldIdByName($geomFieldName);
+		if (!$this->_acl->isAllowed($theGeomFieldId, 'W', $record)) {
+			throw new Exception("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;
+		}
+
+		$itemPath = new stdClass();
+		$itemPath->{$geomFieldName} = "NULL";
+		$itemPath->ID = $id;
+
+		$affected = $this->_dataSource->updateItem($itemPath);
+
+		$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 {
+			$response->type = 'error';
+			$response->msg = "Wystąpiły błędy!";
+			$response->errors = $this->_dataSource->getDbErrors();
+		}
+		$response->record = $this->_dataSource->getItem($id);
+		return $response;
+	}
+
 }