Przeglądaj źródła

added DescribeFeatureTypeAdvanced with p5 attributes in Wfs Data Server

Piotr Labudda 10 lat temu
rodzic
commit
eb53b556d7

+ 18 - 0
SE/se-lib/Api/WfsDataServer.php

@@ -189,6 +189,24 @@ if($DBG){echo 'item['.$itemKey.'] ('.$geomFld.')isEmpty('.empty($item->{$geomFld
 		return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1]);
 	}
 
+	public function describeFeatureTypeAdvancedAction() {
+		$type = V::get('TYPENAME', '', $_REQUEST);
+		if (empty($type)) {
+			$reqContent = Request::getRequestBody();
+			if (!empty($reqContent)) {
+				return $this->_parseDescribeFeatureTypeRequest($reqContent, $simple = false);
+			} else {
+				return $this->_getDescribeFeatureAllTypes($simple = false);
+			}
+			//throw new HttpException("Wrong param TYPENAME", 400);
+		}
+		$typeEx = explode(':', $type);
+		if (count($typeEx) != 2) {
+			throw new HttpException("Wrong param TYPENAME", 400);
+		}
+		return $this->_getDescribeFeatureType($typeEx[0], $typeEx[1], $simple = false);
+	}
+
 	public function getCapabilitiesAction() {
 		$wfsServerUrl = $this->getBaseUri();
 		$serviceTitle = "Web Feature Service";

+ 55 - 10
SE/se-lib/Api/WfsServerBase.php

@@ -1192,11 +1192,11 @@ if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvert
 		return $reqXml->schemaValidateSource($validateConvertedTransactionXsdString);
 	}
 
