AclSimpleSchemaBase.php 11 KB

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