GetFeature.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. Lib::loadClass('Api_WfsGeomTypeConverter');
  3. Lib::loadClass('Core_AclHelper');
  4. class Api_Wfs_GetFeature {
  5. /**
  6. * @param $_GET [ , POST Body - XML file ]
  7. * @return array $args
  8. * TODO: get $acl and check more restrictions
  9. */
  10. public static function parseGetFeatureArgsFromRequest() {
  11. $rawArgs = $_GET;// $_REQUEST; ($_POST values not allowed, only xml in post body)
  12. $args = array();
  13. $args['xsd:type'] = null;// @from: TYPENAME, typeName (typename)
  14. $args['typePrefix'] = null;// @from: TYPENAME, typeName (typename)
  15. $args['typeName'] = null;// @from: TYPENAME, typeName (typename)
  16. $args['srsname'] = null;// @from: SRSNAME (srsname)
  17. $args['limit'] = 0;// @from: MAXFEATURES, maxFeatures, COUNT (maxfeatures, count)
  18. $args['offset'] = 0;// @from: startIndex (startindex, count)
  19. $args['ogc:filter'] = null;// @from: FILTER, Filter (filter)
  20. $args['sortBy'] = null;// @from: sortBy (sortby)
  21. $args['wfs:propertyName'] = null;// @from: propertyName (propertyname)
  22. $args['filterFields'] = array();// array of field names to show - @from: propertyName
  23. $args['resultType'] = null;// @from: resultType (resulttype)
  24. $args['bbox'] = null;// @from: BBOX (bbox)
  25. $args['wfs:featureID'] = null;// @from: featureID, featureId (featureid)
  26. $args['primaryKey'] = null;// primaryKey type - @from: featureID or $req[primaryKey]
  27. $args['xlink'] = V::get('xlink', '', $rawArgs); // xlink = "https://biuro.biall-net.pl/wfs/default_db/CRM_PROCES#PROCES.100" but with '#' replaced with '/'
  28. $args['outputBlobFormat'] = V::get('outputBlobFormat', 'base64', $rawArgs); // how to print blob fields ('base64', 'link') - default base64
  29. $args['resolve'] = V::get('resolve', '', $rawArgs);
  30. $args['resolveDepth'] = V::get('resolveDepth', 0, $rawArgs, 'int'); // TODO: if ('*' === resolveDepth) - recurse resolve
  31. $lowerArgs = array(); foreach ($rawArgs as $name => $value) $lowerArgs[ strtolower($name) ] = trim($value);
  32. $args['xsd:type'] = V::get('typename', '', $lowerArgs);
  33. $exType = explode(':', $args['xsd:type']);
  34. if (count($exType) != 2) throw new HttpException("Wrong param TYPENAME", 400);
  35. $args['typePrefix'] = $exType[0];
  36. $args['typeName'] = $exType[1];
  37. $args['srsname'] = V::get('SRSNAME', '', $lowerArgs);// eg. EPSG:4326
  38. $args['limit'] = V::get('maxfeatures', $args['limit'], $lowerArgs, 'int');
  39. $args['limit'] = V::get('count', $args['limit'], $lowerArgs, 'int');
  40. $defaultFeaturesLimit = ('biuro.biall-net.pl' == V::get('SERVER_NAME', '', $_SERVER)) ? 50000 : 10000;
  41. if ($args['limit'] <= 0) $args['limit'] = $defaultFeaturesLimit;// default limit
  42. $args['offset'] = V::get('startindex', 0, $lowerArgs, 'int');
  43. $args['ogc:filter'] = urldecode(V::get('filter', '', $lowerArgs));
  44. if (empty($args['ogc:filter'])) {// read ogc:Filter from POST body if not defined in GET
  45. $reqBody = Request::getRequestBody();
  46. if (!empty($reqBody)) {
  47. $parsedRequest = self::parseOgcFilterRequest($reqBody);
  48. DBG::log($parsedRequest, 'array', 'parsed ogc query request');
  49. if (!empty($parsedRequest['ogc:Filter'])) $args['ogc:filter'] = $parsedRequest['ogc:Filter'];
  50. if (!empty($parsedRequest['wfs:PropertyName'])) {
  51. $args['filterFields'] = array_merge($args['filterFields'], $parsedRequest['wfs:PropertyName']);
  52. }
  53. } else if ($args['resolve'] && $args['resolveDepth'] > 0) {
  54. $args['filterFields'][] = '*' . str_repeat("/*", $args['resolveDepth'] - 1);
  55. }
  56. }
  57. $args['sortBy'] = V::get('sortby', '', $lowerArgs);// TODO: split to array of array(fieldName, (asc | desc))
  58. $args['wfs:propertyName'] = trim(V::get('propertyname', '', $lowerArgs));
  59. if (strlen($args['wfs:propertyName']) > 0) {
  60. $exFields = explode(',', $args['wfs:propertyName']);
  61. $exFields = array_map('trim', $exFields);
  62. $exFields = array_filter($exFields, [ 'V', 'filterNotEmpty' ]);
  63. $args['filterFields'] = array_merge($args['filterFields'], $exFields);
  64. }
  65. $args['resultType'] = V::get('resulttype', '', $lowerArgs);
  66. $args['bbox'] = V::get('bbox', '', $lowerArgs);
  67. if (!empty($args['bbox'])) {
  68. // BBOX may have EPSG at then end - example: ",EPSG:4326"
  69. $num = "\d+.?\d*?";// "\d+(.\d+)?"
  70. if (!preg_match("/^({$num}),({$num}),({$num}),({$num})(\,EPSG:\d+)?$/", $args['bbox'], $matches)) throw new Exception("Illegal BBOX format");
  71. // QGIS send BBOX in correct order: 54.23580872176457,18.46844302390853,54.25220902538883,18.492990600812696
  72. // first number should not be smaller then second
  73. // example $matches:
  74. // [0] => 18.492990600812696,54.23580872176457,18.46844302390853,54.25220902538883
  75. // [1] => 18.492990600812696
  76. // [2] => 54.23580872176457
  77. // [3] => 18.46844302390853
  78. // [4] => 54.25220902538883
  79. // (optional) EPGS
  80. $bboxPoints = ($matches[1] > $matches[2])
  81. ? [ $matches[1], $matches[2], $matches[3], $matches[4] ]
  82. : [ $matches[2], $matches[3], $matches[4], $matches[1] ];
  83. $args['bbox'] = implode(",", $bboxPoints);
  84. }
  85. $args['wfs:featureID'] = V::get('featureid', null, $lowerArgs);// TODO: allow multiply feature id (csv)
  86. if ($args['wfs:featureID']) {
  87. $dotPos = strpos($args['wfs:featureID'], '.');
  88. if (!$dotPos) throw new Exception("Wrong param featureID");
  89. if ($args['typeName'] != substr($args['wfs:featureID'], 0, $dotPos)) throw new Exception("Wrong typeName in param featureID");
  90. $args['primaryKey'] = substr($args['wfs:featureID'], $dotPos + 1);
  91. }
  92. $pk = V::get('primarykey', '', $lowerArgs);
  93. if ($pk) {
  94. $args['primaryKey'] = $pk;
  95. }
  96. if(DBG::isActive() && V::get('DBG', 0, $_GET)){ echo "\$lowerArgs:";print_r($lowerArgs); }
  97. if(DBG::isActive() && V::get('DBG', 0, $_GET)){ echo "\$args:";print_r($args); }
  98. return $args;
  99. }
  100. public static function parseOgcFilterRequest($requestOgcFilter) {
  101. // GetFeature: @maxFeatures, @traverseXlinkDepth, @traverseXlinkExpiry
  102. // \-- (1..) wfs:Query: @typeName
  103. // \--- (0..) ---: wfs:PropertyName
  104. // : wfs:XlinkPropertyName
  105. // : ogc:Function
  106. // ogc:Filter
  107. // ogc:SortBy
  108. // \--- (1..) ogc:SortProperty
  109. // \---: ogc:PropertyName
  110. // : ogc:SortOrder ( DESC / ASC )
  111. // <ogc:SortBy>
  112. // <ogc:SortProperty>
  113. // <ogc:PropertyName>ASTA</ogc:PropertyName>
  114. // <ogc:SortOrder>ASC</ogc:SortOrder>
  115. // </ogc:SortProperty>
  116. // </ogc:SortBy>
  117. if (empty($requestOgcFilter)) return '';
  118. $requestXml = new DOMDocument();
  119. $requestXml->loadXml($requestOgcFilter);
  120. $rootNode = $requestXml->documentElement;
  121. // if ($rootNode->getAttribute('resolve'))
  122. DBG::log([$rootNode->getAttribute('resolve'), $rootNode->getAttribute('resolveDepth')], 'array', "TODO: use wfs:GetFeature @resolve, @resolveDepth");
  123. $nodesQuery = [];
  124. foreach ($requestXml->getElementsByTagNameNS('http://www.opengis.net/wfs', 'Query') as $element) {
  125. DBG::log($element->nodeName, 'array', "main loop - wfs:Query");
  126. $nodesQuery[] = $element;
  127. }
  128. if (empty($nodesQuery)) { // legacy - try to find 'ogc:Filter'
  129. $tagsFilter = [];
  130. foreach ($requestXml->getElementsByTagNameNS('http://www.opengis.net/ogc', 'Filter') as $element) {
  131. DBG::log($element->nodeName, 'array', "loop element (0 * wfs:Query)");
  132. $tagsFilter[] = $requestXml->saveXML($element);
  133. }
  134. DBG::log($tagsFilter, 'array', "\$tagsFilter (0 * wfs:Query)");
  135. return [
  136. 'ogc:Filter' => (!empty($tagsFilter))
  137. ? reset($tagsFilter) // TODO: only one ogc:Filter allowed
  138. : ''
  139. ];
  140. }
  141. if (1 === count($nodesQuery)) {
  142. $tagsFilter = [];
  143. foreach ($requestXml->getElementsByTagNameNS('http://www.opengis.net/ogc', 'Filter') as $element) {
  144. // DBG::log($element->nodeName, 'array', "loop ogc:Filter (1 * wfs:Query)");
  145. $tagsFilter[] = $requestXml->saveXML($element);
  146. }
  147. DBG::log($tagsFilter, 'array', "\$tagsFilter (1 * wfs:Query)");
  148. $tagsWfsPropertyName = [];
  149. foreach ($requestXml->getElementsByTagNameNS('http://www.opengis.net/wfs', 'PropertyName') as $element) {
  150. // DBG::log($element->nodeValue, 'array', "loop wfs:PropertyName (1 * wfs:Query)");
  151. if ($element->getAttribute('resolve')) DBG::log([$element->getAttribute('resolve'), $element->getAttribute('resolveDepth'), $element->getAttribute('resolvePath')], 'array', "TODO: use wfs:PropertyName @resolve, @resolveDepth, @resolvePath");
  152. $value = $element->nodeValue;
  153. if (in_array($element->getAttribute('resolve'), ['all', 'local', 'remote'])) {
  154. $depth = $element->getAttribute('resolveDepth');
  155. if ('*' !== substr($value, -1)) { // TODO: propertyName is not regex
  156. if ('*' === $depth) $value .= "/**"; // TODO: resolveDepth="*" - resolve all
  157. else if ((int)$depth > 0) $value .= str_repeat("/*", $depth);
  158. }
  159. }
  160. $tagsWfsPropertyName[] = $value;
  161. }
  162. DBG::log($tagsWfsPropertyName, 'array', "\$tagsWfsPropertyName (1 * wfs:Query)");
  163. return array_filter([
  164. 'ogc:Filter' => (!empty($tagsFilter))
  165. ? reset($tagsFilter)
  166. : '',
  167. 'wfs:PropertyName' => (!empty($tagsWfsPropertyName))
  168. ? $tagsWfsPropertyName
  169. : '',
  170. ], function ($value) { return !empty($value); });
  171. }
  172. throw new Exception("Not imeplemented multiple ogc:Query"); // multiple ogc:Query require ogc:Query/@typeName
  173. }
  174. public static function convertOgcPropertyListToFeatureQueryCols(&$schemaCache, $ogcPropertyList, $acl) {
  175. return self::convertOgcPropsRecurse($schemaCache, $ogcPropertyList, $acl, $dbgLoopNr = 0, $dbgXpath = $acl->getNamespace());
  176. }
  177. public static function convertOgcPropsRecurse(&$schemaCache, $ogcPropertyList, $aclOrSchema, $dbgLoopNr = 0, $dbgXpath = '') {
  178. if ($dbgLoopNr > 10) {
  179. DBG::log($ogcPropertyList, 'array', 'convertOgcPropsRecurse: LOOP LIMIT 10! - return []');
  180. return [];
  181. }
  182. $acl = null;
  183. if (is_array($aclOrSchema) && !empty($aclOrSchema['namespace'])) {
  184. $acl = Core_AclHelper::getAclByNamespace($aclOrSchema['namespace'], false, $aclOrSchema);
  185. } else if ($aclOrSchema instanceof Core_AclBase) {
  186. $acl = $aclOrSchema;
  187. } else throw new Exception("Missing acl");
  188. if (empty($ogcPropertyList)) {
  189. $ogcPropertyList = array_values($acl->getFieldListByIdZasob());
  190. $ogcPropertyList = array_filter($ogcPropertyList, function ($fieldName) use ($acl) {
  191. return $acl->canReadField($fieldName);
  192. });
  193. }
  194. $aclFields = array_filter($ogcPropertyList, function ($prop) { return ( false === strpos($prop, '/') ); });
  195. $nestedFields = array_reduce(
  196. array_filter($ogcPropertyList, function ($prop) { return ( false !== strpos($prop, '/') ); }),
  197. function ($ret, $propNested) {
  198. list($childName, $nestedPart) = explode('/', $propNested, 2);
  199. if (!array_key_exists($childName, $ret)) $ret[ $childName ] = [];
  200. $ret[ $childName ][] = $nestedPart;
  201. return $ret;
  202. },
  203. []
  204. );
  205. if (!empty($nestedFields)) {
  206. if (array_key_exists('*', $nestedFields)) { // add missing local fields if xpath like '*/...'
  207. $aclFields[] = '*';
  208. }
  209. }
  210. if (!empty($nestedFields)) { // add nested part to refs if xpath like '*/...'
  211. if (array_key_exists('*', $nestedFields)) {
  212. $nestedParts = $nestedFields['*'];
  213. unset($nestedFields['*']);
  214. DBG::log($nestedParts, 'array', "convertOgcPropsRecurse: TODO: '*' in \$nestedFields - \$nestedParts ns({$dbgXpath})");
  215. $canReadRefs = array_values($acl->getFieldListByIdZasob());
  216. $canReadRefs = array_filter($canReadRefs, function ($fieldName) use ($acl) {
  217. return (false !== strpos($fieldName, ":") && $acl->canReadField($fieldName));
  218. });
  219. foreach ($canReadRefs as $refField) {
  220. foreach ($nestedParts as $nestedPart) {
  221. $nestedFields[ $refField ][] = $nestedPart;
  222. }
  223. }
  224. }
  225. }
  226. if (!empty($nestedFields)) {
  227. $aclFields = array_unique(array_merge($aclFields, array_keys($nestedFields)));
  228. }
  229. if (in_array('*', $aclFields)) {
  230. $aclFields = array_filter($aclFields, function ($fieldName) { return ( '*' !== $fieldName); });
  231. $allAclFields = array_values($acl->getFieldListByIdZasob());
  232. $allAclFields = array_filter($allAclFields, function ($fieldName) use ($acl) {
  233. return $acl->canReadField($fieldName);
  234. });
  235. if ($allAclFields) $aclFields = array_unique(array_merge($aclFields, $allAclFields));
  236. }
  237. DBG::log([$aclFields, $nestedFields], 'array', "convertOgcPropsRecurse: splited to acl fields and nested fields ns({$dbgXpath})");
  238. foreach ($aclFields as $fieldName) {
  239. if (!$acl->canReadField($fieldName)) throw new Exception("Access Denied to read field '{$fieldName}' from '" . $acl->getNamespace() . "'");
  240. }
  241. foreach ($aclFields as $fieldName) {
  242. if (false !== strpos($fieldName, ':')) {
  243. $childNs = str_replace(['__x3A__', ':'], '/', $fieldName);
  244. $schemaCache[$childNs] = SchemaFactory::loadDefaultObject('SystemObject')->getItem($childNs, [ 'propertyName' => '*,field' ]);
  245. }
  246. }
  247. $contextFieldList = $aclFields;
  248. foreach ($nestedFields as $childName => $nestedProps) {
  249. $childNs = str_replace(['__x3A__', ':'], '/', $childName);
  250. if (!array_key_exists($childNs, $schemaCache)) {
  251. $schemaCache[$childNs] = SchemaFactory::loadDefaultObject('SystemObject')->getItem($childNs, [ 'propertyName' => '*,field' ]);
  252. }
  253. $childCtxFields = array_map(function ($childCtxFld) use ($childName) {
  254. return "{$childName}/{$childCtxFld}";
  255. }, self::convertOgcPropsRecurse($schemaCache, $nestedProps, $schemaCache[$childNs], $dbgLoopNr + 1, "{$dbgXpath}/{$childName}"));
  256. DBG::log([ $contextFieldList, $childCtxFields ], 'array', "convertOgcPropsRecurse: array merge ns({$dbgXpath})");
  257. $contextFieldList = array_merge( $contextFieldList, $childCtxFields );
  258. DBG::log($contextFieldList, 'array', "convertOgcPropsRecurse: array merged ns({$dbgXpath})");
  259. }
  260. DBG::log($contextFieldList, 'array', "convertOgcPropsRecurse: return \$contextFieldList ns({$dbgXpath})");
  261. return $contextFieldList;
  262. }
  263. // TODO: add $contextAcl and context xpath to check for special perms by contextAcl
  264. /** @example:
  265. * Api_Wfs_GetFeature::printXmlFeatureRecurse($xmlWriter, $acl, $item, [
  266. * 'fields' => $searchParams['cols'],
  267. * 'tagName' => "{$wfsNs}:{$type}",
  268. * 'showAdvancedAttrs' => !$simple,
  269. * 'xsdAttributes' => array_merge(
  270. * [ 'fid' => "{$type}.{$itemKey}" ],
  271. * (!$simple)
  272. * ? [ "{$rootWfsNs}:web_link" => Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace() . "#EDIT/{$itemKey}" ]
  273. * : []
  274. * )
  275. * ], $schemaCache, $printedFidLog);
  276. */
  277. public static function printXmlFeatureRecurse($xmlWriter, $acl, $item, $params = [], $schemaCache = [], $printedFidLog = []) {
  278. $showFields = V::get('fields', [], $params, 'array'); // TODO: if empty $showFields ?
  279. $tagName = V::get('tagName', '', $params);
  280. $attrs = V::get('xsdAttributes', [], $params, 'array');
  281. $showAdvancedAttrs = V::get('showAdvancedAttrs', false, $params, 'bool');
  282. $dbgFid = V::get('fid', 0, $attrs);
  283. $outputBlobFormat = V::get('outputBlobFormat', '', $params, 'word');
  284. if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("DBG: print Xml Feature Recurse... '{$tagName}'" . ( $dbgFid ? " fid='{$dbgFid}'" : "" )); // TODO: DBG
  285. // DBG::log($acl, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$acl)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$acl");
  286. DBG::log($item, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$item)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$item");
  287. DBG::log($showFields, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$item)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$showFields");
  288. DBG::log([$attrs, $showAdvancedAttrs, array_keys($schemaCache), $printedFidLog], 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$attrs, \$showAdvancedAttrs, keys(\$schemaCache), \$printedFidLog)");
  289. if ($dbgFid) {
  290. if (in_array($dbgFid, $printedFidLog)) {
  291. DBG::log("TODO: in_array({$dbgFid}, \$printedFidLog)");
  292. }
  293. }
  294. if ($dbgFid) $printedFidLog[] = $dbgFid;
  295. // $rootWfsNs = 'p5';
  296. list($itemPrefix, $localName) = explode(':', $tagName);
  297. if (1 === count($item) && !empty($item['xlink'])) {
  298. // @example 'xlink' => 'https://biuro.biall-net.pl/wfs/default_db/CRM_PROCES#PROCES.857'
  299. $xlink = $item['xlink'];
  300. list($xlinkUrl, $xlinkFid) = explode('#', $xlink);
  301. DBG::log($item[$fldName], 'array', "xlinks for '{$tagName}'");
  302. $xmlWriter->startElement($tagName);
  303. foreach ($attrs as $name => $value) {
  304. $xmlWriter->writeAttribute($name, $value);
  305. }
  306. if ($showAdvancedAttrs && !$acl->canReadObjectField($fldName, (object)$item)) {
  307. $xmlWriter->writeAttribute("p5:allow_read", "false");
  308. }
  309. if ($showAdvancedAttrs && $acl->canWriteObjectField($fldName, (object)$item)) {
  310. $xmlWriter->writeAttribute("p5:allow_write", "true");
  311. }
  312. $xmlWriter->writeAttribute('xlink:href', $xlink);
  313. $xmlWriter->endElement();// {$itemPrefix}:{$fldName}
  314. return;
  315. }
  316. $xmlWriter->startElement($tagName);
  317. foreach ($attrs as $name => $value) {
  318. $xmlWriter->writeAttribute($name, $value);
  319. }
  320. // $fldList = $acl->getRealFieldListByIdZasob();
  321. $fldList = $acl->getFieldListByIdZasob();
  322. $geomFld = null;
  323. foreach ($fldList as $fldName) {
  324. if ($acl->isGeomField($fldName)) {
  325. $geomFld = $fldName;
  326. }
  327. }
  328. DBG::log($fldList, 'array', ">>> loop start fields(".count($fldList).")");
  329. // DBG::log($showFields, 'array', "DBG: \$showLocalFields \$showFields 0");
  330. $showLocalFields = ($showFields) // if empty filter cols show all local fields
  331. ? array_filter($showFields, function ($showFieldName) {
  332. return (false === strpos($showFieldName, "/"));
  333. })
  334. : $fldList;
  335. DBG::log($showLocalFields, 'array', "DBG: \$showLocalFields");
  336. $fldList = array_filter($fldList, function ($fieldName) use ($showLocalFields) {
  337. return (in_array($fieldName, $showLocalFields));
  338. });
  339. DBG::log($fldList, 'array', ">>> loop start filtered fields(".count($fldList).")");
  340. foreach ($fldList as $idZasob => $fldName) {
  341. DBG::log(">>> loop {$idZasob} => {$fldName}...");
  342. $fldType = $acl->getXsdFieldType($fldName);
  343. DBG::log(">>> loop '{$fldName}' xsdType: '{$fldType}'");
  344. if (!$acl->canReadObjectField($fldName, (object)$item)) if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("DBG: skip - !canReadObjectField('{$fldName}')"); // TODO: DBG
  345. if (!$acl->canReadObjectField($fldName, (object)$item)) continue;
  346. DBG::log(">>> loop '{$fldName}' can read...");
  347. if ($geomFld != null && $fldName == $geomFld) {
  348. $xmlWriter->startElement("{$itemPrefix}:{$fldName}");
  349. if ($showAdvancedAttrs && !$acl->canReadObjectField($fldName, (object)$item)) {
  350. $xmlWriter->writeAttribute("p5:allow_read", "false");
  351. }
  352. if ($showAdvancedAttrs && $acl->canWriteObjectField($fldName, (object)$item)) {
  353. $xmlWriter->writeAttribute("p5:allow_write", "true");
  354. }
  355. (new Api_WfsGeomTypeConverter())->createGmlFromWkt_xmlWriter($item[$fldName], $xmlWriter);
  356. $xmlWriter->endElement();// {$itemPrefix}:{$fldName}
  357. } else if (is_array($item[$fldName])) {// TODO: by struct - REF field
  358. DBG::log($item[$fldName], 'array', ">>> loop({$itemKey}) REF item[{$itemKey}][{$fldName}]");
  359. if (empty($item[$fldName])) { // SKIP empty fields
  360. if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("DBG: skip empty field '{$fldName}'"); // TODO: DBG
  361. // $xmlWriter->h($fldName);
  362. } else {
  363. if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("DBG: TODO: array field... '{$fldName}'"); // TODO: DBG
  364. // $xmlWriter->writeComment("TODO: ".$acl->getName().".{$itemKey}/{$fldName} ...");
  365. $fieldNs = str_replace(['__x3A__', ':'], '/', $fldName); // substr($xsdType, 4));
  366. if (!array_key_exists($fieldNs, $schemaCache)) {
  367. // maybe only xlinks - acl not needed
  368. $firstItem = reset($item[$fldName]);
  369. if (1 === count($firstItem) && !empty($firstItem['xlink'])) { // TODO: $schemaCache[$fieldNs] must exists for xlinks - xlmns is required
  370. foreach ($item[$fldName] as $childItem) {
  371. self::printXmlFeatureRecurse($xmlWriter, $childAcl = null, $childItem, [
  372. 'fields' => [],
  373. 'tagName' => $fldName,
  374. 'xsdAttributes' => [],
  375. 'showAdvancedAttrs' => $showAdvancedAttrs,
  376. 'outputBlobFormat' => $outputBlobFormat,
  377. ], $schemaCache, $printedFidLog);
  378. }
  379. } else {
  380. DBG::log($schemaCache, 'array', "Error Processing Request - field is not ref or missing acl ".$acl->getName().".{$itemKey}/{$fldName}");
  381. if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("Error Processing Request - field is not ref or missing acl ".$acl->getName().".{$itemKey}/{$fldName}");
  382. }
  383. } else {
  384. DBG::log($schemaCache[$fieldNs], 'array', "fetch child acl ".$acl->getName().".{$itemKey}/{$fldName}");
  385. $childAcl = Core_AclHelper::getAclByNamespace($schemaCache[$fieldNs]['namespace'], false, $schemaCache[$fieldNs]);
  386. $childName = $schemaCache[$fieldNs]['name'];
  387. foreach ($item[$fldName] as $childItem) {
  388. // DBG::log($showFields, 'array', "DBG: fld({$fldName}) \$showFields 0");
  389. $showChildFields = array_filter($showFields, function ($showFieldName) use ($fldName) {
  390. return ("{$fldName}/" === substr("{$showFieldName}", 0, strlen($fldName) + 1));
  391. });
  392. // DBG::log($showChildFields, 'array', "DBG: fld({$fldName}) \$showChildFields 1");
  393. $showChildFields = array_map(function ($showFieldName) use ($fldName) {
  394. return substr($showFieldName, strlen($fldName) + 1);
  395. }, $showChildFields);
  396. // DBG::log($showChildFields, 'array', "DBG: fld({$fldName}) \$showChildFields 2");
  397. $childPK = V::get($childAcl->getPrimaryKeyField(), '', $childItem);
  398. self::printXmlFeatureRecurse($xmlWriter, $childAcl, $childItem, [
  399. 'fields' => $showChildFields,
  400. 'tagName' => $fldName,
  401. 'xsdAttributes' => ($childPK) ? [ 'fid' => "{$childName}.{$childPK}" ] : [],
  402. 'showAdvancedAttrs' => $showAdvancedAttrs,
  403. 'outputBlobFormat' => $outputBlobFormat,
  404. ], $schemaCache, $printedFidLog);
  405. }
  406. // foreach ($item[$fldName] as $refItem) {
  407. // DBG::log($refItem, 'array', "\$refItem fld({$fldName})");
  408. // if (1 == count($refItem) && !empty($refItem['xlink'])) {
  409. // $xmlWriter->startElement($schemaCache[$fieldNs]['typeName']);
  410. // $xmlWriter->writeAttribute("xlink:href", $refItem['xlink']);
  411. // $xmlWriter->endElement();
  412. // } else {
  413. // $xmlWriter->writeComment("DBG: array field ref ... '{$fldName}'"); // TODO: DBG
  414. // $xmlWriter->startElement($schemaCache[$fieldNs]['typeName']);
  415. // foreach ($schemaCache[$fieldNs]['field'] as $field) {
  416. // if (array_key_exists($field['fieldNamespace'], $refItem)) {
  417. // $xmlWriter->writeComment("REF field ({$field['fieldNamespace']}) value({$refItem[$field['fieldNamespace']]}) TODO: get xsdType - TODO: recurse");
  418. // DBG::log($refItem[$field['fieldNamespace']], 'array', "REF field ({$field['fieldNamespace']}) TODO: get xsdType - TODO: recurse");
  419. // if (false !== strpos($field['fieldNamespace'], ':')) { // is ref - TODO: better check by xsdType
  420. // $xmlWriter->startElement($field['fieldNamespace']);
  421. // $xmlWriter->writeComment("TODO: recurse ...");
  422. // // $xmlWriter->text($refItem[$field['fieldNamespace']]);
  423. // $xmlWriter->endElement();
  424. // } else {
  425. // $xmlWriter->startElement("{$schemaCache[$fieldNs]['nsPrefix']}:{$field['fieldNamespace']}");
  426. // $xmlWriter->text($refItem[$field['fieldNamespace']]);
  427. // $xmlWriter->endElement();
  428. // }
  429. // }
  430. // }
  431. // $xmlWriter->endElement();
  432. // }
  433. // }
  434. }
  435. }
  436. } else if ('xsd:base64Binary' === $acl->getXsdFieldType($fldName)) {
  437. if (empty($item[$fldName])) continue; // && '0' !== $item[$fldName]
  438. $xmlWriter->startElement("{$itemPrefix}:{$fldName}");
  439. if ($showAdvancedAttrs && !$acl->canReadObjectField($fldName, (object)$item)) {
  440. $xmlWriter->writeAttribute("p5:allow_read", "false");
  441. }
  442. if ($showAdvancedAttrs && $acl->canWriteObjectField($fldName, (object)$item)) {
  443. $xmlWriter->writeAttribute("p5:allow_write", "true");
  444. }
  445. $pk = V::get($acl->getPrimaryKeyField(), '', $item);
  446. if ($showAdvancedAttrs) {
  447. $xmlWriter->writeAttribute("p5:blobUrl", Request::getPathUri() . "wfs-data.php?" . implode("&", [
  448. "SERVICE=WFS", "VERSION=1.0.0", "REQUEST=GetBlob",
  449. "namespace=" . $acl->getNamespace(),
  450. "primaryKey={$pk}",
  451. "fieldName={$fldName}",
  452. ]));
  453. }
  454. if (in_array($item[$fldName], ['0', '1'])) {
  455. if ('base64' === $outputBlobFormat) {
  456. $content = DB::getPDO( $acl->getDatabaseID() )->getBlob( $acl->getRootTableName(), $fldName, $acl->getPrimaryKeyField(), $pk );
  457. $xmlWriter->text(base64_encode($content));
  458. } else if ('link' === $outputBlobFormat) {
  459. $xmlWriter->text($item[$fldName]);
  460. } else { // hide
  461. throw new Exception("Not implemented outputBlobFormat - expected base64 or link");
  462. }
  463. } else {
  464. $xmlWriter->text(base64_encode($item[$fldName]));
  465. }
  466. $xmlWriter->endElement();// {$itemPrefix}:{$fldName}
  467. } else {
  468. $value = str_replace('&', '&amp;', $item[$fldName]);
  469. if (empty($value) && '0' !== $value) {
  470. continue;
  471. } else {
  472. $xmlWriter->startElement("{$itemPrefix}:{$fldName}");
  473. if ($showAdvancedAttrs && !$acl->canReadObjectField($fldName, (object)$item)) {
  474. $xmlWriter->writeAttribute("p5:allow_read", "false");
  475. }
  476. if ($showAdvancedAttrs && $acl->canWriteObjectField($fldName, (object)$item)) {
  477. $xmlWriter->writeAttribute("p5:allow_write", "true");
  478. }
  479. $xmlWriter->text($value);
  480. $xmlWriter->endElement();// {$itemPrefix}:{$fldName}
  481. }
  482. }
  483. }
  484. $xmlWriter->endElement();
  485. }
  486. }