ViewTableAjax.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('ProcesHelper');
  4. Lib::loadClass('TableAjax');
  5. // Lib::loadClass('Request');
  6. // Lib::loadClass('Response');
  7. Lib::loadClass('UI');
  8. class Route_ViewTableAjax extends RouteBase {
  9. public function defaultAction() {
  10. UI::gora();
  11. UI::menu();
  12. try {
  13. $typeName = V::get('typeName', '', $_GET, 'word');
  14. if (!$typeName) throw new Exception("Wrong param typeName");
  15. $acl = $this->getAclFromTypeName($typeName, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  16. $forceFilterInit = array();
  17. $filterInit = new stdClass();
  18. $filterInit->currSortCol = 'ID';
  19. $filterInit->currSortFlip = 'desc';
  20. foreach ($_GET as $k => $v) {
  21. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && !empty($v)) {// filter prefix
  22. $filterInit->$k = $v;
  23. }
  24. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && !empty($v)) {// special filter prefix
  25. $filterInit->$k = $v;
  26. }
  27. else if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  28. $fldName = substr($k, 3);
  29. $forceFilterInit[$fldName] = $v;
  30. }
  31. }
  32. $tbl = new TableAjax($acl);
  33. $tblLabel = $typeName;
  34. if ('p5_default_db:' == substr($typeName, 0, 14)) {
  35. $tblLabel = array();
  36. $zasobObj = ProcesHelper::getZasobTableInfo($acl->getID());
  37. if (!$zasobObj) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
  38. if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
  39. if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
  40. $tblLabel = implode(" - ", $tblLabel);
  41. }
  42. if (DBG::isActive() && V::get('DBG_ACL', '', $_GET)) {// test load perms
  43. Lib::loadClass('DebugExecutionTime');
  44. $dbgExecTime = new DebugExecutionTime();
  45. $dbgExecTime->activate();
  46. $dbgExecTime->log('start');
  47. UI::startContainer(['style'=>'border:1px solid red']);
  48. UI::tag('p', null, "TEST - load perms from db");
  49. $idTable = $acl->getID();
  50. UI::tag('p', null, "DBG idTable({$idTable})");
  51. $dbgExecTime->log('before sql');
  52. $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
  53. $dbgExecTime->log('after sql', ['sql']);
  54. UI::table(['caption' => "from CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW", 'rows' => $aclTableRows]);
  55. $csvIdProces = array();
  56. foreach ($aclTableRows as $row) {
  57. if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
  58. }
  59. $csvIdProces = implode(",", $csvIdProces);
  60. UI::tag('p', null, "DBG csvIdProces({$csvIdProces})");
  61. if (!empty($csvIdProces)) {
  62. $userLogin = User::getLogin();
  63. $dbgExecTime->log('before sql');
  64. $rows = DB::getPDO()->fetchAll("select ID_PROCES from `CRM_PROCES_idx_USER_to_PROCES_VIEW` where ADM_ACCOUNT = '{$userLogin}' and ID_PROCES in({$csvIdProces}) group by ID_PROCES");
  65. $dbgExecTime->log('after sql', ['sql']);
  66. UI::table(['caption' => "from CRM_PROCES_idx_USER_to_PROCES_VIEW", 'rows' => $rows]);
  67. $userIdProces = array(); foreach ($rows as $row) $userIdProces[] = $row['ID_PROCES'];
  68. $userTablePerms = array();
  69. foreach ($aclTableRows as $row) {
  70. if (!in_array($row['ID_PROCES'], $userIdProces)) continue;
  71. if (array_key_exists($row['CELL_NAME'], $userTablePerms)) {
  72. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_R' ] += $row['PERM_R'];
  73. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_W' ] += $row['PERM_W'];
  74. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_X' ] += $row['PERM_X'];
  75. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_C' ] += $row['PERM_C'];
  76. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_S' ] += $row['PERM_S'];
  77. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_O' ] += $row['PERM_O'];
  78. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_V' ] += $row['PERM_V'];
  79. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_E' ] += $row['PERM_E'];
  80. } else {
  81. $userTablePerms[ $row['CELL_NAME'] ] = $row;
  82. unset($userTablePerms[ $row['CELL_NAME'] ][ 'TABLE_DESCRIPTION' ]);
  83. unset($userTablePerms[ $row['CELL_NAME'] ][ 'ID_PROCES' ]);
  84. unset($userTablePerms[ $row['CELL_NAME'] ][ 'FORM_TREAT' ]);
  85. }
  86. }
  87. UI::table(['caption' => "\$userTablePerms", 'rows' => $userTablePerms]);
  88. } else UI::alert('warning', "brak \$csvIdProces");
  89. $dbgExecTime->printDebug();
  90. UI::endContainer();
  91. }
  92. $tbl->setLabel($tblLabel);
  93. $tbl->setFilterInit($filterInit);
  94. if (!empty($forceFilterInit)) $tbl->setForceFilterInit($forceFilterInit);
  95. $tbl->addRowFunction('edit');
  96. $tbl->addRowFunction('hist');
  97. $tbl->addRowFunction('files');
  98. $tbl->addRowFunction('cp');
  99. $tbl->addRowFunction('msgs');
  100. echo $tbl->render();
  101. } catch (Exception $e) {
  102. UI::startContainer();
  103. UI::alert('danger', "<strong>Wystąpiły błędy!</strong> " . $e->getMessage());
  104. UI::endContainer();
  105. }
  106. UI::dol();
  107. }
  108. /**
  109. * @param string $typeName - 'p5_default_db:TEST_PERMS'
  110. */
  111. public function getAclFromTypeName($typeName, $forceTblAclInit) {
  112. $userAcl = User::getAcl();
  113. $userAcl->fetchGroups();
  114. $typeEx = explode(':', $typeName);
  115. if (2 != count($typeEx)) throw new Exception("Could not get acl for '{$typeName}' - syntax error");
  116. if ('p5_' != substr($typeEx[0], 0, 3)) throw new Exception("Could not get acl for '{$typeName}' - prefix error");
  117. $sourceName = substr($typeEx[0], 3);
  118. $objName = $typeEx[1];
  119. $acl = $userAcl->getObjectAcl($sourceName, $objName);
  120. if (!$acl) throw new Exception("Could not get acl for '{$typeName}'");
  121. $acl->init($forceTblAclInit);
  122. return $acl;
  123. }
  124. }