ViewTableAjax.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. Lib::loadClass('Route_UrlAction');
  11. Lib::loadClass('Router');
  12. Lib::loadClass('Typespecial');
  13. Lib::loadClass('UserProfile');
  14. class Route_ViewTableAjax extends RouteBase {
  15. public function getTableAjaxWidget($acl, $backRefFilter = [], $childRefFilter = []) {
  16. $syncUrl = $this->getLink('', [ 'namespace' => $acl->getNamespace() ]);
  17. $tbl = new TableAjax($acl);
  18. $tbl->setRootUrl($syncUrl);
  19. if (!empty($backRefFilter['namespace']) && !empty($backRefFilter['primaryKey'])) {
  20. $tbl->setBackRefFilter($backRefFilter);
  21. $syncUrl = $this->getLink('', [
  22. 'namespace' => $acl->getNamespace(),
  23. 'backRefNS' => $backRefFilter['namespace'],
  24. 'backRefPK' => $backRefFilter['primaryKey'],
  25. 'backRefField' => $backRefFilter['fieldName'],
  26. ]);
  27. }
  28. if (!empty($childRefFilter['namespace']) && !empty($childRefFilter['primaryKey'])) {
  29. $tbl->setChildRefFilter($childRefFilter);
  30. $syncUrl = $this->getLink('', [
  31. 'namespace' => $acl->getNamespace(),
  32. 'childRefNS' => $childRefFilter['namespace'],
  33. 'childRefPK' => $childRefFilter['primaryKey'],
  34. ]);
  35. }
  36. $tblLabel = $acl->getNamespace();
  37. if ('default_db' == $acl->getSourceName()) {
  38. $tblLabel = array();
  39. $zasobObj = ProcesHelper::getZasobTableInfo($acl->getID());
  40. if (!$zasobObj) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
  41. if (!empty($zasobObj->DESC_PL)) $tblLabel[] = $zasobObj->DESC_PL;
  42. if (!empty($zasobObj->OPIS)) $tblLabel[] = $zasobObj->OPIS;
  43. $tblLabel = implode(" - ", $tblLabel);
  44. }
  45. $tbl->setSyncUrl($syncUrl);
  46. $tbl->showProcesInitFiltr = $this->getLink("procesInitFiltrAjax", [ 'namespace' => $acl->getNamespace() ]);
  47. $tbl->showTableTools = $this->getLink("tableToolsAjax", [ 'namespace' => $acl->getNamespace() ]);
  48. $tbl->useUserTableFilter = $this->getLink("getUserTableFilterAjax");
  49. $tbl->setLabel($tblLabel);
  50. $tbl->addRowFunction('edit');
  51. $tbl->addRowFunction('hist');
  52. $tbl->addRowFunction('files');
  53. $tbl->addRowFunction('cp');
  54. $tbl->addRowFunction('msgs');
  55. return $tbl;
  56. }
  57. public function defaultAction() {
  58. UI::gora();
  59. UI::menu();
  60. try {
  61. $namespace = V::get('namespace', '', $_GET, 'word');
  62. if (!$namespace) {
  63. $typeName = V::get('typeName', '', $_GET, 'word');
  64. if (!$typeName) throw new Exception("Wrong param typeName");
  65. $namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
  66. }
  67. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  68. $forceFilterInit = array();
  69. $filterInit = new stdClass();
  70. $filterInit->currSortCol = $acl->getPrimaryKeyField();
  71. $filterInit->currSortFlip = 'desc';
  72. foreach ($_REQUEST as $k => $v) {
  73. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && !empty($v)) {// filter prefix
  74. $filterInit->$k = $v;
  75. }
  76. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && !empty($v)) {// special filter prefix
  77. $filterInit->$k = $v;
  78. }
  79. else if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  80. $fldName = substr($k, 3);
  81. $forceFilterInit[$fldName] = $v;
  82. }
  83. }
  84. $backRefFilter = [
  85. 'namespace' => V::get('backRefNS', '', $_GET),
  86. 'primaryKey' => V::get('backRefPK', '', $_GET),
  87. 'fieldName' => V::get('backRefField', '', $_GET),
  88. ];
  89. $childRefFilter = [
  90. 'namespace' => V::get('childRefNS', '', $_GET),
  91. 'primaryKey' => V::get('childRefPK', '', $_GET),
  92. ];
  93. $tbl = $this->getTableAjaxWidget($acl, $backRefFilter, $childRefFilter);
  94. $tbl->setFilterInit($filterInit);
  95. if (!empty($forceFilterInit)) $tbl->setForceFilterInit($forceFilterInit);
  96. echo $tbl->render();
  97. if (DBG::isActive() && V::get('DBG_ACL', '', $_GET)) {// test load perms
  98. Lib::loadClass('DebugExecutionTime');
  99. $dbgExecTime = new DebugExecutionTime();
  100. $dbgExecTime->activate();
  101. $dbgExecTime->log('start');
  102. UI::startContainer(['style'=>'border:1px solid red']);
  103. UI::tag('p', null, "TEST - load perms from db");
  104. $idTable = $acl->getID();
  105. UI::tag('p', null, "DBG idTable({$idTable})");
  106. if ($idTable > 0) {
  107. $dbgExecTime->log('before sql');
  108. $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
  109. $dbgExecTime->log('after sql', ['sql']);
  110. UI::table(['caption' => "from CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW", 'rows' => $aclTableRows]);
  111. $csvIdProces = array();
  112. foreach ($aclTableRows as $row) {
  113. if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
  114. }
  115. }
  116. $tableName = $acl->getName();
  117. $databaseName = DB::getPDO()->getDatabaseName();
  118. UI::table([
  119. 'caption' => "Cell to process",
  120. 'rows' => array_map(
  121. function ($row) use ($aclTableRows, $idTable) {
  122. $row['proces'] = array();
  123. $row['id_zasob'] = 0;
  124. $row['PERM_R'] = 0;
  125. $row['PERM_W'] = 0;
  126. $row['PERM_X'] = 0;
  127. $row['PERM_C'] = 0;
  128. $row['PERM_S'] = 0;
  129. $row['PERM_O'] = 0;
  130. $row['PERM_V'] = 0;
  131. $row['PERM_E'] = 0;
  132. foreach ($aclTableRows as $aclInfo) {
  133. if (strtolower($aclInfo['CELL_NAME']) == strtolower($row['COLUMN_NAME'])) {
  134. $row['proces'][] = $aclInfo['ID_PROCES'];
  135. $row['id_zasob'] = $aclInfo['ID_CELL'];
  136. $row['PERM_R'] += $aclInfo['PERM_R'];
  137. $row['PERM_W'] += $aclInfo['PERM_W'];
  138. $row['PERM_X'] += $aclInfo['PERM_X'];
  139. $row['PERM_C'] += $aclInfo['PERM_C'];
  140. $row['PERM_S'] += $aclInfo['PERM_S'];
  141. $row['PERM_O'] += $aclInfo['PERM_O'];
  142. $row['PERM_V'] += $aclInfo['PERM_V'];
  143. $row['PERM_E'] += $aclInfo['PERM_E'];
  144. }
  145. }
  146. $row['proces'] = (empty($row['proces']))
  147. ? "<i style=\"color:red\">Brak</i>"
  148. : implode(", ", $row['proces']);
  149. if (!$row['id_zasob']) $row['id_zasob'] = DB::getPDO()->fetchValue("select ID from CRM_LISTA_ZASOBOW where `DESC` = '{$row['COLUMN_NAME']}' and PARENT_ID = {$idTable} limit 1");
  150. return $row;
  151. }, DB::getPDO()->fetchAll("
  152. select t.TABLE_NAME, t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  153. from `information_schema`.`COLUMNS` t
  154. where t.TABLE_SCHEMA = '{$databaseName}'
  155. and t.TABLE_NAME like '{$tableName}'
  156. ")
  157. )
  158. ]);
  159. if (!empty($csvIdProces)) {
  160. $csvIdProces = implode(",", $csvIdProces);
  161. UI::tag('p', null, "DBG csvIdProces({$csvIdProces})");
  162. $userLogin = User::getLogin();
  163. $dbgExecTime->log('before sql');
  164. $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");
  165. $dbgExecTime->log('after sql', ['sql']);
  166. UI::table(['caption' => "from CRM_PROCES_idx_USER_to_PROCES_VIEW", 'rows' => $rows]);
  167. $userIdProces = array(); foreach ($rows as $row) $userIdProces[] = $row['ID_PROCES'];
  168. $userTablePerms = array();
  169. foreach ($aclTableRows as $row) {
  170. if (!in_array($row['ID_PROCES'], $userIdProces)) continue;
  171. if (array_key_exists($row['CELL_NAME'], $userTablePerms)) {
  172. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_R' ] += $row['PERM_R'];
  173. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_W' ] += $row['PERM_W'];
  174. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_X' ] += $row['PERM_X'];
  175. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_C' ] += $row['PERM_C'];
  176. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_S' ] += $row['PERM_S'];
  177. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_O' ] += $row['PERM_O'];
  178. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_V' ] += $row['PERM_V'];
  179. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_E' ] += $row['PERM_E'];
  180. } else {
  181. $userTablePerms[ $row['CELL_NAME'] ] = $row;
  182. unset($userTablePerms[ $row['CELL_NAME'] ][ 'TABLE_DESCRIPTION' ]);
  183. unset($userTablePerms[ $row['CELL_NAME'] ][ 'ID_PROCES' ]);
  184. unset($userTablePerms[ $row['CELL_NAME'] ][ 'FORM_TREAT' ]);
  185. }
  186. }
  187. UI::table(['caption' => "\$userTablePerms", 'rows' => $userTablePerms]);
  188. } else UI::alert('warning', "brak \$csvIdProces");
  189. $dbgExecTime->printDebug();
  190. UI::endContainer();
  191. }
  192. } catch (Exception $e) {
  193. UI::startContainer();
  194. UI::alert('danger', "<strong>Wystąpiły błędy!</strong> " . $e->getMessage());
  195. UI::endContainer();
  196. DBG::log($e);
  197. }
  198. UI::dol();
  199. }
  200. public function rmUserTableFilterAjaxAction() {
  201. Response::sendTryCatchJson(array($this, 'rmUserTableFilterAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  202. }
  203. public function rmUserTableFilterAjax($args) {
  204. $namespace = V::get('namespace', '', $args);
  205. $filtrName = V::get('filtrName', '', $args);
  206. if (!$namespace) throw new Exception("Missing namespace");
  207. if (!$filtrName) throw new Exception("Missing filtrName");
  208. $userFltrConfKey = "tableColFilters__" . User::getLogin();
  209. $currentFilters = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = '{$userFltrConfKey}' ");
  210. if (!$currentFilters) return [
  211. 'type' => 'warning',
  212. 'msg' => "Brak filtrów w bazie",
  213. ];
  214. $currentFilters = json_decode($currentFilters, 'assoc');
  215. unset($currentFilters[$namespace][$filtrName]);
  216. $affeced = DB::getPDO()->update('CRM_CONFIG', 'CONF_KEY', $userFltrConfKey, [
  217. 'CONF_VAL' => json_encode($currentFilters)
  218. ]);
  219. return [
  220. 'type' => 'success',
  221. 'msg' => 'Zapisano nowy filtr',
  222. 'data' => $currentFilters[$namespace]
  223. ];
  224. }
  225. public function addUserTableFilterAjaxAction() {
  226. Response::sendTryCatchJson(array($this, 'addUserTableFilterAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  227. }
  228. public function addUserTableFilterAjax($args) {
  229. $namespace = V::get('namespace', '', $args);
  230. $filtrName = V::get('filtrName', '', $args);
  231. $visibleCols = V::get('visibleCols', '', $args);
  232. if (!$namespace) throw new Exception("Missing namespace");
  233. if (!$filtrName) throw new Exception("Missing filtrName");
  234. if (!$visibleCols) throw new Exception("Missing visibleCols");
  235. $userFltrConfKey = "tableColFilters__" . User::getLogin();
  236. $currentFilters = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = '{$userFltrConfKey}' ");
  237. $currentFilters = ($currentFilters) ? json_decode($currentFilters, 'assoc') : [];
  238. $currentFilters[$namespace][$filtrName] = $visibleCols;
  239. $sqlFltr = json_encode($currentFilters);
  240. DB::getPDO()->execSql("
  241. insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
  242. values ('$userFltrConfKey', '{$sqlFltr}')
  243. on duplicate key update CONF_VAL = '{$sqlFltr}'
  244. ");
  245. return [
  246. 'type' => 'success',
  247. 'msg' => 'Zapisano nowy filtr',
  248. 'data' => $currentFilters[$namespace]
  249. ];
  250. }
  251. public function getUserTableFilterAjaxAction() {
  252. Response::sendTryCatchJson(array($this, 'getUserTableFilterAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  253. }
  254. public function getUserTableFilterAjax($args) {
  255. $namespace = V::get('namespace', '', $args);
  256. if (!$namespace) throw new Exception("Missing namespace");
  257. $userFltrConfKey = "tableColFilters__" . User::getLogin();
  258. $currentFilters = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = '{$userFltrConfKey}' ");
  259. $currentFilters = ($currentFilters) ? json_decode($currentFilters, 'assoc') : [];
  260. return [
  261. 'type' => 'success',
  262. 'msg' => 'Odczytano filtry użytkownika',
  263. 'data' => (!empty($currentFilters[$namespace])) ? $currentFilters[$namespace] : []
  264. ];
  265. }
  266. public function revertFromHistAjaxAction() {
  267. Response::sendTryCatchJson(array($this, 'revertFromHistAjax'));
  268. }
  269. public function revertFromHistAjax() {
  270. $typeName = V::get('typeName', '', $_REQUEST, 'word');
  271. if (!$typeName) throw new Exception("Wrong param typeName");
  272. // TODO: use namespace from url
  273. // $namespace = V::get('namespace', '', $_GET, 'word');
  274. // if (!$namespace) {
  275. // $typeName = V::get('typeName', '', $_GET, 'word');
  276. // if (!$typeName) throw new Exception("Wrong param typeName");
  277. // $namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
  278. // }
  279. // $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  280. $id = V::get('ID', '', $_REQUEST, 'word');
  281. if (!$id) throw new Exception("Wrong param ID");
  282. $idHist = V::get('idHist', '', $_REQUEST, 'word');
  283. if (!$idHist) throw new Exception("Wrong param idHist");
  284. $fieldName = V::get('fieldName', '', $_REQUEST, 'word');
  285. if (!$fieldName) throw new Exception("Wrong param fieldName");
  286. $acl = Core_AclHelper::getAclByTypeName($typeName);
  287. $item = $acl->getItem($id);
  288. if (!$item) throw new HttpException("Item not found", 404);
  289. if (!$acl->canWriteObjectField($fieldName, $record)) throw new Exception("Missing perm Write for field {$fieldName}");
  290. $histItem = $acl->getHistItem($id, $idHist);
  291. if (!$histItem) throw new HttpException("Hist Item not found", 404);
  292. $histValue = V::get($fieldName, 'N/S;', $histItem);
  293. if ('N/S;' == $histValue) throw new Exception("Missing field value in hist[{$idHist}] for field({$fieldName}) from item[{$id}]");
  294. if ($acl->isGeomField($fieldName)) {
  295. $wktType = strtoupper($acl->getGeomFieldType($fieldName));
  296. if (!$wktType) throw new Exception("Wrong geometry type for field {$fieldName}");
  297. if ($wktType != strtoupper(substr($histValue, 0, strlen($wktType)))) throw new Exception("Wrong geometry type for field {$fieldName} in hist value");
  298. $coords = trim(substr($histValue, strlen($wktType)), '()');
  299. $wktValue = $acl->convertGmlCoordsToWkt($wktType, $coords, ['cs'=>' ', 'ts'=>',']);
  300. if (!$wktValue) throw new Exception("BUG in hist record");
  301. $sqlObj = array();
  302. $sqlObj['ID'] = $id;
  303. $sqlObj[$fieldName] = "GeomFromText('{$wktValue}')";
  304. $affected = DB::getDB()->UPDATE_OBJ($acl->getName(), (object)$sqlObj);
  305. if (0 == $affected) throw new AlertInfoException("Nie wprowadzono żadnych zmian");
  306. else if ($affected < 0) throw new Exception("Wystąpiły błędy podczas aktualizacji rekordu [{$id}]");
  307. $jsonResponse = array();
  308. $jsonResponse['type'] = 'success';
  309. $jsonResponse['msg'] = "Zaktualizowano dane na podstawie wcześniejszej wartości dla rekordu [{$id}]";
  310. $jsonResponse['actions'] = array();
  311. $jsonResponse['actions'][] = ['jsFunction'=>'TableAjax__HIST_Route', 'args'=>[$id]];
  312. return $jsonResponse;
  313. } else {
  314. throw new HttpException("Not implemented - update from hist only for the geom field", 501);
  315. }
  316. throw new Exception("BUG: update field '{$fieldName}' in item[{$id}] from hist[{$idHist}]", 501);
  317. }
  318. public function removeTheGeomAjaxAction() {
  319. Response::sendTryCatchJson(array($this, 'removeTheGeomAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  320. }
  321. public function removeTheGeomAjax($args) {
  322. $namespace = V::get('namespace', '', $args, 'word');
  323. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  324. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  325. $primaryKeyField = $acl->getPrimaryKeyField();
  326. $primaryKey = V::get($primaryKeyField, 0, $args, 'int');
  327. $geomFieldName = 'the_geom';
  328. $response = new stdClass();
  329. if ($primaryKey <= 0) throw new HttpException("Bad Request - Wrong param ID", 400);
  330. $record = $acl->getItem($primaryKey);
  331. if (!$record) throw new HttpException("Nie odnaleziono rekordu nr {$primaryKey}", 404);
  332. if (!$acl->canWriteObjectField($geomFieldName, $record)) throw new HttpException("Brak dostępu do zapisu dla pola {$geomFieldName}", 403);
  333. if (empty($record->{$geomFieldName})) {
  334. $response->type = 'info';
  335. $response->msg = "Rekord nie jest powiązany z żadnym obiektem na mapie";
  336. $response->record = $record;
  337. return $response;
  338. }
  339. $itemPatch = array();
  340. $itemPatch[$geomFieldName] = "NULL";
  341. $itemPatch[$primaryKeyField] = $primaryKey;
  342. $response = new stdClass();
  343. try {
  344. $affected = $acl->updateItem($itemPatch);
  345. if ($affected > 0) {
  346. $response->type = 'success';
  347. $response->msg = "Usunięto obiekt z mapy dla rekordu {$primaryKey}";// Rekord zapisany pomyślnie
  348. } else if ($affected == 0) {
  349. $response->type = 'info';
  350. $response->msg = "Nie wprowadzono żadnych zmian";
  351. }
  352. $response->record = $acl->getItem($primaryKey);
  353. }
  354. catch (Exception $e) {
  355. $response->type = 'error';
  356. $response->msg = $e->getMessage();
  357. }
  358. return $response;
  359. }
  360. public function moreFunctionsCellAjaxAction() {
  361. Response::sendTryCatchJson(array($this, 'moreFunctionsCell'), $args = $_GET);
  362. }
  363. public function moreFunctionsCell($args) {// ajax task 'MORE_FUNCTIONS_CELL'
  364. $id = V::get('ID', 0, $args, 'int');
  365. if ($id <= 0) throw new HttpException("404", 404);
  366. $namespace = V::get('namespace', '', $args, 'word');
  367. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  368. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $args)));
  369. $response = new stdClass();
  370. $response->type = 'success';
  371. $response->msg = 'Funkcje';
  372. $response->rowFunctions = Core_AclHelper::getMoreFunctionsCell($acl, array('primary_key' => $id));
  373. return $response;
  374. }
  375. public function createFormAction() {// TODO: move to createFormJsonAction
  376. try {
  377. $args = $_REQUEST;
  378. $namespace = V::get('namespace', '', $args, 'word');
  379. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  380. $acl = Core_AclHelper::getAclByNamespace($namespace);
  381. $tbl = $this->getTableAjaxWidget($acl);
  382. $tbl->sendAjaxCreate();
  383. } catch (Exception $e) {
  384. DBG::log($e);
  385. throw $e;
  386. }
  387. }
  388. public function createFormJsonAction() {
  389. Response::sendTryCatchJson(array($this, 'createFormJson'), $args = $_REQUEST);
  390. }
  391. public function createFormJson($args) { // namespace, _hash, _primaryKey
  392. $namespace = V::get('namespace', '', $args, 'word');
  393. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  394. $acl = Core_AclHelper::getAclByNamespace($namespace);
  395. $tbl = $this->getTableAjaxWidget($acl);
  396. if (!Core_AclHelper::hasCreatePerms($acl)) {
  397. return [
  398. 'type' => "success",
  399. 'msg' => "Dodaj nowy rekord",
  400. 'body' => [
  401. 'reactNode' => [ 'div', [ 'class' => "alert alert-danger" ], "Brak uprawnień do utworzenia nowego rekordu." ]
  402. ],
  403. ];
  404. // throw new Exception("Brak uprawnień do utworzenia nowego rekordu.");
  405. }
  406. $fieldsList = array();
  407. foreach ($acl->getFieldListByIdZasob() as $kID => $fieldName) {
  408. if ($fieldName == 'ID') continue;
  409. $field['name'] = $fieldName;
  410. $field['opis'] = $acl->getFieldOpis($fieldName);
  411. $field['label'] = $acl->getFieldLabel($fieldName);
  412. if (empty($field['label'])) $field['label'] = str_replace('_', ' ', $fieldName);
  413. $fieldsList[$kID] = $field;
  414. }
  415. $cols = array();
  416. $forceFilterInit = array();
  417. $defaultAclGroup = User::getDefaultAclGroup();
  418. if ($defaultAclGroup) {
  419. $forceFilterInit['A_ADM_COMPANY'] = $defaultAclGroup;
  420. $forceFilterInit['A_CLASSIFIED'] = $defaultAclGroup;
  421. }
  422. foreach ($_GET as $k => $v) { // TODO: read from $args ?
  423. if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  424. $fldName = substr($k, 3);
  425. $forceFilterInit[$fldName] = $v;
  426. }
  427. }
  428. DBG::log($forceFilterInit, 'array', "\$forceFilterInit");
  429. foreach ($fieldsList as $kID => $field) {
  430. $defaultValue = '';
  431. if (!empty($forceFilterInit[$field['name']])) {
  432. $defaultValue = $forceFilterInit[$field['name']];
  433. }
  434. $cols[$kID] = V::get("f{$kID}", $defaultValue, $_POST);
  435. }
  436. DBG::log($cols, 'array', "\$cols - field values");
  437. $tsValues = array();
  438. $featureFunctions = [
  439. // 'edit' => [ 'href' => '#EDIT/{0}', 'ico' => 'glyphicon glyphicon-pencil', 'title' => "Edytuj rekord"],
  440. 'hist' => [ 'href' => '#HIST/{0}', 'ico' => 'glyphicon glyphicon-book', 'title' => "Historia" ],
  441. 'files' => [ 'href' => '#FILES/{0}', 'ico' => 'glyphicon glyphicon-folder-open', 'title' => "Pliki" ],
  442. // 'cp' => [ 'href' => '#', 'ico' => 'glyphicon glyphicon-plus-sign', 'title' => "Kopiuj rekord", 'onclick' => 'return tableAjaxCopy({0});' ],
  443. 'msgs' => [ 'href' => "index.php?_route=TableMsgs&_task=tableRow&idTable=".$acl->getID()."&idRow={0}", 'ico' => 'glyphicon glyphicon-envelope', 'title' => "Wiadomości" ],
  444. ];
  445. $jsFields = [];
  446. $tabindex = 0;
  447. foreach ($fieldsList as $kID => $vCol) {
  448. $fieldName = $vCol['name'];
  449. DBG::log(['$fieldName'=>$fieldName, 'canCreate'=>$acl->canCreateField($fieldName)], 'array', "form field");
  450. if ($acl->canCreateField($fieldName)) {
  451. DBG::log("editFormJson::field({$fieldName})");
  452. $fieldParams = [ 'appendBack' => true, 'tabindex' => (++$tabindex), 'maxGrid' => 8 ];
  453. if (!empty($tsValues[$kID])) $fieldParams['typespecialValue'] = $tsValues[$kID];
  454. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  455. [ 'label', [ 'class' => "control-label", 'for' => "f{$kID}" ], [
  456. [ 'span', [ 'style' => ['padding-right'=>'4px'] ], $vCol['label'] ],
  457. [ 'i', [ 'class' => "glyphicon glyphicon-info-sign frm-help", 'data-toggle' => "popover", 'data-trigger' => "hover", 'title' => "", 'data-content' => htmlspecialchars($vCol['opis']), 'data-original-title' => "[{$kID}] {$fieldName}" ] ],
  458. ] ],
  459. [ 'div', [ 'class' => "" ], [
  460. UI::hGetFormItem($acl, $fieldName, 'C', $kID, "f{$kID}", $cols[$kID], $fieldParams),
  461. ] ]
  462. ] ];
  463. // } else {
  464. // $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  465. // "TODO: SKIP field ({$fieldName}) - ! canWriteObjectField && ! canReadObjectField"
  466. // ]];
  467. }
  468. }
  469. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  470. [ 'div', [ 'class' => "" ], [
  471. ['button', [ 'type' => "submit", 'class' => "btn btn-primary", 'tabindex' => ++$tabindex ], "Zapisz" ]
  472. ] ]
  473. ] ];
  474. $tblLabel = $acl->getNamespace();
  475. if ('default_db' == $acl->getSourceName()) {
  476. $tblLabel = array();
  477. $zasobObj = ProcesHelper::getZasobTableInfo($acl->getID());
  478. if (!$zasobObj) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
  479. if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
  480. if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
  481. $tblLabel = implode(" - ", $tblLabel);
  482. }
  483. $syncUrl = Request::getPathUri() . 'index.php?_route=ViewTableAjax&namespace=' . $acl->getNamespace();
  484. $jsGui = [
  485. 'reactNode' => [ 'div', [ 'class' => "container AjaxFrmHorizontalEdit", 'style' => [ "max-width" => "940px" ] ], [
  486. [ 'h4', [ 'style' => [ "padding-bottom" => "3px", "border-bottom" => "1px solid #ddd" ] ], [
  487. "Dodaj nowy rekord",
  488. ] ],
  489. [ 'P5UI__FeatureCreateForm', [
  490. 'class' => "", 'action' => "", 'method' => "post",
  491. 'id' => "CREATE_FRM_{$this->_htmlID}", // TODO: rm - use React nodes // TODO: $this->_htmlID not exists!
  492. 'ajaxSaveUrl' => "{$syncUrl}&_task=createSaveAjax", // TODO:? &_hash={$this->_htmlID}
  493. 'ajaxSaveLegacyUrl' => "{$syncUrl}&_task=createSaveLegacy", // TODO: Legacy RM
  494. 'namespace' => $acl->getNamespace(),
  495. 'tableLabelHtml' => $tblLabel,
  496. '_htmlID' => $acl->getName(),
  497. ], [
  498. [ 'fieldset', [ 'style' => [ "padding-bottom" => "100px" ] ], $jsFields ] // fieldset
  499. ] ] // form
  500. ] ] // .container
  501. ];
  502. return [
  503. 'type' => "success",
  504. 'msg' => "Dodaj nowy rekord",
  505. 'body' => $jsGui, // TODO: action for GUI: array to render by function h, js to trigger
  506. ];
  507. }
  508. public function createSaveAjaxAction() {
  509. Response::sendTryCatchJson(array($this, 'createSaveAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  510. }
  511. public function createSaveAjax($args) {
  512. throw new Exception("TODO: ...");
  513. }
  514. public function createSaveLegacyAction() { // TODO: Legacy RM
  515. $args = [
  516. 'namespace' => V::get('namespace', '', $_GET),
  517. 'body' => Request::getRequestJson(),
  518. ];
  519. DBG::log($args, 'array', 'createSaveLegacyAction');
  520. Response::sendTryCatchJson(array($this, 'createSaveLegacy'), $args);
  521. }
  522. public function createSaveLegacy($args) { // TODO: Legacy RM
  523. $namespace = V::get('namespace', '', $args, 'word');
  524. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  525. $body = V::get('body', null, $args);
  526. if (!$body) throw new HttpException("Bad Request - missing body", 400);
  527. $acl = Core_AclHelper::getAclByNamespace($namespace);
  528. $tbl = $this->getTableAjaxWidget($acl);
  529. DBG::log($args, 'array', "ajaxCreateSave");
  530. $createdId = null;
  531. try {
  532. $item = $acl->convertObjectFromUserInput($body, $type = 'array_by_id', $prefix = 'f');
  533. $createdId = $acl->addItem($item);
  534. if ($createdId) {
  535. return [
  536. 'type' => 'success',
  537. 'msg' => "Utworzono pomyślnie rekord nr {$createdId}",
  538. 'id' => $createdId,
  539. 'record' => $acl->buildQuery([])->getItem($createdId),
  540. ];
  541. }
  542. else {
  543. return [
  544. 'type' => 'error',
  545. 'msg' => "Nie udało się utworzyć nowego rekordu!",
  546. ];
  547. }
  548. }
  549. catch (Exception $e) {
  550. return [
  551. 'type' => 'error',
  552. 'msg' => $e->getMessage(),
  553. ];
  554. }
  555. }
  556. public function editFormAction() { // TODO: not used - moved to editFormJsonAction
  557. try {
  558. $args = $_REQUEST;
  559. $id = V::get('_primaryKey', 0, $args, 'int');
  560. if ($id <= 0) throw new HttpException("Bad Request - missing primaryKey", 400);
  561. $namespace = V::get('namespace', '', $args, 'word');
  562. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  563. $acl = Core_AclHelper::getAclByNamespace($namespace);
  564. $tbl = $this->getTableAjaxWidget($acl);
  565. $tbl->sendAjaxEdit($id, $args);
  566. } catch (Exception $e) {
  567. DBG::log($e);
  568. throw $e;
  569. }
  570. }
  571. public function editFormJsonAction() {
  572. Response::sendTryCatchJson(array($this, 'editFormJson'), $args = $_REQUEST);
  573. }
  574. public function editFormJson($args) { // namespace, _hash, _primaryKey
  575. $id = V::get('_primaryKey', 0, $args, 'int');
  576. if ($id <= 0) throw new HttpException("Bad Request - missing primaryKey", 400);
  577. $namespace = V::get('namespace', '', $args, 'word');
  578. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  579. $acl = Core_AclHelper::getAclByNamespace($namespace);
  580. $tbl = $this->getTableAjaxWidget($acl);
  581. $record = $acl->buildQuery([])->getItem($id);
  582. if (!$acl->canWriteRecord($record) && !$acl->hasPermSuperWrite()) {
  583. return [
  584. 'type' => "success",
  585. 'msg' => "Edycja rekordu nr {$id}",
  586. 'body' => [
  587. 'reactNode' => [ 'div', [ 'class' => "alert alert-danger" ], "Brak dostępu do rekordu" ]
  588. ],
  589. ];
  590. // throw new Exception("Brak dostępu do rekordu");
  591. }
  592. $fieldsList = array();
  593. foreach ($acl->getFieldListByIdZasob() as $kID => $fieldName) {
  594. if ($fieldName == 'ID') continue;
  595. $field['name'] = $fieldName;
  596. $field['opis'] = $acl->getFieldOpis($fieldName);
  597. $field['label'] = $acl->getFieldLabel($fieldName);
  598. if (empty($field['label'])) $field['label'] = str_replace('_', ' ', $fieldName);
  599. $fieldsList[$kID] = $field;
  600. }
  601. $cols = array();
  602. foreach ($fieldsList as $kID => $field) {
  603. $cols[$kID] = '';
  604. if ($acl->canReadObjectField($field['name'], $record)) {
  605. $cols[$kID] = V::get($field['name'], '', $record);
  606. } else {
  607. $cols[$kID] = '*****';
  608. }
  609. $cols[$kID] = V::get("f{$kID}", $cols[$kID], $_POST);
  610. }
  611. $tsValues = array();
  612. if (!empty($fieldsList)) {
  613. foreach ($fieldsList as $vColID => $vCol) {
  614. $typeSpecial = Typespecial::getInstance($vColID, $vCol['name']);
  615. if ($typeSpecial) {
  616. $colValue = V::get($vCol['name'], '', $record);
  617. $specialValues = $typeSpecial->getEditSelectedValuesByIds($acl->getID(), $record['ID'], $vCol['name'], $colValue);
  618. if (!empty($specialValues)) {
  619. $tsValues[$vColID] = implode('<br>', $specialValues);
  620. }
  621. }
  622. }
  623. }
  624. DBG::log($tsValues, 'array', "editFormJson::tsValues");
  625. foreach ($tsValues as $idx => $value) {
  626. if ('<' === substr($value, 0, 1)) {
  627. // $tsValues[$idx] = UI::convertHtmlToArray($value); // TODO: ...
  628. $tsValues[$idx] = [ 'P5UI__RawHtml', [ 'rawHtml' => $tsValues[$idx] ] ];
  629. }
  630. }
  631. DBG::log($tsValues, 'array', "editFormJson::tsValues parsed");
  632. $featureFunctions = [
  633. // 'edit' => [ 'href' => '#EDIT/{0}', 'ico' => 'glyphicon glyphicon-pencil', 'title' => "Edytuj rekord"],
  634. 'hist' => [ 'href' => '#HIST/{0}', 'ico' => 'glyphicon glyphicon-book', 'title' => "Historia" ],
  635. 'files' => [ 'href' => '#FILES/{0}', 'ico' => 'glyphicon glyphicon-folder-open', 'title' => "Pliki" ],
  636. // 'cp' => [ 'href' => '#', 'ico' => 'glyphicon glyphicon-plus-sign', 'title' => "Kopiuj rekord", 'onclick' => 'return tableAjaxCopy({0});' ],
  637. 'msgs' => [ 'href' => "index.php?_route=TableMsgs&_task=tableRow&idTable=".$acl->getID()."&idRow={0}", 'ico' => 'glyphicon glyphicon-envelope', 'title' => "Wiadomości" ],
  638. ];
  639. $rowFunctionsOut = [ 'P5UI__FeatureRowFunctions', [
  640. 'id' => $record[ $acl->getPrimaryKeyField() ],
  641. 'functions' => $featureFunctions,
  642. 'showLabels' => true,
  643. 'viewMoreDropdown' => [
  644. 'primaryKey' => $record['ID'],
  645. 'uri' => $this->getLink('moreFunctionsCellAjax', [ 'namespace' => $acl->getNamespace(), 'ID' => $record['ID'] ]),
  646. ],
  647. ] ]; // TODO: $this->_showRowFunctions($record['ID'], array('edit', 'cp'), true);
  648. $jsFields = [];
  649. $tabindex = 0;
  650. foreach ($fieldsList as $kID => $vCol) {
  651. $fieldName = $vCol['name'];
  652. DBG::log(['$fieldName'=>$fieldName, '$record'=>$record, 'canWrite'=>$acl->canWriteObjectField($fieldName, $record), 'canRead'=>$acl->canReadObjectField($fieldName, $record)], 'array', "form field");
  653. if ($acl->canWriteObjectField($fieldName, $record)) {
  654. DBG::log("editFormJson::field({$fieldName})");
  655. $fieldParams = [ 'appendBack' => true, 'tabindex' => (++$tabindex), 'maxGrid' => 8 ];
  656. if (!empty($tsValues[$kID])) $fieldParams['typespecialValue'] = $tsValues[$kID];
  657. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  658. [ 'label', [ 'class' => "control-label", 'for' => "f{$kID}" ], [
  659. [ 'span', [ 'style' => ['padding-right'=>'4px'] ], $vCol['label'] ],
  660. [ 'i', [ 'class' => "glyphicon glyphicon-info-sign frm-help", 'data-toggle' => "popover", 'data-trigger' => "hover", 'title' => "", 'data-content' => htmlspecialchars($vCol['opis']), 'data-original-title' => "[{$kID}] {$fieldName}" ] ],
  661. ] ],
  662. [ 'div', [ 'class' => "" ], [
  663. UI::hGetFormItem($acl, $fieldName, 'W', $kID, "f{$kID}", $cols[$kID], $fieldParams, $record),
  664. ] ]
  665. ] ];
  666. } else if ($acl->canReadObjectField($fieldName, $record)) {
  667. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  668. [ 'label', [ 'class' => "control-label", 'for' => "f{$kID}" ], [
  669. [ 'span', [ 'style' => ['padding-right'=>'4px'] ], $vCol['label']],
  670. [ 'i', [ 'class' => "glyphicon glyphicon-info-sign frm-help", 'data-toggle' => "popover", 'data-trigger' => "hover", 'title' => "", 'data-content' => htmlspecialchars($vCol['opis']), 'data-original-title' => "[{$kID}] {$fieldName}" ] ],
  671. ] ],
  672. [ 'div', [ 'class' => "" ], [
  673. ['p', [ 'style' => [ 'margin-top' => '5px' ] ], [
  674. (!empty($tsValues[$kID]))
  675. ? $tsValues[$kID]
  676. : V::get($fieldName, '', $record)
  677. ] ],
  678. ] ]
  679. ] ];
  680. // } else {
  681. // $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  682. // "TODO: SKIP field ({$fieldName}) - ! canWriteObjectField && ! canReadObjectField"
  683. // ]];
  684. }
  685. }
  686. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  687. [ 'div', [ 'class' => "" ], [
  688. ['button', [ 'type' => "submit", 'class' => "btn btn-primary", 'tabindex' => ++$tabindex ], "Zapisz" ]
  689. ] ]
  690. ] ];
  691. $tblLabel = $acl->getNamespace();
  692. if ('default_db' == $acl->getSourceName()) {
  693. $tblLabel = array();
  694. $zasobObj = ProcesHelper::getZasobTableInfo($acl->getID());
  695. if (!$zasobObj) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
  696. if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
  697. if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
  698. $tblLabel = implode(" - ", $tblLabel);
  699. }
  700. $syncUrl = Request::getPathUri() . 'index.php?_route=ViewTableAjax&namespace=' . $acl->getNamespace();
  701. $jsGui = [
  702. 'reactNode' => [ 'div', [ 'class' => "container AjaxFrmHorizontalEdit", 'style' => [ "max-width" => "940px" ] ], [
  703. [ 'h4', [ 'style' => [ "padding-bottom" => "3px", "border-bottom" => "1px solid #ddd" ] ], [
  704. "Edycja rekordu Nr {$record['ID']}",
  705. [ 'small', [ 'class' => "pull-right valign-btns-bottom" ], [ $rowFunctionsOut ] ],
  706. ] ],
  707. [ 'P5UI__FeatureEditForm', [
  708. 'class' => "", 'action' => "", 'method' => "post",
  709. 'id' => "EDIT_FRM_{$this->_htmlID}", // TODO: rm - use React nodes // TODO: $this->_htmlID not exists!
  710. 'ajaxSaveUrl' => "{$syncUrl}&_task=editSaveAjax", // TODO:? &_hash={$this->_htmlID}
  711. 'namespace' => $acl->getNamespace(),
  712. 'idRecord' => $record['ID'],
  713. 'tableLabelHtml' => $tblLabel,
  714. ], [
  715. [ 'fieldset', [ 'style' => [ "padding-bottom" => "100px" ] ], $jsFields ] // fieldset
  716. ] ] // form
  717. ] ] // .container
  718. ];
  719. return [
  720. 'type' => "success",
  721. 'msg' => "Edycja rekordu nr {$id}",
  722. 'body' => $jsGui, // TODO: action for GUI: array to render by function h, js to trigger
  723. ];
  724. }
  725. public function editSaveAjaxAction() {
  726. Response::sendTryCatchJson(array($this, 'editSaveAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  727. }
  728. public function editSaveAjax($args) {
  729. $namespace = V::get('namespace', '', $args, 'word');
  730. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  731. $acl = Core_AclHelper::getAclByNamespace($namespace);
  732. $primaryKeyField = $acl->getPrimaryKeyField();
  733. $primaryKey = V::get('primaryKey', 0, $args, 'int');
  734. if (empty($primaryKey)) throw new HttpException("Bad Request - missing primaryKey!", 400);
  735. $item = $acl->getItem($primaryKey);
  736. if (!$item) throw new HttpException("Item not exists!", 404);
  737. $itemFromUser = $acl->convertObjectFromUserInput($args['form'], $type = 'array_by_id', $prefix = 'f');
  738. $response = new stdClass();
  739. $response->primaryKey = $primaryKey;
  740. try {
  741. $itemFromUser[$primaryKeyField] = $primaryKey;
  742. $affected = $acl->updateItem($itemFromUser);
  743. if ($affected > 0) {
  744. $response->type = 'success';
  745. $response->msg = "Rekord zapisany pomyślnie";//"Record saved successfully";
  746. } else if ($affected == 0) {
  747. $response->type = 'info';
  748. $response->msg = "Nie wprowadzono żadnych zmian";
  749. }
  750. $response->record = $acl->getItem($primaryKey);
  751. $rowFunList = Core_AclHelper::getMoreFunctionsCell($acl, array('primary_key'=>$primaryKey, 'record'=>$response->record));
  752. if (!empty($rowFunList)) $response->rowFunctions = $rowFunList;
  753. }
  754. catch (Exception $e) {
  755. $response->type = 'error';
  756. $response->msg = "Wystąpiły błędy!";
  757. $response->msg .= $e->getMessage();
  758. }
  759. return $response;
  760. }
  761. public function typeSpecialCellAction() {
  762. Response::sendTryCatchJson(array($this, 'typeSpecialCell'), $args = $_REQUEST);
  763. }
  764. public function typeSpecialCell($args) {
  765. $namespace = V::get('namespace', '', $args, 'word');
  766. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  767. $acl = Core_AclHelper::getAclByNamespace($namespace);
  768. $id = V::get('ID', 0, $args, 'int');
  769. $fieldName = V::get('col', '', $args);
  770. if ($id <= 0 || empty($fieldName)) throw new HttpException("Bad Request - missing id or col", 400);
  771. $col = $fieldName;// TODO: RM $col
  772. $jsonData = new stdClass();
  773. $idField = $acl->getFieldIdByName($fieldName);
  774. if (!$idField) throw new Exception("Wrong field");
  775. $item = $acl->getItem($id);
  776. if (!$acl->canReadObjectField($fieldName, $item)) throw new Exception("Brak dostępu");
  777. $typeSpecial = Typespecial::getInstance($idField, $fieldName);
  778. if ($typeSpecial) {
  779. $jsonData->data = $typeSpecial->getReturnData($acl->getID(), $id, $fieldName, '');
  780. $jsonData->namespace = 'default_db/' . V::get('tbl_name', '', $jsonData->data);
  781. }
  782. return $jsonData;
  783. }
  784. /**
  785. * @param $_GET['namespace'] = AclNamespace
  786. * @param $_GET['format'] = 'csv' | 'html'
  787. * @param $_GET['flds'] = csv - coma separated field names
  788. * @param $_GET['sortCol'] = FieldName
  789. * @param $_GET['sortDir'] = SortDir ('desc' | 'asc')
  790. * @param $_GET['f_{$fieldName}'] = filter
  791. * @param $_GET['sf_{$fieldName}'] = force filter
  792. */
  793. public function exportAction() {
  794. $args = $_GET;
  795. $namespace = V::get('namespace', '', $args, 'word');
  796. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  797. $acl = Core_AclHelper::getAclByNamespace($namespace);
  798. $exportLimit = 10000;
  799. $params = array();
  800. $params['limit'] = $exportLimit;
  801. // $params['limitstart'] = 0;
  802. $params['order_by'] = V::get('sortCol', '', $args);
  803. $params['order_dir'] = V::get('sortDir', '', $args);
  804. $params['cols'] = array($acl->getPrimaryKeyField());
  805. $toExportFields = explode(',', V::get('flds', '', $_GET));
  806. if (empty($toExportFields)) throw new Exception("Nie wybrano żandych pól do exportu.");
  807. $allowedExportFieldList = Core_AclHelper::getExportFieldList($acl);
  808. foreach ($toExportFields as $fieldName) {
  809. if ($fieldName == $acl->getPrimaryKeyField()) continue;
  810. if (!in_array($fieldName, $allowedExportFieldList)) throw new Exception("Brak uprawnień do exportu pola '{$fieldName}'");
  811. $params['cols'][] = $fieldName;
  812. }
  813. $labels = array();
  814. foreach ($toExportFields as $fieldName) {
  815. $labels[ $fieldName ] = $acl->getFieldLabel($fieldName);
  816. }
  817. foreach ($args as $k => $v) {
  818. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && strlen($v) > 0) {// filter prefix
  819. $params[$k] = $v;
  820. }
  821. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && strlen($v) > 0) {// special filter prefix
  822. $params[$k] = $v;
  823. }
  824. }
  825. try {
  826. $queryFeatures = $acl->buildQuery($params);
  827. $total = $queryFeatures->getTotal();
  828. $listItems = $queryFeatures->getItems();
  829. $primaryKeyField = $acl->getPrimaryKeyField();
  830. $items = []; foreach ($listItems as $item) $items[ $item[$primaryKeyField] ] = $item;
  831. } catch (Exception $e) {
  832. DBG::log($e);
  833. throw $e;
  834. }
  835. $format = V::get('format', 'html', $_GET);
  836. switch ($format) {
  837. case 'html': {
  838. UI::gora();
  839. echo UI::h('table', ['class'=>'table table-bordered table-hover'], [
  840. UI::h('thead', [], [
  841. UI::h('tr', [], array_map(function ($label) {
  842. return UI::h('th', [], $label);
  843. }, $labels))
  844. ]),
  845. UI::h('tbody', [], array_map(function ($item) use($labels) {
  846. return UI::h('tr', [], array_map(function ($fieldName) use ($item) {
  847. return UI::h('td', [], V::get($fieldName, '', $item));
  848. }, array_keys($labels)));
  849. }, $items)),
  850. ]);
  851. UI::dol();
  852. exit;
  853. }
  854. case 'csv_cp1250':
  855. case 'csv': {
  856. $csvFileName = "Tabela-" . $acl->getName() . "-" . date("Y-m-d_H_s");
  857. header('Content-Type: text/csv; charset=utf-8');
  858. header("Content-Disposition: attachment; filename={$csvFileName}.csv");
  859. $csvSeparator = ';';
  860. $csvHeader = implode($csvSeparator, array_map(function ($label) use ($item) {
  861. return '"' . addslashes($label) . '"';
  862. }, array_values($labels)));
  863. $csvRows = implode("\r\n", array_map(function ($item) use ($labels, $csvSeparator) {
  864. return implode($csvSeparator, array_map(function ($fieldName) use ($item) {
  865. return '"' . addslashes(V::get($fieldName, '', $item)) . '"';
  866. }, array_keys($labels)));
  867. }, $items));
  868. switch ($format) {
  869. case 'csv': echo $csvHeader . "\n" . $csvRows; exit;
  870. case 'csv_cp1250': echo iconv('utf-8', 'Windows-1250//IGNORE', $csvHeader) . "\r\n" . iconv('utf-8', 'Windows-1250//IGNORE', $csvRows); exit;
  871. die("Nieobsługiwane kodowanie danych csv.");
  872. }
  873. exit;
  874. }
  875. }
  876. die("Nieobsługiwany format danych.");
  877. }
  878. public function loadDataAjaxAction() {
  879. $namespace = V::get('namespace', '', $_REQUEST, 'word');
  880. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  881. $acl = Core_AclHelper::getAclByNamespace($namespace);
  882. $backRefFilter = [
  883. 'namespace' => V::get('backRefNS', '', $_REQUEST),
  884. 'primaryKey' => V::get('backRefPK', '', $_REQUEST),
  885. 'fieldName' => V::get('backRefField', '', $_REQUEST),
  886. ];
  887. $childRefFilter = [
  888. 'namespace' => V::get('childRefNS', '', $_GET),
  889. 'primaryKey' => V::get('childRefPK', '', $_GET),
  890. ];
  891. $tbl = $this->getTableAjaxWidget($acl, $backRefFilter, $childRefFilter);
  892. Response::sendTryCatchJson(array($tbl, 'ajaxData'), $args = $_GET);
  893. }
  894. public function uploadFilesAjaxAction() {
  895. Response::sendTryCatchJson([$this, 'uploadFilesAjax'], $args = $_POST);
  896. }
  897. public function uploadFilesAjax($args) {
  898. DBG::log($_FILES, 'array', "\$_FILES");
  899. DBG::log($args, 'array', "\$args");
  900. $namespace = V::get('namespace', '', $args, 'word');
  901. if (!$namespace) throw new Exception("Missing namespace");
  902. $primaryKey = V::get('primaryKey', '', $args, 'int');
  903. if ($primaryKey <= 0) throw new Exception("Missing primaryKey");
  904. if (empty($_FILES)) throw new Exception("Missing files");
  905. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  906. Lib::loadClass('FileUploader');
  907. Lib::loadClass('FoldersConfig');
  908. // $dbID = $acl->getDB();
  909. // $db = DB::getDB($dbID);
  910. // if (!$db) throw new HttpException("No DB ({$dbID})", 406);
  911. $record = $acl->buildQuery([])->getItem($primaryKey);
  912. DBG::log($record, 'array', "\$record");
  913. if (!$record) throw new HttpException("No item ID({$primaryKey})", 404);
  914. if (!$acl->canReadRecord($record)) throw new Exception("Brak uprawnień do odczytu");
  915. if (!$acl->canWriteRecord($record)) throw new Exception("Brak uprawnień do zapisu");
  916. $rootTableName = $acl->getRootTableName();
  917. $confTblName = "{$rootTableName}_COLUMN";
  918. $folderConfAll = FoldersConfig::getRawData();
  919. if (!FoldersConfig::hasConfig($confTblName)) throw new HttpException("Brak danych konfiguracyjnych ({$rootTableName})", 404);
  920. $folderConf = FoldersConfig::getAll($confTblName);
  921. DBG::log($folderConf, 'array', "\$folderConf");
  922. $uploader = new FileUploader($confTblName, (object)$record);
  923. if (!$uploader->setConfig($folderConf)) throw new HttpException("Błąd danych konfiguracyjnych ({$rootTableName})", 404);
  924. $uploader->findFolder();
  925. DBG::log($uploader, 'array', "\$uploader");
  926. // $errorMsg = '';
  927. // if (!empty($args['SCANS_COLUMN_ADD'])) {
  928. // $uploaded = $uploader->tryMoveFromScanAjax($errorMsg);
  929. // }
  930. // else {
  931. // $uploaded = $uploader->tryUploadAjax($errorMsg);
  932. // }
  933. $destPath = $uploader->getDestLocalPath($show_if_not_found = true);
  934. DBG::log($destPath, 'array', "\$destPath");
  935. if (!file_exists($destPath)) {
  936. if (!$uploader->tryCreateDestFolder($destPath)) throw new Exception("Wystąpił błąd podczas tworzenie katalogu dla rekordu '{$primaryKey}'");
  937. }
  938. $generateSafeFileName = function($destPath, $fileName) {
  939. if (!file_exists("{$destPath}/{$fileName}")) return $fileName;
  940. $infoPath = pathinfo($fileName);
  941. // pathinfo('/path/t1/t2/fileName.ext') = [
  942. // [dirname] => /path/t1/t2
  943. // [basename] => fileName.ext
  944. // [extension] => ext
  945. // [filename] => fileName
  946. // ]
  947. return $infoPath['filename'] . "--" . date("Y-m-d_H-i-s") . "." . $infoPath['extension'];
  948. };
  949. $moveActions = array_map(function ($file) use ($destPath, $generateSafeFileName) {
  950. $safeName = $generateSafeFileName($destPath, $file['name']);
  951. return [
  952. $file['tmp_name'],
  953. "{$destPath}/{$safeName}",
  954. $safeName,
  955. ];
  956. }, $_FILES);
  957. DBG::log($moveActions, 'array', "\$moveActions"); // [ [ srcPath, descPath ] ]
  958. $errorMsgs = [];
  959. $pkField = $acl->getSqlPrimaryKeyField();
  960. foreach ($moveActions as $fileMoveAction) {
  961. if (!move_uploaded_file($fileMoveAction[0], $fileMoveAction[1])) {
  962. $errorMsgs[] = "Nie udało się wgrać pliku '{$fileMoveAction[2]}'";
  963. } else {
  964. try {
  965. $affected = DB::getPDO($acl->getDB())->update($rootTableName, $pkField, $primaryKey, [
  966. 'M_DIST_FILES' => "Wrano plik '{$fileMoveAction[2]}'",
  967. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  968. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  969. ]);
  970. if ($affected) {
  971. DB::getPDO($acl->getDB())->insert("{$rootTableName}_HIST", [
  972. 'ID_USERS2' => $primaryKey,
  973. 'M_DIST_FILES' => "Wrano plik '{$fileMoveAction[2]}'",
  974. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  975. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  976. ]);
  977. }
  978. } catch (Exception $e) {
  979. DBG::log($e);
  980. $errorMsgs[] = $e->getMessage();
  981. }
  982. }
  983. }
  984. if (!empty($errorMsgs)) {
  985. return [
  986. 'type' => "error",
  987. 'msg' => "Wystąpiły błędy podczas wgrywania plików dla '{$primaryKey}'",
  988. 'errors' => $errorMsgs,
  989. ];
  990. }
  991. return [
  992. 'type' => "success",
  993. 'msg' => "Wgrano nowe pliki dla '{$primaryKey}'",
  994. ];
  995. }
  996. public function removeFileAjaxAction() {
  997. Response::sendTryCatchJson([$this, 'removeFileAjax'], $args = $_REQUEST);
  998. }
  999. public function removeFileAjax($args) { // ajaxFileRemove
  1000. $namespace = V::get('namespace', '', $args, 'word');
  1001. if (!$namespace) throw new Exception("Missing namespace");
  1002. $id = V::get('ID', 0, $args, 'int');
  1003. if ($id <= 0) throw new Exception("Missing ID");
  1004. $filename = V::get('filename', '', $args);
  1005. if (empty($filename)) throw new Exception("Nie wybrano pliku do usunięcia");
  1006. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1007. $dbID = $acl->getDB();
  1008. $db = DB::getPDO($dbID);
  1009. if (!$db) throw new HttpException("No DB ({$dbID})", 406);
  1010. $record = $acl->buildQuery([])->getItem($id);
  1011. if (!$record) throw new HttpException("No item ID({$id})", 404);
  1012. if (!$acl->canReadRecord($record)) throw new Exception("Brak uprawnień do odczytu");
  1013. if (!$acl->canWriteRecord($record)) throw new Exception("Brak uprawnień do zapisu");
  1014. Lib::loadClass('FileUploader');
  1015. Lib::loadClass('FoldersConfig');
  1016. $tblName = $acl->getName();
  1017. $confTblName = "{$tblName}_COLUMN";
  1018. $folderConfAll = FoldersConfig::getRawData();
  1019. if (!FoldersConfig::hasConfig($confTblName)) throw new HttpException("Brak danych konfiguracyjnych ({$tblName})", 404);
  1020. $folderConf = FoldersConfig::getAll($confTblName);
  1021. $uploader = new FileUploader($confTblName, (object)$record);
  1022. if (!$uploader->setConfig($folderConf)) throw new HttpException("Błąd danych konfiguracyjnych ({$tblName})", 404);
  1023. $uploader->findFolder();
  1024. $errorMsg = '';
  1025. $removed = $uploader->tryRemoveFromAjax($filename, $errorMsg);
  1026. if (!$removed) throw new Exception($errorMsg);
  1027. // $affected = DB::getPDO($acl->getDB())->update();
  1028. $rootTableName = $acl->getRootTableName();
  1029. $pkField = $acl->getSqlPrimaryKeyField();
  1030. $primaryKey = $id;
  1031. try {
  1032. $affected = DB::getPDO($acl->getDB())->update($rootTableName, $pkField, $primaryKey, [
  1033. 'M_DIST_FILES' => "Usunięto plik '{$filename}'",
  1034. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1035. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1036. ]);
  1037. if ($affected) {
  1038. DB::getPDO($acl->getDB())->insert("{$rootTableName}_HIST", [
  1039. 'ID_USERS2' => $primaryKey,
  1040. 'M_DIST_FILES' => "Usunięto plik '{$filename}'",
  1041. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1042. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1043. ]);
  1044. }
  1045. } catch (Exception $e) {
  1046. DBG::log($e);
  1047. }
  1048. return [
  1049. 'type' => 'success',
  1050. 'msg' => 'Plik został usunięty',
  1051. ];
  1052. }
  1053. public function procesInitFiltrAjaxAction() {
  1054. Response::sendTryCatchJson([$this, 'procesInitFiltrAjax'], $args = $_GET);
  1055. }
  1056. public function procesInitFiltrAjax($args) { // ajaxFileRemove
  1057. $namespace = V::get('namespace', '', $args, 'word');
  1058. if (!$namespace) throw new Exception("Missing namespace");
  1059. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1060. $pInitList = User::getAcl()->getTableProcesInitList($acl->getID());
  1061. DBG::log($pInitList, 'array', "\$pInitList");
  1062. if (!empty($pInitList)) {
  1063. $procesIds = array_keys($pInitList);
  1064. $mapTree = ACL::getProcesInitMapTreeOnlyIds($procesIds);
  1065. DBG::log($mapTree, 'array', "\$mapTree");
  1066. DBG::log($pInitList, 'array', "\$pInitList");
  1067. $pInitListSelected = User::getAcl()->getPermsFiltrProcesId();
  1068. return [
  1069. 'type' => 'success',
  1070. 'msg' => 'ok',
  1071. 'pInitData' => [
  1072. 'pInitList' => $pInitList,
  1073. 'mapTree' => $mapTree,
  1074. 'pInitListSelected' => $pInitListSelected,
  1075. ],
  1076. ];
  1077. }
  1078. return [
  1079. 'type' => 'success'
  1080. ];
  1081. }
  1082. public function tableToolsAjaxAction() {
  1083. Response::sendTryCatchJson([$this, 'tableToolsAjax'], $args = $_GET);
  1084. }
  1085. public function tableToolsAjax($args) { // ajaxFileRemove
  1086. $namespace = V::get('namespace', '', $args, 'word');
  1087. if (!$namespace) throw new Exception("Missing namespace");
  1088. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1089. $listUrlFunctions = Route_UrlAction::getTableFunctions($acl->getID(), $idRecord = 0, $acl->getName(), User::getLogin());
  1090. DBG::log($listUrlFunctions, 'array', "\$listUrlFunctions");
  1091. $listUrlFunctions = array_map(function ($urlFunction) use ($namespace) {
  1092. if ('index.php?' === substr($urlFunction['baseLink'], 0, strlen('index.php?'))) $urlFunction['baseLink'] .= "&_fromNamespace={$namespace}";
  1093. return $urlFunction;
  1094. }, $listUrlFunctions);
  1095. return [
  1096. 'type' => "success",
  1097. 'msg' => 'ok',
  1098. 'body' => [
  1099. 'tableTools' => array_values(array_map(function ($urlFunction) {
  1100. return [
  1101. 'url' => $urlFunction['baseLink'],
  1102. 'label' => $urlFunction['label'],
  1103. // TODO: $urlFunction['link_target'] // "_blank"
  1104. // ? $urlFunction['name']
  1105. ];
  1106. }, array_filter($listUrlFunctions, function ($urlFunction) {
  1107. return empty($urlFunction['cell_id_params']);
  1108. }))),
  1109. ],
  1110. ];
  1111. }
  1112. }