| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- Lib::loadClass('RouteBase');
- Lib::loadClass('ProcesHelper');
- Lib::loadClass('TableAjax');
- // Lib::loadClass('Request');
- Lib::loadClass('Response');
- Lib::loadClass('UI');
- Lib::loadClass('Api_WfsNs');
- Lib::loadClass('Core_AclBase');
- class Route_ViewTableAjax extends RouteBase {
- public function defaultAction() {
- UI::gora();
- UI::menu();
- try {
- $namespace = V::get('namespace', '', $_GET, 'word');
- if (!$namespace) {
- $typeName = V::get('typeName', '', $_GET, 'word');
- if (!$typeName) throw new Exception("Wrong param typeName");
- $namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
- }
- $acl = Core_AclBase::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
- $forceFilterInit = array();
- $filterInit = new stdClass();
- $filterInit->currSortCol = 'ID';
- $filterInit->currSortFlip = 'desc';
- foreach ($_GET as $k => $v) {
- if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && !empty($v)) {// filter prefix
- $filterInit->$k = $v;
- }
- else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && !empty($v)) {// special filter prefix
- $filterInit->$k = $v;
- }
- else if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
- $fldName = substr($k, 3);
- $forceFilterInit[$fldName] = $v;
- }
- }
- $tbl = new TableAjax($acl);
- $tblLabel = $typeName;
- if ('p5_default_db:' == substr($typeName, 0, 14)) {
- $tblLabel = array();
- $zasobObj = ProcesHelper::getZasobTableInfo($acl->getID());
- if (!$zasobObj) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
- if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
- if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
- $tblLabel = implode(" - ", $tblLabel);
- }
- if (DBG::isActive() && V::get('DBG_ACL', '', $_GET)) {// test load perms
- Lib::loadClass('DebugExecutionTime');
- $dbgExecTime = new DebugExecutionTime();
- $dbgExecTime->activate();
- $dbgExecTime->log('start');
- UI::startContainer(['style'=>'border:1px solid red']);
- UI::tag('p', null, "TEST - load perms from db");
- $idTable = $acl->getID();
- UI::tag('p', null, "DBG idTable({$idTable})");
- $dbgExecTime->log('before sql');
- $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
- $dbgExecTime->log('after sql', ['sql']);
- UI::table(['caption' => "from CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW", 'rows' => $aclTableRows]);
- $csvIdProces = array();
- foreach ($aclTableRows as $row) {
- if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
- }
- $csvIdProces = implode(",", $csvIdProces);
- UI::tag('p', null, "DBG csvIdProces({$csvIdProces})");
- if (!empty($csvIdProces)) {
- $userLogin = User::getLogin();
- $dbgExecTime->log('before sql');
- $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");
- $dbgExecTime->log('after sql', ['sql']);
- UI::table(['caption' => "from CRM_PROCES_idx_USER_to_PROCES_VIEW", 'rows' => $rows]);
- $userIdProces = array(); foreach ($rows as $row) $userIdProces[] = $row['ID_PROCES'];
- $userTablePerms = array();
- foreach ($aclTableRows as $row) {
- if (!in_array($row['ID_PROCES'], $userIdProces)) continue;
- if (array_key_exists($row['CELL_NAME'], $userTablePerms)) {
- $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_R' ] += $row['PERM_R'];
- $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_W' ] += $row['PERM_W'];
- $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_X' ] += $row['PERM_X'];
- $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_C' ] += $row['PERM_C'];
- $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_S' ] += $row['PERM_S'];
- $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_O' ] += $row['PERM_O'];
- $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_V' ] += $row['PERM_V'];
- $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_E' ] += $row['PERM_E'];
- } else {
- $userTablePerms[ $row['CELL_NAME'] ] = $row;
- unset($userTablePerms[ $row['CELL_NAME'] ][ 'TABLE_DESCRIPTION' ]);
- unset($userTablePerms[ $row['CELL_NAME'] ][ 'ID_PROCES' ]);
- unset($userTablePerms[ $row['CELL_NAME'] ][ 'FORM_TREAT' ]);
- }
- }
- UI::table(['caption' => "\$userTablePerms", 'rows' => $userTablePerms]);
- } else UI::alert('warning', "brak \$csvIdProces");
- $dbgExecTime->printDebug();
- UI::endContainer();
- }
- $tbl->setLabel($tblLabel);
- $tbl->setFilterInit($filterInit);
- if (!empty($forceFilterInit)) $tbl->setForceFilterInit($forceFilterInit);
- $tbl->addRowFunction('edit');
- $tbl->addRowFunction('hist');
- $tbl->addRowFunction('files');
- $tbl->addRowFunction('cp');
- $tbl->addRowFunction('msgs');
- echo $tbl->render();
- } catch (Exception $e) {
- UI::startContainer();
- UI::alert('danger', "<strong>Wystąpiły błędy!</strong> " . $e->getMessage());
- UI::endContainer();
- }
- UI::dol();
- }
- public function revertFromHistAjaxAction() {
- Response::sendTryCatchJson(array($this, 'revertFromHistAjax'));
- }
- public function revertFromHistAjax() {
- $typeName = V::get('typeName', '', $_REQUEST, 'word');
- if (!$typeName) throw new Exception("Wrong param typeName");
- $id = V::get('ID', '', $_REQUEST, 'word');
- if (!$id) throw new Exception("Wrong param ID");
- $idHist = V::get('idHist', '', $_REQUEST, 'word');
- if (!$idHist) throw new Exception("Wrong param idHist");
- $fieldName = V::get('fieldName', '', $_REQUEST, 'word');
- if (!$fieldName) throw new Exception("Wrong param fieldName");
- $acl = $this->getAclFromTypeName($typeName);
- $item = $acl->getItem($id);
- if (!$item) throw new HttpException("Item not found", 404);
- if (!$acl->canWriteObjectField($fieldName, $record)) throw new Exception("Missing perm Write for field {$fieldName}");
- $histItem = $acl->getHistItem($id, $idHist);
- if (!$histItem) throw new HttpException("Hist Item not found", 404);
- $histValue = V::get($fieldName, 'N/S;', $histItem);
- if ('N/S;' == $histValue) throw new Exception("Missing field value in hist[{$idHist}] for field({$fieldName}) from item[{$id}]");
- if ($acl->isGeomField($fieldName)) {
- $wktType = strtoupper($acl->getGeomFieldType($fieldName));
- if (!$wktType) throw new Exception("Wrong geometry type for field {$fieldName}");
- if ($wktType != strtoupper(substr($histValue, 0, strlen($wktType)))) throw new Exception("Wrong geometry type for field {$fieldName} in hist value");
- $coords = trim(substr($histValue, strlen($wktType)), '()');
- $wktValue = $acl->convertGmlCoordsToWkt($wktType, $coords, ['cs'=>' ', 'ts'=>',']);
- if (!$wktValue) throw new Exception("BUG in hist record");
- $sqlObj = array();
- $sqlObj['ID'] = $id;
- $sqlObj[$fieldName] = "GeomFromText('{$wktValue}')";
- $affected = DB::getDB()->UPDATE_OBJ($acl->getName(), (object)$sqlObj);
- if (0 == $affected) throw new AlertInfoException("Nie wprowadzono żadnych zmian");
- else if ($affected < 0) throw new Exception("Wystąpiły błędy podczas aktualizacji rekordu [{$id}]");
- $jsonResponse = array();
- $jsonResponse['type'] = 'success';
- $jsonResponse['msg'] = "Zaktualizowano dane na podstawie wcześniejszej wartości dla rekordu [{$id}]";
- $jsonResponse['actions'] = array();
- $jsonResponse['actions'][] = ['jsFunction'=>'TableAjax__HIST_Route', 'args'=>[$id]];
- return $jsonResponse;
- } else {
- throw new HttpException("Not implemented - update from hist only for the geom field", 501);
- }
- throw new Exception("BUG: update field '{$fieldName}' in item[{$id}] from hist[{$idHist}]", 501);
- }
- /**
- * @param string $typeName - 'p5_default_db:TEST_PERMS'
- */
- public function getAclFromTypeName($typeName, $forceTblAclInit) {
- $userAcl = User::getAcl();
- $userAcl->fetchGroups();
- $typeEx = explode(':', $typeName);
- if (2 != count($typeEx)) throw new Exception("Could not get acl for '{$typeName}' - syntax error");
- if ('p5_' != substr($typeEx[0], 0, 3)) throw new Exception("Could not get acl for '{$typeName}' - prefix error");
- $sourceName = substr($typeEx[0], 3);
- $objName = $typeEx[1];
- $acl = $userAcl->getObjectAcl($sourceName, $objName);
- if (!$acl) throw new Exception("Could not get acl for '{$typeName}'");
- $acl->init($forceTblAclInit);
- return $acl;
- }
- }
|