WfsBiAudit.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_WfsBiAudit extends RouteBase {
  4. const maxResolveDepth = 3;
  5. private $dom, $path = [], $relations = [], $tablesUsed = [];
  6. public $_wfsRootNode = null;
  7. public $_xmlnsTablePrefix = [];
  8. public function handleAuth() {
  9. if (!empty($_GET['sid'])) {
  10. session_write_close();
  11. session_id($_GET['sid']); // TODO: security BUG
  12. error_log('wfs-data.php _GET[sid] was read' .$_GET['sid'].' ; ');
  13. session_start();
  14. session_write_close();
  15. }
  16. if (!User::logged()) throw new HttpException('Unauthorized', 401);
  17. }
  18. private static function output($output) {
  19. header("Content-Type: application/xml");
  20. header("Content-Transfer-Encoding: binary");
  21. header("Content-Length: " . strlen($output));
  22. echo $output;
  23. }
  24. private static function throwServiceException($message) {
  25. $xml = <<<EOT
  26. <?xml version="1.0" encoding="UTF-8"?>
  27. <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">
  28. <ServiceException>{$message}</ServiceException>
  29. </ServiceExceptionReport>
  30. EOT;
  31. self::output($xml);
  32. die();
  33. }
  34. public function objectStructureAction($namespace) { // objectStructAction
  35. Lib::loadClass('SchemaFactory');
  36. Lib::loadClass('ACL');
  37. try {
  38. error_log('objectStructureAction for '.$namespace);
  39. if (empty($namespace)) throw new Exception("Missing param namespace");
  40. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  41. $return=$this->objectStructView($item);
  42. return $return;
  43. } catch (Exception $e) {
  44. //error_log('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  45. DBG::log($e);
  46. }
  47. }
  48. //input param $item['namespace']='default_db/BI_audit_CEIDG_powiazania/BI_audit_CEIDG_powiazania'
  49. public function objectStructView($item) {
  50. $namespace = $item['namespace'];
  51. $thisGetLink = [ $this, 'getLink' ];
  52. $getBackRefList=ACL::getBackRefList($item['namespace']);
  53. foreach($getBackRefList as $ind=>$backref) {
  54. $exploded_ns = explode('/', $backref['namespace']);
  55. $return['backrefs'][]=$exploded_ns[1].'__x3A__'.$exploded_ns[2].':'.$exploded_ns[3];
  56. }
  57. foreach($item['field'] as $ind=>$refs) {
  58. $return['refs'] []=$refs['fieldNamespace'];
  59. $return['refs__rootTableName'][]=$refs['_rootTableName'];
  60. $return['refs__objectNamespace'][]=$refs['objectNamespace'];
  61. }
  62. $return['table']=$item['name'];
  63. $return['primaryKey']=$item['primaryKey'];
  64. //print_r($this->BaseStruct);
  65. return $return;
  66. }
  67. private function addChild($node, $name, $value = null) {
  68. $child = $this->dom->createElement($name, $value);
  69. $node->appendChild($child);
  70. return $child;
  71. }
  72. private function addAttribute($node, $name, $value) {
  73. $attr = $this->dom->createAttribute($name);
  74. $attr->value = $value;
  75. $node->appendChild($attr);
  76. }
  77. public function setRootNode($node) {
  78. $this->_wfsRootNode = $node;
  79. }
  80. public function addRootXmlnsTablePrefix($typeName) {
  81. if (!$this->_wfsRootNode) throw new Exception("Root node not set");
  82. list($nsPrefix, $nsBaseName) = explode(':', $typeName);
  83. list($nsSourceName, $nsBaseTable) = explode('__x3A__', $nsPrefix);
  84. if (!in_array($nsBaseTable, $this->_xmlnsTablePrefix)) {
  85. $this->_xmlnsTablePrefix[] = $nsBaseTable;
  86. $this->addAttribute($this->_wfsRootNode, "xmlns:default_db__x3A__{$nsBaseTable}", "https://biuro.biall-net.pl/wfs/default_db/{$nsBaseTable}");
  87. }
  88. }
  89. private function relationName($ID) {
  90. if (!isset($this->relations[$ID])) {
  91. $query = "select `RELATION` from `BI_audit_ALL_ref_RELATIONS` where `ID` & {$ID} order by `ID`";
  92. $result = DB::getPDO()->fetchAll($query);
  93. $rels = array_map('reset', $result);
  94. $this->relations[$ID] = implode(", ", $rels);
  95. }
  96. return $this->relations[$ID];
  97. }
  98. private function findRelations_base($node, $ID, $resolveDepth, $relation = null, $BaseStruct=null,$table=null) {
  99. if (in_array($ID, $this->path)) return;
  100. $this->path[] = $ID;
  101. error_log('#208 findRelations_base passing');
  102. foreach($BaseStruct['refs__objectNamespace'] as $ind=>$refs){
  103. $getRefTable=ACL::getRefTable('default_db/'.$table.'/'.$table,$refs);
  104. $query = "select `REMOTE_PRIMARY_KEY` from `".$getRefTable."` where `PRIMARY_KEY` = {$ID}";
  105. error_log('#114 findRelations_base passing query '.$query);
  106. if (!($row = DB::getPDO()->fetchFirst($query))) {
  107. } else {
  108. /*{
  109. $this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  110. self::throwServiceException("Błąd danych #102 findRelations_base");
  111. }*/
  112. $this->addRootXmlnsTablePrefix("default_db__x3A__{$BaseStruct['_rootTableName'][$ind]}:{$BaseStruct['_rootTableName'][$ind]}"); // if (!in_array($BaseStruct['_rootTableName'][$ind], $this->tablesUsed)) $this->tablesUsed[] = $BaseStruct['_rootTableName'][$ind];
  113. }
  114. if ($resolveDepth) {
  115. $feature = $this->addChild($node, $BaseStruct['refs'][$ind]);
  116. $this->addAttribute($feature, 'fid', $BaseStruct['_rootTableName'][$ind].$row['REMOTE_PRIMARY_KEY']);
  117. $this->addChild($feature, "default_db__x3A__".$BaseStruct['_rootTableName'][$ind].":ID", $row['REMOTE_PRIMARY_KEY']);
  118. if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
  119. //$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  120. //$where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
  121. $RefBaseStruct=$this->objectStructureAction($BaseStruct['objectNamespace'][$ind]);
  122. $query = "select `".$RefBaseStruct['primaryKey']."` from `".$RefBaseStruct['table']."` where `".$RefBaseStruct['primaryKey']."` = ".$row['REMOTE_PRIMARY_KEY'] ;
  123. $result = DB::getPDO()->fetchAll($query);
  124. foreach ($result as $row) $this->findRelations($feature, $row['REMOTE_PRIMARY_KEY'], $resolveDepth - 1, null,$BaseStruct['table'],$RefBaseStruct['table']);
  125. } else {
  126. $xlink = $this->addChild($node, $BaseStruct['refs'][$ind]);
  127. $this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/{$RefBaseStruct['table']}?BI_audit_ALL_ref_RELATIONS={$relation}#{$RefBaseStruct['table']}.{$row['REMOTE_PRIMARY_KEY']}");
  128. }
  129. array_pop($this->path);
  130. }
  131. }
  132. private function findRelations($node, $ID, $resolveDepth, $relation = null, $BaseTableFrom = null, $BaseTableTo = null) {
  133. if (in_array($ID, $this->path)) return;
  134. $this->path[] = $ID;
  135. if (!empty($BaseTableFrom) && !empty($BaseTableTo)) {
  136. $query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID} and `REMOTE_TABLE`='".$BaseTableTo."'"; //todo nie wiem czy nie na odwrot $BaseTableTo
  137. } else {
  138. $query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID}";
  139. }
  140. if (!($row = DB::getPDO()->fetchFirst($query))) {
  141. if (empty($BaseTableFrom) && empty($BaseTableTo)) {
  142. self::throwServiceException("Błąd danych z BaseStruct/findRelations #161 ");
  143. }
  144. //$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  145. // self::throwServiceException("Błąd danych findRelations #130");
  146. $this->BaseStruct=$this->objectStructureAction("default_db/".$BaseTableTo."/".$BaseTableTo );
  147. $query = "select `".$this->BaseStruct['primaryKey']."` from `".$BaseTableTo."` where ".$this->BaseStruct['primaryKey']." = {$ID}";
  148. if (!($ID = DB::getPDO()->fetchValue($query))) {
  149. self::throwServiceException("Błąd danych z BaseStruct/findRelations #162 ");
  150. }
  151. $this->addRootXmlnsTablePrefix("default_db__x3A__{$BaseTableTo}:{$BaseTableTo}"); // if (!in_array($BaseTableTo, $this->tablesUsed)) $this->tablesUsed[] = $BaseTableTo;
  152. if ($resolveDepth) {
  153. $feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  154. $this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  155. $this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
  156. if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
  157. //$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  158. //$where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
  159. //$query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
  160. //$result = DB::getPDO()->fetchAll($query);
  161. //foreach ($result as $row)
  162. // $this->findRelations($feature, $row['ID2'], $resolveDepth - 1, $row['RELATION_ID'],);
  163. findRelations_base($node, $ID, $resolveDepth, $relation = null, $this->BaseStruct,$BaseTableTo) ;
  164. } else {
  165. $xlink = $this->addChild($node, "default_db__x3A__{$BaseTableTo}:{$BaseTableTo}");
  166. $this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/".$BaseTableTo."?BI_audit_ALL_ref_RELATIONS={$BaseTableTo}#{$BaseTableTo}.{$row[$this->BaseStruct['primaryKey']]}");
  167. }
  168. array_pop($this->path);
  169. } else {
  170. // $row = [ 'REMOTE_TABLE' => BI_audit_KRS_person, 'REMOTE_ID' => 366074 ]
  171. $this->addRootXmlnsTablePrefix("default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}"); // if (!in_array($row['REMOTE_TABLE'], $this->tablesUsed)) $this->tablesUsed[] = $row['REMOTE_TABLE'];
  172. if ($resolveDepth) {
  173. $featureNode = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  174. $this->addAttribute($featureNode, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  175. $this->addChild($featureNode, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
  176. if ($relation) $this->addChild($featureNode, "relation_from", $this->relationName($relation));
  177. //$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  178. $where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
  179. $query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
  180. $result = DB::getPDO()->fetchAll($query);
  181. foreach ($result as $childRow) $this->findRelations($featureNode, $childRow['ID2'], $resolveDepth - 1, $childRow['RELATION_ID']);
  182. $this->addAclInfo(
  183. $dataNode = $featureNode,
  184. $typeName = "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}",
  185. $primaryKey = $row['REMOTE_ID'],
  186. $skipFields = [ 'ID' ],
  187. $resolveDepth - 1
  188. );
  189. } else {
  190. $xlink = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  191. $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']}");
  192. }
  193. array_pop($this->path);
  194. }
  195. }
  196. public function addAclInfo($dataNode, $typeName, $primaryKey, $skipFields = [], $resolveDepth = 0) {
  197. try {
  198. $acl = ACL::getAclByTypeName($typeName);
  199. list($nsPrefix, $nsBaseName) = explode(':', $typeName);
  200. // list($nsSourceName, $nsBaseTable) = explode('__x3A__', $nsPrefix);
  201. $searchParams = [
  202. 'cols' => array_values($acl->getFieldListByIdZasob())
  203. ];
  204. // echo "TODO: \$typeName({$typeName}) \$resolveDepth({$resolveDepth})\n";
  205. if ($resolveDepth > 0) {
  206. $filterFields = implode("/", array_fill(0, $resolveDepth + 1, "*"));
  207. // echo "TODO: \$typeName({$typeName}) \$resolveDepth({$resolveDepth}) \$filterFields({$filterFields})\n";
  208. $schemaCache = array();
  209. try {
  210. Lib::loadClass('Api_Wfs_GetFeature');
  211. $searchParams['cols'] = Api_Wfs_GetFeature::convertOgcPropertyListToFeatureQueryCols($schemaCache, [ $filterFields ], $acl, $isRoot = $args['root']); // convert $args['filterFields'] to field list
  212. // print_r($searchParams);
  213. } catch (Exception $e) {
  214. DBG::log($e);
  215. throw $e;
  216. }
  217. }
  218. $item = $acl->buildQuery($searchParams)->getItem($primaryKey);
  219. if (!$item) throw new Exception("Item not found {$primaryKey}");
  220. // $item = Array( [ID] => 56977, [krs] => 0000080725 , ... )
  221. // $item[default_db__x3A__BI_audit_MSIG_address:BI_audit_MSIG_address] => Array( [0] => [ 'xlink' => ${url} ] )
  222. // $item[default_db__x3A__BI_audit_MSIG_address:BI_audit_MSIG_address] => Array( [11] => [ 'p5:links' => ${p5Links} ] )
  223. // $p5Links = Array( 'p5:next' => ${p5NextLink} )
  224. // $p5NextLink = Array(
  225. // [@typeName] => default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person
  226. // [@backRefNS] => default_db/BI_audit_KRS/BI_audit_KRS
  227. // [@backRefPK] => 56977
  228. // [@startIndex] => 10
  229. // [@maxFeatures] => 10
  230. // [value] => https://biuro.biall-net.pl/dev-pl/se-master/index.php?SERVICE=WFS&VERSION=1.0.0&TYPENAME=default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person&REQUEST=GetFeature&backRefNS=default_db/BI_audit_KRS/BI_audit_KRS&backRefPK=56977&backRefField=default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person&maxFeatures=10&startIndex=10
  231. // $dataNode = $this->addChild($featureNode, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  232. DBG::log(['item'=>$item,'searchParams'=>$searchParams], 'array', "DBG WfsBiAudit \$item");
  233. foreach ($item as $fieldName => $value) {
  234. if (in_array($fieldName, $skipFields)) continue;
  235. // $this->addAttribute($dataNode, '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']}");
  236. if (is_scalar($value)) {
  237. $this->addChild($dataNode, "{$nsPrefix}:{$fieldName}", $value);
  238. } else if (is_array($value) && false !== strpos($fieldName, ':')) {
  239. $this->addRootXmlnsTablePrefix($fieldName);
  240. // <default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person xlink:href="https://biuro.biall-net.pl/wfs/default_db/BI_audit_KRS_person#BI_audit_KRS_person.366064"/>
  241. // <p5:links>
  242. // <p5:next p5:typeName="default_db__x3A__BI_audit_KRS_person:BI_audit_KRS_person" ...
  243. // </p5:links>
  244. foreach ($value as $refKey => $refValue) {
  245. if (is_array($refValue) && 1 === count($refValue) && !empty($refValue['xlink'])) {
  246. list($refPrefix, $refBaseName) = explode(':', $fieldName);
  247. list($refSourceName, $refBaseTable) = explode('__x3A__', $refPrefix);
  248. $xlink = $this->addChild($dataNode, $fieldName);
  249. $this->addAttribute($xlink, 'xlink:href', $refValue['xlink']); // "https://biuro.biall-net.pl/wfs/default_db/{$refBaseTable}#{$refBaseName}.{$row['REMOTE_ID']}");
  250. } else if (is_array($refValue) && empty($refValue)) {
  251. // skip empty array
  252. } else if (is_array($refValue) && 1 === count($refValue) && !empty($refValue['p5:links'])) {
  253. $p5LinkNode = $this->addChild($dataNode, 'p5:links');
  254. foreach ($refValue['p5:links'] as $p5LinkKey => $p5LinkValue) {
  255. if ('p5:next' === $p5LinkKey) {
  256. $p5NextLinkNode = $this->addChild($p5LinkNode, 'p5:next', str_replace('&', '&amp;', $p5LinkValue['value']));
  257. foreach ($p5LinkValue as $p5NextKey => $p5NextValue) {
  258. if ('@' === $p5NextKey[0]) {
  259. $this->addAttribute($p5NextLinkNode, "p5:" . substr($p5NextKey, 1), $p5NextValue);
  260. } else {
  261. // skip
  262. }
  263. }
  264. } else {
  265. $this->addChild($dataNode, $fieldName, "<!-- TODO 'p5:links/{$p5LinkKey}': " . str_replace('&', '&amp;', var_export($p5LinkValue, true)) . " -->");
  266. }
  267. }
  268. } else {
  269. $this->addChild($dataNode, $fieldName, "<!-- TODO add info ref ({$refKey}): " . str_replace('&', '&amp;', var_export($refValue, true)) . " -->");
  270. }
  271. }
  272. } else if (NULL === $value) {
  273. $this->addChild($dataNode, "{$nsPrefix}:{$fieldName}"); // xs:nil ?
  274. } else {
  275. $this->addChild($dataNode, $fieldName, "<!-- TODO: " . var_export($value, true) . " -->");
  276. }
  277. }
  278. // add backRef links
  279. $namespace = $acl->getNamespace();
  280. $backRefList = ACL::getBackRefList($namespace);
  281. DBG::log($backRefList, 'array', "\$backRefList for({$namespace};{$primaryKey})");
  282. $backRefStats = array_map(function ($backRef) use ($namespace, $primaryKey) { // [ namespace, idInstance ], @returns { namespace, idInstance, label, shortName, total }
  283. $label = $backRef['namespace']; // TODO: get DESC from Zasoby
  284. $nameShort = explode("/", $label);
  285. $nameShort = array_pop($nameShort);
  286. $nameShort = (strlen($nameShort) > 20) ? substr($nameShort, 0, 20) . "..." : $nameShort;
  287. $total = 0;
  288. try {
  289. $total = ACL::fetchBackRefs($namespace, $primaryKey, $backRef['namespace'], [ 'total' => true ]);
  290. } catch (Exception $e) {
  291. DBG::log($e);
  292. }
  293. return array_merge($backRef, [
  294. 'primaryKey' => $primaryKey,
  295. 'label' => $label,
  296. 'shortName' => $nameShort,
  297. 'total' => $total,
  298. ]);
  299. }, $backRefList);
  300. DBG::log($backRefStats, 'array', "\$backRefStats for({$namespace};{$primaryKey})");
  301. $foundBackRef = array_filter($backRefStats, function ($statsBackRef) {
  302. return ($statsBackRef['total'] > 0);
  303. });
  304. DBG::log($foundBackRef, 'array', "\$foundBackRef for({$namespace};{$primaryKey})");
  305. // $rowFunList[] = [
  306. // 'ico' => 'glyphicon glyphicon-random',
  307. // 'href' => Router::getRoute('ViewTableAjax')->getLink('', [
  308. // 'namespace' => $backRef['namespace'],
  309. // 'childRefNS' => $acl->getNamespace(),
  310. // 'childRefPK' => $id,
  311. // ]),
  312. // 'title' => "Wyszukaj powiązania z '{$backRefLabel}'",
  313. // 'label' => "Wyszukaj powiązania z '{$backRefShort}' <span class=\"badge\">{$totalBackRefs}</span>",
  314. // ];
  315. if (!empty($foundBackRef)) {
  316. $p5LinkNode = $this->addChild($dataNode, 'p5:links');
  317. foreach ($foundBackRef as $backRef) {
  318. $linkBackRef = null; // TODO: generate back ref link
  319. $p5BackRefNode = $this->addChild($p5LinkNode, 'p5:backRef', $linkBackRef);
  320. $this->addAttribute($p5BackRefNode, "p5:total", $backRef['total']);
  321. $this->addAttribute($p5BackRefNode, "p5:label", $backRef['label']);
  322. $this->addAttribute($p5BackRefNode, "p5:shortName", $backRef['shortName']);
  323. $this->addAttribute($p5BackRefNode, "p5:namespace", $backRef['namespace']);
  324. $this->addAttribute($p5BackRefNode, "p5:primaryKey", $backRef['primaryKey']);
  325. $this->addAttribute($p5BackRefNode, "p5:from", $namespace);
  326. $this->addAttribute($p5BackRefNode, "p5:to", $backRef['namespace']);
  327. }
  328. }
  329. } catch (Exception $e) {
  330. DBG::log($e);
  331. }
  332. }
  333. public function defaultAction() {
  334. $TYPENAME = V::get('TYPENAME', '', $_GET);
  335. $primaryKey = V::get('primaryKey', 0, $_GET, 'int');
  336. $resolveDepth = V::get('resolveDepth', 1, $_GET, 'int');
  337. $TYPENAME_exploded = explode(":", $TYPENAME);
  338. if (!(count($TYPENAME_exploded) == 2 && $primaryKey && $resolveDepth >= 1 && $resolveDepth <= self::maxResolveDepth)) self::throwServiceException("WFS request error");
  339. $table = $TYPENAME_exploded[1];
  340. try {
  341. $this->dom = new DOMDocument('1.0', 'UTF-8');
  342. $this->dom->preserveWhiteSpace = false;
  343. $this->dom->formatOutput = true;
  344. $attrs = [
  345. 'xmlns:wfs' => 'http://www.opengis.net/wfs',
  346. 'xmlns' => 'http://www.opengis.net/wfs',
  347. 'xmlns:gml' => 'http://www.opengis.net/gml',
  348. 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
  349. 'xmlns:xlink' => 'http://www.w3.org/1999/xlink',
  350. ];
  351. $wfs = $this->addChild($this->dom, 'wfs:FeatureCollection');
  352. $this->setRootNode($wfs);
  353. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  354. $gml = $this->addChild($wfs, 'gml:featureMember');
  355. $query = "select `ID` from `BI_audit_ALL` where `REMOTE_TABLE` = " . DB::getPDO()->quote($table) . " and `REMOTE_ID` = {$primaryKey}";
  356. if (!($ID = DB::getPDO()->fetchValue($query))) {
  357. error_log('#233 default base passing');
  358. // $this->BaseStruct=$this->objectStructureAction("default_db/".$table."/".$table );
  359. //
  360. // $query = "select `".$this->BaseStruct['primaryKey']."` from `".$table."` where ".$this->BaseStruct['primaryKey']." = {$primaryKey}";
  361. // if (!($ID = DB::getPDO()->fetchValue($query))) {
  362. // self::throwServiceException("Błąd danych z BaseStruct ");
  363. // }
  364. // $this->findRelations_base($gml, $ID, $resolveDepth, $table, $this->BaseStruct, $table);
  365. $featureNode = $this->addChild($gml, $TYPENAME);
  366. $this->addRootXmlnsTablePrefix($TYPENAME);
  367. list($nsPrefix, $nsBaseName) = explode(':', $TYPENAME);
  368. $this->addAttribute($featureNode, 'fid', "{$nsBaseName}.{$primaryKey}");
  369. $this->addAclInfo(
  370. $dataNode = $featureNode,
  371. $typeName = $TYPENAME,
  372. $primaryKey = $primaryKey,
  373. $skipFields = [],
  374. $resolveDepth
  375. );
  376. // foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
  377. $attrs = [
  378. 'numberMatched' => 'unknown',
  379. 'numberReturned' => '1',
  380. ];
  381. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  382. $xml = $this->dom->saveXML();
  383. self::output($xml);
  384. } else {
  385. error_log('#254 default passing');
  386. $this->findRelations($gml, $ID, $resolveDepth);
  387. // foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
  388. $attrs = [
  389. 'numberMatched' => 'unknown',
  390. 'numberReturned' => '1',
  391. ];
  392. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  393. $xml = $this->dom->saveXML();
  394. self::output($xml);
  395. }
  396. } catch (Exception $e) {
  397. //$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  398. self::throwServiceException($e->getMessage());
  399. }
  400. }
  401. }