-	public function _getDescribeFeatureType($nsPrefix, $type) {
-		return $this->_getDescribeFeatureTypes(array(array($nsPrefix, $type)));
+	public function _getDescribeFeatureType($nsPrefix, $type, $simple = true) {
+		return $this->_getDescribeFeatureTypes(array(array($nsPrefix, $type)), $simple);
 	}
 
-	public function _parseDescribeFeatureTypeRequest($reqContent) {
+	public function _parseDescribeFeatureTypeRequest($reqContent, $simple = true) {
 		$parserXml = xml_parser_create();
 		xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
 		xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
@@ -1231,10 +1231,10 @@ if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvert
 			$typeNames[] = explode(':', $requestXmlTags[$i]['value'], 2);
 		}
 		//echo "typeNames: " . json_encode($typeNames) . "\n";
-		return $this->_getDescribeFeatureTypes($typeNames);
+		return $this->_getDescribeFeatureTypes($typeNames, $simple);
 	}
 
-	public function _getDescribeFeatureAllTypes() {
+	public function _getDescribeFeatureAllTypes($simple = true) {
 		$db = DB::getDB();
 		$idDefaultDB = $db->_zasob_id;
 
@@ -1244,16 +1244,18 @@ if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvert
 			$tblName = $tblAcl->getName();
 			$typeNames[] = array("p5_{$dataSourceName}", $tblName);
 		}
-		return $this->_getDescribeFeatureTypes($typeNames);
+		return $this->_getDescribeFeatureTypes($typeNames, $simple);
 	}
 
 	// @param $typeNames = array( array( $nsPrefix, $type ) )
-	public function _getDescribeFeatureTypes($typeNames) {
+	public function _getDescribeFeatureTypes($typeNames, $simple = true) {
 		if (empty($typeNames)) {
 			throw new HttpException("Feature Type Names not defined", 400);
 		}
 		$baseNsUri = $this->getBaseNamespaceUri();
 
+		$rootWfsNs = 'p5';
+		$rootWfsNsUri = "{$baseNsUri}";
 		$wfsNs = 'p5_default_db';
 		$wfsNsUri = "{$baseNsUri}/default_db";
 		$featureTypeUri = $this->getBaseUri() . "?SERVICE=WFS&VERSION=1.0.0&REQUEST=DescribeFeatureType";
@@ -1269,6 +1271,7 @@ if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvert
 		$dom->appendChild($rootNode);
 		$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:gml', 'http://www.opengis.net/gml');
 		$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $wfsNs, $wfsNsUri);
+		$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$rootWfsNs}", $rootWfsNsUri);
 		$rootNode->setAttribute('elementFormDefault', 'qualified');
 		$rootNode->setAttribute('targetNamespace', $wfsNsUri);
 		{// <xsd:import namespace="http://www.opengis.net/gml" schemaLocation="...../gml/2.1.2/feature.xsd"/>
@@ -1306,6 +1309,7 @@ if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvert
 
 			// <xsd:element maxOccurs="1" minOccurs="0" name="{$fldName}" nillable="true" type="xsd:integer"/>
 			$pKeyField = $acl->getPrimaryKeyField();
+			$p5Attributes = ($simple)? array() : $acl->getAttributesFromZasoby();
 			foreach ($fldList as $fldName) {
 				$elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
 				$seqNode->appendChild($elNode);
@@ -1319,6 +1323,7 @@ if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvert
 				$elNode->setAttribute('minOccurs', $minOccurs);
 				$elNode->setAttribute('maxOccurs', '1');
 				$elNode->setAttribute('nillable', 'true');
+				$fldType = 'xsd:string';
 				if ($acl->isIntegerField($fldName)) {
 					$fldType = 'xsd:integer';
 				}
@@ -1344,17 +1349,57 @@ if($DBG){echo '$validateConvertedTransactionXsdString:';print_r($validateConvert
 						$fldType = 'gml:GeometryPropertyType';
 					}
 				}
-				else {
-					$fldType = 'xsd:string';
+				else if ($acl->isEnumerationField($fldName)) {
+					$fldType = ($simple)? 'xsd:string' : "{$nsPrefix}:{$fldName}Type";
 				}
 				$elNode->setAttribute('type', $fldType);
+
+				if (!$simple) {
+					if (!empty($p5Attributes[$fldName]['id_zasob'])) {
+						$elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:id_zasob", $p5Attributes[$fldName]['id_zasob']);
+					}
+					if (!empty($p5Attributes[$fldName]['label'])) {
+						$elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5Attributes[$fldName]['label']);
+					}
+					if (!empty($p5Attributes[$fldName]['description'])) {
+						$elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:description", $p5Attributes[$fldName]['description']);
+					}
+				}
 			}
 
 			$elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:element');
 			$rootNode->appendChild($elNode);
 			$elNode->setAttribute('name', $type);
 			$elNode->setAttribute('type', $wfsNs . ':' . $typeName);
-			$elNode->setAttribute('substitutionGroup', 'gml:_Feature');// substitutionGroup="gml:_Feature"
+			if ($simple) {
+				$elNode->setAttribute('substitutionGroup', 'gml:_Feature');
+			} else {
+				$elNode->setAttribute('substitutionGroup', 'gml:AbstractFeature');
+			}
+
+			if (!$simple) {
+				foreach ($fldList as $fldName) {
+					if ($acl->isEnumerationField($fldName)) {
+						$enum = $acl->getEnumerations($fldName);
+						$stNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:simpleType');
+						$stNode->setAttribute('name', "{$fldName}Type");//"{$nsPrefix}:{$fldName}Type");
+						$rootNode->appendChild($stNode);
+							$resNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:restriction');
+							$stNode->appendChild($resNode);
+							$resNode->setAttribute('base', 'xsd:string');
+							// TODO: if (!empty($p5Attributes[$fldName]['valuesMap'])) -> show only this values
+							foreach ($enum as $val => $label) {
+								$enumNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xsd:enumeration');
+								$resNode->appendChild($enumNode);
+								$enumNode->setAttribute('value', "{$val}");
+
+								if (!empty($p5Attributes[$fldName]['valuesMap'][$val])) {
+									$enumNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:label", $p5Attributes[$fldName]['valuesMap'][$val]);
+								}
+							}
+					}
+				}
+			}
 		}
 
 		return $dom->saveXML();

+ 73 - 9
SE/se-lib/TableAcl.php

@@ -45,13 +45,13 @@ class TableAcl {
 	}
 	
 	public function setNameByTableId($tableID) {
-	//used for init without knowing table name	
-		$sql="select `DESC` from CRM_LISTA_ZASOBOW where ID=".$tableID." and `TYPE`='TABELA'";
-		$res=DB::query($sql);
-		$res_=DB::fetch($res);
+		//used for init without knowing table name
+		$db = DB::getDB();
+		$sql = "select `DESC` from CRM_LISTA_ZASOBOW where ID={$tableID} and `TYPE`='TABELA'";
+		$res = $db->query($sql);
+		$res_ = $db->fetch($res);
 		//DEBUG_S(-3,'setNameByTableId',$res_,__FILE__,__FUNCTION__,__LINE__);
 		self::setName($res->DESC);
-		
 	}
 
 	public function getName() {
@@ -1357,14 +1357,78 @@ class TableAcl {
 		$type = $this->getFieldType($fldName);
 		if (!$type) return false;
 
-		if (substr($colType['type'], 0, 4) == 'text'
-				|| substr($colType['type'], 0, 8) == 'tinytext'
-				|| substr($colType['type'], 0, 10) == 'mediumtext'
-				|| substr($colType['type'], 0, 8) == 'longtext'
+		if (substr($type['type'], 0, 4) == 'text'
+				|| substr($type['type'], 0, 8) == 'tinytext'
+				|| substr($type['type'], 0, 10) == 'mediumtext'
+				|| substr($type['type'], 0, 8) == 'longtext'
 		) {
 			return true;
 		}
 		return false;
 	}
 
+	public function isEnumerationField($fldName) {
+		$type = $this->getFieldType($fldName);
+		if (!$type) return false;
+
+		if (substr($type['type'], 0, 4) == 'enum') {
+			return true;
+		}
+		return false;
+	}
+
+	public function getEnumerations($fldName) {
+		$enum = array();
+		$type = $this->getFieldType($fldName);
+		if (!$type) return $enum;
+		if (!$this->isEnumerationField($fldName)) return $enum;
+
+		$values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
+		foreach ($values as $val) {
+			$enum[$val] = $val;
+		}
+		return $enum;
+	}
+
+	public function getAttributesFromZasoby() {
+		$attributes = array();
+		$db = DB::getDB();
+		$sql = "
+			select `ID` as `id_zasob`
+				, `DESC` as `field_name`
+				, `DESC_PL` as `label`
+				, `OPIS` as `description`
+			from `CRM_LISTA_ZASOBOW`
+			where `PARENT_ID`={$this->_zasobID}
+				and `TYPE`='KOMORKA'
+		";
+		$res = $db->query($sql);
+		while ($r = $db->fetch($res)) {
+			$fldAttr = array();
+			$fldAttr['id_zasob'] = $r->id_zasob;
+			if (!empty($r->label)) $fldAttr['label'] = $r->label;
+			if (!empty($r->description)) $fldAttr['description'] = $r->description;
+			$attributes[$r->field_name] = $fldAttr;
+		}
+		// fetch field values
+		$sql = "
+			select z.`ID` as `id_zasob`
+				, z.`DESC` as `field_name`
+				, v.`DESC` as `value`
+				, v.`DESC_PL` as `label`
+			--	, v.`OPIS` as `description`
+			from `CRM_LISTA_ZASOBOW` z
+				join `CRM_LISTA_ZASOBOW` v on(v.`PARENT_ID` = z.`ID`)
+			where z.`PARENT_ID`={$this->_zasobID}
+				and z.`TYPE`='KOMORKA'
+		";
+		$res = $db->query($sql);
+		while ($r = $db->fetch($res)) {
+			$valuesMap = V::get('valuesMap', array(), $attributes[$r->field_name]);
+			$valuesMap[$r->value] = $r->label;
+			$attributes[$r->field_name]['valuesMap'] = $valuesMap;
+		}
+		return $attributes;
+	}
+
 }