AclSimpleSchemaBase.php 10 KB

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