浏览代码

fixed procesEditor, added wfs client p5WFS_GetFeature

Piotr Labudda 8 年之前
父节点
当前提交
4e4085d745
共有 4 个文件被更改,包括 152 次插入66 次删除
  1. 1 1
      SE/se-lib/Route/UrlAction/ProcesEditor.php
  2. 4 0
      SE/se-lib/tmpl/_layout_gora.php
  3. 86 0
      SE/static/p5WFS/GetFeature.js
  4. 61 65
      SE/static/procesEditor.js

+ 1 - 1
SE/se-lib/Route/UrlAction/ProcesEditor.php

@@ -425,7 +425,7 @@ class Route_UrlAction_ProcesEditor extends RouteBase {// TODO: UrlActionBase @se
 
 		<script src="static/videoPlayer.js?v=19<?php echo "&_time=" . time(); ?>"></script>
 		<script src="static/jquery.fileupload.js"></script>
-		<script src="static/procesEditor.js?v=20<?php if (V::get('DBG', '', $_GET)) echo "&_time=" . time(); ?>"></script>
+		<script src="static/procesEditor.js?v=21<?php if (V::get('DBG', '', $_GET)) echo "&_time=" . time(); ?>"></script>
 <?php
 	}
 

+ 4 - 0
SE/se-lib/tmpl/_layout_gora.php

@@ -51,6 +51,10 @@
 	UI::inlineJS(APP_PATH_WWW . '/static/p5UI/clickable.js'); // , [ 'DBG' => DBG::isActive() ]);
 	UI::inlineJS(APP_PATH_WWW . '/static/p5UI/ajax.js');
 	UI::inlineJS(APP_PATH_WWW . '/static/p5UI/legacy.js');
+	Lib::loadClass('Request');
+	UI::inlineJS(APP_PATH_WWW . '/static/p5WFS/GetFeature.js', [
+		'WFS_URL' => Request::getPathUri() . '/wfs-data.php/default_db/',
+	]);
 	UI::hButtonAjaxJsFunction();
 ?>
 <?php if (User::hasAccess('dbg')) : ?>

+ 86 - 0
SE/static/p5WFS/GetFeature.js

@@ -0,0 +1,86 @@
+// @require WFS_URL
+if (!WFS_URL) throw "Missing WFS_URL"
+
+function p5WFS_ParseFeatureMember(featureMember) { // @returns object from xml element <gml:featureMemeber>
+	if (!featureMember) return null
+	if (!featureMember.children.length) return null
+	var featureNode = featureMember.children[0]
+	if (!featureNode) return null
+	if (!featureNode.children.length) return null
+	var item = {}
+	var i = 0
+	for (i = 0; i < featureNode.children.length; i++) {
+		var field = featureNode.children[i]
+		item[ field.localName ] = field.textContent
+	}
+	return item
+}
+
+/**
+ * @usage
+
+	p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
+		'featureID': 'CRM_PROCES.' + id,
+	}).then(function (features) {
+		// ...
+	}).catch(function (e) {
+		// ...
+	})
+
+ * @usage
+
+	p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
+		'featureID': 'CRM_PROCES.' + id,
+	}).then(function (features) {
+		if (!features) throw "Nie odnaleziono rekordu id = " + id
+		if (!features.length) throw "Nie odnaleziono rekordu id = " + id
+		if (1 !== features.length) throw "Bład: API zwróciło za dużo rekordów"
+		return features[0]
+		console.log('fetched features', features)
+	}).then(function (feature) {
+		// ...
+	}).catch(function (e) {
+		// ...
+	})
+
+ */
+function p5WFS_GetFeature(typeName, query) { // @returns Promise
+	var link = WFS_URL + '?SERVICE=WFS&VERSION=1.0.0' +
+		'&REQUEST=GetFeature' +
+		'&TYPENAME=' + typeName +
+		'&SRSNAME=EPSG:3003'
+	//	'&Filter=<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>ID</ogc:PropertyName><ogc:Literal>' + id + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>'
+	var query = query || {}
+	if ('featureID' in query) link += '&featureID=' + query['featureID']
+	if ('Filter' in query && 'string' === typeof query['Filter']) {
+		var ogcFilter = ('<ogc:Filter>' !== query['Filter'].substr(0, '<ogc:Filter>'.length))
+			? '<ogc:Filter>' + query['Filter'] + '</ogc:Filter>'
+			: query['Filter']
+		;
+		link += '&Filter=' + ogcFilter
+	}
+	if ('sortBy' in query) link += '&sortBy=' + query['sortBy']
+	var method = 'GET' // TODO: if long 'ogc:Filter' then POST
+
+	return window.fetch(link, {
+		method: method,
+		credentials: 'same-origin',
+	}).then(function (response) {
+		return response.text()
+	}).then(function (responseText) {
+		return (new DOMParser()).parseFromString(responseText, "text/xml");
+	}).then(function (xmlResponse) {
+		if (!xmlResponse.children.length) throw "Missing response xml root element"
+		return xmlResponse.children[0]
+	}).then(function (xmlRoot) {
+		if ('FeatureCollection' !== xmlRoot.localName) throw "Missing FeatureCollection as root element in wfs response"
+		var features = []
+		for (var i = 0; i < xmlRoot.children.length; i++) {
+			features.push(p5WFS_ParseFeatureMember(xmlRoot.children[i]))
+		}
+		return features.filter(function (item) { return !!item; })
+	})
+}
+
+global.p5WFS_ParseFeatureMember = p5WFS_ParseFeatureMember
+global.p5WFS_GetFeature = p5WFS_GetFeature

