SystemObjectFieldStorageAcl.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. Lib::loadClass('Core_AclSimpleSchemaBase');
  3. Lib::loadClass('ParseOgcFilter');
  4. Lib::loadClass('Router');
  5. class Schema_SystemObjectFieldStorageAcl extends Core_AclSimpleSchemaBase {
  6. public $_simpleSchema = [
  7. 'root' => [
  8. '@namespace' => 'default_objects/SystemObject',
  9. '@primaryKey' => 'idZasob',
  10. 'idZasob' => [ '@type' => 'xsd:integer' ],
  11. 'idDatabase' => [ '@type' => 'xsd:integer' ],
  12. 'namespace' => [ '@type' => 'xsd:string' ],
  13. '_rootTableName' => [ '@type' => 'xsd:string' ],
  14. '_type' => [ '@type' => 'xsd:string' ],
  15. 'isActive' => [ '@type' => 'xsd:integer' ], // installed
  16. 'description' => [ '@type' => 'xsd:string' ],
  17. 'name' => [ '@type' => 'p5:string' ],
  18. 'typeName' => [ '@type' => 'p5:string' ],
  19. 'reinstallLink' => [ '@type' => 'p5:www_link' ],
  20. // 'A_RECORD_CREATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'autor' ],
  21. // 'A_RECORD_CREATE_DATE' => [ '@type' => 'xsd:date' , '@label' => 'utworzono' ],
  22. // 'A_RECORD_UPDATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'zaktualizował' ],
  23. // 'A_RECORD_UPDATE_DATE' => [ '@type' => 'xsd:date', '@label' => 'zaktualizowano' ],
  24. '@key' => 'namespace'
  25. ]
  26. ];
  27. // public $_rootTableName = 'CRM_LISTA_ZASOBOW';
  28. public $_rootTableName = 'CRM_#CACHE_ACL_OBJECT_FIELD';
  29. public $_version = '1';
  30. public function _parseWhere($params = []) {
  31. $sqlWhere = [];
  32. DBG::log($params, 'array', "SystemObject::_parseWhere");
  33. if (!empty($params['#refFrom'])) {
  34. // '#refFrom' => [
  35. // 'namespace' => 'default_objects/SystemSource',
  36. // 'primaryKey' => $sourceItem['idZasob']
  37. // ]
  38. if (empty($params['#refFrom']['namespace'])) throw new Exception("Missing refFrom/namespace");
  39. if (empty($params['#refFrom']['primaryKey'])) throw new Exception("Missing refFrom/primaryKey");
  40. if ('default_objects/SystemSource' != $params['#refFrom']['namespace']) throw new Exception("Unsupported refFrom/namespace '{$params['#refFrom']['namespace']}'");
  41. $sqlWhere[] = "t.idDatabase = " . DB::getPDO()->quote($params['#refFrom']['primaryKey'], PDO::PARAM_INT);
  42. }
  43. {
  44. $filterParams = [];
  45. $xsdFields = $this->getXsdTypes();
  46. foreach ($params as $k => $v) {
  47. if ('f_' != substr($k, 0, 2)) continue;
  48. $fieldName = substr($k, 2);
  49. if (!array_key_exists($fieldName, $xsdFields)) {
  50. // TODO: check query by xpath or use different param prefix
  51. throw new Exception("Field '{$fieldName}' not found in '{$this->_namespace}'");
  52. }
  53. if ('p5:' == substr($xsdFields[$fieldName], 0, 3)) {
  54. continue;
  55. }
  56. $filterParams[$fieldName] = $v;
  57. }
  58. }
  59. if (!empty($filterParams)) {
  60. DBG::log($filterParams, 'array', "SystemObject::_parseWhere TODO \$filterParams");
  61. foreach ($filterParams as $fieldName => $value) {
  62. if (is_array($value)) {
  63. DBG::log($value, 'array', "TODO SystemObject::_parseWhere array value for \$filterParams[{$fieldName}]");
  64. } else if (is_scalar($value)) {
  65. if ('=' == substr($value, 0, 1)) {
  66. $sqlWhere[] = "t.{$fieldName} = " . DB::getPDO()->quote(substr($value, 1), PDO::PARAM_STR);
  67. } else {
  68. $sqlWhere[] = "t.{$fieldName} like " . DB::getPDO()->quote("%{$value}%", PDO::PARAM_STR);
  69. }
  70. } else {
  71. DBG::log($value, 'array', "BUG SystemObject::_parseWhere unknown type for \$filterParams[{$fieldName}]");
  72. }
  73. }
  74. }
  75. return (!empty($sqlWhere)) ? "where " . implode(" and ", $sqlWhere) : '';
  76. }
  77. public function getTotal($params = []) {
  78. $sqlWhere = $this->_parseWhere($params);
  79. return DB::getPDO()->fetchValue("
  80. select count(1) as cnt
  81. from `{$this->_rootTableName}` t
  82. {$sqlWhere}
  83. ");
  84. }
  85. public function getItems($params = []) {
  86. $sqlWhere = $this->_parseWhere($params);
  87. $currSortCol = V::get('order_by', 'idZasob', $params);
  88. $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
  89. // TODO: validate $currSortCol is in field list
  90. // TODO: validate $currSortFlip ('asc' or 'desc')
  91. $xsdFields = $this->getXsdTypes();
  92. if (!array_key_exists($currSortCol, $xsdFields)) throw new Exception("Field '{$currSortCol}' not found in '{$this->_namespace}'");
  93. if (!in_array($currSortFlip, ['asc', 'desc'])) throw new Exception("Sort dir not allowed");
  94. $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}";
  95. $limit = V::get('limit', 0, $params, 'int');
  96. $limit = ($limit < 0) ? 0 : $limit;
  97. $offset = V::get('limitstart', 0, $params, 'int');
  98. $offset = ($offset < 0) ? 0 : $offset;
  99. $sqlLimit = ($limit > 0)
  100. ? "limit {$limit} offset {$offset}"
  101. : '';
  102. return array_map(array($this, 'buildFeatureFromSqlRow'), DB::getPDO()->fetchAll("
  103. select t.*
  104. from `{$this->_rootTableName}` t
  105. {$sqlWhere}
  106. {$sqlOrderBy}
  107. {$sqlLimit}
  108. "));
  109. }
  110. public function buildFeatureFromSqlRow($item) {
  111. $exNs = explode('/', $item['namespace']);
  112. $item['name'] = array_pop($exNs);
  113. $item['typeName'] = implode('__x3A__', $exNs) . ':' . $item['name'];
  114. $item['reinstallLink'] = Router::getRoute('Storage')->getLink('objectReinstall', [ 'namespace' => $item['namespace'] ]);
  115. return $item;
  116. }
  117. }