WfsBiAudit.php 8.5 KB

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