| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- Lib::loadClass('RouteBase');
- class Route_WfsBiAudit extends RouteBase {
- const maxResolveDepth = 3;
- private $dom, $path = [], $relations = [], $tablesUsed = [];
- private static function output($output) {
- header("Content-Type: application/xml");
- header("Content-Transfer-Encoding: binary");
- header("Content-Length: " . strlen($output));
- echo $output;
- }
- private static function throwServiceException($message) {
- $xml = <<<EOT
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <ServiceException>{$message}</ServiceException>
- </ServiceExceptionReport>
- EOT;
- self::output($xml);
- die();
- }
- private function addChild($node, $name, $value = null) {
- $child = $this->dom->createElement($name, $value);
- $node->appendChild($child);
- return $child;
- }
- private function addAttribute($node, $name, $value) {
- $attr = $this->dom->createAttribute($name);
- $attr->value = $value;
- $node->appendChild($attr);
- }
- private function relationName($ID) {
- if (!isset($this->relations[$ID])) {
- $query = "select `RELATION` from `BI_audit_ALL_ref_RELATIONS` where `ID` & {$ID} order by `ID`";
- $result = DB::getPDO()->fetchAll($query);
- $rels = array_map('reset', $result);
- $this->relations[$ID] = implode(", ", $rels);
- }
- return $this->relations[$ID];
- }
- private function findRelations($node, $ID, $resolveDepth, $relation = null) {
- if (in_array($ID, $this->path)) return;
- $this->path[] = $ID;
- $query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID}";
- if (!($row = DB::getPDO()->fetchFirst($query))) self::throwServiceException("Błąd danych");
- 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']}");
- }
- array_pop($this->path);
- }
- public function defaultAction() {
- $TYPENAME = V::get('TYPENAME', '', $_GET);
- $primaryKey = V::get('primaryKey', 0, $_GET, 'int');
- $resolveDepth = V::get('resolveDepth', 1, $_GET, 'int');
- $TYPENAME_exploded = explode(":", $TYPENAME);
- if (!(count($TYPENAME_exploded) == 2 && $primaryKey && $resolveDepth >= 1 && $resolveDepth <= self::maxResolveDepth)) self::throwServiceException("WFS request error");
- $table = $TYPENAME_exploded[1];
- try {
- $this->dom = new DOMDocument('1.0', 'UTF-8');
- $this->dom->preserveWhiteSpace = false;
- $this->dom->formatOutput = true;
- $attrs = [
- 'xmlns:wfs' => 'http://www.opengis.net/wfs',
- 'xmlns' => 'http://www.opengis.net/wfs',
- 'xmlns:gml' => 'http://www.opengis.net/gml',
- 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
- 'xmlns:xlink' => 'http://www.w3.org/1999/xlink',
- ];
- $wfs = $this->addChild($this->dom, 'wfs:FeatureCollection');
- 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))) self::throwServiceException("Błąd danych");
- $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) {
- self::throwServiceException($e->getMessage());
- }
- }
- }
|