| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?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();
- }
- public function objectStructureAction($namespace) { // objectStructAction
- Lib::loadClass('SchemaFactory');
- try {
- error_log('objectStructureAction for '.$namespace);
- if (empty($namespace)) throw new Exception("Missing param namespace");
- $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
- $return=$this->objectStructView($item);
- return $return;
-
- } catch (Exception $e) {
- //error_log('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
- DBG::log($e);
- }
- }
- //input param $item['namespace']='default_db/BI_audit_CEIDG_powiazania/BI_audit_CEIDG_powiazania'
- public function objectStructView($item) {
- $namespace = $item['namespace'];
- $thisGetLink = [ $this, 'getLink' ];
-
- $getBackRefList=ACL::getBackRefList($item['namespace']);
-
-
- foreach($getBackRefList as $ind=>$backref) {
- $exploded_ns = explode('/', $backref['namespace']);
- $return['backrefs'][]=$exploded_ns[1].'__x3A__'.$exploded_ns[2].':'.$exploded_ns[3];
- }
- foreach($item['field'] as $ind=>$refs) {
-
- $return['refs'][]=$refs['fieldNamespace'];
- }
-
- $return['table']=$item['name'];
- $return['primaryKey']=$item['primaryKey'];
-
- //print_r($this->BaseStruct);
-
-
- return $return;
-
- }
- 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_base($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)))
- {
- $this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
- self::throwServiceException("Błąd danych #102 findRelations_base");
- }
- 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);
- }
- 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)))
- {
- $this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
- self::throwServiceException("Błąd danych findRelations #130");
- }
- 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))) {
-
-
- $this->BaseStruct=$this->objectStructureAction("default_db/".$table."/".$table );
-
-
-
- $query = "select `ID` from ".DB::getPDO()->quote($table)." where ".$this->BaseStruct['primaryKey']." = {$primaryKey}";
- if (!($ID = DB::getPDO()->fetchValue($query))) {
- self::throwServiceException("Błąd danych z BaseStruct ");
- }
-
- } else {
- $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) {
- //$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
-
- self::throwServiceException($e->getMessage());
- }
- }
- }
|