WfsBiAudit.php 9.4 KB

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