getNamespace(); $tbl = new TableAjax($acl); $tblLabel = $acl->getNamespace(); if ('default_db' == $acl->getSourceName()) { $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); } $tbl->setSyncUrl($syncUrl); $tbl->setLabel($tblLabel); $tbl->setFilterInit($filterInit); $tbl->addRowFunction('edit'); $tbl->addRowFunction('hist'); $tbl->addRowFunction('files'); $tbl->addRowFunction('cp'); $tbl->addRowFunction('msgs'); return $tbl; } 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_AclHelper::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 = $this->getTableAjaxWidget($acl); if (!empty($forceFilterInit)) $tbl->setForceFilterInit($forceFilterInit); echo $tbl->render(); 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(); } } catch (Exception $e) { UI::startContainer(); UI::alert('danger', "Wystąpiły błędy! " . $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"); // TODO: use namespace from url // $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_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET))); $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); } public function removeTheGeomAjaxAction() { Response::sendTryCatchJson(array($this, 'removeTheGeomAjax'), $args = 'JSON_FROM_REQUEST_BODY'); } public function removeTheGeomAjax($args) { $namespace = V::get('namespace', '', $args, 'word'); if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400); $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET))); $primaryKeyField = $acl->getPrimaryKeyField(); $primaryKey = V::get($primaryKeyField, 0, $args, 'int'); $geomFieldName = 'the_geom'; $response = new stdClass(); if ($primaryKey <= 0) throw new HttpException("Bad Request - Wrong param ID", 400); $record = $acl->getItem($primaryKey); if (!$record) throw new HttpException("Nie odnaleziono rekordu nr {$primaryKey}", 404); if (!$acl->canWriteObjectField($geomFieldName, $record)) throw new HttpException("Brak dostępu do zapisu dla pola {$geomFieldName}", 403); if (empty($record->{$geomFieldName})) { $response->type = 'info'; $response->msg = "Rekord nie jest powiązany z żadnym obiektem na mapie"; $response->record = $record; return $response; } $itemPatch = array(); $itemPatch[$geomFieldName] = "NULL"; $itemPatch[$primaryKeyField] = $primaryKey; $response = new stdClass(); try { $affected = $acl->updateItem($itemPatch); if ($affected > 0) { $response->type = 'success'; $response->msg = "Usunięto obiekt z mapy dla rekordu {$primaryKey}";// Rekord zapisany pomyślnie } else if ($affected == 0) { $response->type = 'info'; $response->msg = "Nie wprowadzono żadnych zmian"; } $response->record = $acl->getItem($primaryKey); } catch (Exception $e) { $response->type = 'error'; $response->msg = $e->getMessage(); } return $response; } public function moreFunctionsCellAjaxAction() { Response::sendTryCatchJson(array($this, 'moreFunctionsCell'), $args = $_GET); } public function moreFunctionsCell($args) {// ajax task 'MORE_FUNCTIONS_CELL' $id = V::get('ID', 0, $args, 'int'); if ($id <= 0) throw new HttpException("404", 404); $namespace = V::get('namespace', '', $args, 'word'); if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400); $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $args))); $response = new stdClass(); $response->type = 'success'; $response->msg = 'Funkcje'; $response->rowFunctions = Core_AclHelper::getMoreFunctionsCell($acl, array('primary_key' => $id)); return $response; } public function editFormAction() {// namespace, _hash, _primaryKey $args = $_REQUEST; $id = V::get('_primaryKey', 0, $args, 'int'); if ($id <= 0) throw new HttpException("Bad Request - missing primaryKey", 400); $namespace = V::get('namespace', '', $args, 'word'); if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400); $acl = Core_AclHelper::getAclByNamespace($namespace); $tbl = $this->getTableAjaxWidget($acl); $tbl->sendAjaxEdit($id, $args); } public function editSaveAjaxAction() { Response::sendTryCatchJson(array($this, 'editSaveAjax'), $args = 'JSON_FROM_REQUEST_BODY'); } public function editSaveAjax($args) { $namespace = V::get('namespace', '', $args, 'word'); if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400); $acl = Core_AclHelper::getAclByNamespace($namespace); $primaryKeyField = $acl->getPrimaryKeyField(); $primaryKey = V::get('primaryKey', 0, $args, 'int'); if (empty($primaryKey)) throw new HttpException("Wrong param id!", 400); $item = $acl->getItem($primaryKey); if (!$item) throw new HttpException("Item not exists!", 404); $itemFromUser = $acl->convertObjectFromUserInput($args['form'], $type = 'array_by_id', $prefix = 'f'); $response = new stdClass(); $response->primaryKey = $primaryKey; try { $itemFromUser[$primaryKeyField] = $primaryKey; $affected = $acl->updateItem($itemFromUser); if ($affected > 0) { $response->type = 'success'; $response->msg = "Rekord zapisany pomyślnie";//"Record saved successfully"; } else if ($affected == 0) { $response->type = 'info'; $response->msg = "Nie wprowadzono żadnych zmian"; } $response->record = $acl->getItem($primaryKey); $rowFunList = Core_AclHelper::getMoreFunctionsCell($acl, array('primary_key'=>$primaryKey, 'record'=>$response->record)); if (!empty($rowFunList)) $response->rowFunctions = $rowFunList; } catch (Exception $e) { $response->type = 'error'; $response->msg = "Wystąpiły błędy!"; $response->msg .= $e->getMessage(); } return $response; } public function loadDataAjaxAction() { $namespace = V::get('namespace', '', $_REQUEST, 'word'); if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400); $acl = Core_AclHelper::getAclByNamespace($namespace); $tbl = $this->getTableAjaxWidget($acl); Response::sendTryCatchJson(array($tbl, 'ajaxData'), $args = $_GET); } /** * @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; } }