WfsBiAudit.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_WfsBiAudit extends RouteBase {
  4. const maxResolveDepth = 3;
  5. private $dom, $path = [], $relations = [], $tablesUsed = [];
  6. private static function output($output) {
  7. header("Content-Type: application/xml");
  8. header("Content-Transfer-Encoding: binary");
  9. header("Content-Length: " . strlen($output));
  10. echo $output;
  11. }
  12. private static function throwServiceException($message) {
  13. $xml = <<<EOT
  14. <?xml version="1.0" encoding="UTF-8"?>
  15. <ServiceExceptionReport xmlns="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd" version="1.2.0">
  16. <ServiceException>{$message}</ServiceException>
  17. </ServiceExceptionReport>
  18. EOT;
  19. self::output($xml);
  20. die();
  21. }
  22. public function objectStructureAction($namespace) { // objectStructAction
  23. Lib::loadClass('SchemaFactory');
  24. try {
  25. error_log('objectStructureAction for '.$namespace);
  26. if (empty($namespace)) throw new Exception("Missing param namespace");
  27. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  28. $return=$this->objectStructView($item);
  29. return $return;
  30. } catch (Exception $e) {
  31. //error_log('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  32. DBG::log($e);
  33. }
  34. }
  35. //input param $item['namespace']='default_db/BI_audit_CEIDG_powiazania/BI_audit_CEIDG_powiazania'
  36. public function objectStructView($item) {
  37. $namespace = $item['namespace'];
  38. $thisGetLink = [ $this, 'getLink' ];
  39. $getBackRefList=ACL::getBackRefList($item['namespace']);
  40. foreach($getBackRefList as $ind=>$backref) {
  41. $exploded_ns = explode('/', $backref['namespace']);
  42. $return['backrefs'][]=$exploded_ns[1].'__x3A__'.$exploded_ns[2].':'.$exploded_ns[3];
  43. }
  44. foreach($item['field'] as $ind=>$refs) {
  45. $return['refs'][]=$refs['fieldNamespace'];
  46. }
  47. $return['table']=$item['name'];
  48. $return['primaryKey']=$item['primaryKey'];
  49. //print_r($this->BaseStruct);
  50. return $return;
  51. }
  52. private function addChild($node, $name, $value = null) {
  53. $child = $this->dom->createElement($name, $value);
  54. $node->appendChild($child);
  55. return $child;
  56. }
  57. private function addAttribute($node, $name, $value) {
  58. $attr = $this->dom->createAttribute($name);
  59. $attr->value = $value;
  60. $node->appendChild($attr);
  61. }
  62. private function relationName($ID) {
  63. if (!isset($this->relations[$ID])) {
  64. $query = "select `RELATION` from `BI_audit_ALL_ref_RELATIONS` where `ID` & {$ID} order by `ID`";
  65. $result = DB::getPDO()->fetchAll($query);
  66. $rels = array_map('reset', $result);
  67. $this->relations[$ID] = implode(", ", $rels);
  68. }
  69. return $this->relations[$ID];
  70. }
  71. private function findRelations_base($node, $ID, $resolveDepth, $relation = null) {
  72. if (in_array($ID, $this->path)) return;
  73. $this->path[] = $ID;
  74. $query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID}";
  75. if (!($row = DB::getPDO()->fetchFirst($query)))
  76. {
  77. $this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  78. self::throwServiceException("Błąd danych #102 findRelations_base");
  79. }
  80. if (!in_array($row['REMOTE_TABLE'], $this->tablesUsed)) $this->tablesUsed[] = $row['REMOTE_TABLE'];
  81. if ($resolveDepth) {
  82. $feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  83. $this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  84. $this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
  85. if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
  86. //$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  87. $where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
  88. $query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
  89. $result = DB::getPDO()->fetchAll($query);
  90. foreach ($result as $row) $this->findRelations($feature, $row['ID2'], $resolveDepth - 1, $row['RELATION_ID']);
  91. } else {
  92. $xlink = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  93. $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']}");
  94. }
  95. array_pop($this->path);
  96. }
  97. private function findRelations($node, $ID, $resolveDepth, $relation = null) {
  98. if (in_array($ID, $this->path)) return;
  99. $this->path[] = $ID;
  100. $query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID}";
  101. if (!($row = DB::getPDO()->fetchFirst($query)))
  102. {
  103. $this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  104. self::throwServiceException("Błąd danych findRelations #130");
  105. }
  106. if (!in_array($row['REMOTE_TABLE'], $this->tablesUsed)) $this->tablesUsed[] = $row['REMOTE_TABLE'];
  107. if ($resolveDepth) {
  108. $feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  109. $this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  110. $this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
  111. if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
  112. //$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  113. $where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
  114. $query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
  115. $result = DB::getPDO()->fetchAll($query);
  116. foreach ($result as $row) $this->findRelations($feature, $row['ID2'], $resolveDepth - 1, $row['RELATION_ID']);
  117. } else {
  118. $xlink = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  119. $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']}");
  120. }
  121. array_pop($this->path);
  122. }
  123. public function defaultAction() {
  124. $TYPENAME = V::get('TYPENAME', '', $_GET);
  125. $primaryKey = V::get('primaryKey', 0, $_GET, 'int');
  126. $resolveDepth = V::get('resolveDepth', 1, $_GET, 'int');
  127. $TYPENAME_exploded = explode(":", $TYPENAME);
  128. if (!(count($TYPENAME_exploded) == 2 && $primaryKey && $resolveDepth >= 1 && $resolveDepth <= self::maxResolveDepth)) self::throwServiceException("WFS request error");
  129. $table = $TYPENAME_exploded[1];
  130. try {
  131. $this->dom = new DOMDocument('1.0', 'UTF-8');
  132. $this->dom->preserveWhiteSpace = false;
  133. $this->dom->formatOutput = true;
  134. $attrs = [
  135. 'xmlns:wfs' => 'http://www.opengis.net/wfs',
  136. 'xmlns' => 'http://www.opengis.net/wfs',
  137. 'xmlns:gml' => 'http://www.opengis.net/gml',
  138. 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
  139. 'xmlns:xlink' => 'http://www.w3.org/1999/xlink',
  140. ];
  141. $wfs = $this->addChild($this->dom, 'wfs:FeatureCollection');
  142. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  143. $gml = $this->addChild($wfs, 'gml:featureMember');
  144. $query = "select `ID` from `BI_audit_ALL` where `REMOTE_TABLE` = " . DB::getPDO()->quote($table) . " and `REMOTE_ID` = {$primaryKey}";
  145. if (!($ID = DB::getPDO()->fetchValue($query))) {
  146. $this->BaseStruct=$this->objectStructureAction("default_db/".$table."/".$table );
  147. $query = "select `ID` from ".DB::getPDO()->quote($table)." where ".$this->BaseStruct['primaryKey']." = {$primaryKey}";
  148. if (!($ID = DB::getPDO()->fetchValue($query))) {
  149. self::throwServiceException("Błąd danych z BaseStruct ");
  150. }
  151. } else {
  152. $this->findRelations($gml, $ID, $resolveDepth);
  153. foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
  154. $attrs = [
  155. 'numberMatched' => 'unknown',
  156. 'numberReturned' => '1',
  157. ];
  158. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  159. $xml = $this->dom->saveXML();
  160. self::output($xml);
  161. }
  162. } catch (Exception $e) {
  163. //$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  164. self::throwServiceException($e->getMessage());
  165. }
  166. }
  167. }