Piotr Labudda 8 лет назад
Родитель
Сommit
fe336ac931
1 измененных файлов с 171 добавлено и 89 удалено
  1. 171 89
      SE/se-lib/Route/WfsBiAudit.php

+ 171 - 89
SE/se-lib/Route/WfsBiAudit.php

@@ -6,6 +6,8 @@ class Route_WfsBiAudit extends RouteBase {
 
 	const maxResolveDepth = 3;
 	private $dom, $path = [], $relations = [], $tablesUsed = [];
+	public $_wfsRootNode = null;
+	public $_xmlnsTablePrefix = [];
 
 	public function handleAuth() {
 		if (!empty($_GET['sid'])) {
@@ -103,6 +105,20 @@ public function objectStructView($item) {
 		$node->appendChild($attr);
 	}
 
+	public function setRootNode($node) {
+		$this->_wfsRootNode = $node;
+	}
+	public function addRootXmlnsTablePrefix($typeName) {
+		if (!$this->_wfsRootNode) throw new Exception("Root node not set");
+		list($nsPrefix, $nsBaseName) = explode(':', $typeName);
+		list($nsSourceName, $nsBaseTable) = explode('__x3A__', $nsPrefix);
+		if (!in_array($nsBaseTable, $this->_xmlnsTablePrefix)) {
+			$this->_xmlnsTablePrefix[] = $nsBaseTable;
+
+			$this->addAttribute($this->_wfsRootNode, "xmlns:default_db__x3A__{$nsBaseTable}", "https://biuro.biall-net.pl/wfs/default_db/{$nsBaseTable}");
+		}
+	}
+
 	private function relationName($ID) {
 		if (!isset($this->relations[$ID])) {
 			$query = "select `RELATION` from `BI_audit_ALL_ref_RELATIONS` where `ID` & {$ID} order by `ID`";
@@ -132,7 +148,7 @@ public function objectStructView($item) {
 							 self::throwServiceException("Błąd danych #102 findRelations_base");
 						}*/
 
-				if (!in_array($BaseStruct['_rootTableName'][$ind], $this->tablesUsed)) $this->tablesUsed[] = $BaseStruct['_rootTableName'][$ind];
+					$this->addRootXmlnsTablePrefix("default_db__x3A__{$BaseStruct['_rootTableName'][$ind]}:{$BaseStruct['_rootTableName'][$ind]}"); // if (!in_array($BaseStruct['_rootTableName'][$ind], $this->tablesUsed)) $this->tablesUsed[] = $BaseStruct['_rootTableName'][$ind];
 				}
 
 				if ($resolveDepth) {
@@ -157,62 +173,121 @@ public function objectStructView($item) {
 
 	}
 
-	private function findRelations($node, $ID, $resolveDepth, $relation = null,$BaseTableFrom,$BaseTableTo) {
+	private function findRelations($node, $ID, $resolveDepth, $relation = null, $BaseTableFrom = null, $BaseTableTo = null) {
 		if (in_array($ID, $this->path)) return;
 		$this->path[] = $ID;
-		if(isset($BaseTableFrom)) {
+		if (!empty($BaseTableFrom) && !empty($BaseTableTo)) {
 			$query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID} and `REMOTE_TABLE`='".$BaseTableTo."'"; //todo nie wiem czy nie na odwrot $BaseTableTo
 		} else {
 			$query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID}";
 		}
 
-		if (!($row = DB::getPDO()->fetchFirst($query)))
-		{
-				//$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
-				// self::throwServiceException("Błąd danych findRelations #130");
-				 $this->BaseStruct=$this->objectStructureAction("default_db/".$BaseTableTo."/".$BaseTableTo );
-				 $query = "select `".$this->BaseStruct['primaryKey']."` from `".$BaseTableTo."` where ".$this->BaseStruct['primaryKey']." = {$ID}";
-				if (!($ID = DB::getPDO()->fetchValue($query))) {
-					self::throwServiceException("Błąd danych z BaseStruct/findRelations #162 ");
-				}
-
-				if (!in_array($BaseTableTo, $this->tablesUsed)) $this->tablesUsed[] = $BaseTableTo;
-				if ($resolveDepth) {
-						$feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
-						$this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
-						$this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
-						if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
-						//$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
-						//$where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
-						//$query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
-						//$result = DB::getPDO()->fetchAll($query);
-						//foreach ($result as $row)
-						// $this->findRelations($feature, $row['ID2'], $resolveDepth - 1, $row['RELATION_ID'],);
-						findRelations_base($node, $ID, $resolveDepth, $relation = null, $this->BaseStruct,$BaseTableTo) ;
-
-					} else {
-						$xlink = $this->addChild($node, "default_db__x3A__{$BaseTableTo}:{$BaseTableTo}");
-						$this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/".$BaseTableTo."?BI_audit_ALL_ref_RELATIONS={$BaseTableTo}#{$BaseTableTo}.{$row[$this->BaseStruct['primaryKey']]}");
-					}
-					array_pop($this->path);
-
+		if (!($row = DB::getPDO()->fetchFirst($query))) {
+			if (empty($BaseTableFrom) && empty($BaseTableTo)) {
+				self::throwServiceException("Błąd danych z BaseStruct/findRelations #161 ");
+			}
+			//$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
+			// self::throwServiceException("Błąd danych findRelations #130");
+			$this->BaseStruct=$this->objectStructureAction("default_db/".$BaseTableTo."/".$BaseTableTo );
+			$query = "select `".$this->BaseStruct['primaryKey']."` from `".$BaseTableTo."` where ".$this->BaseStruct['primaryKey']." = {$ID}";
+			if (!($ID = DB::getPDO()->fetchValue($query))) {
+				self::throwServiceException("Błąd danych z BaseStruct/findRelations #162 ");
+			}
+
+			$this->addRootXmlnsTablePrefix("default_db__x3A__{$BaseTableTo}:{$BaseTableTo}"); // if (!in_array($BaseTableTo, $this->tablesUsed)) $this->tablesUsed[] = $BaseTableTo;
+			if ($resolveDepth) {
+				$feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
+				$this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
+				$this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
+				if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
+				//$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
+				//$where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
+				//$query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
+				//$result = DB::getPDO()->fetchAll($query);
+				//foreach ($result as $row)
+				// $this->findRelations($feature, $row['ID2'], $resolveDepth - 1, $row['RELATION_ID'],);
+				findRelations_base($node, $ID, $resolveDepth, $relation = null, $this->BaseStruct,$BaseTableTo) ;
+
+			} else {
+				$xlink = $this->addChild($node, "default_db__x3A__{$BaseTableTo}:{$BaseTableTo}");
+				$this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/".$BaseTableTo."?BI_audit_ALL_ref_RELATIONS={$BaseTableTo}#{$BaseTableTo}.{$row[$this->BaseStruct['primaryKey']]}");
+			}
+			array_pop($this->path);
 		} else {
-					if (!in_array($row['REMOTE_TABLE'], $this->tablesUsed)) $this->tablesUsed[] = $row['REMOTE_TABLE'];
-					if ($resolveDepth) {
-						$feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
-						$this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
-						$this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
-						if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
-						//$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
-						$where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
-						$query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
-						$result = DB::getPDO()->fetchAll($query);
-						foreach ($result as $row) $this->findRelations($feature, $row['ID2'], $resolveDepth - 1, $row['RELATION_ID']);
-					} else {
-						$xlink = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
-						$this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/{$row['REMOTE_TABLE']}?BI_audit_ALL_ref_RELATIONS={$relation}#{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
+			// $row = [ 'REMOTE_TABLE' => BI_audit_KRS_person, 'REMOTE_ID' => 366074 ]
+			$this->addRootXmlnsTablePrefix("default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}"); // if (!in_array($row['REMOTE_TABLE'], $this->tablesUsed)) $this->tablesUsed[] = $row['REMOTE_TABLE'];
+			if ($resolveDepth) {
+				$featureNode = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
+				$this->addAttribute($featureNode, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
+				$this->addChild($featureNode, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
+				if ($relation) $this->addChild($featureNode, "relation_from", $this->relationName($relation));
+				//$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
+				$where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
+				$query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
+				$result = DB::getPDO()->fetchAll($query);
+				foreach ($result as $childRow) $this->findRelations($featureNode, $childRow['ID2'], $resolveDepth - 1, $childRow['RELATION_ID']);
+
+				$this->addAclInfo(
+					$dataNode = $featureNode,
+					$typeName = "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}",
+					$primaryKey = $row['REMOTE_ID'],
+					$skipFields = [ 'ID' ]
+				);
+			} else {
+				$xlink = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
+				$this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/{$row['REMOTE_TABLE']}?BI_audit_ALL_ref_RELATIONS={$relation}#{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
+			}
+			array_pop($this->path);
+		}
+	}
+
+	public function addAclInfo($dataNode, $typeName, $primaryKey, $skipFields = []) {
+		try {
+			$acl = ACL::getAclByTypeName($typeName);
+			$item = $acl->buildQuery([
+				'cols' => array_values($acl->getFieldListByIdZasob()),
+			])->getItem($primaryKey);
+			if (!$item) throw new \Exception("Item not found {$primaryKey}");
+			// $item = Array( [ID] => 56977, [krs] => 0000080725 , ... )
+			// $item[default_db__x3A__BI_audit_MSIG_address:BI_audit_MSIG_address] => Array( [0] => [ 'xlink' => ${url} ] )
+			// $item[default_db__x3A__BI_audit_MSIG_address:BI_audit_MSIG_address] => Array( [11] => [ 'p5:links' => ${p5Links} ] )
+			// $p5Links = Array( 'p5:next' => ${p5NextLink} )
+			// $p5NextLink = Array(
+			//                     [@typeName] => default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person
+			//                     [@backRefNS] => default_db/BI_audit_KRS/BI_audit_KRS
+			//                     [@backRefPK] => 56977
+			//                     [@startIndex] => 10
+			//                     [@maxFeatures] => 10
+			//                     [value] => https://biuro.biall-net.pl/dev-pl/se-master/index.php?SERVICE=WFS&VERSION=1.0.0&TYPENAME=default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person&REQUEST=GetFeature&backRefNS=default_db/BI_audit_KRS/BI_audit_KRS&backRefPK=56977&backRefField=default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person&maxFeatures=10&startIndex=10
+			// $dataNode = $this->addChild($featureNode, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
+			foreach ($item as $fieldName => $value) {
+				if (in_array($fieldName, $skipFields)) continue;
+				// $this->addAttribute($dataNode, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/{$row['REMOTE_TABLE']}?BI_audit_ALL_ref_RELATIONS={$relation}#{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
+				if (is_scalar($value)) {
+					$this->addChild($dataNode, "default_db__x3A__{$row['REMOTE_TABLE']}:{$fieldName}", $value);
+				} else if (is_array($value) && false !== strpos($fieldName, ':')) {
+					$this->addRootXmlnsTablePrefix($fieldName);
+
+					foreach ($value as $refKey => $refValue) {
+						if (is_array($refValue) && 1 === count($refValue) && !empty($refValue['xlink'])) {
+							list($refPrefix, $refBaseName) = explode(':', $fieldName);
+							list($refSourceName, $refBaseTable) = explode('__x3A__', $refPrefix);
+							$xlink = $this->addChild($dataNode, $fieldName);
+							$this->addAttribute($xlink, 'xlink:href', $refValue['xlink']); // "https://biuro.biall-net.pl/wfs/default_db/{$refBaseTable}#{$refBaseName}.{$row['REMOTE_ID']}");
+						} else if (is_array($refValue) && empty($refValue)) {
+							// skip empty array
+						} else {
+							$this->addChild($dataNode, $fieldName, "<!-- TODO add info ref ({$refKey}): " . str_replace('&', '&amp;', var_export($refValue, true)) . " -->");
+						}
 					}
-					array_pop($this->path);
+				} else if (NULL === $value) {
+					$this->addChild($dataNode, $fieldName); // xs:nil ?
+				} else {
+					$this->addChild($dataNode, $fieldName, "<!-- TODO: " . var_export($value, true) . " -->");
+				}
+			}
+		} catch (Exception $e) {
+			DBG::log($e);
 		}
 	}
 
@@ -238,54 +313,61 @@ public function objectStructView($item) {
 			];
 
 			$wfs = $this->addChild($this->dom, 'wfs:FeatureCollection');
+			$this->setRootNode($wfs);
 			foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
 			$gml = $this->addChild($wfs, 'gml:featureMember');
 
 			$query = "select `ID` from `BI_audit_ALL` where `REMOTE_TABLE` = " . DB::getPDO()->quote($table) . " and `REMOTE_ID` = {$primaryKey}";
 			if (!($ID = DB::getPDO()->fetchValue($query))) {
-
 				error_log('#233 default base passing');
-				$this->BaseStruct=$this->objectStructureAction("default_db/".$table."/".$table );
-
-
-				$query = "select `".$this->BaseStruct['primaryKey']."` from `".$table."` where ".$this->BaseStruct['primaryKey']." = {$primaryKey}";
-				if (!($ID = DB::getPDO()->fetchValue($query))) {
-					self::throwServiceException("Błąd danych z BaseStruct ");
-				}
-					$this->findRelations_base($gml, $ID, $resolveDepth,$table,$this->BaseStruct,$table);
-
-						foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
-						$attrs = [
-							'numberMatched' => 'unknown',
-							'numberReturned' => '1',
-						];
-						foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
-
-						$xml = $this->dom->saveXML();
-						self::output($xml);
-
-				} else {
-					error_log('#254 default  passing');
 
-					$this->findRelations($gml, $ID, $resolveDepth);
-
-					foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
-					$attrs = [
-						'numberMatched' => 'unknown',
-						'numberReturned' => '1',
-					];
-					foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
-
-					$xml = $this->dom->saveXML();
-					self::output($xml);
-
-				}
-
-			} catch (Exception $e) {
-					//$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
-
-					self::throwServiceException($e->getMessage());
-		    }
+				// $this->BaseStruct=$this->objectStructureAction("default_db/".$table."/".$table );
+        //
+				// $query = "select `".$this->BaseStruct['primaryKey']."` from `".$table."` where ".$this->BaseStruct['primaryKey']." = {$primaryKey}";
+				// if (!($ID = DB::getPDO()->fetchValue($query))) {
+				// 	self::throwServiceException("Błąd danych z BaseStruct ");
+				// }
+				// $this->findRelations_base($gml, $ID, $resolveDepth, $table, $this->BaseStruct, $table);
+
+				$featureNode = $this->addChild($gml, $TYPENAME);
+				$this->addRootXmlnsTablePrefix($TYPENAME);
+				list($nsPrefix, $nsBaseName) = explode(':', $TYPENAME);
+				$this->addAttribute($featureNode, 'fid', "{$nsBaseName}.{$primaryKey}");
+				$this->addAclInfo(
+					$dataNode = $featureNode,
+					$typeName = $TYPENAME,
+					$primaryKey = $primaryKey,
+					$skipFields = []
+				);
+
+				// foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
+				$attrs = [
+					'numberMatched' => 'unknown',
+					'numberReturned' => '1',
+				];
+				foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
+
+				$xml = $this->dom->saveXML();
+				self::output($xml);
+			} else {
+				error_log('#254 default  passing');
+
+				$this->findRelations($gml, $ID, $resolveDepth);
+
+				// foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
+				$attrs = [
+					'numberMatched' => 'unknown',
+					'numberReturned' => '1',
+				];
+				foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
+
+				$xml = $this->dom->saveXML();
+				self::output($xml);
+			}
+		} catch (Exception $e) {
+			//$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
+			self::throwServiceException($e->getMessage());
+		}
 	}
 
 }