AclSimpleSchemaBase.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. '@primaryKey' => 'ID',
  14. 'ID' => 'xsd:integer', // short syntax - define only simpleType
  15. 'KWOTA' => [ // long syntax - define type with another params like restrictions
  16. '@type' => 'xsd:decimal'
  17. ],
  18. 'worker' => 'ref:Worker' // short syntax - define only type = ref
  19. 'pozycja' => [ // long syntax - define ref with maxOccurs, TODO: use more xsd attributes
  20. '@ref' => 'ZaliczkaPozycja',
  21. '@maxOccurs' => 'unbounded'
  22. ]
  23. ],
  24. 'Worker' => [
  25. '@namespace' => 'default_objects/AccessOwner',
  26. ...
  27. ],
  28. 'ZaliczkaPozycja' => [
  29. '@namespace' => 'default_db/ZALICZKA_POZYCJA/ZaliczkaPozycja',
  30. ...
  31. ]
  32. 'YT_LINK' => [ '@type' => 'p5:typeSpecialSimpleLink',
  33. '@label' => "Youtube link",
  34. '@@params' => [// $acl->getXsdFieldParam($col, 'format');
  35. 'format' => '<a href="https://www.youtube.com/watch?v={LINK}" target="_blank"></a>',
  36. 'aliasMap' => [
  37. 'LINK' => 'LINK'
  38. ]
  39. ]
  40. ],
  41. ]
  42. */
  43. public $_simpleSchema = array();
  44. public $_xsdTypes = null;// set by parseXsdTypes()
  45. public $_name = '';
  46. public $_namespace = '';
  47. public $_primaryKey = '';
  48. public $_rootTableName = '';
  49. public $_sourceNamespace = '';
  50. public $_xmlnsMap = [];
  51. public function __construct($simpleSchema = null) {
  52. if ($simpleSchema) $this->_simpleSchema = $simpleSchema;
  53. if (!$this->_simpleSchema) throw new Exception("Missing simpleSchema");
  54. if (empty($this->_simpleSchema['root'])) throw new Exception("Wrong simpleSchema syntax");
  55. if (empty($this->_simpleSchema['root']['@namespace'])) throw new Exception("Missing @namespace in simpleSchema");
  56. $this->_namespace = $this->_simpleSchema['root']['@namespace'];
  57. $ns = explode('/', $this->_namespace);
  58. $this->_name = end($ns);
  59. if (empty($this->_rootTableName)) {
  60. if (count($ns) < 3) throw new Exception("Wrong @namespace syntax in simpleSchema ns({$this->_simpleSchema['root']['@namespace']})");
  61. $this->_rootTableName = $ns[1];
  62. }
  63. {
  64. $this->_sourceNamespace = explode('/', $this->_namespace);
  65. array_pop($this->_sourceNamespace);// remove name
  66. $this->_sourceNamespace = implode('__x3A__', $this->_sourceNamespace);
  67. }
  68. if (empty($this->_simpleSchema['root']['@primaryKey'])) $this->_simpleSchema['root']['@primaryKey'] = 'ID';// TODO: throw new Exception("Missing @primaryKey in simpleSchema '{$this->_name}'");
  69. $this->_primaryKey = $this->_simpleSchema['root']['@primaryKey'];// TODO: check if field exists
  70. {// validate and fix _simpleSchema:
  71. // - convert field scalar to [ '@type' => ... ]
  72. // - check required @namespace attribute
  73. foreach ($this->_simpleSchema as $keySchema => $schema) {
  74. foreach ($schema as $fieldName => $params) {
  75. if ('@' == substr($fieldName, 0, 1)) continue;// skip params
  76. if (is_scalar($params)) {
  77. $this->_simpleSchema[ $keySchema ][ $fieldName ] = [ '@type' => $params ];
  78. } else if (!is_array($params)) {
  79. throw new Exception("Parse error - simpleSchema field type");
  80. }
  81. }
  82. if (empty($schema['@namespace'])) throw new Exception("Missing @namespace in schema for '{$keySchema}'");
  83. $ns = explode('/', $schema['@namespace']);
  84. $name = end($ns);
  85. if (count($ns) < 2) throw new Exception("Wrong @namespace syntax in schema for '{$keySchema}'");
  86. }
  87. }
  88. // $this->parseXsdTypes();// parse xsdTypes
  89. $this->_xsdTypes = array();
  90. {
  91. $generatedIdZasob = 10000;// fake zasob id
  92. foreach ($this->_simpleSchema['root'] as $key => $value) {
  93. if ('@' == substr($key, 0, 1)) continue;// skip attributes
  94. if (is_array($value)) {
  95. $fieldName = $key;
  96. $field = [ 'name' => $fieldName, 'perms' => '', 'idZasob' => $generatedIdZasob ];
  97. if (!empty($value['@label'])) $field['label'] = $value['@label'];
  98. if (!empty($value['@type'])) $field['xsdType'] = "{$value['@type']}";
  99. else if (!empty($value['@ref'])) {
  100. $field['xsdType'] = (false !== strpos($value['@ref'], '/'))
  101. ? "ref_uri:{$value['@ref']}"
  102. : "ref:{$value['@ref']}";
  103. }
  104. else throw new Exception("StorageAcl - field type not defined '{$key}'");
  105. if (!empty($value['@maxOccurs'])) $field['maxOccurs'] = $value['@maxOccurs'];
  106. $this->_xsdTypes[$fieldName] = $field;
  107. } else if (is_scalar($value)) {// short syntax: $fieldName => $xsdType
  108. $fieldName = $key;
  109. $field = [ 'name' => $fieldName, 'perms' => '', 'idZasob' => $generatedIdZasob ];
  110. $field['xsdType'] = $value;
  111. $this->_xsdTypes[$fieldName] = $field;
  112. } else {
  113. throw new Exception("StorageAcl - TODO: Unimplemented value type in simpleSchema: " . json_encode($value));
  114. }
  115. $generatedIdZasob++;
  116. }
  117. }
  118. // TODO: fix 'ref:*' types - use Core_AclHelper::parseNamespaceUrl($namespace)
  119. }
  120. public function __toString() {
  121. $out = "xsd @prefix(default_db__x3A__{$this->_rootTableName})" . "\n";
  122. $aliasRefUri = array();
  123. foreach ($this->_simpleSchema as $objectName => $schema) {
  124. if ('root' == $objectName) $objectName = $this->_name;
  125. $out .= "\t" . "{$objectName}";
  126. $out .= " @namespace({$schema['@namespace']})";
  127. $out .= "\n";
  128. foreach ($schema as $fieldName => $field) {
  129. if ('@' == substr($fieldName, 0, 1)) continue;// skip tags
  130. $out .= "\t\t" . "{$fieldName}";
  131. if (!empty($field['@type'])) {
  132. $out .= " @type({$field['@type']})";
  133. if (!empty($field['@alias'])) $out .= " @alias({$field['@alias']})";
  134. } else if (!empty($field['@ref'])) {
  135. $out .= " @ref({$field['@ref']})";
  136. if (false !== strpos($field['@ref'], '/')) $aliasRefUri[ $fieldName ] = $field['@ref'];
  137. } else {
  138. $out .= " @BUG('missing @type or @ref')";
  139. }
  140. // TODO: maxOccurs, nillable, etc.
  141. $out .= "\n";
  142. }
  143. foreach ($aliasRefUri as $fieldName => $nsUri) {
  144. $out .= "\t" . "{$objectName} @ref({$nsUri})" . "\n";
  145. // TODO: maxOccurs, nillable, etc.
  146. }
  147. }
  148. return $out;
  149. }
  150. public function hasSimpleSchema() { return true; }
  151. public function getSimpleSchema() { return $this->_simpleSchema; }
  152. public function getSimpleSchemaTree() {
  153. $tree = array();
  154. foreach ($this->_simpleSchema['root'] as $fieldName => $field) {
  155. $tree[ $fieldName ] = $field;
  156. if (is_array($field) && !empty($field['@ref'])) {
  157. $tree[ $fieldName ] = $this->_getSimpleSchemaTreeRec($field['@ref']);
  158. }
  159. }
  160. return $tree;
  161. }
  162. public function _getSimpleSchemaTreeRec($ref) {
  163. // echo "<p>DBG: F._getSimpleSchemaTreeRec({$ref})</p>";
  164. $tree = array();
  165. if (!empty($this->_simpleSchema[ $ref ])) {
  166. $tree = array();
  167. foreach ($this->_simpleSchema[ $ref ] as $fieldName => $field) {
  168. // echo "<p>DBG: F._getSimpleSchemaTreeRec({$ref}) 1/'{$fieldName}'</p>";
  169. $tree[ $fieldName ] = $field;
  170. if (is_array($field) && !empty($field['@ref'])) {
  171. $tree[ $fieldName ] = $this->_getSimpleSchemaTreeRec($field['@ref']);
  172. }
  173. }
  174. } else {
  175. $acl = Core_AclHelper::getAclByNamespace($ref);
  176. $tree[ '@namespace' ] = $acl->getNamespace();
  177. foreach ($acl->getXsdTypes() as $fieldName => $field) {
  178. // echo "<p>DBG: F._getSimpleSchemaTreeRec({$ref}) 2/'{$fieldName}'</p>";
  179. $tree[ $fieldName ] = $field;
  180. if (is_array($field) && !empty($field['@ref'])) {
  181. $tree[ $fieldName ] = $this->_getSimpleSchemaTreeRec($field['@ref']);
  182. }
  183. }
  184. }
  185. // echo'<pre>F._getSimpleSchemaTreeRec('.$ref.') tree';print_r($tree);echo'</pre>';
  186. return $tree;
  187. }
  188. public function getName() { return $this->_name; }
  189. public function getRootTableName() { return $this->_rootTableName; }
  190. public function getXsdTypes() { return $this->_xsdTypes; }
  191. public function getNamespace() { return $this->_namespace; }
  192. public function getSourceName() { return $this->_sourceNamespace; }
  193. public function init($force = false) {}
  194. public function isInitialized() { return true; }
  195. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  196. public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  197. public function getVirtualFieldListByIdZasob() { return array(); }
  198. public function getRealFieldListByIdZasob($force = false) {
  199. $cols = array();
  200. foreach ($this->getFields() as $idField => $field) {
  201. $cols[$idField] = $field['name'];
  202. }
  203. return $cols;
  204. }
  205. public function getFields() {// @returns array - $this->_fields
  206. $fieldsById = array();
  207. foreach ($this->getXsdTypes() as $fieldName => $field) {
  208. $field['name'] = $fieldName;
  209. $field['label'] = V::get('label', $fieldName, $field);
  210. if ('p5:www_link' == $field['xsdType']) $field['simpleType'] = 'p5:www_link';
  211. $fieldsById[ $field['idZasob'] ] = $field;
  212. }
  213. return $fieldsById;
  214. }
  215. public function getFieldType($fieldName) {
  216. foreach ($this->getFields() as $field) {
  217. if ($fieldName == $field['name']) return $field;
  218. }
  219. return null;
  220. }
  221. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  222. public function canCreateField($fieldName) { return false; }// TODO: perms from Procesy
  223. public function canReadField($fieldName) { return true; }// TODO: perms from Procesy
  224. public function canReadObjectField($fieldName, $record) { return true; }// TODO: perms from Procesy
  225. public function canWriteField($fieldName) { return false; }// TODO: perms from Procesy
  226. public function canWriteObjectField($fieldName, $record) { return false; }// TODO: perms from Procesy
  227. public function getTotal($params = array()) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }// TODO: use ParseOgcQuery
  228. public function getItem($primaryKey) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }
  229. public function getItems($params = array()) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }// TODO: use ParseOgcQuery
  230. public function fetchItemRef(&$items) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }// TODO: , $fieldName = ''
  231. public function fetchItemFieldRefs($primaryKey, $fieldName) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }
  232. public function addItem($itemTodo) { throw new Exception("Unimplemented - TODO: " . get_class($this) . "::" . __FUNCTION__); }
  233. public function updateItem($itemPatch) { throw new Exception("Unimplemented - TODO: F." . __FUNCTION__); }
  234. public function getGeomFieldType($fieldName) { return null; }
  235. public function getPrimaryKeyField() { return $this->_primaryKey; }
  236. public function getSqlPrimaryKeyField() {
  237. return (!empty($this->_simpleSchema['root'][$this->_primaryKey]['@alias']))
  238. ? $this->_simpleSchema['root'][$this->_primaryKey]['@alias']
  239. : $this->_primaryKey;
  240. }
  241. public function getSqlFieldName($fieldName) {
  242. if (empty($this->_simpleSchema['root'][$fieldName])) throw new Exception("Missing field in schema '{$fieldName}'");
  243. return (!empty($this->_simpleSchema['root'][$fieldName]['@alias']))
  244. ? $this->_simpleSchema['root'][$fieldName]['@alias']
  245. : $fieldName;
  246. }
  247. public function getID() { return 0; }
  248. public function getAttributesFromZasoby() { return array(); }
  249. public function isEnumerationField($fieldName) { return false; }
  250. public function getEnumerations($fieldName) { return null; }
  251. public function getXsdFieldType($fieldName) {
  252. $xsdTypes = $this->getXsdTypes();
  253. if (empty($xsdTypes[$fieldName])) throw new Exception("Field '{$fieldName}' not exists");
  254. return $xsdTypes[$fieldName]['xsdType'];
  255. }
  256. public function isGeomField($fieldName) {
  257. if ('File' == $fieldName) return false;
  258. if ('AccessGroupRead' == $fieldName) return false;
  259. if ('AccessGroupWrite' == $fieldName) return false;
  260. if ('AccessOwner' == $fieldName) return false;
  261. // if ('NestedObjectTest' == $fieldName) return false;
  262. // return $this->parentAcl->isGeomField($fieldName);
  263. }
  264. public function generateSqlSelectFromRootTable($prefix = 't') {
  265. $sqlSelect = [];
  266. foreach ($this->_simpleSchema['root'] as $key => $field) {
  267. if ('@' == substr($key, 0, 1)) continue;// skip attr
  268. if (!empty($field['@ref'])) continue;// skip ref
  269. if (empty($field['@type'])) continue;// skip wrong simpleType structure - BUG
  270. if ('xsd:' != substr($field['@type'], 0, 4)) continue;// skip non xsd types - eg. p5:typeSpecialSimpleLink
  271. $sqlField = (!empty($field['@alias'])) ? $field['@alias'] : $key;
  272. $sqlSelect[] = "{$prefix}.`{$sqlField}` as `$key`";
  273. }
  274. return implode("\n, ", $sqlSelect);
  275. }
  276. public function getXsdFieldParam($fieldName, $paramKey) {
  277. if (empty($this->_simpleSchema['root'][$fieldName])) return null;
  278. if (empty($this->_simpleSchema['root'][$fieldName]['@@params'])) return null;
  279. if (empty($this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey])) return null;
  280. return $this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey];
  281. }
  282. public function addP5Types(&$item, $key) {
  283. DBG::_('DBG_ACL', '>1', "\$item", $item, __CLASS__, __FUNCTION__, __LINE__);
  284. $sqlSelect = [];
  285. foreach ($this->_simpleSchema['root'] as $key => $field) {
  286. if ('@' == substr($key, 0, 1)) continue;// skip attr
  287. if (empty($field['@type'])) continue;// skip ref
  288. if ('p5:' != substr($field['@type'], 0, 3)) continue;// skip non p5 types
  289. $fieldName = $key;
  290. $item[$fieldName] = '';
  291. switch ($field['@type']) {
  292. case 'p5:typeSpecialSimpleLink': {
  293. // '@@params' => [// $acl->getXsdFieldParam($col, 'format');
  294. // 'format' => '<a href="https://www.youtube.com/watch?v={LINK}" target="_blank"></a>',
  295. // 'aliasMap' => [
  296. // 'LINK' => 'LINK'
  297. // ]
  298. // ]
  299. $link = $this->getXsdFieldParam($fieldName, 'format');
  300. // $.each(_fieldProps._tsSimpleLink.aliasMap, function(i, v) {
  301. // //console.log('simpleLink aliasMap columnName:', columnName, 'i:', i, 'v:', v, 'props['+v+']', props[v], 'val', val, 'typeof val', typeof val);
  302. // if (undefined !== row[v]) {
  303. // valLink = valLink.replace(new RegExp('\{' + i + '\}', 'g'), row[v]);
  304. // }
  305. // });
  306. foreach ($this->getXsdFieldParam($fieldName, 'aliasMap') as $itemFieldName => $alias) {
  307. DBG::_('DBG_ACL', '>1', "aliasMap({$itemFieldName} => {$alias})", $item[$itemFieldName], __CLASS__, __FUNCTION__, __LINE__);
  308. $link = str_replace("{{$alias}}", $item[$itemFieldName], $link);
  309. }
  310. $item[$fieldName] = $link;
  311. } break;
  312. }
  313. }
  314. }
  315. }