SystemObjectStorageAcl.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. Lib::loadClass('Core_AclSimpleSchemaBase');
  3. Lib::loadClass('ParseOgcFilter');
  4. class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
  5. public $_simpleSchema = [
  6. 'root' => [
  7. '@namespace' => 'default_objects/SystemObject',
  8. 'ID' => [ '@type' => 'xsd:integer' ],
  9. 'namespace' => [ '@type' => 'xsd:string' ],
  10. 'typeName' => [ '@type' => 'xsd:string' ],
  11. 'tabela' => [ '@type' => 'xsd:string', '@alias' => 'DESC' ],
  12. 'nazwa' => [ '@type' => 'xsd:string', '@alias' => 'DESC' ],
  13. 'opis' => [ '@type' => 'xsd:string', '@alias' => 'OPIS' ],
  14. 'id_zasob' => [ '@type' => 'xsd:integer' ],
  15. 'autor' => [ '@type' => 'xsd:string' , '@alias' => 'A_RECORD_CREATE_AUTHOR' ],
  16. 'utworzono' => [ '@type' => 'xsd:date' , '@alias' => 'A_RECORD_CREATE_DATE' ],
  17. 'zaktualizował' => [ '@type' => 'xsd:string' , '@alias' => 'A_RECORD_UPDATE_AUTHOR' ],
  18. 'zaktualizowano' => [ '@type' => 'xsd:date', '@alias' => 'A_RECORD_UPDATE_DATE' ]
  19. ]
  20. ];
  21. public $_rootTableName = 'CRM_LISTA_ZASOBOW';
  22. public function getTotal($params = []) {
  23. return count($this->_getAllItems());
  24. }
  25. public function getItems($params = []) {
  26. $items = $this->_getAllItems();
  27. $currSortCol = V::get('order_by', 'ID', $params);
  28. $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
  29. // TODO: validate $currSortCol is in field list
  30. // TODO: validate $currSortFlip ('asc' or 'desc')
  31. $aliasMap = array();
  32. foreach ($this->_simpleSchema['root'] as $key => $field) {
  33. if ('@' === substr($key, 0, 1)) continue;
  34. $aliasMap[ $key ] = $key;// (!empty($field['@alias'])) ? $field['@alias'] : $key;
  35. }
  36. // TODO: if (!array_key_exists($currSortCol, $aliasMap)) throw new Exception("field name not allowed to sort");
  37. $currSortCol = (array_key_exists($currSortCol, $aliasMap)) ? $aliasMap[$currSortCol] : null;
  38. if (!empty($currSortCol) && ('asc' == $currSortFlip || 'desc' == $currSortFlip)) {
  39. usort($items, function ($itemA, $itemB) use ($currSortCol, $currSortFlip) {
  40. $a = strtolower(V::get($currSortCol, '', $itemA));
  41. $b = strtolower(V::get($currSortCol, '', $itemB));
  42. if ($a == $b) return 0;
  43. else if ('asc' == $currSortFlip) return ($a < $b) ? -1 : 1;
  44. else if ('desc' == $currSortFlip) return ($a > $b) ? -1 : 1;
  45. throw new Exception("BUG - Wrong sort param - order dir");
  46. });
  47. }
  48. $limit = V::get('limit', 0, $params);
  49. $limit = ($limit < 0) ? 0 : $limit;
  50. $offset = V::get('limitstart', 0, $params);
  51. $offset = ($offset < 0) ? 0 : $offset;
  52. return array_slice($items, $offset, ($limit > 0) ? $limit : null, $preserve_keys = true);
  53. }
  54. public function _generateUniqueKeyFromNamespace($namespace) {
  55. return str_replace('/', '__x3A__', $namespace);
  56. }
  57. public function _getAllItems($params = []) {
  58. static $_cacheAllItems = null;
  59. if (null !== $_cacheAllItems) return $_cacheAllItems;
  60. $idMainDatabase = DB::getPDO()->getZasobId();
  61. $_cacheAllItems = array_filter(
  62. array_merge(
  63. array_map(
  64. function ($row) {
  65. $row['namespace'] = "default_db/{$row['tabela']}";
  66. $row['typeName'] = "default_db:{$row['tabela']}";
  67. $row['ID'] = $this->_generateUniqueKeyFromNamespace($row['namespace']);
  68. return $row;
  69. }
  70. , DB::getPDO()->fetchAll("
  71. select z.ID as id_zasob
  72. , z.`DESC` as tabela
  73. , IF(z.`DESC_PL` != '', z.`DESC_PL`, z.`DESC`) as nazwa
  74. , z.`OPIS` as opis
  75. , z.A_RECORD_CREATE_AUTHOR as `autor`
  76. , z.A_RECORD_CREATE_DATE as `utworzono`
  77. , z.A_RECORD_UPDATE_AUTHOR as `zaktualizował`
  78. , z.A_RECORD_UPDATE_DATE as `zaktualizowano`
  79. from `CRM_LISTA_ZASOBOW` z
  80. where z.PARENT_ID = {$idMainDatabase}
  81. and z.`TYPE` = 'TABELA'
  82. and z.`A_STATUS` not in('DELETED', 'OFF_HARD', 'OFF_SOFT')
  83. ")
  84. )
  85. , array_map(
  86. function ($typeName) {
  87. $namespace = str_replace(':', '/', $typeName);
  88. $namespace = str_replace('__x3A__', '/', $namespace);
  89. return [
  90. 'ID' => $this->_generateUniqueKeyFromNamespace($namespace),
  91. 'namespace' => $namespace,
  92. 'typeName' => $typeName,
  93. 'tabela' => '', // TODO: $acl->getRootTableName(),
  94. 'nazwa' => substr($typeName, strrpos($typeName, ':') + 1),
  95. 'opis' => '...',
  96. 'autor' => '',
  97. 'utworzono' => '',
  98. 'zaktualizował' => '',
  99. 'zaktualizowano' => ''
  100. ];
  101. }
  102. , Core_AclHelper::getAclList()
  103. )
  104. )
  105. , function ($item) use ($params) {
  106. return true;
  107. }
  108. );
  109. return $_cacheAllItems;
  110. }
  111. }