ViewTableAjax.php 67 KB

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