+ 61 - 65
SE/static/procesEditor.js

@@ -2517,53 +2517,50 @@ $(document).ready(function() {
     }
 
     function genTree(parent_id) {
-
-        var link = BASE_URL + '/wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=p5_default_db:CRM_PROCES&SRSNAME=EPSG:3003&Filter=<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>PARENT_ID</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>&sortBy=SORT_PRIO+A,ID';
-        $.ajax({
-            url: link,
-            success: function(data) {
-                var i = 0;
-                $.each($(data).find("featureMember"), function() {
-                    var temp = {};
-                    if ($(this).find("ID").text() == "0")
-                        return;
-                    var parentIndex = getIndexById(parent_id);
-                    temp["id"] = $(this).find("ID").text();
-                    temp["desc"] = $(this).find("DESC").text();
-                    temp["opis"] = $(this).find("OPIS").text();
-                    temp["parent_id"] = $(this).find("PARENT_ID").text();
-                    temp["depth"] = state[parentIndex]["depth"] + 1;
-                    temp["step"] = 0;
-                    temp["prof"] = new Array();
-                    temp["res"] = new Array();
-                    temp["goto"] = {};
-                    temp["goto"]["id"] = $(this).find("IF_TRUE_GOTO").text();
-                    temp["goto"]["flag"] = $(this).find("IF_TRUE_GOTO_FLAG").text();
-                    temp["stepDesc"] = "";
-                    i++;
-                    state.splice(parentIndex + i, 0, temp);
-                    // var linkImg = BASE_URL + 'wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=p5_default_db:CRM_IMAGE&SRSNAME=EPSG:3003&Filter=<ogc:Filter><ogc:And><ogc:PropertyIsEqualTo><ogc:PropertyName>REMOTE_ID</ogc:PropertyName><ogc:Literal>' + temp["id"] + '</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>REMOTE_TABLE</ogc:PropertyName><ogc:Literal>CRM_PROCES</ogc:Literal></ogc:PropertyIsEqualTo></ogc:And></ogc:Filter>';
-                    // $.get(linkImg, function(imgData) {
-                    //     var temp = Array();
-                    //     $.each($(imgData).find("featureMember"), function() {
-                    //         temp.push({
-                    //             "src": base64_decode($(this).find("IMAGE").text()),
-                    //             "title": $(this).find("NAME").text(),
-                    //             "id": $(this).find("ID").text(),
-                    //         });
-                    //     })
-                    //     state[getIndexById(temp["id"])]["img"] = temp;
-                    // });
-                    genTree($(this).find("ID").text());
-                });
-            }
-        });
+		var link = BASE_URL + '/wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=p5_default_db:CRM_PROCES&SRSNAME=EPSG:3003&Filter=<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>PARENT_ID</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>&sortBy=SORT_PRIO+A,ID';
+		// '<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>PARENT_ID</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>&sortBy=SORT_PRIO+A,ID';
+		p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
+			'Filter': '<ogc:PropertyIsEqualTo><ogc:PropertyName>' + "PARENT_ID" + '</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo>',
+			'sortBy': 'SORT_PRIO+A,ID',
+		}).then(function (features) {
+			var feature = null
+			for (var i = 0, total = features.length; i < total; i++) {
+				feature = features[i]
+				var temp = {}
+				var parentIndex = getIndexById(parent_id);
+				temp["id"] = feature['ID']
+				temp["desc"] = feature['DESC'] || ''
+				temp["opis"] = feature['OPIS'] || ''
+				temp["parent_id"] = feature['PARENT_ID'] || 0
+				temp["depth"] = state[parentIndex]["depth"] + 1;
+				temp["step"] = 0;
+				temp["prof"] = new Array();
+				temp["res"] = new Array();
+				temp["goto"] = {};
+				temp["goto"]["id"] = feature['IF_TRUE_GOTO'] || 0
+				temp["goto"]["flag"] = feature['IF_TRUE_GOTO_FLAG'] || ''
+				temp["stepDesc"] = "";
+				state.splice(parentIndex + (i + 1), 0, temp)
+				// var linkImg = BASE_URL + 'wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=p5_default_db:CRM_IMAGE&SRSNAME=EPSG:3003&Filter=<ogc:Filter><ogc:And><ogc:PropertyIsEqualTo><ogc:PropertyName>REMOTE_ID</ogc:PropertyName><ogc:Literal>' + temp["id"] + '</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>REMOTE_TABLE</ogc:PropertyName><ogc:Literal>CRM_PROCES</ogc:Literal></ogc:PropertyIsEqualTo></ogc:And></ogc:Filter>';
+				// $.get(linkImg, function(imgData) {
+				//     var temp = Array();
+				//     $.each($(imgData).find("featureMember"), function() {
+				//         temp.push({
+				//             "src": base64_decode($(this).find("IMAGE").text()),
+				//             "title": $(this).find("NAME").text(),
+				//             "id": $(this).find("ID").text(),
+				//         });
+				//     })
+				//     state[getIndexById(temp["id"])]["img"] = temp;
+				// });
+				genTree(feature['ID']);
+			}
+		}).catch(function (e) {
+			console.log('Error', e)
+		})
     }
 
