SystemObjectStorageAcl.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 ($limit > 0)
  53. ? $this->_fixKeyItems(array_splice($items, $offset, $limit))
  54. : $this->_fixKeyItems(array_splice($items, $offset));
  55. }
  56. public function _fixKeyItems($items) {
  57. $fixed = array();
  58. foreach ($items as $item) {
  59. $fixed[ $this->_generateUniqueKeyFromNamespace($item['namespace']) ] = $item;
  60. }
  61. return $fixed;
  62. }
  63. public function _generateUniqueKeyFromNamespace($namespace) {
  64. return str_replace('/', '__x3A__', $namespace);
  65. }
  66. public function _getAllItems($params = []) {
  67. static $_cacheAllItems = null;
  68. if (null !== $_cacheAllItems) return $_cacheAllItems;
  69. $idMainDatabase = DB::getPDO()->getZasobId();
  70. $_cacheAllItems = array_filter(
  71. array_merge(
  72. array_map(
  73. function ($row) {
  74. $row['namespace'] = "default_db/{$row['tabela']}";
  75. $row['typeName'] = "default_db:{$row['tabela']}";
  76. $row['ID'] = $this->_generateUniqueKeyFromNamespace($row['namespace']);
  77. return $row;
  78. }
  79. , DB::getPDO()->fetchAll("
  80. select z.ID as id_zasob
  81. , z.`DESC` as tabela
  82. , IF(z.`DESC_PL` != '', z.`DESC_PL`, z.`DESC`) as nazwa
  83. , z.`OPIS` as opis
  84. , z.A_RECORD_CREATE_AUTHOR as `autor`
  85. , z.A_RECORD_CREATE_DATE as `utworzono`
  86. , z.A_RECORD_UPDATE_AUTHOR as `zaktualizował`
  87. , z.A_RECORD_UPDATE_DATE as `zaktualizowano`
  88. from `CRM_LISTA_ZASOBOW` z
  89. where z.PARENT_ID = {$idMainDatabase}
  90. and z.`TYPE` = 'TABELA'
  91. and z.`A_STATUS` not in('DELETED', 'OFF_HARD', 'OFF_SOFT')
  92. ")
  93. )
  94. , array_map(
  95. function ($typeName) {
  96. $namespace = str_replace(':', '/', $typeName);
  97. $namespace = str_replace('__x3A__', '/', $namespace);
  98. return [
  99. 'ID' => $this->_generateUniqueKeyFromNamespace($namespace),
  100. 'namespace' => $namespace,
  101. 'typeName' => $typeName,
  102. 'tabela' => '', // TODO: $acl->getRootTableName(),
  103. 'nazwa' => substr($typeName, strrpos($typeName, ':') + 1),
  104. 'opis' => '...',
  105. 'autor' => '',
  106. 'utworzono' => '',
  107. 'zaktualizował' => '',
  108. 'zaktualizowano' => ''
  109. ];
  110. }
  111. , Core_AclHelper::getAclList()
  112. )
  113. )
  114. , function ($item) use ($params) {
  115. return true;
  116. }
  117. );
  118. return $_cacheAllItems;
  119. }
  120. }