AclSimpleSchemaBase.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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['@label'])) $field['label'] = $value['@label'];
  85. if (!empty($value['@type'])) $field['xsdType'] = "{$value['@type']}";
  86. else if (!empty($value['@ref'])) {
  87. $field['xsdType'] = (false !== strpos($value['@ref'], '/'))
  88. ? "ref_uri:{$value['@ref']}"
  89. : "ref:{$value['@ref']}";
  90. }
  91. else throw new Exception("StorageAcl - field type not defined '{$key}'");
  92. if (!empty($value['@maxOccurs'])) $field['maxOccurs'] = $value['@maxOccurs'];
  93. $this->_xsdTypes[$fieldName] = $field;
  94. } else if (is_scalar($value)) {// short syntax: $fieldName => $xsdType
  95. $fieldName = $key;
  96. $field = [ 'name' => $fieldName, 'perms' => '', 'idZasob' => $generatedIdZasob ];
  97. $field['xsdType'] = $value;
  98. $this->_xsdTypes[$fieldName] = $field;
  99. } else {
  100. throw new Exception("StorageAcl - TODO: Unimplemented value type in simpleSchema: " . json_encode($value));
  101. }
  102. $generatedIdZasob++;
  103. }
  104. }
  105. // TODO: fix 'ref:*' types - use Core_AclHelper::parseNamespaceUrl($namespace)
  106. }
  107. public function __toString() {
  108. $out = "xsd @prefix(default_db__x3A__{$this->_rootTableName})" . "\n";
  109. $aliasRefUri = array();
  110. foreach ($this->_simpleSchema as $objectName => $schema) {
  111. if ('root' == $objectName) $objectName = $this->_name;
  112. $out .= "\t" . "{$objectName}";
  113. $out .= " @namespace({$schema['@namespace']})";
  114. $out .= "\n";
  115. foreach ($schema as $fieldName => $field) {
  116. if ('@' == substr($fieldName, 0, 1)) continue;// skip tags
  117. $out .= "\t\t" . "{$fieldName}";
  118. if (!empty($field['@type'])) {
  119. $out .= " @type({$field['@type']})";
  120. if (!empty($field['@alias'])) $out .= " @alias({$field['@alias']})";
  121. } else if (!empty($field['@ref'])) {
  122. $out .= " @ref({$field['@ref']})";
  123. if (false !== strpos($field['@ref'], '/')) $aliasRefUri[ $fieldName ] = $field['@ref'];
  124. } else {
  125. $out .= " @BUG('missing @type or @ref')";
  126. }
  127. // TODO: maxOccurs, nillable, etc.
  128. $out .= "\n";
  129. }
  130. foreach ($aliasRefUri as $fieldName => $nsUri) {
  131. $out .= "\t" . "{$objectName} @ref({$nsUri})" . "\n";
  132. // TODO: maxOccurs, nillable, etc.
  133. }
  134. }
  135. return $out;
  136. }
  137. public function hasSimpleSchema() { return true; }
  138. public function getSimpleSchema() { return $this->_simpleSchema; }
  139. public function getSimpleSchemaTree() {
  140. $tree = array();
  141. foreach ($this->_simpleSchema['root'] as $fieldName => $field) {
  142. $tree[ $fieldName ] = $field;
  143. if (is_array($field) && !empty($field['@ref'])) {
  144. $tree[ $fieldName ] = $this->_getSimpleSchemaTreeRec($field['@ref']);
  145. }
  146. }
  147. return $tree;
  148. }
  149. public function _getSimpleSchemaTreeRec($ref) {
  150. // echo "<p>DBG: F._getSimpleSchemaTreeRec({$ref})</p>";
  151. $tree = array();
  152. if (!empty($this->_simpleSchema[ $ref ])) {
  153. $tree = array();
  154. foreach ($this->_simpleSchema[ $ref ] as $fieldName => $field) {
  155. // echo "<p>DBG: F._getSimpleSchemaTreeRec({$ref}) 1/'{$fieldName}'</p>";
  156. $tree[ $fieldName ] = $field;
  157. if (is_array($field) && !empty($field['@ref'])) {
  158. $tree[ $fieldName ] = $this->_getSimpleSchemaTreeRec($field['@ref']);
  159. }
  160. }
  161. } else {
  162. $acl = Core_AclHelper::getAclByNamespace($ref);
  163. $tree[ '@namespace' ] = $acl->getNamespace();
  164. foreach ($acl->getXsdTypes() as $fieldName => $field) {
  165. // echo "<p>DBG: F._getSimpleSchemaTreeRec({$ref}) 2/'{$fieldName}'</p>";
  166. $tree[ $fieldName ] = $field;
  167. if (is_array($field) && !empty($field['@ref'])) {
  168. $tree[ $fieldName ] = $this->_getSimpleSchemaTreeRec($field['@ref']);
  169. }
  170. }
  171. }
  172. // echo'<pre>F._getSimpleSchemaTreeRec('.$ref.') tree';print_r($tree);echo'</pre>';
  173. return $tree;
  174. }
  175. public function getName() { return $this->_name; }
  176. public function getRootTableName() { return $this->_rootTableName; }
  177. public function getXsdTypes() { return $this->_xsdTypes; }
  178. public function getNamespace() { return $this->_namespace; }
  179. public function getSourceName() { return $this->_sourceNamespace; }
  180. public function init($force = false) {}
  181. public function isInitialized() { return true; }
  182. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  183. public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  184. public function getVirtualFieldListByIdZasob() { return array(); }
  185. public function getRealFieldListByIdZasob($force = false) {
  186. $cols = array();
  187. foreach ($this->getFields() as $idField => $field) {
  188. $cols[$idField] = $field['name'];
  189. }
  190. return $cols;
  191. }
  192. public function getFields() {// @returns array - $this->_fields
  193. $fieldsById = array();
  194. foreach ($this->getXsdTypes() as $fieldName => $field) {
  195. $field['name'] = $fieldName;
  196. $field['label'] = V::get('label', $fieldName, $field);
  197. if ('p5:www_link' == $field['xsdType']) $field['simpleType'] = 'p5:www_link';
  198. $fieldsById[ $field['idZasob'] ] = $field;
  199. }
  200. return $fieldsById;
  201. }
  202. public function getFieldType($fieldName) {
  203. foreach ($this->getFields() as $field) {
  204. if ($fieldName == $field['name']) return $field;
  205. }
  206. return null;
  207. }
  208. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  209. public function canCreateField($fieldName) { return false; }// TODO: perms from Procesy
  210. public function canReadField($fieldName) { return true; }// TODO: perms from Procesy
  211. public function canReadObjectField($fieldName, $record) { return true; }// TODO: perms from Procesy
  212. public function canWriteField($fieldName) { return false; }// TODO: perms from Procesy
  213. public function canWriteObjectField($fieldName, $record) { return false; }// TODO: perms from Procesy
  214. public function getTotal($params = array()) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }// TODO: use ParseOgcQuery
  215. public function getItem($primaryKey) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }
  216. public function getItems($params = array()) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }// TODO: use ParseOgcQuery
  217. public function fetchItemRef(&$items) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }
  218. public function addItem($itemTodo) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }
  219. public function updateItem($itemPatch) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }
  220. public function getGeomFieldType($fieldName) { return null; }
  221. public function getPrimaryKeyField() { return 'ID'; }
  222. public function getID() { return 0; }
  223. public function getAttributesFromZasoby() { return array(); }
  224. public function isEnumerationField($fieldName) { return false; }
  225. public function getEnumerations($fieldName) { return null; }
  226. public function getXsdFieldType($fieldName) {
  227. $xsdTypes = $this->getXsdTypes();
  228. if (empty($xsdTypes[$fieldName])) throw new Exception("Field '{$fieldName}' not exists");
  229. return $xsdTypes[$fieldName]['xsdType'];
  230. }
  231. public function isGeomField($fieldName) {
  232. if ('File' == $fieldName) return false;
  233. if ('AccessGroupRead' == $fieldName) return false;
  234. if ('AccessGroupWrite' == $fieldName) return false;
  235. if ('AccessOwner' == $fieldName) return false;
  236. // if ('NestedObjectTest' == $fieldName) return false;
  237. // return $this->parentAcl->isGeomField($fieldName);
  238. }
  239. }