ViewTableAjax.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  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. public function typespecialAction() { Response::sendTryCatchJson(array($this, 'typespecial'), $args = $_REQUEST); }
  1048. public function typespecial($args) { // @required idField, @optional: q (query), selected (selected value), idRecord (fetch value for given record)
  1049. $idField = V::get('idField', 0, $args, 'int');
  1050. if (!$idField) throw new HttpException("Bad Request - missing idField", 400);
  1051. $query = V::get('q', '', $_REQUEST);
  1052. DBG::log("\$query({$query})");
  1053. $cellInfo = DB::getPDO()->fetchFirst("
  1054. select *
  1055. from CRM_PROCES_idx_TABLES_INFO_VIEW
  1056. where ID_CELL = :id
  1057. ", [ ':id' => $idField ]);
  1058. DBG::log($cellInfo, 'array', "cell info");
  1059. if (!$cellInfo) throw new HttpException("Bad Request - wrong idField", 400);
  1060. // 'ID_CELL' => '24310',
  1061. // 'CELL_NAME' => 'L_APPOITMENT_USER',
  1062. // 'CELL_LABEL' => 'Osoba odpowiedzialna',
  1063. // 'CELL_DESCRIPTION' => '',
  1064. // 'CELL_SORT_PRIO' => '3',
  1065. // 'ID_TABLE' => '13051',
  1066. // 'TABLE_NAME' => 'TEST_PERMS',
  1067. // 'TABLE_LABEL' => 'Test permy',
  1068. // 'TABLE_DESCRIPTION' => '',
  1069. // 'ID_DATABASE' => '36',
  1070. $namespace = (false !== strpos($cellInfo['TABLE_NAME'], '/')) ? $cellInfo['TABLE_NAME'] : "default_db/{$cellInfo['TABLE_NAME']}";
  1071. if (!$namespace) throw new HttpException("Bad Request - wrong idField, cannot find namespace", 400);
  1072. $acl = Core_AclHelper::getAclByNamespace($namespace);
  1073. $fieldName = $cellInfo['CELL_NAME'];
  1074. $jsonData = array();
  1075. $typeSpecial = Typespecial::getInstance($idField, $fieldName);
  1076. if ($typeSpecial) {
  1077. // if (idRecord or selected) { // TODO
  1078. // $jsonData->data = $typeSpecial->getReturnData($acl->getID(), $id, $fieldName, '');
  1079. // $jsonData->namespace = 'default_db/' . V::get('tbl_name', '', $jsonData->data);
  1080. $rawRows = null;
  1081. $rows = $typeSpecial->getValuesWithExports($query);
  1082. DBG::log($rows, 'array', "\$rows({$query})");
  1083. foreach ($rows as $kID => $vItem) {
  1084. $itemJson = new stdClass();
  1085. $itemJson->id = $vItem->id;
  1086. $itemJson->name = $vItem->param_out;
  1087. if (!empty($vItem->exports)) {
  1088. $itemJson->exports = $vItem->exports;
  1089. }
  1090. if (!empty($vItem->{'$order'})) {
  1091. $itemJson->{'$order'} = $vItem->{'$order'};
  1092. }
  1093. $jsonData[] = $itemJson;
  1094. }
  1095. }
  1096. return $jsonData;
  1097. }
  1098. /**
  1099. * @param $_GET['namespace'] = AclNamespace
  1100. * @param $_GET['format'] = 'csv' | 'html'
  1101. * @param $_GET['flds'] = csv - coma separated field names
  1102. * @param $_GET['sortCol'] = FieldName
  1103. * @param $_GET['sortDir'] = SortDir ('desc' | 'asc')
  1104. * @param $_GET['f_{$fieldName}'] = filter
  1105. * @param $_GET['sf_{$fieldName}'] = force filter
  1106. */
  1107. public function exportAction() {
  1108. $args = $_GET;
  1109. $namespace = V::get('namespace', '', $args, 'word');
  1110. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  1111. $acl = Core_AclHelper::getAclByNamespace($namespace);
  1112. $exportLimit = 10000;
  1113. $params = array();
  1114. $params['limit'] = $exportLimit;
  1115. // $params['limitstart'] = 0;
  1116. $params['order_by'] = V::get('sortCol', '', $args);
  1117. $params['order_dir'] = V::get('sortDir', '', $args);
  1118. $params['cols'] = array($acl->getPrimaryKeyField());
  1119. $toExportFields = explode(',', V::get('flds', '', $_GET));
  1120. if (empty($toExportFields)) throw new Exception("Nie wybrano żandych pól do exportu.");
  1121. $allowedExportFieldList = Core_AclHelper::getExportFieldList($acl);
  1122. foreach ($toExportFields as $fieldName) {
  1123. if ($fieldName == $acl->getPrimaryKeyField()) continue;
  1124. if (!in_array($fieldName, $allowedExportFieldList)) throw new Exception("Brak uprawnień do exportu pola '{$fieldName}'");
  1125. $params['cols'][] = $fieldName;
  1126. }
  1127. $labels = array();
  1128. foreach ($toExportFields as $fieldName) {
  1129. $labels[ $fieldName ] = $acl->getFieldLabel($fieldName);
  1130. }
  1131. foreach ($args as $k => $v) {
  1132. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && strlen($v) > 0) {// filter prefix
  1133. $params[$k] = $v;
  1134. }
  1135. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && strlen($v) > 0) {// special filter prefix
  1136. $params[$k] = $v;
  1137. }
  1138. }
  1139. try {
  1140. $queryFeatures = $acl->buildQuery($params);
  1141. $total = $queryFeatures->getTotal();
  1142. $listItems = $queryFeatures->getItems();
  1143. $primaryKeyField = $acl->getPrimaryKeyField();
  1144. $items = []; foreach ($listItems as $item) $items[ $item[$primaryKeyField] ] = $item;
  1145. } catch (Exception $e) {
  1146. DBG::log($e);
  1147. throw $e;
  1148. }
  1149. $format = V::get('format', 'html', $_GET);
  1150. switch ($format) {
  1151. case 'html': {
  1152. UI::gora();
  1153. echo UI::h('table', ['class'=>'table table-bordered table-hover'], [
  1154. UI::h('thead', [], [
  1155. UI::h('tr', [], array_map(function ($label) {
  1156. return UI::h('th', [], $label);
  1157. }, $labels))
  1158. ]),
  1159. UI::h('tbody', [], array_map(function ($item) use($labels) {
  1160. return UI::h('tr', [], array_map(function ($fieldName) use ($item) {
  1161. return UI::h('td', [], V::get($fieldName, '', $item));
  1162. }, array_keys($labels)));
  1163. }, $items)),
  1164. ]);
  1165. UI::dol();
  1166. exit;
  1167. }
  1168. case 'csv_cp1250':
  1169. case 'csv': {
  1170. $csvFileName = "Tabela-" . $acl->getName() . "-" . date("Y-m-d_H_s");
  1171. header('Content-Type: text/csv; charset=utf-8');
  1172. header("Content-Disposition: attachment; filename={$csvFileName}.csv");
  1173. $csvSeparator = ';';
  1174. $csvHeader = implode($csvSeparator, array_map(function ($label) use ($item) {
  1175. return '"' . addslashes($label) . '"';
  1176. }, array_values($labels)));
  1177. $csvRows = implode("\r\n", array_map(function ($item) use ($labels, $csvSeparator) {
  1178. return implode($csvSeparator, array_map(function ($fieldName) use ($item) {
  1179. return '"' . addslashes(V::get($fieldName, '', $item)) . '"';
  1180. }, array_keys($labels)));
  1181. }, $items));
  1182. switch ($format) {
  1183. case 'csv': echo $csvHeader . "\n" . $csvRows; exit;
  1184. case 'csv_cp1250': echo iconv('utf-8', 'Windows-1250//IGNORE', $csvHeader) . "\r\n" . iconv('utf-8', 'Windows-1250//IGNORE', $csvRows); exit;
  1185. die("Nieobsługiwane kodowanie danych csv.");
  1186. }
  1187. exit;
  1188. }
  1189. }
  1190. die("Nieobsługiwany format danych.");
  1191. }
  1192. public function loadDataAjaxAction() {
  1193. $namespace = V::get('namespace', '', $_REQUEST, 'word');
  1194. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  1195. $acl = Core_AclHelper::getAclByNamespace($namespace);
  1196. $backRefFilter = [
  1197. 'namespace' => V::get('backRefNS', '', $_REQUEST),
  1198. 'primaryKey' => V::get('backRefPK', '', $_REQUEST),
  1199. 'fieldName' => V::get('backRefField', '', $_REQUEST),
  1200. ];
  1201. $childRefFilter = [
  1202. 'namespace' => V::get('childRefNS', '', $_GET),
  1203. 'primaryKey' => V::get('childRefPK', '', $_GET),
  1204. ];
  1205. $tbl = $this->getTableAjaxWidget($acl, $backRefFilter, $childRefFilter);
  1206. Response::sendTryCatchJson(array($tbl, 'ajaxData'), $args = $_GET);
  1207. }
  1208. public function uploadFilesAjaxAction() {
  1209. Response::sendTryCatchJson([$this, 'uploadFilesAjax'], $args = $_POST);
  1210. }
  1211. public function uploadFilesAjax($args) {
  1212. DBG::log($_FILES, 'array', "\$_FILES");
  1213. DBG::log($args, 'array', "\$args");
  1214. $namespace = V::get('namespace', '', $args, 'word');
  1215. if (!$namespace) throw new Exception("Missing namespace");
  1216. $primaryKey = V::get('primaryKey', '', $args, 'int');
  1217. if ($primaryKey <= 0) throw new Exception("Missing primaryKey");
  1218. if (empty($_FILES)) throw new Exception("Missing files");
  1219. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1220. Lib::loadClass('FileUploader');
  1221. Lib::loadClass('FoldersConfig');
  1222. // $dbID = $acl->getDB();
  1223. // $db = DB::getDB($dbID);
  1224. // if (!$db) throw new HttpException("No DB ({$dbID})", 406);
  1225. $record = $acl->buildQuery([])->getItem($primaryKey);
  1226. DBG::log($record, 'array', "\$record");
  1227. if (!$record) throw new HttpException("No item ID({$primaryKey})", 404);
  1228. if (!$acl->canReadRecord($record)) throw new Exception("Brak uprawnień do odczytu");
  1229. if (!$acl->canWriteRecord($record)) throw new Exception("Brak uprawnień do zapisu");
  1230. $rootTableName = $acl->getRootTableName();
  1231. $confTblName = "{$rootTableName}_COLUMN";
  1232. $folderConfAll = FoldersConfig::getRawData();
  1233. if (!FoldersConfig::hasConfig($confTblName)) throw new HttpException("Brak danych konfiguracyjnych ({$rootTableName})", 404);
  1234. $folderConf = FoldersConfig::getAll($confTblName);
  1235. DBG::log($folderConf, 'array', "\$folderConf");
  1236. $uploader = new FileUploader($confTblName, (object)$record);
  1237. if (!$uploader->setConfig($folderConf)) throw new HttpException("Błąd danych konfiguracyjnych ({$rootTableName})", 404);
  1238. $uploader->findFolder();
  1239. DBG::log($uploader, 'array', "\$uploader");
  1240. // $errorMsg = '';
  1241. // if (!empty($args['SCANS_COLUMN_ADD'])) {
  1242. // $uploaded = $uploader->tryMoveFromScanAjax($errorMsg);
  1243. // }
  1244. // else {
  1245. // $uploaded = $uploader->tryUploadAjax($errorMsg);
  1246. // }
  1247. $destPath = $uploader->getDestLocalPath($show_if_not_found = true);
  1248. DBG::log($destPath, 'array', "\$destPath");
  1249. if (!file_exists($destPath)) {
  1250. if (!$uploader->tryCreateDestFolder($destPath)) throw new Exception("Wystąpił błąd podczas tworzenie katalogu dla rekordu '{$primaryKey}'");
  1251. }
  1252. $generateSafeFileName = function($destPath, $fileName) {
  1253. if (!file_exists("{$destPath}/{$fileName}")) return $fileName;
  1254. $infoPath = pathinfo($fileName);
  1255. // pathinfo('/path/t1/t2/fileName.ext') = [
  1256. // [dirname] => /path/t1/t2
  1257. // [basename] => fileName.ext
  1258. // [extension] => ext
  1259. // [filename] => fileName
  1260. // ]
  1261. return $infoPath['filename'] . "--" . date("Y-m-d_H-i-s") . "." . $infoPath['extension'];
  1262. };
  1263. $moveActions = array_map(function ($file) use ($destPath, $generateSafeFileName) {
  1264. $safeName = $generateSafeFileName($destPath, $file['name']);
  1265. return [
  1266. $file['tmp_name'],
  1267. "{$destPath}/{$safeName}",
  1268. $safeName,
  1269. ];
  1270. }, $_FILES);
  1271. DBG::log($moveActions, 'array', "\$moveActions"); // [ [ srcPath, descPath ] ]
  1272. $errorMsgs = [];
  1273. $pkField = $acl->getSqlPrimaryKeyField();
  1274. foreach ($moveActions as $fileMoveAction) {
  1275. if (!move_uploaded_file($fileMoveAction[0], $fileMoveAction[1])) {
  1276. $errorMsgs[] = "Nie udało się wgrać pliku '{$fileMoveAction[2]}'";
  1277. } else {
  1278. try {
  1279. $affected = DB::getPDO($acl->getDB())->update($rootTableName, $pkField, $primaryKey, [
  1280. 'M_DIST_FILES' => "Wrano plik '{$fileMoveAction[2]}'",
  1281. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1282. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1283. ]);
  1284. if ($affected) {
  1285. DB::getPDO($acl->getDB())->insert("{$rootTableName}_HIST", [
  1286. 'ID_USERS2' => $primaryKey,
  1287. 'M_DIST_FILES' => "Wrano plik '{$fileMoveAction[2]}'",
  1288. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1289. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1290. ]);
  1291. }
  1292. } catch (Exception $e) {
  1293. DBG::log($e);
  1294. $errorMsgs[] = $e->getMessage();
  1295. }
  1296. }
  1297. }
  1298. if (!empty($errorMsgs)) {
  1299. return [
  1300. 'type' => "error",
  1301. 'msg' => "Wystąpiły błędy podczas wgrywania plików dla '{$primaryKey}'",
  1302. 'errors' => $errorMsgs,
  1303. ];
  1304. }
  1305. return [
  1306. 'type' => "success",
  1307. 'msg' => "Wgrano nowe pliki dla '{$primaryKey}'",
  1308. ];
  1309. }
  1310. public function removeFileAjaxAction() {
  1311. Response::sendTryCatchJson([$this, 'removeFileAjax'], $args = $_REQUEST);
  1312. }
  1313. public function removeFileAjax($args) { // ajaxFileRemove
  1314. $namespace = V::get('namespace', '', $args, 'word');
  1315. if (!$namespace) throw new Exception("Missing namespace");
  1316. $id = V::get('ID', 0, $args, 'int');
  1317. if ($id <= 0) throw new Exception("Missing ID");
  1318. $filename = V::get('filename', '', $args);
  1319. if (empty($filename)) throw new Exception("Nie wybrano pliku do usunięcia");
  1320. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1321. $dbID = $acl->getDB();
  1322. $db = DB::getPDO($dbID);
  1323. if (!$db) throw new HttpException("No DB ({$dbID})", 406);
  1324. $record = $acl->buildQuery([])->getItem($id);
  1325. if (!$record) throw new HttpException("No item ID({$id})", 404);
  1326. if (!$acl->canReadRecord($record)) throw new Exception("Brak uprawnień do odczytu");
  1327. if (!$acl->canWriteRecord($record)) throw new Exception("Brak uprawnień do zapisu");
  1328. Lib::loadClass('FileUploader');
  1329. Lib::loadClass('FoldersConfig');
  1330. $tblName = $acl->getName();
  1331. $confTblName = "{$tblName}_COLUMN";
  1332. $folderConfAll = FoldersConfig::getRawData();
  1333. if (!FoldersConfig::hasConfig($confTblName)) throw new HttpException("Brak danych konfiguracyjnych ({$tblName})", 404);
  1334. $folderConf = FoldersConfig::getAll($confTblName);
  1335. $uploader = new FileUploader($confTblName, (object)$record);
  1336. if (!$uploader->setConfig($folderConf)) throw new HttpException("Błąd danych konfiguracyjnych ({$tblName})", 404);
  1337. $uploader->findFolder();
  1338. $errorMsg = '';
  1339. $removed = $uploader->tryRemoveFromAjax($filename, $errorMsg);
  1340. if (!$removed) throw new Exception($errorMsg);
  1341. // $affected = DB::getPDO($acl->getDB())->update();
  1342. $rootTableName = $acl->getRootTableName();
  1343. $pkField = $acl->getSqlPrimaryKeyField();
  1344. $primaryKey = $id;
  1345. try {
  1346. $affected = DB::getPDO($acl->getDB())->update($rootTableName, $pkField, $primaryKey, [
  1347. 'M_DIST_FILES' => "Usunięto plik '{$filename}'",
  1348. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1349. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1350. ]);
  1351. if ($affected) {
  1352. DB::getPDO($acl->getDB())->insert("{$rootTableName}_HIST", [
  1353. 'ID_USERS2' => $primaryKey,
  1354. 'M_DIST_FILES' => "Usunięto plik '{$filename}'",
  1355. 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  1356. 'A_RECORD_UPDATE_DATE' => 'NOW()',
  1357. ]);
  1358. }
  1359. } catch (Exception $e) {
  1360. DBG::log($e);
  1361. }
  1362. return [
  1363. 'type' => 'success',
  1364. 'msg' => 'Plik został usunięty',
  1365. ];
  1366. }
  1367. public function procesInitFiltrAjaxAction() {
  1368. Response::sendTryCatchJson([$this, 'procesInitFiltrAjax'], $args = $_GET);
  1369. }
  1370. public function procesInitFiltrAjax($args) { // ajaxFileRemove
  1371. $namespace = V::get('namespace', '', $args, 'word');
  1372. if (!$namespace) throw new Exception("Missing namespace");
  1373. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1374. $pInitList = User::getAcl()->getTableProcesInitList($acl->getID());
  1375. DBG::log($pInitList, 'array', "\$pInitList");
  1376. if (!empty($pInitList)) {
  1377. $procesIds = array_keys($pInitList);
  1378. $mapTree = ACL::getProcesInitMapTreeOnlyIds($procesIds);
  1379. DBG::log($mapTree, 'array', "\$mapTree");
  1380. DBG::log($pInitList, 'array', "\$pInitList");
  1381. $pInitListSelected = User::getAcl()->getPermsFiltrProcesId();
  1382. return [
  1383. 'type' => 'success',
  1384. 'msg' => 'ok',
  1385. 'pInitData' => [
  1386. 'pInitList' => $pInitList,
  1387. 'mapTree' => $mapTree,
  1388. 'pInitListSelected' => $pInitListSelected,
  1389. ],
  1390. ];
  1391. }
  1392. return [
  1393. 'type' => 'success'
  1394. ];
  1395. }
  1396. public function tableToolsAjaxAction() {
  1397. Response::sendTryCatchJson([$this, 'tableToolsAjax'], $args = $_GET);
  1398. }
  1399. public function tableToolsAjax($args) { // ajaxFileRemove
  1400. $namespace = V::get('namespace', '', $args, 'word');
  1401. if (!$namespace) throw new Exception("Missing namespace");
  1402. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  1403. $listUrlFunctions = Route_UrlAction::getTableFunctions($acl->getID(), $idRecord = 0, $acl->getName(), User::getLogin());
  1404. DBG::log($listUrlFunctions, 'array', "\$listUrlFunctions");
  1405. $listUrlFunctions = array_map(function ($urlFunction) use ($namespace) {
  1406. if ('index.php?' === substr($urlFunction['baseLink'], 0, strlen('index.php?'))) $urlFunction['baseLink'] .= "&_fromNamespace={$namespace}";
  1407. return $urlFunction;
  1408. }, $listUrlFunctions);
  1409. return [
  1410. 'type' => "success",
  1411. 'msg' => 'ok',
  1412. 'body' => [
  1413. 'tableTools' => array_values(array_map(function ($urlFunction) {
  1414. return [
  1415. 'url' => $urlFunction['baseLink'],
  1416. 'label' => $urlFunction['label'],
  1417. // TODO: $urlFunction['link_target'] // "_blank"
  1418. // ? $urlFunction['name']
  1419. ];
  1420. }, array_filter($listUrlFunctions, function ($urlFunction) {
  1421. return empty($urlFunction['cell_id_params']);
  1422. }))),
  1423. ],
  1424. ];
  1425. }
  1426. }