WfsBiAudit.php 22 KB

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