ViewTableAjax.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. Lib::loadClass('Api_WfsNs');
  9. Lib::loadClass('Core_AclHelper');
  10. class Route_ViewTableAjax extends RouteBase {
  11. public function defaultAction() {
  12. UI::gora();
  13. UI::menu();
  14. try {
  15. $namespace = V::get('namespace', '', $_GET, 'word');
  16. if (!$namespace) {
  17. $typeName = V::get('typeName', '', $_GET, 'word');
  18. if (!$typeName) throw new Exception("Wrong param typeName");
  19. $namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
  20. }
  21. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  22. $forceFilterInit = array();
  23. $filterInit = new stdClass();
  24. $filterInit->currSortCol = 'ID';
  25. $filterInit->currSortFlip = 'desc';
  26. foreach ($_GET as $k => $v) {
  27. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && !empty($v)) {// filter prefix
  28. $filterInit->$k = $v;
  29. }
  30. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && !empty($v)) {// special filter prefix
  31. $filterInit->$k = $v;
  32. }
  33. else if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  34. $fldName = substr($k, 3);
  35. $forceFilterInit[$fldName] = $v;
  36. }
  37. }
  38. $tbl = new TableAjax($acl);
  39. $tblLabel = $typeName;
  40. if ('default_db' == $acl->getSourceName()) {
  41. $tblLabel = array();
  42. $zasobObj = ProcesHelper::getZasobTableInfo($acl->getID());
  43. if (!$zasobObj) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
  44. if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
  45. if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
  46. $tblLabel = implode(" - ", $tblLabel);
  47. }
  48. if (DBG::isActive() && V::get('DBG_ACL', '', $_GET)) {// test load perms
  49. Lib::loadClass('DebugExecutionTime');
  50. $dbgExecTime = new DebugExecutionTime();
  51. $dbgExecTime->activate();
  52. $dbgExecTime->log('start');
  53. UI::startContainer(['style'=>'border:1px solid red']);
  54. UI::tag('p', null, "TEST - load perms from db");
  55. $idTable = $acl->getID();
  56. UI::tag('p', null, "DBG idTable({$idTable})");
  57. $dbgExecTime->log('before sql');
  58. $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
  59. $dbgExecTime->log('after sql', ['sql']);
  60. UI::table(['caption' => "from CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW", 'rows' => $aclTableRows]);
  61. $csvIdProces = array();
  62. foreach ($aclTableRows as $row) {
  63. if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
  64. }
  65. $csvIdProces = implode(",", $csvIdProces);
  66. UI::tag('p', null, "DBG csvIdProces({$csvIdProces})");
  67. if (!empty($csvIdProces)) {
  68. $userLogin = User::getLogin();
  69. $dbgExecTime->log('before sql');
  70. $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");
  71. $dbgExecTime->log('after sql', ['sql']);
  72. UI::table(['caption' => "from CRM_PROCES_idx_USER_to_PROCES_VIEW", 'rows' => $rows]);
  73. $userIdProces = array(); foreach ($rows as $row) $userIdProces[] = $row['ID_PROCES'];
  74. $userTablePerms = array();
  75. foreach ($aclTableRows as $row) {
  76. if (!in_array($row['ID_PROCES'], $userIdProces)) continue;
  77. if (array_key_exists($row['CELL_NAME'], $userTablePerms)) {
  78. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_R' ] += $row['PERM_R'];
  79. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_W' ] += $row['PERM_W'];
  80. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_X' ] += $row['PERM_X'];
  81. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_C' ] += $row['PERM_C'];
  82. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_S' ] += $row['PERM_S'];
  83. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_O' ] += $row['PERM_O'];
  84. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_V' ] += $row['PERM_V'];
  85. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_E' ] += $row['PERM_E'];
  86. } else {
  87. $userTablePerms[ $row['CELL_NAME'] ] = $row;
  88. unset($userTablePerms[ $row['CELL_NAME'] ][ 'TABLE_DESCRIPTION' ]);
  89. unset($userTablePerms[ $row['CELL_NAME'] ][ 'ID_PROCES' ]);
  90. unset($userTablePerms[ $row['CELL_NAME'] ][ 'FORM_TREAT' ]);
  91. }
  92. }
  93. UI::table(['caption' => "\$userTablePerms", 'rows' => $userTablePerms]);
  94. } else UI::alert('warning', "brak \$csvIdProces");
  95. $dbgExecTime->printDebug();
  96. UI::endContainer();
  97. }
  98. $tbl->setLabel($tblLabel);
  99. $tbl->setFilterInit($filterInit);
  100. if (!empty($forceFilterInit)) $tbl->setForceFilterInit($forceFilterInit);
  101. $tbl->addRowFunction('edit');
  102. $tbl->addRowFunction('hist');
  103. $tbl->addRowFunction('files');
  104. $tbl->addRowFunction('cp');
  105. $tbl->addRowFunction('msgs');
  106. echo $tbl->render();
  107. } catch (Exception $e) {
  108. UI::startContainer();
  109. UI::alert('danger', "<strong>Wystąpiły błędy!</strong> " . $e->getMessage());
  110. UI::endContainer();
  111. }
  112. UI::dol();
  113. }
  114. public function revertFromHistAjaxAction() {
  115. Response::sendTryCatchJson(array($this, 'revertFromHistAjax'));
  116. }
  117. public function revertFromHistAjax() {
  118. $typeName = V::get('typeName', '', $_REQUEST, 'word');
  119. if (!$typeName) throw new Exception("Wrong param typeName");
  120. $id = V::get('ID', '', $_REQUEST, 'word');
  121. if (!$id) throw new Exception("Wrong param ID");
  122. $idHist = V::get('idHist', '', $_REQUEST, 'word');
  123. if (!$idHist) throw new Exception("Wrong param idHist");
  124. $fieldName = V::get('fieldName', '', $_REQUEST, 'word');
  125. if (!$fieldName) throw new Exception("Wrong param fieldName");
  126. $acl = $this->getAclFromTypeName($typeName);
  127. $item = $acl->getItem($id);
  128. if (!$item) throw new HttpException("Item not found", 404);
  129. if (!$acl->canWriteObjectField($fieldName, $record)) throw new Exception("Missing perm Write for field {$fieldName}");
  130. $histItem = $acl->getHistItem($id, $idHist);
  131. if (!$histItem) throw new HttpException("Hist Item not found", 404);
  132. $histValue = V::get($fieldName, 'N/S;', $histItem);
  133. if ('N/S;' == $histValue) throw new Exception("Missing field value in hist[{$idHist}] for field({$fieldName}) from item[{$id}]");
  134. if ($acl->isGeomField($fieldName)) {
  135. $wktType = strtoupper($acl->getGeomFieldType($fieldName));
  136. if (!$wktType) throw new Exception("Wrong geometry type for field {$fieldName}");
  137. if ($wktType != strtoupper(substr($histValue, 0, strlen($wktType)))) throw new Exception("Wrong geometry type for field {$fieldName} in hist value");
  138. $coords = trim(substr($histValue, strlen($wktType)), '()');
  139. $wktValue = $acl->convertGmlCoordsToWkt($wktType, $coords, ['cs'=>' ', 'ts'=>',']);
  140. if (!$wktValue) throw new Exception("BUG in hist record");
  141. $sqlObj = array();
  142. $sqlObj['ID'] = $id;
  143. $sqlObj[$fieldName] = "GeomFromText('{$wktValue}')";
  144. $affected = DB::getDB()->UPDATE_OBJ($acl->getName(), (object)$sqlObj);
  145. if (0 == $affected) throw new AlertInfoException("Nie wprowadzono żadnych zmian");
  146. else if ($affected < 0) throw new Exception("Wystąpiły błędy podczas aktualizacji rekordu [{$id}]");
  147. $jsonResponse = array();
  148. $jsonResponse['type'] = 'success';
  149. $jsonResponse['msg'] = "Zaktualizowano dane na podstawie wcześniejszej wartości dla rekordu [{$id}]";
  150. $jsonResponse['actions'] = array();
  151. $jsonResponse['actions'][] = ['jsFunction'=>'TableAjax__HIST_Route', 'args'=>[$id]];
  152. return $jsonResponse;
  153. } else {
  154. throw new HttpException("Not implemented - update from hist only for the geom field", 501);
  155. }
  156. throw new Exception("BUG: update field '{$fieldName}' in item[{$id}] from hist[{$idHist}]", 501);
  157. }
  158. /**
  159. * @param string $typeName - 'p5_default_db:TEST_PERMS'
  160. */
  161. public function getAclFromTypeName($typeName, $forceTblAclInit) {
  162. $userAcl = User::getAcl();
  163. $userAcl->fetchGroups();
  164. $typeEx = explode(':', $typeName);
  165. if (2 != count($typeEx)) throw new Exception("Could not get acl for '{$typeName}' - syntax error");
  166. if ('p5_' != substr($typeEx[0], 0, 3)) throw new Exception("Could not get acl for '{$typeName}' - prefix error");
  167. $sourceName = substr($typeEx[0], 3);
  168. $objName = $typeEx[1];
  169. $acl = $userAcl->getObjectAcl($sourceName, $objName);
  170. if (!$acl) throw new Exception("Could not get acl for '{$typeName}'");
  171. $acl->init($forceTblAclInit);
  172. return $acl;
  173. }
  174. }