ViewTableAjax.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. public function revertFromHistAjaxAction() {
  109. Response::sendTryCatchJson(array($this, 'revertFromHistAjax'));
  110. }
  111. public function revertFromHistAjax() {
  112. $typeName = V::get('typeName', '', $_REQUEST, 'word');
  113. if (!$typeName) throw new Exception("Wrong param typeName");
  114. $id = V::get('ID', '', $_REQUEST, 'word');
  115. if (!$id) throw new Exception("Wrong param ID");
  116. $idHist = V::get('idHist', '', $_REQUEST, 'word');
  117. if (!$idHist) throw new Exception("Wrong param idHist");
  118. $fieldName = V::get('fieldName', '', $_REQUEST, 'word');
  119. if (!$fieldName) throw new Exception("Wrong param fieldName");
  120. $acl = $this->getAclFromTypeName($typeName);
  121. $item = $acl->getItem($id);
  122. if (!$item) throw new HttpException("Item not found", 404);
  123. if (!$acl->canWriteObjectField($fieldName, $record)) throw new Exception("Missing perm Write for field {$fieldName}");
  124. $histItem = $acl->getHistItem($id, $idHist);
  125. if (!$histItem) throw new HttpException("Hist Item not found", 404);
  126. $histValue = V::get($fieldName, 'N/S;', $histItem);
  127. if ('N/S;' == $histValue) throw new Exception("Missing field value in hist[{$idHist}] for field({$fieldName}) from item[{$id}]");
  128. if ($acl->isGeomField($fieldName)) {
  129. $wktType = strtoupper($acl->getGeomFieldType($fieldName));
  130. if (!$wktType) throw new Exception("Wrong geometry type for field {$fieldName}");
  131. if ($wktType != strtoupper(substr($histValue, 0, strlen($wktType)))) throw new Exception("Wrong geometry type for field {$fieldName} in hist value");
  132. $coords = trim(substr($histValue, strlen($wktType)), '()');
  133. $wktValue = $acl->convertGmlCoordsToWkt($wktType, $coords, ['cs'=>' ', 'ts'=>',']);
  134. if (!$wktValue) throw new Exception("BUG in hist record");
  135. $sqlObj = array();
  136. $sqlObj['ID'] = $id;
  137. $sqlObj[$fieldName] = "GeomFromText('{$wktValue}')";
  138. $affected = DB::getDB()->UPDATE_OBJ($acl->getName(), (object)$sqlObj);
  139. if (0 == $affected) throw new AlertInfoException("Nie wprowadzono żadnych zmian");
  140. else if ($affected < 0) throw new Exception("Wystąpiły błędy podczas aktualizacji rekordu [{$id}]");
  141. $jsonResponse = array();
  142. $jsonResponse['type'] = 'success';
  143. $jsonResponse['msg'] = "Zaktualizowano dane na podstawie wcześniejszej wartości dla rekordu [{$id}]";
  144. $jsonResponse['actions'] = array();
  145. $jsonResponse['actions'][] = ['jsFunction'=>'TableAjax__HIST_Route', 'args'=>[$id]];
  146. return $jsonResponse;
  147. } else {
  148. throw new HttpException("Not implemented - update from hist only for the geom field", 501);
  149. }
  150. throw new Exception("BUG: update field '{$fieldName}' in item[{$id}] from hist[{$idHist}]", 501);
  151. }
  152. /**
  153. * @param string $typeName - 'p5_default_db:TEST_PERMS'
  154. */
  155. public function getAclFromTypeName($typeName, $forceTblAclInit) {
  156. $userAcl = User::getAcl();
  157. $userAcl->fetchGroups();
  158. $typeEx = explode(':', $typeName);
  159. if (2 != count($typeEx)) throw new Exception("Could not get acl for '{$typeName}' - syntax error");
  160. if ('p5_' != substr($typeEx[0], 0, 3)) throw new Exception("Could not get acl for '{$typeName}' - prefix error");
  161. $sourceName = substr($typeEx[0], 3);
  162. $objName = $typeEx[1];
  163. $acl = $userAcl->getObjectAcl($sourceName, $objName);
  164. if (!$acl) throw new Exception("Could not get acl for '{$typeName}'");
  165. $acl->init($forceTblAclInit);
  166. return $acl;
  167. }
  168. }