ViewTableAjax.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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. if (V::get('DBG_INST', '', $_GET)) { // TODO: TEST namespace
  97. $siblings = ACL::getNamespaceSiblings($namespace);
  98. DBG::nicePrint($siblings, '$siblings');
  99. $filtrInstance = V::get('f_instance', [], $_POST, 'array');
  100. DBG::nicePrint($filtrInstance, '$filtrInstance');
  101. $sibling = 'default_db/CRM_PROCES/PROCES_INIT'; DBG::nicePrint(array_merge(['type'=>"radio", 'name'=>"f_instance[{$sibling}]", 'value'=>'YES'], ('YES' === V::get($sibling, '', $filtrInstance)) ? ['checked' => "checked"] : []), "merge {$sibling} YES");
  102. $sibling = 'default_db/CRM_PROCES/PROCES_INIT'; DBG::nicePrint(array_merge(['type'=>"radio", 'name'=>"f_instance[{$sibling}]", 'value'=>'NO'], ('NO' === V::get($sibling, '', $filtrInstance)) ? ['checked' => "checked"] : []), "merge {$sibling} NO");
  103. $_ = array(UI, 'h');
  104. echo $_('form', ['method' => "POST", 'style' => "width:600px; border:1px solid #ddd; border-radius:2px"], [
  105. $_('div', ['style' => "background-color:#ddd"], "Test Filtr instancji"),
  106. $_('div', ['style' => "padding:8px"], array_map(function ($sibling) use ($filtrInstance, $_) {
  107. return $_('div', [], [
  108. $_('label', ['style' => "margin:0 8px"], [
  109. $_('input', array_merge(['type'=>"radio", 'name'=>"f_instance[{$sibling}]", 'value'=>'YES'], ('YES' === V::get($sibling, '', $filtrInstance)) ? ['checked' => "checked"] : [])),
  110. " TAK "
  111. ]),
  112. $_('label', ['style' => "margin:0 8px"], [
  113. $_('input', array_merge(['type'=>"radio", 'name'=>"f_instance[{$sibling}]", 'value'=>'NO'], ('NO' === V::get($sibling, '', $filtrInstance)) ? ['checked' => "checked"] : [])),
  114. " NIE "
  115. ]),
  116. $_('label', ['style' => "margin:0 8px"], [
  117. $_('input', ['type'=>"radio", 'name'=>"f_instance[{$sibling}]", 'value'=>'x']),
  118. " pomiń "
  119. ]),
  120. // $_('button', ['onClick'=>"this.form.f_instance['{$sibling}'].value = ''; return false"], "odznacz"),
  121. $_('button', ['onClick'=>"console.log(this.form.elements['f_instance[{$sibling}]']); this.form.elements['f_instance[{$sibling}]'].value = 'x'; return false"], "odznacz"),
  122. " - {$sibling}"
  123. ]);
  124. }, $siblings)),
  125. $_('div', [], [
  126. $_('input', ['type'=>"hidden", 'name'=>'_route', 'value'=>"ViewObject"]),
  127. $_('input', ['type'=>"hidden", 'name'=>'namespace', 'value'=>$namespace]),
  128. $_('input', ['type'=>"submit", 'value'=>"Filtruj - TEST"]),
  129. ]),
  130. ]);
  131. DBG::nicePrint($_POST, '$_POST');
  132. {
  133. $args = $_POST;
  134. $fIsInstance = [];
  135. $fIsNotInstance = [];
  136. if (!empty($args['f_instance'])) {
  137. foreach ($args['f_instance'] as $inst => $selected) {
  138. if ('YES' === $selected) {
  139. $fIsInstance[] = $inst;
  140. } else if ('NO' === $selected) {
  141. $fIsNotInstance[] = $inst;
  142. }
  143. }
  144. }
  145. }
  146. $queryFeatures = $acl->buildQuery([
  147. // TODO: 'propertyName' => "*,@instance",
  148. 'f_is_instance' => $fIsInstance,
  149. 'f_is_not_instance' => $fIsNotInstance,
  150. '@instances' => '1',
  151. 'limit' => 10
  152. ]);
  153. // $total = $queryFeatures->getTotal();
  154. $items = $queryFeatures->getItems();
  155. $rootNamespace = $acl->getRootNamespace();
  156. DBG::nicePrint($rootNamespace, '$rootNamespace');
  157. $jsRenderFunName = 'render_dropdown_instances_' . substr(md5(time()), 0, 6);
  158. DBG::nicePrint($jsRenderFunName, '$jsRenderFunName');
  159. UI::table([
  160. 'rows' => array_map(function($row) use ($namespace, $siblings, $rootNamespace, $jsRenderFunName) {
  161. return [
  162. 'ID' => $row['ID'],
  163. 'DESC' => $row['DESC'],
  164. 'TYPE' => $row['TYPE'],
  165. 'Typ' => UI::h('div', ['class'=>"p5UI__dropdown-wrap"], [
  166. UI::h('button', [
  167. // 'onClick' => "p5UI__dropdown(event, this, 'left bottom')",
  168. 'onClick' => "p5UI__dropdown(event, this, 'left bottom', {$jsRenderFunName}({$row['ID']}))",
  169. 'class' => "btn btn-xs btn-default p5UI__dropdown-btn"
  170. ], [
  171. UI::h('i', ['class' => "glyphicon glyphicon-tags", 'title' => "Ustaw typ danych (instancje)"]),
  172. ]),
  173. UI::h('div', ['class' => "p5UI__dropdown-content"]
  174. , array_merge(
  175. [
  176. UI::h('input', ['type' => "text", 'placeholder' => "Search..", 'class' => "p5UI__dropdown-input", 'onkeyup' => "p5_ViewObject_instances_filterInput(this)"], null),
  177. ]
  178. , array_map(function ($sibling) use ($row, $namespace, $rootNamespace) {
  179. return UI::h('div', ['label'=>$sibling, 'style'=>"padding:4px 0"], [
  180. UI::h('button', array_merge(['class' => "btn btn-xs btn-default",
  181. 'style' => "margin:0 4px 0 0",
  182. 'title' => "Ustaw instancje '{$sibling}'",
  183. 'onClick' => "return p5_ViewObject_instance_set(this, '{$row['ID']}', '{$sibling}', 'yes')"],
  184. (in_array($sibling, explode(',', $row['@instances']))) ? ['disabled' => "disabled"] : []
  185. ), "+"),
  186. UI::h('button', ['class' => "btn btn-xs btn-default",
  187. 'style' => "margin:0 4px 0 0",
  188. 'title' => "Usuń instancje '{$sibling}'",
  189. 'onClick' => "return p5_ViewObject_instance_set(this, '{$row['ID']}', '{$sibling}', 'no')"], "-"),
  190. UI::h('span', [], substr($sibling, strlen($rootNamespace) + 1)),
  191. ]);
  192. }, $siblings)
  193. )
  194. )
  195. ]),
  196. 'instancesList' => implode(', ', ACL::getFeatureNamespaces($namespace, $row['ID'])),
  197. ];
  198. }, $items)
  199. ]);
  200. echo UI::h('style', ['type' => "text/css"], "
  201. .p5UI__dropdown-content { min-width:400px; border:1px solid #ccc; background-color: #fff; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2) }
  202. .p5UI__dropdown-content .p5UI__dropdown-item { display:block; color:#000; padding:4px; text-decoration:none }
  203. .p5UI__dropdown-content .p5UI__dropdown-item:hover { background-color:#ebebeb }
  204. ");
  205. echo UI::h('script', ['src'=>"static/vendor.js", 'type'=>"text/javascript"]);
  206. $jsArgs = [
  207. 'SET_INSTANCE_URL' => $this->getLink('setInstanceAjax'),
  208. 'NAMESPACE' => $namespace,
  209. 'DBG' => DBG::isActive() ? 'true' : 'false',
  210. ];
  211. UI::inlineJS(__FILE__ . '.instancesDropdown.js', [
  212. 'JS_GLOBAL_FUNCTION_NAME' => $jsRenderFunName,
  213. 'NAMESPACE' => $namespace,
  214. 'SET_INSTANCE_URL' => $this->getLink('setInstanceAjax'),
  215. 'DBG' => DBG::isActive() ? 1 : 0,
  216. 'INITIAL_DROPDOWN_DATA' => [
  217. // 'allowed_instances' => $siblings,
  218. 'allowed_instances' => array_map(function ($sibling) use ($rootNamespace) {
  219. return [
  220. 'namespace' => $sibling,
  221. 'label' => substr($sibling, strlen($rootNamespace) + 1)
  222. ];
  223. }, $siblings),
  224. 'items' => array_map(function($row) use ($namespace, $siblings, $rootNamespace, $jsRenderFunName) {
  225. return [
  226. 'pk' => $row['ID'], // TODO: $primaryKeyField
  227. 'instances' => explode(',', $row['@instances']),
  228. ];
  229. }, $items)
  230. ],
  231. ]);
  232. echo UI::h('script', [], "
  233. var SET_INSTANCE_URL = '{$jsArgs['SET_INSTANCE_URL']}'
  234. var NAMESPACE = '{$jsArgs['NAMESPACE']}'
  235. function p5_ViewObject_instance_set(n, pk, sibling, toConnect) {
  236. console.log('p5_ViewObject_instance_set pk('+pk+'), sibling('+sibling+'), toConnect('+toConnect+'), n', n);
  237. window.fetch(SET_INSTANCE_URL, {
  238. method: 'POST',
  239. headers: { 'Content-Type': 'application/json' },
  240. credentials: 'same-origin',
  241. body: JSON.stringify({
  242. namespace: NAMESPACE,
  243. primaryKey: pk,
  244. instance: sibling,
  245. toConnect: toConnect,
  246. })
  247. }).then(function (response) {
  248. return response.json()
  249. }).then(function (response) {
  250. p5UI__notifyAjaxCallback(response)
  251. console.log(response) // TODO: render list
  252. })
  253. }
  254. ");
  255. echo UI::h('script', [], "
  256. function p5_ViewObject_instances_filterInput(n) {
  257. var input, filter, ul, li, a, i, div;
  258. input = n // .id-myInput
  259. filter = input.value.toUpperCase()
  260. div = n.parentNode // .id-myDropdown
  261. a = div.getElementsByTagName('div')
  262. for (i = 0; i < a.length; i++) {
  263. if (a[i].getAttribute('label') && a[i].getAttribute('label').toUpperCase().indexOf(filter) > -1) {
  264. a[i].style.display = 'block'
  265. } else {
  266. a[i].style.display = 'none'
  267. }
  268. }
  269. }
  270. ");
  271. echo '<hr style="margin-top:300px">';
  272. exit;
  273. }
  274. echo $tbl->render();
  275. if (DBG::isActive() && V::get('DBG_ACL', '', $_GET)) {// test load perms
  276. Lib::loadClass('DebugExecutionTime');
  277. $dbgExecTime = new DebugExecutionTime();
  278. $dbgExecTime->activate();
  279. $dbgExecTime->log('start');
  280. UI::startContainer(['style'=>'border:1px solid red']);
  281. UI::tag('p', null, "TEST - load perms from db");
  282. $idTable = $acl->getID();
  283. UI::tag('p', null, "DBG idTable({$idTable})");
  284. if ($idTable > 0) {
  285. $dbgExecTime->log('before sql');
  286. $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
  287. $dbgExecTime->log('after sql', ['sql']);
  288. UI::table(['caption' => "from CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW", 'rows' => $aclTableRows]);
  289. $csvIdProces = array();
  290. foreach ($aclTableRows as $row) {
  291. if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
  292. }
  293. }
  294. $tableName = $acl->getName();
  295. $databaseName = DB::getPDO()->getDatabaseName();
  296. UI::table([
  297. 'caption' => "Cell to process",
  298. 'rows' => array_map(
  299. function ($row) use ($aclTableRows, $idTable) {
  300. $row['proces'] = array();
  301. $row['id_zasob'] = 0;
  302. $row['PERM_R'] = 0;
  303. $row['PERM_W'] = 0;
  304. $row['PERM_X'] = 0;
  305. $row['PERM_C'] = 0;
  306. $row['PERM_S'] = 0;
  307. $row['PERM_O'] = 0;
  308. $row['PERM_V'] = 0;
  309. $row['PERM_E'] = 0;
  310. foreach ($aclTableRows as $aclInfo) {
  311. if (strtolower($aclInfo['CELL_NAME']) == strtolower($row['COLUMN_NAME'])) {
  312. $row['proces'][] = $aclInfo['ID_PROCES'];
  313. $row['id_zasob'] = $aclInfo['ID_CELL'];
  314. $row['PERM_R'] += $aclInfo['PERM_R'];
  315. $row['PERM_W'] += $aclInfo['PERM_W'];
  316. $row['PERM_X'] += $aclInfo['PERM_X'];
  317. $row['PERM_C'] += $aclInfo['PERM_C'];
  318. $row['PERM_S'] += $aclInfo['PERM_S'];
  319. $row['PERM_O'] += $aclInfo['PERM_O'];
  320. $row['PERM_V'] += $aclInfo['PERM_V'];
  321. $row['PERM_E'] += $aclInfo['PERM_E'];
  322. }
  323. }
  324. $row['proces'] = (empty($row['proces']))
  325. ? "<i style=\"color:red\">Brak</i>"
  326. : implode(", ", $row['proces']);
  327. 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");
  328. return $row;
  329. }, DB::getPDO()->fetchAll("
  330. select t.TABLE_NAME, t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  331. from `information_schema`.`COLUMNS` t
  332. where t.TABLE_SCHEMA = '{$databaseName}'
  333. and t.TABLE_NAME like '{$tableName}'
  334. ")
  335. )
  336. ]);
  337. if (!empty($csvIdProces)) {
  338. $csvIdProces = implode(",", $csvIdProces);
  339. UI::tag('p', null, "DBG csvIdProces({$csvIdProces})");
  340. $userLogin = User::getLogin();
  341. $dbgExecTime->log('before sql');
  342. $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");
  343. $dbgExecTime->log('after sql', ['sql']);
  344. UI::table(['caption' => "from CRM_PROCES_idx_USER_to_PROCES_VIEW", 'rows' => $rows]);
  345. $userIdProces = array(); foreach ($rows as $row) $userIdProces[] = $row['ID_PROCES'];
  346. $userTablePerms = array();
  347. foreach ($aclTableRows as $row) {
  348. if (!in_array($row['ID_PROCES'], $userIdProces)) continue;
  349. if (array_key_exists($row['CELL_NAME'], $userTablePerms)) {
  350. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_R' ] += $row['PERM_R'];
  351. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_W' ] += $row['PERM_W'];
  352. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_X' ] += $row['PERM_X'];
  353. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_C' ] += $row['PERM_C'];
  354. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_S' ] += $row['PERM_S'];
  355. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_O' ] += $row['PERM_O'];
  356. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_V' ] += $row['PERM_V'];
  357. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_E' ] += $row['PERM_E'];
  358. } else {
  359. $userTablePerms[ $row['CELL_NAME'] ] = $row;
  360. unset($userTablePerms[ $row['CELL_NAME'] ][ 'TABLE_DESCRIPTION' ]);
  361. unset($userTablePerms[ $row['CELL_NAME'] ][ 'ID_PROCES' ]);
  362. unset($userTablePerms[ $row['CELL_NAME'] ][ 'FORM_TREAT' ]);
  363. }
  364. }
  365. UI::table(['caption' => "\$userTablePerms", 'rows' => $userTablePerms]);
  366. } else UI::alert('warning', "brak \$csvIdProces");
  367. $dbgExecTime->printDebug();
  368. UI::endContainer();
  369. }
  370. } catch (Exception $e) {
  371. UI::startContainer();
  372. UI::alert('danger', "<strong>Wystąpiły błędy!</strong> " . $e->getMessage());
  373. UI::endContainer();
  374. DBG::log($e);
  375. }
  376. UI::dol();
  377. }
  378. public function setInstanceAjaxAction() {
  379. Response::sendTryCatchJson(array($this, 'setInstanceAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  380. }
  381. public function setInstanceAjax($args) {
  382. // namespace => default_db/CRM_PROCES/PROCES_INIT
  383. // primaryKey => 6
  384. // instance => default_db/CRM_PROCES/PROCES_TREE
  385. // toConnect => yes
  386. if (empty($args['namespace'])) throw new Exception("Missing namespace");
  387. if (empty($args['primaryKey'])) throw new Exception("Missing primaryKey");
  388. if (empty($args['instance'])) throw new Exception("Missing instance");
  389. if (empty($args['toConnect'])) throw new Exception("Missing toConnect");
  390. if (!in_array($args['toConnect'], ['yes', 'no'])) throw new Exception("Wrong param toConnect");
  391. $idInstance = ACL::getInstanceId($args['instance']);
  392. throw new Exception("TODO: check instance config"); // if 'view' then set instance is not allowed
  393. // $instanceTable = ACL::getInstanceTable($args['namespace']);
  394. // switch ($args['toConnect']) {
  395. // case 'yes': {
  396. // // TODO: _HIST info - waiting
  397. // $ret = DB::getPDO()->execSql("
  398. // insert into `{$instanceTable}` (`pk`, `idInstance`)
  399. // values ( :pk , :idInstance )
  400. // ", [
  401. // 'pk' => $args['primaryKey'],
  402. // 'idInstance' => $idInstance,
  403. // ]);
  404. // // TODO: _HIST info - mark as done
  405. // } break;
  406. // case 'no': {
  407. // // TODO: _HIST info - waiting
  408. // $ret = DB::getPDO()->execSql("
  409. // delete from `{$instanceTable}`
  410. // where `pk` = :pk
  411. // and `idInstance` = :idInstance
  412. // ", [
  413. // 'pk' => $args['primaryKey'],
  414. // 'idInstance' => $idInstance,
  415. // ]);
  416. // // TODO: _HIST info - mark as done
  417. // } break;
  418. // }
  419. // return [
  420. // 'type' => 'success',
  421. // 'msg' => "Wprowadzono zmiany ({$ret})",
  422. // // TODO: 'data' => all instances for pk
  423. // ];
  424. }
  425. public function rmUserTableFilterAjaxAction() {
  426. Response::sendTryCatchJson(array($this, 'rmUserTableFilterAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  427. }
  428. public function rmUserTableFilterAjax($args) {
  429. $namespace = V::get('namespace', '', $args);
  430. $filtrName = V::get('filtrName', '', $args);
  431. if (!$namespace) throw new Exception("Missing namespace");
  432. if (!$filtrName) throw new Exception("Missing filtrName");
  433. $userFltrConfKey = "tableColFilters__" . User::getLogin();
  434. $currentFilters = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = '{$userFltrConfKey}' ");
  435. if (!$currentFilters) return [
  436. 'type' => 'warning',
  437. 'msg' => "Brak filtrów w bazie",
  438. ];
  439. $currentFilters = json_decode($currentFilters, 'assoc');
  440. unset($currentFilters[$namespace][$filtrName]);
  441. $affeced = DB::getPDO()->update('CRM_CONFIG', 'CONF_KEY', $userFltrConfKey, [
  442. 'CONF_VAL' => json_encode($currentFilters)
  443. ]);
  444. return [
  445. 'type' => 'success',
  446. 'msg' => 'Zapisano nowy filtr',
  447. 'data' => $currentFilters[$namespace]
  448. ];
  449. }
  450. public function addUserTableFilterAjaxAction() {
  451. Response::sendTryCatchJson(array($this, 'addUserTableFilterAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  452. }
  453. public function addUserTableFilterAjax($args) {
  454. $namespace = V::get('namespace', '', $args);
  455. $filtrName = V::get('filtrName', '', $args);
  456. $visibleCols = V::get('visibleCols', '', $args);
  457. if (!$namespace) throw new Exception("Missing namespace");
  458. if (!$filtrName) throw new Exception("Missing filtrName");
  459. if (!$visibleCols) throw new Exception("Missing visibleCols");
  460. $userFltrConfKey = "tableColFilters__" . User::getLogin();
  461. $currentFilters = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = '{$userFltrConfKey}' ");
  462. $currentFilters = ($currentFilters) ? json_decode($currentFilters, 'assoc') : [];
  463. $currentFilters[$namespace][$filtrName] = $visibleCols;
  464. $sqlFltr = json_encode($currentFilters);
  465. DB::getPDO()->execSql("
  466. insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
  467. values ('$userFltrConfKey', '{$sqlFltr}')
  468. on duplicate key update CONF_VAL = '{$sqlFltr}'
  469. ");
  470. return [
  471. 'type' => 'success',
  472. 'msg' => 'Zapisano nowy filtr',
  473. 'data' => $currentFilters[$namespace]
  474. ];
  475. }
  476. public function getUserTableFilterAjaxAction() {
  477. Response::sendTryCatchJson(array($this, 'getUserTableFilterAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  478. }
  479. public function getUserTableFilterAjax($args) {
  480. $namespace = V::get('namespace', '', $args);
  481. if (!$namespace) throw new Exception("Missing namespace");
  482. $userFltrConfKey = "tableColFilters__" . User::getLogin();
  483. $currentFilters = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = '{$userFltrConfKey}' ");
  484. $currentFilters = ($currentFilters) ? json_decode($currentFilters, 'assoc') : [];
  485. return [
  486. 'type' => 'success',
  487. 'msg' => 'Odczytano filtry użytkownika',
  488. 'data' => (!empty($currentFilters[$namespace])) ? $currentFilters[$namespace] : []
  489. ];
  490. }
  491. public function revertFromHistAjaxAction() {
  492. Response::sendTryCatchJson(array($this, 'revertFromHistAjax'));
  493. }
  494. public function revertFromHistAjax() {
  495. $typeName = V::get('typeName', '', $_REQUEST, 'word');
  496. if (!$typeName) throw new Exception("Wrong param typeName");
  497. // TODO: use namespace from url
  498. // $namespace = V::get('namespace', '', $_GET, 'word');
  499. // if (!$namespace) {
  500. // $typeName = V::get('typeName', '', $_GET, 'word');
  501. // if (!$typeName) throw new Exception("Wrong param typeName");
  502. // $namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
  503. // }
  504. // $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  505. $id = V::get('ID', '', $_REQUEST, 'word');
  506. if (!$id) throw new Exception("Wrong param ID");
  507. $idHist = V::get('idHist', '', $_REQUEST, 'word');
  508. if (!$idHist) throw new Exception("Wrong param idHist");
  509. $fieldName = V::get('fieldName', '', $_REQUEST, 'word');
  510. if (!$fieldName) throw new Exception("Wrong param fieldName");
  511. $acl = Core_AclHelper::getAclByTypeName($typeName);
  512. $item = $acl->getItem($id);
  513. if (!$item) throw new HttpException("Item not found", 404);
  514. if (!$acl->canWriteObjectField($fieldName, $record)) throw new Exception("Missing perm Write for field {$fieldName}");
  515. $histItem = $acl->getHistItem($id, $idHist);
  516. if (!$histItem) throw new HttpException("Hist Item not found", 404);
  517. $histValue = V::get($fieldName, 'N/S;', $histItem);
  518. if ('N/S;' == $histValue) throw new Exception("Missing field value in hist[{$idHist}] for field({$fieldName}) from item[{$id}]");
  519. if ($acl->isGeomField($fieldName)) {
  520. $wktType = strtoupper($acl->getGeomFieldType($fieldName));
  521. if (!$wktType) throw new Exception("Wrong geometry type for field {$fieldName}");
  522. if ($wktType != strtoupper(substr($histValue, 0, strlen($wktType)))) throw new Exception("Wrong geometry type for field {$fieldName} in hist value");
  523. $coords = trim(substr($histValue, strlen($wktType)), '()');
  524. $wktValue = $acl->convertGmlCoordsToWkt($wktType, $coords, ['cs'=>' ', 'ts'=>',']);
  525. if (!$wktValue) throw new Exception("BUG in hist record");
  526. $sqlObj = array();
  527. $sqlObj['ID'] = $id;
  528. $sqlObj[$fieldName] = "GeomFromText('{$wktValue}')";
  529. $affected = DB::getDB()->UPDATE_OBJ($acl->getName(), (object)$sqlObj);
  530. if (0 == $affected) throw new AlertInfoException("Nie wprowadzono żadnych zmian");
  531. else if ($affected < 0) throw new Exception("Wystąpiły błędy podczas aktualizacji rekordu [{$id}]");
  532. $jsonResponse = array();
  533. $jsonResponse['type'] = 'success';
  534. $jsonResponse['msg'] = "Zaktualizowano dane na podstawie wcześniejszej wartości dla rekordu [{$id}]";
  535. $jsonResponse['actions'] = array();
  536. $jsonResponse['actions'][] = ['jsFunction'=>'TableAjax__HIST_Route', 'args'=>[$id]];
  537. return $jsonResponse;
  538. } else {
  539. throw new HttpException("Not implemented - update from hist only for the geom field", 501);
  540. }
  541. throw new Exception("BUG: update field '{$fieldName}' in item[{$id}] from hist[{$idHist}]", 501);
  542. }
  543. public function removeTheGeomAjaxAction() {
  544. Response::sendTryCatchJson(array($this, 'removeTheGeomAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  545. }
  546. public function removeTheGeomAjax($args) {
  547. $namespace = V::get('namespace', '', $args, 'word');
  548. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  549. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  550. $primaryKeyField = $acl->getPrimaryKeyField();
  551. $primaryKey = V::get($primaryKeyField, 0, $args, 'int');
  552. $geomFieldName = 'the_geom';
  553. $response = new stdClass();
  554. if ($primaryKey <= 0) throw new HttpException("Bad Request - Wrong param ID", 400);
  555. $record = $acl->getItem($primaryKey);
  556. if (!$record) throw new HttpException("Nie odnaleziono rekordu nr {$primaryKey}", 404);
  557. if (!$acl->canWriteObjectField($geomFieldName, $record)) throw new HttpException("Brak dostępu do zapisu dla pola {$geomFieldName}", 403);
  558. if (empty($record->{$geomFieldName})) {
  559. $response->type = 'info';
  560. $response->msg = "Rekord nie jest powiązany z żadnym obiektem na mapie";
  561. $response->record = $record;
  562. return $response;
  563. }
  564. $itemPatch = array();
  565. $itemPatch[$geomFieldName] = "NULL";
  566. $itemPatch[$primaryKeyField] = $primaryKey;
  567. $response = new stdClass();
  568. try {
  569. $affected = $acl->updateItem($itemPatch);
  570. if ($affected > 0) {
  571. $response->type = 'success';
  572. $response->msg = "Usunięto obiekt z mapy dla rekordu {$primaryKey}";// Rekord zapisany pomyślnie
  573. } else if ($affected == 0) {
  574. $response->type = 'info';
  575. $response->msg = "Nie wprowadzono żadnych zmian";
  576. }
  577. $response->record = $acl->getItem($primaryKey);
  578. }
  579. catch (Exception $e) {
  580. $response->type = 'error';
  581. $response->msg = $e->getMessage();
  582. }
  583. return $response;
  584. }
  585. public function getCsvTheGeomAjaxAction() {
  586. try {
  587. self::getCsvTheGeomAjax();
  588. } catch (Exception $e) {
  589. SE_Layout::gora();
  590. SE_Layout::menu();
  591. SE_Layout::alert('danger', $e->getMessage());
  592. ?>
  593. <div style="text-align: center;">
  594. <a href="<?=$_SERVER['HTTP_REFERER']?>" class="btn btn-primary" style="width: 80px;">Powrót</a>
  595. </div>
  596. <?php
  597. SE_Layout::dol();
  598. }
  599. }
  600. public function getCsvTheGeomAjax() {
  601. Lib::loadClass('EpsgConversion');
  602. $namespace = V::get('namespace', '', $_GET, 'word');
  603. $acl = Core_AclHelper::getAclByNamespace($namespace);
  604. $table = $acl->getRootTableName();
  605. $id = V::get('id', 0, $_GET, 'int');
  606. $query = "select st_astext(`the_geom`) as `the_geom` from `{$table}` where `ID` = {$id}";
  607. try {
  608. $result = DB::getPDO()->fetchValue($query);
  609. } catch (Exception $e) {
  610. throw new Exception('Błąd zapytania SQL');
  611. }
  612. if (!preg_match('/^[[:alpha:]]+\((.*)\)$/', $result, $matches)) throw new Exception('Błąd danych georeferencyjnych');
  613. $points = explode(',', $matches[1]);
  614. $csv = implode("\n", array_map(function ($point, $i) {
  615. list($x, $y) = explode(" ", $point, 2);
  616. $wgs84 = EpsgConversion::LonLatToPUWGWGS84($x, $y);
  617. return $i++ . ',' . round($wgs84->y, 3) . ',' . round($wgs84->x, 3) . ',0,Pikieta';
  618. }, $points, range(1, count($points))));
  619. Response::sendCsv($csv, "{$table}.{$id}");
  620. }
  621. public function moreFunctionsCellAjaxAction() {
  622. Response::sendTryCatchJson(array($this, 'moreFunctionsCell'), $args = $_GET);
  623. }
  624. public function moreFunctionsCell($args) {// ajax task 'MORE_FUNCTIONS_CELL'
  625. $id = V::get('ID', 0, $args, 'int');
  626. if ($id <= 0) throw new HttpException("404", 404);
  627. $namespace = V::get('namespace', '', $args, 'word');
  628. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  629. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $args)));
  630. $response = new stdClass();
  631. $response->type = 'success';
  632. $response->msg = 'Funkcje';
  633. $response->rowFunctions = Core_AclHelper::getMoreFunctionsCell($acl, array('primary_key' => $id));
  634. return $response;
  635. }
  636. public function createFormAction() {// TODO: move to createFormJsonAction
  637. try {
  638. $args = $_REQUEST;
  639. $namespace = V::get('namespace', '', $args, 'word');
  640. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  641. $acl = Core_AclHelper::getAclByNamespace($namespace);
  642. $tbl = $this->getTableAjaxWidget($acl);
  643. $tbl->sendAjaxCreate();
  644. } catch (Exception $e) {
  645. DBG::log($e);
  646. throw $e;
  647. }
  648. }
  649. public function createFormJsonAction() {
  650. Response::sendTryCatchJson(array($this, 'createFormJson'), $args = $_REQUEST);
  651. }
  652. public function createFormJson($args) { // namespace, _hash, _primaryKey
  653. $namespace = V::get('namespace', '', $args, 'word');
  654. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  655. $acl = Core_AclHelper::getAclByNamespace($namespace);
  656. $tbl = $this->getTableAjaxWidget($acl);
  657. if (!Core_AclHelper::hasCreatePerms($acl)) {
  658. return [
  659. 'type' => "success",
  660. 'msg' => "Dodaj nowy rekord",
  661. 'body' => [
  662. 'reactNode' => [ 'div', [ 'class' => "alert alert-danger" ], "Brak uprawnień do utworzenia nowego rekordu." ]
  663. ],
  664. ];
  665. // throw new Exception("Brak uprawnień do utworzenia nowego rekordu.");
  666. }
  667. $fieldsList = array();
  668. foreach ($acl->getFieldListByIdZasob() as $kID => $fieldName) {
  669. if ($fieldName == 'ID') continue;
  670. $field['name'] = $fieldName;
  671. $field['opis'] = $acl->getFieldOpis($fieldName);
  672. $field['label'] = $acl->getFieldLabel($fieldName);
  673. if (empty($field['label'])) $field['label'] = str_replace('_', ' ', $fieldName);
  674. $fieldsList[$kID] = $field;
  675. }
  676. $cols = array();
  677. $forceFilterInit = array();
  678. $defaultAclGroup = User::getDefaultAclGroup();
  679. if ($defaultAclGroup) {
  680. $forceFilterInit['A_ADM_COMPANY'] = $defaultAclGroup;
  681. $forceFilterInit['A_CLASSIFIED'] = $defaultAclGroup;
  682. }
  683. foreach ($_GET as $k => $v) { // TODO: read from $args ?
  684. if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  685. $fldName = substr($k, 3);
  686. $forceFilterInit[$fldName] = $v;
  687. }
  688. }
  689. DBG::log($forceFilterInit, 'array', "\$forceFilterInit");
  690. foreach ($fieldsList as $kID => $field) {
  691. $defaultValue = '';
  692. if (!empty($forceFilterInit[$field['name']])) {
  693. $defaultValue = $forceFilterInit[$field['name']];
  694. }
  695. $cols[$kID] = V::get("f{$kID}", $defaultValue, $_POST);
  696. }
  697. DBG::log($cols, 'array', "\$cols - field values");
  698. $tsValues = array();
  699. $featureFunctions = [
  700. // 'edit' => [ 'href' => '#EDIT/{0}', 'ico' => 'glyphicon glyphicon-pencil', 'title' => "Edytuj rekord"],
  701. 'hist' => [ 'href' => '#HIST/{0}', 'ico' => 'glyphicon glyphicon-book', 'title' => "Historia" ],
  702. 'files' => [ 'href' => '#FILES/{0}', 'ico' => 'glyphicon glyphicon-folder-open', 'title' => "Pliki" ],
  703. // 'cp' => [ 'href' => '#', 'ico' => 'glyphicon glyphicon-plus-sign', 'title' => "Kopiuj rekord", 'onclick' => 'return tableAjaxCopy({0});' ],
  704. 'msgs' => [ 'href' => "index.php?_route=TableMsgs&_task=tableRow&idTable=".$acl->getID()."&idRow={0}", 'ico' => 'glyphicon glyphicon-envelope', 'title' => "Wiadomości" ],
  705. ];
  706. $jsFields = [];
  707. $tabindex = 0;
  708. foreach ($fieldsList as $kID => $vCol) {
  709. $fieldName = $vCol['name'];
  710. $fieldLabel = str_replace('<br>', ' ', $vCol['label']);
  711. DBG::log(['$fieldName'=>$fieldName, 'canCreate'=>$acl->canCreateField($fieldName)], 'array', "form field");
  712. if ($acl->canCreateField($fieldName)) {
  713. DBG::log("editFormJson::field({$fieldName})");
  714. $fieldParams = [ 'appendBack' => true, 'tabindex' => (++$tabindex), 'maxGrid' => 8 ];
  715. if (!empty($tsValues[$kID])) $fieldParams['typespecialValue'] = $tsValues[$kID];
  716. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  717. [ 'label', [ 'class' => "control-label", 'for' => "f{$kID}" ], [
  718. [ 'span', [ 'style' => ['padding-right'=>'4px'] ], $fieldLabel ],
  719. [ '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}" ] ],
  720. ] ],
  721. [ 'div', [ 'class' => "" ], [
  722. UI::hGetFormItem($acl, $fieldName, 'C', $kID, "f{$kID}", $cols[$kID], $fieldParams),
  723. ] ]
  724. ] ];
  725. // } else {
  726. // $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  727. // "TODO: SKIP field ({$fieldName}) - ! canWriteObjectField && ! canReadObjectField"
  728. // ]];
  729. }
  730. }
  731. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  732. [ 'div', [ 'class' => "" ], [
  733. ['button', [ 'type' => "submit", 'class' => "btn btn-primary", 'tabindex' => ++$tabindex ], "Zapisz" ]
  734. ] ]
  735. ] ];
  736. $tblLabel = $acl->getNamespace();
  737. if ('default_db' == $acl->getSourceName()) {
  738. $tblLabel = array();
  739. $zasobObj = ProcesHelper::getZasobTableInfo($acl->getID());
  740. if (!$zasobObj) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
  741. if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
  742. if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
  743. $tblLabel = implode(" - ", $tblLabel);
  744. }
  745. $syncUrl = Request::getPathUri() . 'index.php?_route=ViewTableAjax&namespace=' . $acl->getNamespace();
  746. $jsGui = [
  747. 'reactNode' => [ 'div', [ 'class' => "container AjaxFrmHorizontalEdit", 'style' => [ "max-width" => "940px" ] ], [
  748. [ 'h4', [ 'style' => [ "padding-bottom" => "3px", "border-bottom" => "1px solid #ddd" ] ], [
  749. "Dodaj nowy rekord",
  750. ] ],
  751. [ 'P5UI__FeatureCreateForm', [
  752. 'class' => "", 'action' => "", 'method' => "post",
  753. 'id' => "CREATE_FRM_{$this->_htmlID}", // TODO: rm - use React nodes // TODO: $this->_htmlID not exists!
  754. 'ajaxSaveUrl' => "{$syncUrl}&_task=createSaveAjax", // TODO:? &_hash={$this->_htmlID}
  755. 'ajaxSaveLegacyUrl' => "{$syncUrl}&_task=createSaveLegacy", // TODO: Legacy RM
  756. 'namespace' => $acl->getNamespace(),
  757. 'tableLabelHtml' => $tblLabel,
  758. '_htmlID' => $acl->getName(),
  759. ], [
  760. [ 'fieldset', [ 'style' => [ "padding-bottom" => "100px" ] ], $jsFields ] // fieldset
  761. ] ] // form
  762. ] ] // .container
  763. ];
  764. return [
  765. 'type' => "success",
  766. 'msg' => "Dodaj nowy rekord",
  767. 'body' => $jsGui, // TODO: action for GUI: array to render by function h, js to trigger
  768. ];
  769. }
  770. public function createSaveAjaxAction() {
  771. Response::sendTryCatchJson(array($this, 'createSaveAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  772. }
  773. public function createSaveAjax($args) {
  774. throw new Exception("TODO: ...");
  775. }
  776. public function createSaveLegacyAction() { // TODO: Legacy RM
  777. $args = [
  778. 'namespace' => V::get('namespace', '', $_GET),
  779. 'body' => Request::getRequestJson(),
  780. ];
  781. DBG::log($args, 'array', 'createSaveLegacyAction');
  782. Response::sendTryCatchJson(array($this, 'createSaveLegacy'), $args);
  783. }
  784. public function createSaveLegacy($args) { // TODO: Legacy RM
  785. $namespace = V::get('namespace', '', $args, 'word');
  786. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  787. $body = V::get('body', null, $args);
  788. if (!$body) throw new HttpException("Bad Request - missing body", 400);
  789. $acl = Core_AclHelper::getAclByNamespace($namespace);
  790. $tbl = $this->getTableAjaxWidget($acl);
  791. DBG::log($args, 'array', "ajaxCreateSave");
  792. $createdId = null;
  793. try {
  794. $item = $acl->convertObjectFromUserInput($body, $type = 'array_by_id', $prefix = 'f');
  795. $createdId = $acl->addItem($item);
  796. if ($createdId) {
  797. return [
  798. 'type' => 'success',
  799. 'msg' => "Utworzono pomyślnie rekord nr {$createdId}",
  800. 'id' => $createdId,
  801. 'record' => $acl->buildQuery([])->getItem($createdId),
  802. ];
  803. }
  804. else {
  805. return [
  806. 'type' => 'error',
  807. 'msg' => "Nie udało się utworzyć nowego rekordu!",
  808. ];
  809. }
  810. }
  811. catch (Exception $e) {
  812. return [
  813. 'type' => 'error',
  814. 'msg' => $e->getMessage(),
  815. ];
  816. }
  817. }
  818. public function editFormAction() { // TODO: not used - moved to editFormJsonAction
  819. try {
  820. $args = $_REQUEST;
  821. $id = V::get('_primaryKey', 0, $args, 'int');
  822. if ($id <= 0) throw new HttpException("Bad Request - missing primaryKey", 400);
  823. $namespace = V::get('namespace', '', $args, 'word');
  824. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  825. $acl = Core_AclHelper::getAclByNamespace($namespace);
  826. $tbl = $this->getTableAjaxWidget($acl);
  827. $tbl->sendAjaxEdit($id, $args);
  828. } catch (Exception $e) {
  829. DBG::log($e);
  830. throw $e;
  831. }
  832. }
  833. public function editFormJsonAction() {
  834. Response::sendTryCatchJson(array($this, 'editFormJson'), $args = $_REQUEST);
  835. }
  836. public function editFormJson($args) { // namespace, _hash, _primaryKey
  837. $id = V::get('_primaryKey', 0, $args, 'int');
  838. if ($id <= 0) throw new HttpException("Bad Request - missing primaryKey", 400);
  839. $namespace = V::get('namespace', '', $args, 'word');
  840. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  841. $acl = Core_AclHelper::getAclByNamespace($namespace);
  842. $tbl = $this->getTableAjaxWidget($acl);
  843. $record = $acl->buildQuery([])->getItem($id);
  844. if (!$acl->canWriteRecord($record) && !$acl->hasPermSuperWrite()) {
  845. return [
  846. 'type' => "success",
  847. 'msg' => "Edycja rekordu nr {$id}",
  848. 'body' => [
  849. 'reactNode' => [ 'div', [ 'class' => "alert alert-danger" ], "Brak dostępu do rekordu" ]
  850. ],
  851. ];
  852. // throw new Exception("Brak dostępu do rekordu");
  853. }
  854. $fieldsList = array();
  855. foreach ($acl->getFieldListByIdZasob() as $kID => $fieldName) {
  856. if ($fieldName == 'ID') continue;
  857. $field['name'] = $fieldName;
  858. $field['opis'] = $acl->getFieldOpis($fieldName);
  859. $field['label'] = $acl->getFieldLabel($fieldName);
  860. if (empty($field['label'])) $field['label'] = str_replace('_', ' ', $fieldName);
  861. $fieldsList[$kID] = $field;
  862. }
  863. $cols = array();
  864. foreach ($fieldsList as $kID => $field) {
  865. $cols[$kID] = '';
  866. if ($acl->canReadObjectField($field['name'], $record)) {
  867. $cols[$kID] = V::get($field['name'], '', $record);
  868. } else {
  869. $cols[$kID] = '*****';
  870. }
  871. $cols[$kID] = V::get("f{$kID}", $cols[$kID], $_POST);
  872. }
  873. $tsValues = array();
  874. if (!empty($fieldsList)) {
  875. foreach ($fieldsList as $vColID => $vCol) {
  876. $typeSpecial = Typespecial::getInstance($vColID, $vCol['name']);
  877. if ($typeSpecial) {
  878. $colValue = V::get($vCol['name'], '', $record);
  879. $specialValues = $typeSpecial->getEditSelectedValuesByIds($acl->getID(), $record['ID'], $vCol['name'], $colValue);
  880. if (!empty($specialValues)) {
  881. $tsValues[$vColID] = implode('<br>', $specialValues);
  882. }
  883. }
  884. }
  885. }
  886. DBG::log($tsValues, 'array', "editFormJson::tsValues");
  887. foreach ($tsValues as $idx => $value) {
  888. if ('<' === substr($value, 0, 1)) {
  889. // $tsValues[$idx] = UI::convertHtmlToArray($value); // TODO: ...
  890. $tsValues[$idx] = [ 'P5UI__RawHtml', [ 'rawHtml' => $tsValues[$idx] ] ];
  891. }
  892. }
  893. DBG::log($tsValues, 'array', "editFormJson::tsValues parsed");
  894. $featureFunctions = [
  895. // 'edit' => [ 'href' => '#EDIT/{0}', 'ico' => 'glyphicon glyphicon-pencil', 'title' => "Edytuj rekord"],
  896. 'hist' => [ 'href' => '#HIST/{0}', 'ico' => 'glyphicon glyphicon-book', 'title' => "Historia" ],
  897. 'files' => [ 'href' => '#FILES/{0}', 'ico' => 'glyphicon glyphicon-folder-open', 'title' => "Pliki" ],
  898. // 'cp' => [ 'href' => '#', 'ico' => 'glyphicon glyphicon-plus-sign', 'title' => "Kopiuj rekord", 'onclick' => 'return tableAjaxCopy({0});' ],
  899. 'msgs' => [ 'href' => "index.php?_route=TableMsgs&_task=tableRow&idTable=".$acl->getID()."&idRow={0}", 'ico' => 'glyphicon glyphicon-envelope', 'title' => "Wiadomości" ],
  900. ];
  901. $rowFunctionsOut = [ 'P5UI__FeatureRowFunctions', [
  902. 'id' => $record[ $acl->getPrimaryKeyField() ],
  903. 'functions' => $featureFunctions,
  904. 'showLabels' => true,
  905. 'viewMoreDropdown' => [
  906. 'primaryKey' => $record['ID'],
  907. 'uri' => $this->getLink('moreFunctionsCellAjax', [ 'namespace' => $acl->getNamespace(), 'ID' => $record['ID'] ]),
  908. ],
  909. ] ]; // TODO: $this->_showRowFunctions($record['ID'], array('edit', 'cp'), true);
  910. $jsFields = [];
  911. $tabindex = 0;
  912. foreach ($fieldsList as $kID => $vCol) {
  913. $fieldName = $vCol['name'];
  914. $fieldLabel = str_replace('<br>', ' ', $vCol['label']);
  915. DBG::log(['$fieldName'=>$fieldName, '$record'=>$record, 'canWrite'=>$acl->canWriteObjectField($fieldName, $record), 'canRead'=>$acl->canReadObjectField($fieldName, $record)], 'array', "form field");
  916. if ($acl->canWriteObjectField($fieldName, $record)) {
  917. DBG::log("editFormJson::field({$fieldName})");
  918. $fieldParams = [ 'appendBack' => true, 'tabindex' => (++$tabindex), 'maxGrid' => 8 ];
  919. if (!empty($tsValues[$kID])) $fieldParams['typespecialValue'] = $tsValues[$kID];
  920. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  921. [ 'label', [ 'class' => "control-label", 'for' => "f{$kID}" ], [
  922. [ 'span', [ 'style' => ['padding-right'=>'4px'] ], $fieldLabel ],
  923. [ '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}" ] ],
  924. ] ],
  925. [ 'div', [ 'class' => "" ], [
  926. UI::hGetFormItem($acl, $fieldName, 'W', $kID, "f{$kID}", $cols[$kID], $fieldParams, $record),
  927. ] ]
  928. ] ];
  929. } else if ($acl->canReadObjectField($fieldName, $record)) {
  930. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  931. [ 'label', [ 'class' => "control-label", 'for' => "f{$kID}" ], [
  932. [ 'span', [ 'style' => ['padding-right'=>'4px'] ], $fieldLabel ],
  933. [ '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}" ] ],
  934. ] ],
  935. [ 'div', [ 'class' => "" ], [
  936. ['p', [ 'style' => [ 'margin-top' => '5px' ] ], [
  937. (!empty($tsValues[$kID]))
  938. ? $tsValues[$kID]
  939. : V::get($fieldName, '', $record)
  940. ] ],
  941. ] ]
  942. ] ];
  943. // } else {
  944. // $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  945. // "TODO: SKIP field ({$fieldName}) - ! canWriteObjectField && ! canReadObjectField"
  946. // ]];
  947. }
  948. }
  949. $jsFields[] = [ 'div', [ 'class' => "form-group" ], [
  950. [ 'div', [ 'class' => "" ], [
  951. ['button', [ 'type' => "submit", 'class' => "btn btn-primary", 'tabindex' => ++$tabindex ], "Zapisz" ]
  952. ] ]
  953. ] ];
  954. $tblLabel = $acl->getNamespace();
  955. if ('default_db' == $acl->getSourceName()) {
  956. $tblLabel = array();
  957. $zasobObj = ProcesHelper::getZasobTableInfo($acl->getID());
  958. if (!$zasobObj) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
  959. if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
  960. if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
  961. $tblLabel = implode(" - ", $tblLabel);
  962. }
  963. $syncUrl = Request::getPathUri() . 'index.php?_route=ViewTableAjax&namespace=' . $acl->getNamespace();
  964. $jsGui = [
  965. 'reactNode' => [ 'div', [ 'class' => "container AjaxFrmHorizontalEdit", 'style' => [ "max-width" => "940px" ] ], [
  966. [ 'h4', [ 'style' => [ "padding-bottom" => "3px", "border-bottom" => "1px solid #ddd" ] ], [
  967. "Edycja rekordu Nr {$record['ID']}",
  968. [ 'small', [ 'class' => "pull-right valign-btns-bottom" ], [ $rowFunctionsOut ] ],
  969. ] ],
  970. [ 'P5UI__FeatureEditForm', [
  971. 'class' => "", 'action' => "", 'method' => "post",
  972. 'id' => "EDIT_FRM_{$this->_htmlID}", // TODO: rm - use React nodes // TODO: $this->_htmlID not exists!
  973. 'ajaxSaveUrl' => "{$syncUrl}&_task=editSaveAjax", // TODO:? &_hash={$this->_htmlID}
  974. 'namespace' => $acl->getNamespace(),
  975. 'idRecord' => $record['ID'],
  976. 'tableLabelHtml' => $tblLabel,
  977. ], [
  978. [ 'fieldset', [ 'style' => [ "padding-bottom" => "100px" ] ], $jsFields ] // fieldset
  979. ] ] // form
  980. ] ] // .container
  981. ];
  982. return [
  983. 'type' => "success",
  984. 'msg' => "Edycja rekordu nr {$id}",
  985. 'body' => $jsGui, // TODO: action for GUI: array to render by function h, js to trigger
  986. ];
  987. }
  988. public function editSaveAjaxAction() {
  989. Response::sendTryCatchJson(array($this, 'editSaveAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  990. }
  991. public function editSaveAjax($args) {
  992. $namespace = V::get('namespace', '', $args, 'word');
  993. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  994. $acl = Core_AclHelper::getAclByNamespace($namespace);
  995. $primaryKeyField = $acl->getPrimaryKeyField();
  996. $primaryKey = V::get('primaryKey', 0, $args, 'int');
  997. if (empty($primaryKey)) throw new HttpException("Bad Request - missing primaryKey!", 400);
  998. $item = $acl->getItem($primaryKey);
  999. if (!$item) throw new HttpException("Item not exists!", 404);
  1000. $itemFromUser = $acl->convertObjectFromUserInput($args['form'], $type = 'array_by_id', $prefix = 'f');
  1001. $response = new stdClass();
  1002. $response->primaryKey = $primaryKey;
  1003. try {
  1004. $itemFromUser[$primaryKeyField] = $primaryKey;
  1005. $affected = $acl->updateItem($itemFromUser);
  1006. if ($affected > 0) {
  1007. $response->type = 'success';
  1008. $response->msg = "Rekord zapisany pomyślnie";//"Record saved successfully";
  1009. } else if ($affected == 0) {
  1010. $response->type = 'info';
  1011. $response->msg = "Nie wprowadzono żadnych zmian";
  1012. }
  1013. $response->record = $acl->getItem($primaryKey);
  1014. $rowFunList = Core_AclHelper::getMoreFunctionsCell($acl, array('primary_key'=>$primaryKey, 'record'=>$response->record));
  1015. if (!empty($rowFunList)) $response->rowFunctions = $rowFunList;
  1016. }
  1017. catch (Exception $e) {
  1018. $response->type = 'error';
  1019. $response->msg = "Wystąpiły błędy!";
  1020. $response->msg .= $e->getMessage();
  1021. }
  1022. return $response;
  1023. }
  1024. public function typeSpecialCellAction() {
  1025. Response::sendTryCatchJson(array($this, 'typeSpecialCell'), $args = $_REQUEST);
  1026. }
  1027. public function typeSpecialCell($args) {
  1028. $namespace = V::get('namespace', '', $args, 'word');
  1029. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  1030. $acl = Core_AclHelper::getAclByNamespace($namespace);
  1031. $id = V::get('ID', 0, $args, 'int');
  1032. $fieldName = V::get('col', '', $args);
  1033. if ($id <= 0 || empty($fieldName)) throw new HttpException("Bad Request - missing id or col", 400);
  1034. $col = $fieldName;// TODO: RM $col
  1035. $jsonData = new stdClass();
  1036. $idField = $acl->getFieldIdByName($fieldName);
  1037. if (!$idField) throw new Exception("Wrong field");
  1038. $item = $acl->getItem($id);
  1039. if (!$acl->canReadObjectField($fieldName, $item)) throw new Exception("Brak dostępu");
  1040. $typeSpecial = Typespecial::getInstance($idField, $fieldName);
  1041. if ($typeSpecial) {
  1042. $jsonData->data = $typeSpecial->getReturnData($acl->getID(), $id, $fieldName, '');
  1043. $jsonData->namespace = 'default_db/' . V::get('tbl_name', '', $jsonData->data);
  1044. }
  1045. return $jsonData;
  1046. }
  1047. /**
  1048. * @param $_GET['namespace'] = AclNamespace
  1049. * @param $_GET['format'] = 'csv' | 'html'
  1050. * @param $_GET['flds'] = csv - coma separated field names
  1051. * @param $_GET['sortCol'] = FieldName
  1052. * @param $_GET['sortDir'] = SortDir ('desc' | 'asc')
  1053. * @param $_GET['f_{$fieldName}'] = filter
  1054. * @param $_GET['sf_{$fieldName}'] = force filter
  1055. */
  1056. public function exportAction() {
  1057. $args = $_GET;
  1058. $namespace = V::get('namespace', '', $args, 'word');
  1059. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  1060. $acl = Core_AclHelper::getAclByNamespace($namespace);
  1061. $exportLimit = 10000;
  1062. $params = array();
  1063. $params['limit'] = $exportLimit;
  1064. // $params['limitstart'] = 0;
  1065. $params['order_by'] = V::get('sortCol', '', $args);
  1066. $params['order_dir'] = V::get('sortDir', '', $args);
  1067. $params['cols'] = array($acl->getPrimaryKeyField());
  1068. $toExportFields = explode(',', V::get('flds', '', $_GET));
  1069. if (empty($toExportFields)) throw new Exception("Nie wybrano żandych pól do exportu.");
  1070. $allowedExportFieldList = Core_AclHelper::getExportFieldList($acl);
  1071. foreach ($toExportFields as $fieldName) {
  1072. if ($fieldName == $acl->getPrimaryKeyField()) continue;
  1073. if (!in_array($fieldName, $allowedExportFieldList)) throw new Exception("Brak uprawnień do exportu pola '{$fieldName}'");
  1074. $params['cols'][] = $fieldName;
  1075. }
  1076. $labels = array();
  1077. foreach ($toExportFields as $fieldName) {
  1078. $labels[ $fieldName ] = $acl->getFieldLabel($fieldName);
  1079. }
  1080. foreach ($args as $k => $v) {
  1081. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && strlen($v) > 0) {// filter prefix
  1082. $params[$k] = $v;
  1083. }
  1084. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && strlen($v) > 0) {// special filter prefix
  1085. $params[$k] = $v;
  1086. }
  1087. }
  1088. try {
  1089. $queryFeatures = $acl->buildQuery($params);
  1090. $total = $queryFeatures->getTotal();
  1091. $listItems = $queryFeatures->getItems();
  1092. $primaryKeyField = $acl->getPrimaryKeyField();
  1093. $items = []; foreach ($listItems as $item) $items[ $item[$primaryKeyField] ] = $item;
  1094. } catch (Exception $e) {
  1095. DBG::log($e);
  1096. throw $e;
  1097. }
  1098. $format = V::get('format', 'html', $_GET);
  1099. switch ($format) {
  1100. case 'html': {
  1101. UI::gora();
  1102. echo UI::h('table', ['class'=>'table table-bordered table-hover'], [
  1103. UI::h('thead', [], [
  1104. UI::h('tr', [], array_map(function ($label) {
  1105. return UI::h('th', [], $label);
  1106. }, $labels))
  1107. ]),
  1108. UI::h('tbody', [], array_map(function ($item) use($labels) {
  1109. return UI::h('tr', [], array_map(function ($fieldName) use ($item) {
  1110. return UI::h('td', [], V::get($fieldName, '', $item));
  1111. }, array_keys($labels)));
  1112. }, $items)),
  1113. ]);
  1114. UI::dol();
  1115. exit;
  1116. }
  1117. case 'csv_cp1250':
  1118. case 'csv': {
  1119. $csvFileName = "Tabela-" . $acl->getName() . "-" . date("Y-m-d_H_s");
  1120. header('Content-Type: text/csv; charset=utf-8');
  1121. header("Content-Disposition: attachment; filename={$csvFileName}.csv");
  1122. $csvSeparator = ';';
  1123. $csvHeader = implode($csvSeparator, array_map(function ($label) use ($item) {
  1124. return '"' . addslashes($label) . '"';
  1125. }, array_values($labels)));
  1126. $csvRows = implode("\r\n", array_map(function ($item) use ($labels, $csvSeparator) {
  1127. return implode($csvSeparator, array_map(function ($fieldName) use ($item) {
  1128. return '"' . addslashes(V::get($fieldName, '', $item)) . '"';
  1129. }, array_keys($labels)));
  1130. }, $items));
  1131. switch ($format) {
  1132. case 'csv': echo $csvHeader . "\n" . $csvRows; exit;
  1133. case 'csv_cp1250': echo iconv('utf-8', 'Windows-1250//IGNORE', $csvHeader) . "\r\n" . iconv('utf-8', 'Windows-1250//IGNORE', $csvRows); exit;
  1134. die("Nieobsługiwane kodowanie danych csv.");
  1135. }
  1136. exit;
  1137. }
  1138. }
  1139. die("Nieobsługiwany format danych.");
  1140. }
  1141. public function loadDataAjaxAction() {
  1142. $namespace = V::get('namespace', '', $_REQUEST, 'word');
  1143. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  1144. $acl = Core_AclHelper::getAclByNamespace($namespace);
  1145. $backRefFilter = [
  1146. 'namespace' => V::get('backRefNS', '', $_REQUEST),
  1147. 'primaryKey' => V::get('backRefPK', '', $_REQUEST),
  1148. 'fieldName' => V::get('backRefField', '', $_REQUEST),
  1149. ];
  1150. $childRefFilter = [
  1151. 'namespace' => V::get('childRefNS', '', $_GET),
  1152. 'primaryKey' => V::get('childRefPK', '', $_GET),
  1153. ];
  1154. $tbl = $this->getTableAjaxWidget($acl, $backRefFilter, $childRefFilter);
  1155. Response::sendTryCatchJson(array($tbl, 'ajaxData'), $args = $_GET);
  1156. }
  1157. public function uploadFilesAjaxAction() {
  1158. Response::sendTryCatchJson([$this, 'uploadFilesAjax'], $args = $_POST);
  1159. }
  1160. public function uploadFilesAjax($args) {
  1161. DBG::log($_FILES, 'array', "\$_FILES");
  1162. DBG::log($args, 'array', "\$args");
  1163. $namespace = V::get('namespace', '', $args, 'word');
  1164. if (!$namespace) throw new Exception("Missing namespace");
  1165. $primaryKey = V::get('primaryKey', '', $args, 'int');
  1166. if ($primaryKey <= 0) throw new Exception("Missing primaryKey");
  1167. if (empty($_FILES)) throw new Exception("Missing files");
  1168. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1169. Lib::loadClass('FileUploader');
  1170. Lib::loadClass('FoldersConfig');
  1171. // $dbID = $acl->getDB();
  1172. // $db = DB::getDB($dbID);
  1173. // if (!$db) throw new HttpException("No DB ({$dbID})", 406);
  1174. $record = $acl->buildQuery([])->getItem($primaryKey);
  1175. DBG::log($record, 'array', "\$record");
  1176. if (!$record) throw new HttpException("No item ID({$primaryKey})", 404);
  1177. if (!$acl->canReadRecord($record)) throw new Exception("Brak uprawnień do odczytu");
  1178. if (!$acl->canWriteRecord($record)) throw new Exception("Brak uprawnień do zapisu");
  1179. $rootTableName = $acl->getRootTableName();
  1180. $confTblName = "{$rootTableName}_COLUMN";
  1181. $folderConfAll = FoldersConfig::getRawData();
  1182. if (!FoldersConfig::hasConfig($confTblName)) throw new HttpException("Brak danych konfiguracyjnych ({$rootTableName})", 404);
  1183. $folderConf = FoldersConfig::getAll($confTblName);
  1184. DBG::log($folderConf, 'array', "\$folderConf");
  1185. $uploader = new FileUploader($confTblName, (object)$record);
  1186. if (!$uploader->setConfig($folderConf)) throw new HttpException("Błąd danych konfiguracyjnych ({$rootTableName})", 404);
  1187. $uploader->findFolder();
  1188. DBG::log($uploader, 'array', "\$uploader");
  1189. // $errorMsg = '';
  1190. // if (!empty($args['SCANS_COLUMN_ADD'])) {
  1191. // $uploaded = $uploader->tryMoveFromScanAjax($errorMsg);
  1192. // }
  1193. // else {
  1194. // $uploaded = $uploader->tryUploadAjax($errorMsg);
  1195. // }
  1196. $destPath = $uploader->getDestLocalPath($show_if_not_found = true);
  1197. DBG::log($destPath, 'array', "\$destPath");
  1198. if (!file_exists($destPath)) {
  1199. if (!$uploader->tryCreateDestFolder($destPath)) throw new Exception("Wystąpił błąd podczas tworzenie katalogu dla rekordu '{$primaryKey}'");
  1200. }
  1201. $generateSafeFileName = function($destPath, $fileName) {
  1202. if (!file_exists("{$destPath}/{$fileName}")) return $fileName;
  1203. $infoPath = pathinfo($fileName);
  1204. // pathinfo('/path/t1/t2/fileName.ext') = [
  1205. // [dirname] => /path/t1/t2
  1206. // [basename] => fileName.ext
  1207. // [extension] => ext
  1208. // [filename] => fileName
  1209. // ]
  1210. return $infoPath['filename'] . "--" . date("Y-m-d_H-i-s") . "." . $infoPath['extension'];
  1211. };
  1212. $moveActions = array_map(function ($file) use ($destPath, $generateSafeFileName) {
  1213. $safeName = $generateSafeFileName($destPath, $file['name']);
  1214. return [
  1215. $file['tmp_name'],
  1216. "{$destPath}/{$safeName}",
  1217. $safeName,
  1218. ];
  1219. }, $_FILES);
  1220. DBG::log($moveActions, 'array', "\$moveActions"); // [ [ srcPath, descPath ] ]
  1221. $errorMsgs = [];
  1222. $pkField = $acl->getSqlPrimaryKeyField();
  1223. foreach ($moveActions as $fileMoveAction) {
  1224. if (!move_uploaded_file($fileMoveAction[0], $fileMoveAction[1])) {
  1225. $errorMsgs[] = "Nie udało się wgrać pliku '{$fileMoveAction[2]}'";
  1226. } else {
  1227. try {
  1228. $affected = DB::getPDO($acl->getDB())->update($rootTableName, $pkField, $primaryKey, [
  1229. 'M_DIST_FILES' => "Wrano plik '{$fileMoveAction[2]}'",
  1230. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1231. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1232. ]);
  1233. if ($affected) {
  1234. DB::getPDO($acl->getDB())->insert("{$rootTableName}_HIST", [
  1235. 'ID_USERS2' => $primaryKey,
  1236. 'M_DIST_FILES' => "Wrano plik '{$fileMoveAction[2]}'",
  1237. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1238. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1239. ]);
  1240. }
  1241. } catch (Exception $e) {
  1242. DBG::log($e);
  1243. $errorMsgs[] = $e->getMessage();
  1244. }
  1245. }
  1246. }
  1247. if (!empty($errorMsgs)) {
  1248. return [
  1249. 'type' => "error",
  1250. 'msg' => "Wystąpiły błędy podczas wgrywania plików dla '{$primaryKey}'",
  1251. 'errors' => $errorMsgs,
  1252. ];
  1253. }
  1254. return [
  1255. 'type' => "success",
  1256. 'msg' => "Wgrano nowe pliki dla '{$primaryKey}'",
  1257. ];
  1258. }
  1259. public function removeFileAjaxAction() {
  1260. Response::sendTryCatchJson([$this, 'removeFileAjax'], $args = $_REQUEST);
  1261. }
  1262. public function removeFileAjax($args) { // ajaxFileRemove
  1263. $namespace = V::get('namespace', '', $args, 'word');
  1264. if (!$namespace) throw new Exception("Missing namespace");
  1265. $id = V::get('ID', 0, $args, 'int');
  1266. if ($id <= 0) throw new Exception("Missing ID");
  1267. $filename = V::get('filename', '', $args);
  1268. if (empty($filename)) throw new Exception("Nie wybrano pliku do usunięcia");
  1269. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1270. $dbID = $acl->getDB();
  1271. $db = DB::getPDO($dbID);
  1272. if (!$db) throw new HttpException("No DB ({$dbID})", 406);
  1273. $record = $acl->buildQuery([])->getItem($id);
  1274. if (!$record) throw new HttpException("No item ID({$id})", 404);
  1275. if (!$acl->canReadRecord($record)) throw new Exception("Brak uprawnień do odczytu");
  1276. if (!$acl->canWriteRecord($record)) throw new Exception("Brak uprawnień do zapisu");
  1277. Lib::loadClass('FileUploader');
  1278. Lib::loadClass('FoldersConfig');
  1279. $tblName = $acl->getName();
  1280. $confTblName = "{$tblName}_COLUMN";
  1281. $folderConfAll = FoldersConfig::getRawData();
  1282. if (!FoldersConfig::hasConfig($confTblName)) throw new HttpException("Brak danych konfiguracyjnych ({$tblName})", 404);
  1283. $folderConf = FoldersConfig::getAll($confTblName);
  1284. $uploader = new FileUploader($confTblName, (object)$record);
  1285. if (!$uploader->setConfig($folderConf)) throw new HttpException("Błąd danych konfiguracyjnych ({$tblName})", 404);
  1286. $uploader->findFolder();
  1287. $errorMsg = '';
  1288. $removed = $uploader->tryRemoveFromAjax($filename, $errorMsg);
  1289. if (!$removed) throw new Exception($errorMsg);
  1290. // $affected = DB::getPDO($acl->getDB())->update();
  1291. $rootTableName = $acl->getRootTableName();
  1292. $pkField = $acl->getSqlPrimaryKeyField();
  1293. $primaryKey = $id;
  1294. try {
  1295. $affected = DB::getPDO($acl->getDB())->update($rootTableName, $pkField, $primaryKey, [
  1296. 'M_DIST_FILES' => "Usunięto plik '{$filename}'",
  1297. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1298. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1299. ]);
  1300. if ($affected) {
  1301. DB::getPDO($acl->getDB())->insert("{$rootTableName}_HIST", [
  1302. 'ID_USERS2' => $primaryKey,
  1303. 'M_DIST_FILES' => "Usunięto plik '{$filename}'",
  1304. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1305. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1306. ]);
  1307. }
  1308. } catch (Exception $e) {
  1309. DBG::log($e);
  1310. }
  1311. return [
  1312. 'type' => 'success',
  1313. 'msg' => 'Plik został usunięty',
  1314. ];
  1315. }
  1316. public function procesInitFiltrAjaxAction() {
  1317. Response::sendTryCatchJson([$this, 'procesInitFiltrAjax'], $args = $_GET);
  1318. }
  1319. public function procesInitFiltrAjax($args) { // ajaxFileRemove
  1320. $namespace = V::get('namespace', '', $args, 'word');
  1321. if (!$namespace) throw new Exception("Missing namespace");
  1322. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1323. $pInitList = User::getAcl()->getTableProcesInitList($acl->getID());
  1324. DBG::log($pInitList, 'array', "\$pInitList");
  1325. if (!empty($pInitList)) {
  1326. $procesIds = array_keys($pInitList);
  1327. $mapTree = ACL::getProcesInitMapTreeOnlyIds($procesIds);
  1328. DBG::log($mapTree, 'array', "\$mapTree");
  1329. DBG::log($pInitList, 'array', "\$pInitList");
  1330. $pInitListSelected = User::getAcl()->getPermsFiltrProcesId();
  1331. return [
  1332. 'type' => 'success',
  1333. 'msg' => 'ok',
  1334. 'pInitData' => [
  1335. 'pInitList' => $pInitList,
  1336. 'mapTree' => $mapTree,
  1337. 'pInitListSelected' => $pInitListSelected,
  1338. ],
  1339. ];
  1340. }
  1341. return [
  1342. 'type' => 'success'
  1343. ];
  1344. }
  1345. public function tableToolsAjaxAction() {
  1346. Response::sendTryCatchJson([$this, 'tableToolsAjax'], $args = $_GET);
  1347. }
  1348. public function tableToolsAjax($args) { // ajaxFileRemove
  1349. $namespace = V::get('namespace', '', $args, 'word');
  1350. if (!$namespace) throw new Exception("Missing namespace");
  1351. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1352. $listUrlFunctions = Route_UrlAction::getTableFunctions($acl->getID(), $idRecord = 0, $acl->getName(), User::getLogin());
  1353. DBG::log($listUrlFunctions, 'array', "\$listUrlFunctions");
  1354. $listUrlFunctions = array_map(function ($urlFunction) use ($namespace) {
  1355. if ('index.php?' === substr($urlFunction['baseLink'], 0, strlen('index.php?'))) $urlFunction['baseLink'] .= "&_fromNamespace={$namespace}";
  1356. return $urlFunction;
  1357. }, $listUrlFunctions);
  1358. return [
  1359. 'type' => "success",
  1360. 'msg' => 'ok',
  1361. 'body' => [
  1362. 'tableTools' => array_values(array_map(function ($urlFunction) {
  1363. return [
  1364. 'url' => $urlFunction['baseLink'],
  1365. 'label' => $urlFunction['label'],
  1366. // TODO: $urlFunction['link_target'] // "_blank"
  1367. // ? $urlFunction['name']
  1368. ];
  1369. }, array_filter($listUrlFunctions, function ($urlFunction) {
  1370. return empty($urlFunction['cell_id_params']);
  1371. }))),
  1372. ],
  1373. ];
  1374. }
  1375. }