AclSimpleSchemaBase.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. Lib::loadClass('Api_WfsNs');
  3. Lib::loadClass('Api_WfsException');
  4. Lib::loadClass('User');
  5. Lib::loadClass('Core_AclHelper');
  6. Lib::loadClass('Core_AclBase');
  7. class Core_AclSimpleSchemaBase extends Core_AclBase {
  8. /** simpleSchema - php structure:
  9. $simpleSchema = [
  10. 'root' => [
  11. '@namespace' => 'default_db/ZALICZKA/Zaliczka',
  12. 'ID' => 'xsd:integer', // short syntax - define only simpleType
  13. 'KWOTA' => [ // long syntax - define type with another params like restrictions
  14. '@type' => 'xsd:decimal'
  15. ],
  16. 'worker' => 'ref:Worker' // short syntax - define only type = ref
  17. 'pozycja' => [ // long syntax - define ref with maxOccurs, TODO: use more xsd attributes
  18. '@ref' => 'ZaliczkaPozycja',
  19. '@maxOccurs' => 'unbounded'
  20. ]
  21. ],
  22. 'Worker' => [
  23. '@namespace' => 'default_objects/AccessOwner',
  24. ...
  25. ],
  26. 'ZaliczkaPozycja' => [
  27. '@namespace' => 'default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja',
  28. ...
  29. ]
  30. ]
  31. */
  32. public $_simpleSchema = array();
  33. public $_xsdTypes = null;// set by parseXsdTypes()
  34. public $_name = '';
  35. public $_rootTableName = '';
  36. public $_xmlnsMap = [];
  37. public function __construct($simpleSchema = null) {
  38. if ($simpleSchema) $this->_simpleSchema = $simpleSchema;
  39. if (!$this->_simpleSchema) throw new Exception("Missing simpleSchema");
  40. if (empty($this->_simpleSchema['root'])) throw new Exception("Wrong simpleSchema syntax");
  41. if (empty($this->_simpleSchema['root']['@namespace'])) throw new Exception("Missing @namespace in simpleSchema");
  42. $ns = explode('/', $this->_simpleSchema['root']['@namespace']);
  43. $this->_name = end($ns);
  44. if (count($ns) < 2) throw new Exception("Wrong @namespace syntax in simpleSchema");
  45. $this->_rootTableName = $ns[1];
  46. {// validate and fix _simpleSchema:
  47. // - convert field scalar to [ '@type' => ... ]
  48. // - check required @namespace attribute
  49. foreach ($this->_simpleSchema as $keySchema => $schema) {
  50. foreach ($schema as $fieldName => $params) {
  51. if ('@' == substr($fieldName, 0, 1)) continue;// skip params
  52. if (is_scalar($params)) {
  53. $this->_simpleSchema[ $keySchema ][ $fieldName ] = [ '@type' => $params ];
  54. } else if (!is_array($params)) {
  55. throw new Exception("Parse error - simpleSchema field type");
  56. }
  57. }
  58. if (empty($schema['@namespace'])) throw new Exception("Missing @namespace in schema for '{$keySchema}'");
  59. $ns = explode('/', $schema['@namespace']);
  60. $name = end($ns);
  61. if (count($ns) < 2) throw new Exception("Wrong @namespace syntax in schema for '{$keySchema}'");
  62. }
  63. }
  64. // $this->parseXsdTypes();// parse xsdTypes
  65. $this->_xsdTypes = array();
  66. {
  67. $generatedIdZasob = 10000;// fake zasob id
  68. foreach ($this->_simpleSchema['root'] as $key => $value) {
  69. if ('@' == substr($key, 0, 1)) continue;// skip attributes
  70. if (is_array($value)) {
  71. $fieldName = $key;
  72. $field = [ 'name' => $fieldName, 'perms' => '', 'idZasob' => $generatedIdZasob ];
  73. if (!empty($value['@type'])) $field['xsdType'] = "{$value['@type']}";
  74. else if (!empty($value['@ref'])) {
  75. $field['xsdType'] = (false !== strpos($value['@ref'], '/'))
  76. ? "ref_uri:{$value['@ref']}"
  77. : "ref:{$value['@ref']}";
  78. }
  79. else throw new Exception("StorageAcl - field type not defined '{$key}'");
  80. if (!empty($value['@maxOccurs'])) $field['maxOccurs'] = $value['@maxOccurs'];
  81. $this->_xsdTypes[$fieldName] = $field;
  82. } else if (is_scalar($value)) {// short syntax: $fieldName => $xsdType
  83. $fieldName = $key;
  84. $field = [ 'name' => $fieldName, 'perms' => '', 'idZasob' => $generatedIdZasob ];
  85. $field['xsdType'] = $value;
  86. $this->_xsdTypes[$fieldName] = $field;
  87. } else {
  88. throw new Exception("StorageAcl - TODO: Unimplemented value type in simpleSchema: " . json_encode($value));
  89. }
  90. $generatedIdZasob++;
  91. }
  92. }
  93. // TODO: fix 'ref:*' types - use Core_AclHelper::parseNamespaceUrl($namespace)
  94. }
  95. public function __toString() {
  96. $out = "xsd @prefix(default_db__x3A__{$this->_rootTableName})" . "\n";
  97. $aliasRefUri = array();
  98. foreach ($this->_simpleSchema as $objectName => $schema) {
  99. if ('root' == $objectName) $objectName = $this->_name;
  100. $out .= "\t" . "{$objectName}";
  101. $out .= " @namespace({$schema['@namespace']})";
  102. $out .= "\n";
  103. foreach ($schema as $fieldName => $field) {
  104. if ('@' == substr($fieldName, 0, 1)) continue;// skip tags
  105. $out .= "\t\t" . "{$fieldName}";
  106. if (!empty($field['@type'])) {
  107. $out .= " @type({$field['@type']})";
  108. if (!empty($field['@alias'])) $out .= " @alias({$field['@alias']})";
  109. } else if (!empty($field['@ref'])) {
  110. $out .= " @ref({$field['@ref']})";
  111. if (false !== strpos($field['@ref'], '/')) $aliasRefUri[ $fieldName ] = $field['@ref'];
  112. } else {
  113. $out .= " @BUG('missing @type or @ref')";
  114. }
  115. // TODO: maxOccurs, nillable, etc.
  116. $out .= "\n";
  117. }
  118. foreach ($aliasRefUri as $fieldName => $nsUri) {
  119. $out .= "\t" . "{$objectName} @ref({$nsUri})" . "\n";
  120. // TODO: maxOccurs, nillable, etc.
  121. }
  122. }
  123. return $out;
  124. }
  125. public function hasSimpleSchema() { return true; }
  126. public function getSimpleSchema() { return $this->_simpleSchema; }
  127. public function getName() { return $this->_name; }
  128. public function getRootTableName() { $this->_rootTableName; }
  129. public function getXsdTypes() { return $this->_xsdTypes; }
  130. public function getNamespace() { return 'default_db/' . $this->_rootTableName . '/' . $this->_name; }
  131. public function getSourceName() { return 'default_db__x3A__' . $this->_rootTableName; }
  132. public function init($force = false) {}
  133. public function isInitialized() { return true; }
  134. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  135. public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  136. public function getVirtualFieldListByIdZasob() { return array(); }
  137. public function getRealFieldListByIdZasob($force = false) {
  138. $cols = array();
  139. foreach ($this->getFields() as $idField => $field) {
  140. $cols[$idField] = $field['name'];
  141. }
  142. return $cols;
  143. }
  144. public function getFields() {// @returns array - $this->_fields
  145. $fieldsById = array();
  146. foreach ($this->getXsdTypes() as $fieldName => $field) {
  147. $field['name'] = $fieldName;
  148. $fieldsById[ $field['idZasob'] ] = $field;
  149. }
  150. return $fieldsById;
  151. }
  152. public function getFieldType($fieldName) {
  153. foreach ($this->getFields() as $field) {
  154. if ($fieldName == $field['name']) return $field;
  155. }
  156. return null;
  157. }
  158. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  159. public function canCreateField($fieldName) {
  160. return false;// TODO: perms from Procesy
  161. }
  162. public function canReadField($fieldName) {
  163. return true;// TODO: perms from Procesy
  164. }
  165. public function canReadObjectField($fieldName, $record) {
  166. return true;// TODO: perms from Procesy
  167. }
  168. public function canWriteField($fieldName) {
  169. return false;// TODO: perms from Procesy
  170. }
  171. public function canWriteObjectField($fieldName, $record) {
  172. return false;// TODO: perms from Procesy
  173. }
  174. public function getTotal($params = array()) {// TODO: use ParseOgcQuery
  175. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  176. }
  177. public function getItem($primaryKey) {
  178. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  179. }
  180. public function getItems($params = array()) {// TODO: use ParseOgcQuery
  181. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  182. }
  183. public function fetchItemRef(&$items) {
  184. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  185. }
  186. public function addItem($itemTodo) {
  187. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  188. // return $this->parentAcl->addItem($itemTodo);
  189. }
  190. public function updateItem($itemPatch) {
  191. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  192. // return $this->parentAcl->updateItem($itemPatch);
  193. }
  194. public function getGeomFieldType($fieldName) { return null; }
  195. public function getPrimaryKeyField() { return 'ID'; }
  196. public function getID() { return 0; }
  197. public function getAttributesFromZasoby() { return array(); }
  198. public function isEnumerationField($fieldName) { return false; }
  199. public function getEnumerations($fieldName) { return null; }
  200. public function getXsdFieldType($fieldName) {
  201. $xsdTypes = $this->getXsdTypes();
  202. if (empty($xsdTypes[$fieldName])) throw new Exception("Field '{$fieldName}' not exists");
  203. return $xsdTypes[$fieldName]['xsdType'];
  204. }
  205. public function isGeomField($fldName) {
  206. if ('File' == $fieldName) return false;
  207. if ('AccessGroupRead' == $fieldName) return false;
  208. if ('AccessGroupWrite' == $fieldName) return false;
  209. if ('AccessOwner' == $fieldName) return false;
  210. // if ('NestedObjectTest' == $fieldName) return false;
  211. // return $this->parentAcl->isGeomField($fldName);
  212. }
  213. }