AclSimpleSchemaBase.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 getSimpleSchemaTree() {
  128. $tree = array();
  129. foreach ($this->_simpleSchema['root'] as $fieldName => $field) {
  130. $tree[ $fieldName ] = $field;
  131. if (is_array($field) && !empty($field['@ref'])) {
  132. $tree[ $fieldName ] = $this->_getSimpleSchemaTreeRec($field['@ref']);
  133. }
  134. }
  135. return $tree;
  136. }
  137. public function _getSimpleSchemaTreeRec($ref) {
  138. // echo "<p>DBG: F._getSimpleSchemaTreeRec({$ref})</p>";
  139. $tree = array();
  140. if (!empty($this->_simpleSchema[ $ref ])) {
  141. $tree = array();
  142. foreach ($this->_simpleSchema[ $ref ] as $fieldName => $field) {
  143. // echo "<p>DBG: F._getSimpleSchemaTreeRec({$ref}) 1/'{$fieldName}'</p>";
  144. $tree[ $fieldName ] = $field;
  145. if (is_array($field) && !empty($field['@ref'])) {
  146. $tree[ $fieldName ] = $this->_getSimpleSchemaTreeRec($field['@ref']);
  147. }
  148. }
  149. } else {
  150. $acl = Core_AclHelper::getAclByNamespace($ref);
  151. $tree[ '@namespace' ] = $acl->getNamespace();
  152. foreach ($acl->getXsdTypes() as $fieldName => $field) {
  153. // echo "<p>DBG: F._getSimpleSchemaTreeRec({$ref}) 2/'{$fieldName}'</p>";
  154. $tree[ $fieldName ] = $field;
  155. if (is_array($field) && !empty($field['@ref'])) {
  156. $tree[ $fieldName ] = $this->_getSimpleSchemaTreeRec($field['@ref']);
  157. }
  158. }
  159. }
  160. // echo'<pre>F._getSimpleSchemaTreeRec('.$ref.') tree';print_r($tree);echo'</pre>';
  161. return $tree;
  162. }
  163. public function getName() { return $this->_name; }
  164. public function getRootTableName() { $this->_rootTableName; }
  165. public function getXsdTypes() { return $this->_xsdTypes; }
  166. public function getNamespace() { return 'default_db/' . $this->_rootTableName . '/' . $this->_name; }
  167. public function getSourceName() { return 'default_db__x3A__' . $this->_rootTableName; }
  168. public function init($force = false) {}
  169. public function isInitialized() { return true; }
  170. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  171. public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  172. public function getVirtualFieldListByIdZasob() { return array(); }
  173. public function getRealFieldListByIdZasob($force = false) {
  174. $cols = array();
  175. foreach ($this->getFields() as $idField => $field) {
  176. $cols[$idField] = $field['name'];
  177. }
  178. return $cols;
  179. }
  180. public function getFields() {// @returns array - $this->_fields
  181. $fieldsById = array();
  182. foreach ($this->getXsdTypes() as $fieldName => $field) {
  183. $field['name'] = $fieldName;
  184. $fieldsById[ $field['idZasob'] ] = $field;
  185. }
  186. return $fieldsById;
  187. }
  188. public function getFieldType($fieldName) {
  189. foreach ($this->getFields() as $field) {
  190. if ($fieldName == $field['name']) return $field;
  191. }
  192. return null;
  193. }
  194. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  195. public function canCreateField($fieldName) {
  196. return false;// TODO: perms from Procesy
  197. }
  198. public function canReadField($fieldName) {
  199. return true;// TODO: perms from Procesy
  200. }
  201. public function canReadObjectField($fieldName, $record) {
  202. return true;// TODO: perms from Procesy
  203. }
  204. public function canWriteField($fieldName) {
  205. return false;// TODO: perms from Procesy
  206. }
  207. public function canWriteObjectField($fieldName, $record) {
  208. return false;// TODO: perms from Procesy
  209. }
  210. public function getTotal($params = array()) {// TODO: use ParseOgcQuery
  211. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  212. }
  213. public function getItem($primaryKey) {
  214. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  215. }
  216. public function getItems($params = array()) {// TODO: use ParseOgcQuery
  217. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  218. }
  219. public function fetchItemRef(&$items) {
  220. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  221. }
  222. public function addItem($itemTodo) {
  223. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  224. // return $this->parentAcl->addItem($itemTodo);
  225. }
  226. public function updateItem($itemPatch) {
  227. throw new Exception("Unimplemented - TODO: F." . __FUNCTION__);
  228. // return $this->parentAcl->updateItem($itemPatch);
  229. }
  230. public function getGeomFieldType($fieldName) { return null; }
  231. public function getPrimaryKeyField() { return 'ID'; }
  232. public function getID() { return 0; }
  233. public function getAttributesFromZasoby() { return array(); }
  234. public function isEnumerationField($fieldName) { return false; }
  235. public function getEnumerations($fieldName) { return null; }
  236. public function getXsdFieldType($fieldName) {
  237. $xsdTypes = $this->getXsdTypes();
  238. if (empty($xsdTypes[$fieldName])) throw new Exception("Field '{$fieldName}' not exists");
  239. return $xsdTypes[$fieldName]['xsdType'];
  240. }
  241. public function isGeomField($fldName) {
  242. if ('File' == $fieldName) return false;
  243. if ('AccessGroupRead' == $fieldName) return false;
  244. if ('AccessGroupWrite' == $fieldName) return false;
  245. if ('AccessOwner' == $fieldName) return false;
  246. // if ('NestedObjectTest' == $fieldName) return false;
  247. // return $this->parentAcl->isGeomField($fldName);
  248. }
  249. }