-
-
     function base64_decode(input) {
-
         var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
         var output = "";
         var chr1, chr2, chr3;
@@ -2591,7 +2588,6 @@ $(document).ready(function() {
             if (enc4 != 64) {
                 output = output + String.fromCharCode(chr3);
             }
-
         }
 
         var string = "";
@@ -2599,9 +2595,7 @@ $(document).ready(function() {
         var c = c1 = c2 = 0;
 
         while (i < output.length) {
-
             c = output.charCodeAt(i);
-
             if (c < 128) {
                 string += String.fromCharCode(c);
                 i++;
@@ -2615,27 +2609,19 @@ $(document).ready(function() {
                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                 i += 3;
             }
-
         }
-
         return string;
-
-
     }
 
     //Pierwszy render, tworzy potrzebna strukture
     function showPreview() {
-        if (mainProces_id == -1)
-            newSteps = -1;
-        else
-            newSteps = 0;
+        newSteps = (mainProces_id == -1) ? -1 : 0
         changes = false;
         $("#clearBtn").hide();
         lastSearch = "";
         filtr = 0;
         selectedArea = false;
         state = [];
-
         newRes = 0;
         deletedId = [];
         deletedRes = [];
@@ -2673,22 +2659,29 @@ $(document).ready(function() {
         } else {
             $("#main").html("<center>Synchronizowanie danych z serwerem.</center>");
             var id = mainProces_id;
-            var link = BASE_URL + '/wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=p5_default_db:CRM_PROCES&SRSNAME=EPSG:3003&Filter=<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>ID</ogc:PropertyName><ogc:Literal>' + id + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>';
-            $.get(link, function(data) {
-                var temp = {};
+			p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
+				'featureID': 'CRM_PROCES.' + id,
+			}).then(function (features) {
+				if (!features) throw "Nie odnaleziono rekordu id = " + id
+				if (!features.length) throw "Nie odnaleziono rekordu id = " + id
+				if (1 !== features.length) throw "Bład: API zwróciło za dużo rekordów"
+				return features[0]
+				console.log('fetched features', features)
+			}).then(function (feature) {
+				var temp = {};
                 loaded = false;
                 temp["id"] = mainProces_id;
                 temp["depth"] = 0;
-                temp["parent_id"] = $(data).find("featureMember").find("PARENT_ID").text();
+                temp["parent_id"] = feature["PARENT_ID"]
                 temp["step"] = 0;
                 temp["goto"] = {};
-                temp["goto"]["id"] = $(data).find("featureMember").find("IF_TRUE_GOTO").text();
-                temp["goto"]["flag"] = $(data).find("featureMember").find("IF_TRUE_GOTO_FLAG").text();
+                temp["goto"]["id"] = feature["IF_TRUE_GOTO"]
+                temp["goto"]["flag"] = feature["IF_TRUE_GOTO_FLAG"]
                 temp["stepDesc"] = "";
                 temp["prof"] = Array();
                 temp["res"] = Array();
-                temp["desc"] = $(data).find("featureMember").find("DESC").text();
-                temp["opis"] = $(data).find("featureMember").find("OPIS").text()
+                temp["desc"] = feature["DESC"]
+                temp["opis"] = feature["OPIS"]
 
                 state[0] = temp;
                 // var linkImg = BASE_URL + 'wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=p5_default_db:CRM_IMAGE&SRSNAME=EPSG:3003&Filter=<ogc:Filter><ogc:And><ogc:PropertyIsEqualTo><ogc:PropertyName>REMOTE_ID</ogc:PropertyName><ogc:Literal>' + mainProces_id + '</ogc:Literal></ogc:PropertyIsEqualTo><ogc:PropertyIsEqualTo><ogc:PropertyName>REMOTE_TABLE</ogc:PropertyName><ogc:Literal>CRM_PROCES</ogc:Literal></ogc:PropertyIsEqualTo></ogc:And></ogc:Filter>';
@@ -2704,7 +2697,10 @@ $(document).ready(function() {
                 //     state[0]["img"] = temp;
                 // });
                 genTree(mainProces_id);
-            });
+			}).catch(function (e) {
+				console.log('Error', e)
+			})
+
             actSite = "preview";
         }
     }