WfsBiAudit.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. try {
  24. if (empty($namespace)) throw new Exception("Missing param namespace");
  25. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  26. $this->objectStructView($item);
  27. } catch (Exception $e) {
  28. //error_log('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  29. DBG::log($e);
  30. }
  31. }
  32. //input param $item['namespace']='default_db/BI_audit_CEIDG_powiazania/BI_audit_CEIDG_powiazania'
  33. public function objectStructView($item) {
  34. $namespace = $item['namespace'];
  35. $thisGetLink = [ $this, 'getLink' ];
  36. $getBackRefList=ACL::getBackRefList($item['namespace']);
  37. echo "item";
  38. print_r($item);
  39. echo "thisGetLink";
  40. print_r($thisGetLink);
  41. echo "getBackRefList";
  42. print_r($getBackRefList);
  43. //$refTable = ACL::getRefTable($field['objectNamespace'], $field['fieldNamespace']);
  44. }
  45. private function addChild($node, $name, $value = null) {
  46. $child = $this->dom->createElement($name, $value);
  47. $node->appendChild($child);
  48. return $child;
  49. }
  50. private function addAttribute($node, $name, $value) {
  51. $attr = $this->dom->createAttribute($name);
  52. $attr->value = $value;
  53. $node->appendChild($attr);
  54. }
  55. private function relationName($ID) {
  56. if (!isset($this->relations[$ID])) {
  57. $query = "select `RELATION` from `BI_audit_ALL_ref_RELATIONS` where `ID` & {$ID} order by `ID`";
  58. $result = DB::getPDO()->fetchAll($query);
  59. $rels = array_map('reset', $result);
  60. $this->relations[$ID] = implode(", ", $rels);
  61. }
  62. return $this->relations[$ID];
  63. }
  64. private function findRelations($node, $ID, $resolveDepth, $relation = null) {
  65. if (in_array($ID, $this->path)) return;
  66. $this->path[] = $ID;
  67. $query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID}";
  68. if (!($row = DB::getPDO()->fetchFirst($query)))
  69. {
  70. objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  71. self::throwServiceException("Błąd danych");
  72. }
  73. if (!in_array($row['REMOTE_TABLE'], $this->tablesUsed)) $this->tablesUsed[] = $row['REMOTE_TABLE'];
  74. if ($resolveDepth) {
  75. $feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  76. $this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  77. $this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
  78. if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
  79. //$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  80. $where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
  81. $query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
  82. $result = DB::getPDO()->fetchAll($query);
  83. foreach ($result as $row) $this->findRelations($feature, $row['ID2'], $resolveDepth - 1, $row['RELATION_ID']);
  84. } else {
  85. $xlink = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  86. $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']}");
  87. }
  88. array_pop($this->path);
  89. }
  90. public function defaultAction() {
  91. $TYPENAME = V::get('TYPENAME', '', $_GET);
  92. $primaryKey = V::get('primaryKey', 0, $_GET, 'int');
  93. $resolveDepth = V::get('resolveDepth', 1, $_GET, 'int');
  94. $TYPENAME_exploded = explode(":", $TYPENAME);
  95. if (!(count($TYPENAME_exploded) == 2 && $primaryKey && $resolveDepth >= 1 && $resolveDepth <= self::maxResolveDepth)) self::throwServiceException("WFS request error");
  96. $table = $TYPENAME_exploded[1];
  97. try {
  98. $this->dom = new DOMDocument('1.0', 'UTF-8');
  99. $this->dom->preserveWhiteSpace = false;
  100. $this->dom->formatOutput = true;
  101. $attrs = [
  102. 'xmlns:wfs' => 'http://www.opengis.net/wfs',
  103. 'xmlns' => 'http://www.opengis.net/wfs',
  104. 'xmlns:gml' => 'http://www.opengis.net/gml',
  105. 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
  106. 'xmlns:xlink' => 'http://www.w3.org/1999/xlink',
  107. ];
  108. $wfs = $this->addChild($this->dom, 'wfs:FeatureCollection');
  109. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  110. $gml = $this->addChild($wfs, 'gml:featureMember');
  111. $query = "select `ID` from `BI_audit_ALL` where `REMOTE_TABLE` = " . DB::getPDO()->quote($table) . " and `REMOTE_ID` = {$primaryKey}";
  112. if (!($ID = DB::getPDO()->fetchValue($query))) self::throwServiceException("Błąd danych");
  113. $this->findRelations($gml, $ID, $resolveDepth);
  114. foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
  115. $attrs = [
  116. 'numberMatched' => 'unknown',
  117. 'numberReturned' => '1',
  118. ];
  119. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  120. $xml = $this->dom->saveXML();
  121. self::output($xml);
  122. } catch (Exception $e) {
  123. self::throwServiceException($e->getMessage());
  124. }
  125. }
  126. }