WpsHelper.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. Lib::loadClass('Type_WpsProcess');
  3. Lib::loadClass('Api_OwsException');
  4. class Api_WpsHelper {
  5. static function getUserWpsProcessList($userLogin = null) {
  6. // TODO: $userLogin
  7. $defaultWpsProcessList = self::getDefaultWpsProcessList();
  8. if (array_key_exists($identifier, $defaultWpsProcessList)) return Type_WpsProcess::build( $defaultWpsProcessList[$identifier] );
  9. $wpsProcessList = [];
  10. // TODO: fetch from DB - `CRM_#CACHE_ACL_FUNCTIONS` or object like UrlActions
  11. $wpsProcessList = array_merge($defaultWpsProcessList, $wpsProcessList);
  12. return array_map(function ($wpsProcess, $identifier) {
  13. return (object)[ 'identifier' => $identifier, 'title' => $wpsProcess['title'], 'description' => $wpsProcess['description'] ];
  14. }, $wpsProcessList, array_keys($wpsProcessList));
  15. return $wpsProcessList;
  16. }
  17. static function getUserWpsProcess($identifier, $userLogin = null) {
  18. // TODO: $userLogin
  19. $defaultWpsProcessList = self::getDefaultWpsProcessList();
  20. if (array_key_exists($identifier, $defaultWpsProcessList)) return Type_WpsProcess::build( $defaultWpsProcessList[$identifier] );
  21. // TODO: fetch from DB - `CRM_#CACHE_ACL_FUNCTIONS` or object like UrlActions
  22. throw new Exception("Forbidden - wps function not exists or access denied", 403);
  23. }
  24. static function getDefaultWpsProcessList() {
  25. $defaultWpsProcessList = [];
  26. {
  27. $selectFeatureArgTypeName = [
  28. 'minOccurs' => "1", 'maxOccurs' => "1", // 1 arg is required
  29. 'identifier' => 'typeName',
  30. 'title' => "Feature typeName",
  31. 'description' => "Feature typeName eg.: 'default_db/PROBLEMS'",
  32. 'type' => 'literal',
  33. ];
  34. $selectFeatureArgPrimaryKey = [
  35. 'minOccurs' => "1", 'maxOccurs' => "unbounded", // min 1 arg is required
  36. 'identifier' => 'primaryKey',
  37. 'title' => "Feature primaryKey",
  38. 'description' => "Feature primaryKey",
  39. 'type' => 'literal',
  40. ];
  41. $selectFeatureArgFilterQuery = [
  42. 'minOccurs' => "0", 'maxOccurs' => "1", // optional max 1 arg
  43. 'identifier' => 'filterQuery',
  44. 'title' => "Query string",
  45. 'description' => "Query string eg.: 'f_ID=123&sf_Msgs=NEW_MSGS'",
  46. 'type' => 'literal',
  47. ];
  48. $selectFeatureBase = [
  49. 'dataInputs' => [
  50. $selectFeatureArgTypeName,
  51. $selectFeatureArgPrimaryKey,
  52. ],
  53. // 'MimeType' => text/xml, 'Encoding' => base64, 'Schema' => http://foo.bar/gml/3.1.0/polygon.xsd
  54. 'defaultOutput' => [ 'MimeType' => "application/json" ], // TODO: result type - like Insert Wfs Transaction result - list of: Success (primaryKey = 123), ...
  55. 'supportedOutput' => [
  56. [ 'MimeType' => "application/json" ],
  57. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.0" ],
  58. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.1" ],
  59. // [ 'MimeType' => "application/wfs-collection-1.0" ],
  60. // [ 'MimeType' => "application/wfs-collection-1.1" ],
  61. // [ 'MimeType' => "application/zip" ],
  62. ]
  63. ];
  64. $defaultWpsProcessList['p5:selectFeature'] = array_merge([ 'title' => "Select feature", 'descriptin' => "Select feature (set attribute @selected to true)" ], $selectFeatureBase);
  65. $defaultWpsProcessList['p5:unselectFeature'] = array_merge([ 'title' => "Unselect feature", 'descriptin' => "Unselect feature (set attribute @selected to false)" ], $selectFeatureBase);
  66. $defaultWpsProcessList['p5:getSelectedFeatures'] = array_merge([ 'title' => "Get selected features", 'descriptin' => "Get list of selected features (with attribute @selected = true)" ], $selectFeatureBase);
  67. $defaultWpsProcessList['p5:unselectAllFeatures'] = array_merge([ 'title' => "Unselect all features", 'descriptin' => "Unselect all features (set attribute @selected to false)" ], $selectFeatureBase, [
  68. 'dataInputs' => [
  69. $selectFeatureArgTypeName,
  70. ]
  71. ]);
  72. $defaultWpsProcessList['p5:selectAllFeaturesMatchingFilter'] = array_merge([ 'title' => "Select all features matching filter" ], $selectFeatureBase, [
  73. 'dataInputs' => [
  74. $selectFeatureArgTypeName,
  75. array_merge($selectFeatureArgPrimaryKey, [ 'minOccurs' => 0 ]), // optional arg
  76. $selectFeatureArgFilterQuery,
  77. ]
  78. ]);
  79. }
  80. array_walk($defaultWpsProcessList, function (&$wpsFunction, $identifier) {
  81. $wpsFunction['identifier'] = $identifier;
  82. });
  83. return $defaultWpsProcessList;
  84. }
  85. static function validateProcessArgs($wpsProcess, $args) {
  86. foreach ($wpsProcess->dataInputs as $input) {
  87. $identifier = $input->identifier;
  88. // TODO: required? minOccurs > 0
  89. if ($input->minOccurs > 0 && empty($args[ $identifier ])) throw new Api_OwsException("Missing value for '{$identifier}'", 501, null, 'MissingParameterValue', 'request');
  90. $totalValues = count($args[ $identifier ]);
  91. if ($totalValues > 1) {
  92. if ( 'unbounded' !== $input->maxOccurs && $totalValues > $input->maxOccurs ) {
  93. throw new Api_OwsException("Too many values for '{$identifier}'", 501, null, 'TooManyParameterValues', 'request');
  94. }
  95. }
  96. // TODO: validate values with type definition - _xsdType
  97. }
  98. }
  99. static function executeProcess($wpsProcess, $args, $responseForm = 'json') {
  100. $className = implode("_", array_merge(
  101. [
  102. 'Api',
  103. 'Process',
  104. ], array_map('ucfirst', explode(':', $wpsProcess->identifier))
  105. ) );
  106. if (!Lib::tryLoadClass($className)) {
  107. throw new Api_OwsException("Api Process not found '{$wpsProcess->identifier}'", 500, null, 'ApiProcessNotFound', 'request');
  108. }
  109. $className::run($args, $responseForm);
  110. }
  111. }