ViewObject.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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. Lib::loadClass('P5');
  15. Lib::loadClass('Route_ViewTableAjax');
  16. class Route_ViewObject extends Route_ViewTableAjax {
  17. public function getTableAjaxWidget($acl) {
  18. $syncUrl = Request::getPathUri() . 'index.php?_route=ViewObject&namespace=' . $acl->getNamespace();
  19. $tbl = new TableAjax($acl);
  20. $tbl->showProcesInit(false); // TODO: activate when ready
  21. $tblLabel = $acl->getNamespace();
  22. if ('default_db' == $acl->getSourceName()) {
  23. $tblLabel = array();
  24. DBG::nicePrint($acl->getID(), '$acl->getID()');
  25. $zasobObj = ProcesHelper::getZasobTableInfo($acl->getID());
  26. DBG::nicePrint($zasobObj, '$zasobObj');
  27. $sqlId = $acl->getID();
  28. if (!$sqlId) throw new Exception("Missing zasob id!");
  29. $zasobItem = DB::getPDO()->fetchFirst("
  30. select z.ID, z.`DESC`, z.DESC_PL, z.OPIS
  31. from CRM_LISTA_ZASOBOW z
  32. where z.ID = {$sqlId}
  33. and z.`TYPE` = 'TABELA'
  34. ");
  35. DBG::nicePrint($zasobItem, '$zasobItem');
  36. // if (!$zasobObj) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
  37. // if (!empty($zasobObj->DESC_PL)) $tblLabel []= $zasobObj->DESC_PL;
  38. // if (!empty($zasobObj->OPIS)) $tblLabel []= $zasobObj->OPIS;
  39. if (!$zasobItem) throw new Exception("Zasob TABELA ID=" . $acl->getID() . " nie istnieje");
  40. if (!empty($zasobItem['DESC_PL'])) $tblLabel []= $zasobItem['DESC_PL'];
  41. if (!empty($zasobItem['OPIS'])) $tblLabel []= $zasobItem['OPIS'];
  42. $tblLabel = implode(" - ", $tblLabel);
  43. }
  44. $tbl->setSyncUrl($syncUrl);
  45. $tbl->setLabel($tblLabel);
  46. $tbl->addRowFunction('edit');
  47. $tbl->addRowFunction('hist');
  48. $tbl->addRowFunction('files');
  49. $tbl->addRowFunction('cp');
  50. $tbl->addRowFunction('msgs');
  51. return $tbl;
  52. }
  53. public function defaultAction() {
  54. UI::gora();
  55. UI::menu();
  56. try {
  57. $namespace = V::get('namespace', '', $_GET, 'word');
  58. if (!$namespace) {
  59. $typeName = V::get('typeName', '', $_GET, 'word');
  60. if (!$typeName) throw new Exception("Wrong param typeName");
  61. $namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
  62. }
  63. $acl = P5::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  64. $forceFilterInit = array();
  65. $filterInit = new stdClass();
  66. $filterInit->currSortCol = $acl->getPrimaryKeyField();
  67. $filterInit->currSortFlip = 'desc';
  68. foreach ($_GET as $k => $v) {
  69. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && !empty($v)) {// filter prefix
  70. $filterInit->$k = $v;
  71. }
  72. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && !empty($v)) {// special filter prefix
  73. $filterInit->$k = $v;
  74. }
  75. else if (strlen($k) > 4 && substr($k, 0, 3) == 'ff_' && !empty($v)) {// force filter prefix
  76. $fldName = substr($k, 3);
  77. $forceFilterInit[$fldName] = $v;
  78. }
  79. }
  80. $tbl = $this->getTableAjaxWidget($acl);
  81. $tbl->setFilterInit($filterInit);
  82. if (!empty($forceFilterInit)) $tbl->setForceFilterInit($forceFilterInit);
  83. if (V::get('DBG_INST', '', $_GET)) { // TODO: TEST namespace
  84. $siblings = ACL::getNamespaceSiblings($namespace);
  85. DBG::nicePrint($siblings, '$siblings');
  86. $filtrInstance = V::get('f_instance', [], $_POST, 'array');
  87. DBG::nicePrint($filtrInstance, '$filtrInstance');
  88. $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");
  89. $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");
  90. $_ = array(UI, 'h');
  91. echo $_('form', ['method' => "POST", 'style' => "width:600px; border:1px solid #ddd; border-radius:2px"], [
  92. $_('div', ['style' => "background-color:#ddd"], "Test Filtr instancji"),
  93. $_('div', ['style' => "padding:8px"], array_map(function ($sibling) use ($filtrInstance, $_) {
  94. return $_('div', [], [
  95. $_('label', ['style' => "margin:0 8px"], [
  96. $_('input', array_merge(['type'=>"radio", 'name'=>"f_instance[{$sibling}]", 'value'=>'YES'], ('YES' === V::get($sibling, '', $filtrInstance)) ? ['checked' => "checked"] : [])),
  97. " TAK "
  98. ]),
  99. $_('label', ['style' => "margin:0 8px"], [
  100. $_('input', array_merge(['type'=>"radio", 'name'=>"f_instance[{$sibling}]", 'value'=>'NO'], ('NO' === V::get($sibling, '', $filtrInstance)) ? ['checked' => "checked"] : [])),
  101. " NIE "
  102. ]),
  103. $_('label', ['style' => "margin:0 8px"], [
  104. $_('input', ['type'=>"radio", 'name'=>"f_instance[{$sibling}]", 'value'=>'x']),
  105. " pomiń "
  106. ]),
  107. // $_('button', ['onClick'=>"this.form.f_instance['{$sibling}'].value = ''; return false"], "odznacz"),
  108. $_('button', ['onClick'=>"console.log(this.form.elements['f_instance[{$sibling}]']); this.form.elements['f_instance[{$sibling}]'].value = 'x'; return false"], "odznacz"),
  109. " - {$sibling}"
  110. ]);
  111. }, $siblings)),
  112. $_('div', [], [
  113. $_('input', ['type'=>"hidden", 'name'=>'_route', 'value'=>"ViewObject"]),
  114. $_('input', ['type'=>"hidden", 'name'=>'namespace', 'value'=>$namespace]),
  115. $_('input', ['type'=>"submit", 'value'=>"Filtruj - TEST"]),
  116. ]),
  117. ]);
  118. DBG::nicePrint($_POST, '$_POST');
  119. {
  120. $args = $_POST;
  121. $fIsInstance = [];
  122. $fIsNotInstance = [];
  123. if (!empty($args['f_instance'])) {
  124. foreach ($args['f_instance'] as $inst => $selected) {
  125. if ('YES' === $selected) {
  126. $fIsInstance[] = $inst;
  127. } else if ('NO' === $selected) {
  128. $fIsNotInstance[] = $inst;
  129. }
  130. }
  131. }
  132. }
  133. $items = $acl->getItems([
  134. // TODO: 'propertyName' => "*,@instance",
  135. 'f_is_instance' => $fIsInstance,
  136. 'f_is_not_instance' => $fIsNotInstance,
  137. '@instances' => '1',
  138. 'limit' => 10
  139. ]);
  140. $rootNamespace = $acl->getRootNamespace();
  141. DBG::nicePrint($rootNamespace, '$rootNamespace');
  142. $jsRenderFunName = 'render_dropdown_instances_' . substr(md5(time()), 0, 6);
  143. DBG::nicePrint($jsRenderFunName, '$jsRenderFunName');
  144. UI::table([
  145. 'rows' => array_map(function($row) use ($namespace, $siblings, $rootNamespace, $jsRenderFunName) {
  146. return [
  147. 'ID' => $row['ID'],
  148. 'DESC' => $row['DESC'],
  149. 'TYPE' => $row['TYPE'],
  150. 'instances' => UI::h('div', ['class'=>"p5UI__dropdown-wrap"], [
  151. UI::h('button', [
  152. // 'onClick' => "p5UI__dropdown(event, this, 'left bottom')",
  153. 'onClick' => "p5UI__dropdown(event, this, 'left bottom', {$jsRenderFunName}({$row['ID']}))",
  154. 'class' => "btn btn-xs btn-default p5UI__dropdown-btn"
  155. ], [
  156. '<i class="glyphicon glyphicon-menu-hamburger"></i>',
  157. " ustal instancje"
  158. ]),
  159. UI::h('div', ['class' => "p5UI__dropdown-content"]
  160. , array_merge(
  161. [
  162. UI::h('input', ['type' => "text", 'placeholder' => "Search..", 'class' => "p5UI__dropdown-input", 'onkeyup' => "p5_ViewObject_instances_filterInput(this)"], null),
  163. ]
  164. , array_map(function ($sibling) use ($row, $namespace, $rootNamespace) {
  165. return UI::h('div', ['label'=>$sibling, 'style'=>"padding:4px 0"], [
  166. UI::h('button', array_merge(['class' => "btn btn-xs btn-default",
  167. 'style' => "margin:0 4px 0 0",
  168. 'title' => "Ustaw instancje '{$sibling}'",
  169. 'onClick' => "return p5_ViewObject_instance_set(this, '{$row['ID']}', '{$sibling}', 'yes')"],
  170. (in_array($sibling, explode(',', $row['@instances']))) ? ['disabled' => "disabled"] : []
  171. ), "+"),
  172. UI::h('button', ['class' => "btn btn-xs btn-default",
  173. 'style' => "margin:0 4px 0 0",
  174. 'title' => "Usuń instancje '{$sibling}'",
  175. 'onClick' => "return p5_ViewObject_instance_set(this, '{$row['ID']}', '{$sibling}', 'no')"], "-"),
  176. UI::h('span', [], substr($sibling, strlen($rootNamespace) + 1)),
  177. ]);
  178. }, $siblings)
  179. )
  180. )
  181. ]),
  182. 'instancesList' => implode(', ', ACL::getFeatureNamespaces($namespace, $row['ID'])),
  183. ];
  184. }, $items)
  185. ]);
  186. echo UI::h('style', ['type' => "text/css"], "
  187. .p5UI__dropdown-content { min-width:400px; padding:8px; background-color: #f6f6f6; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2) }
  188. .p5UI__dropdown-content .p5UI__dropdown-item { display:block; color:#000; padding:4px; text-decoration:none }
  189. .p5UI__dropdown-content .p5UI__dropdown-item:hover { background-color:#ebebeb }
  190. ");
  191. echo UI::h('script', ['src'=>"static/vendor.js", 'type'=>"text/javascript"]);
  192. $jsArgs = [
  193. 'SET_INSTANCE_URL' => $this->getLink('setInstanceAjax'),
  194. 'NAMESPACE' => $namespace,
  195. 'DBG' => DBG::isActive() ? 'true' : 'false',
  196. 'INITIAL_DROPDOWN_DATA' => json_encode([
  197. // 'allowed_instances' => $siblings,
  198. 'allowed_instances' => array_map(function ($sibling) use ($rootNamespace) {
  199. return [
  200. 'namespace' => $sibling,
  201. 'label' => substr($sibling, strlen($rootNamespace) + 1)
  202. ];
  203. }, $siblings),
  204. 'items' => array_map(function($row) use ($namespace, $siblings, $rootNamespace, $jsRenderFunName) {
  205. return [
  206. 'pk' => $row['ID'], // TODO: $primaryKeyField
  207. 'instances' => explode(',', $row['@instances']),
  208. ];
  209. }, $items)
  210. ])
  211. ];
  212. echo UI::h('script', [], "
  213. var DBG = {$jsArgs['DBG']};
  214. var NAMESPACE = '{$jsArgs['NAMESPACE']}'
  215. var SET_INSTANCE_URL = '{$jsArgs['SET_INSTANCE_URL']}'
  216. var initialData = {$jsArgs['INITIAL_DROPDOWN_DATA']};
  217. function {$jsRenderFunName}(pk) {
  218. if (!window.p5VendorJs.React) throw 'Missing p5VendorJs React'
  219. if (!window.p5VendorJs.ReactDOM) throw 'Missing p5VendorJs ReactDOM'
  220. var React = window.p5VendorJs.React
  221. var ReactDOM = window.p5VendorJs.ReactDOM
  222. var h = React.createElement
  223. if(DBG)console.log('F.{$jsRenderFunName}', { pk: pk, initialData: initialData })
  224. var _state = {
  225. allowed_instances: initialData.allowed_instances,
  226. query: '',
  227. filter: initialData.allowed_instances,
  228. item: initialData.items.filter(function (data) {
  229. return data['pk'] == pk
  230. }).pop(),
  231. waitingSet: [],
  232. waitingUnSet: [],
  233. }
  234. var _btnNode = null
  235. var _dropdownNode = null
  236. var _update = function (type, payload) {
  237. if(DBG)console.log('_update', { pk: pk, type: type, payload: payload, state: _state, btnNode: _btnNode, dropdownNode: _dropdownNode })
  238. switch (type) {
  239. case 'filter': {
  240. if(DBG)console.log('TODO: filter payload:', payload)
  241. _state.query = payload
  242. _state.filter = _state.allowed_instances.filter(function (inst) {
  243. return (-1 !== inst.label.toLowerCase().indexOf(payload.toLowerCase()))
  244. })
  245. } break;
  246. case 'send_set': {
  247. _state.waitingSet.push(payload)
  248. window.fetch(SET_INSTANCE_URL, {
  249. method: 'POST',
  250. headers: { 'Content-Type': 'application/json' },
  251. credentials: 'same-origin',
  252. body: JSON.stringify({
  253. namespace: NAMESPACE,
  254. primaryKey: _state.item.pk,
  255. instance: payload,
  256. toConnect: 'yes',
  257. })
  258. }).then(function (response) {
  259. return response.json()
  260. }).then(function (response) {
  261. p5UI__notifyAjaxCallback(response)
  262. if(DBG)console.log('response', response) // TODO: render list
  263. _update('set', payload)
  264. })
  265. } break;
  266. case 'send_unset': {
  267. _state.waitingUnSet.push(payload)
  268. window.fetch(SET_INSTANCE_URL, {
  269. method: 'POST',
  270. headers: { 'Content-Type': 'application/json' },
  271. credentials: 'same-origin',
  272. body: JSON.stringify({
  273. namespace: NAMESPACE,
  274. primaryKey: _state.item.pk,
  275. instance: payload,
  276. toConnect: 'no',
  277. })
  278. }).then(function (response) {
  279. return response.json()
  280. }).then(function (response) {
  281. p5UI__notifyAjaxCallback(response)
  282. if(DBG)console.log('response', response) // TODO: render list
  283. _update('unset', payload)
  284. })
  285. } break;
  286. case 'set': {
  287. _state.item.instances.push(payload)
  288. _state.waitingSet = _state.waitingSet.filter(function (inst) {
  289. return inst !== payload
  290. })
  291. } break; // TODO: sync url
  292. case 'unset': {
  293. _state.item.instances = _state.item.instances.filter(function (inst) {
  294. return inst !== payload
  295. })
  296. _state.waitingUnSet = _state.waitingSet.filter(function (inst) {
  297. return inst !== payload
  298. })
  299. } break; // TODO: sync url
  300. }
  301. _render()
  302. }
  303. var btnToggleInstance = function (props) {
  304. if (-1 === _state.item.instances.indexOf(props.namespace)) {
  305. if (-1 === _state.waitingSet.indexOf(props.namespace)) {
  306. return h('button', { style: { margin: '0 6px 0 0' },
  307. className: 'btn btn-xs btn-default',
  308. onClick: function () { _update('send_set', props.namespace) }
  309. }, 'ustaw')
  310. } else {
  311. return h('button', { style: { margin: '0 6px 0 0' },
  312. className: 'btn btn-xs btn-default disabled',
  313. }, 'ustaw...')
  314. }
  315. } else {
  316. if (-1 === _state.waitingUnSet.indexOf(props.namespace)) {
  317. return h('button', { style: { margin: '0 6px 0 0' },
  318. className: 'btn btn-xs btn-default',
  319. onClick: function () { _update('send_unset', props.namespace) }
  320. }, 'usuń')
  321. } else {
  322. return h('button', { style: { margin: '0 6px 0 0' },
  323. className: 'btn btn-xs btn-default disabled',
  324. }, 'usuń...')
  325. }
  326. }
  327. }
  328. var _render = function () {
  329. if(DBG)console.log('_render', { pk: pk, state: _state, btnNode: _btnNode, dropdownNode: _dropdownNode })
  330. if (!_dropdownNode) throw 'Missing dropdownNode'
  331. ReactDOM.render(
  332. h('div', {}, [].concat(
  333. h('input', { type: 'text',
  334. placeholder: 'Szukaj...',
  335. className: 'p5UI__dropdown-input',
  336. onChange: function (e) { _update('filter', e.target.value); },
  337. autoFocus: true,
  338. value: _state.query,
  339. })
  340. ).concat(
  341. _state.filter.map(function (inst) {
  342. return h('div', { className: 'p5UI__dropdown-item', style: {padding: '4px 0'} }, [
  343. btnToggleInstance({ namespace: inst.namespace }),
  344. inst.label,
  345. ])
  346. })
  347. )
  348. ),
  349. _dropdownNode
  350. );
  351. setTimeout(function () {
  352. if (_dropdownNode.firstChild && _dropdownNode.firstChild.firstChild)
  353. _dropdownNode.firstChild.firstChild.focus()
  354. }, 100)
  355. }
  356. return function (btnNode, dropdownNode) {
  357. _btnNode = btnNode
  358. _dropdownNode = dropdownNode
  359. if(DBG)console.log('F.{$jsRenderFunName}', { pk: pk, initialData: initialData, btnNode: btnNode, dropdownNode: dropdownNode, state: _state })
  360. _render()
  361. }
  362. }
  363. ");
  364. echo UI::h('script', [], "
  365. var SET_INSTANCE_URL = '{$jsArgs['SET_INSTANCE_URL']}'
  366. var NAMESPACE = '{$jsArgs['NAMESPACE']}'
  367. function p5_ViewObject_instance_set(n, pk, sibling, toConnect) {
  368. console.log('p5_ViewObject_instance_set pk('+pk+'), sibling('+sibling+'), toConnect('+toConnect+'), n', n);
  369. window.fetch(SET_INSTANCE_URL, {
  370. method: 'POST',
  371. headers: { 'Content-Type': 'application/json' },
  372. credentials: 'same-origin',
  373. body: JSON.stringify({
  374. namespace: NAMESPACE,
  375. primaryKey: pk,
  376. instance: sibling,
  377. toConnect: toConnect,
  378. })
  379. }).then(function (response) {
  380. return response.json()
  381. }).then(function (response) {
  382. p5UI__notifyAjaxCallback(response)
  383. console.log(response) // TODO: render list
  384. })
  385. }
  386. ");
  387. echo UI::h('script', [], "
  388. function p5_ViewObject_instances_filterInput(n) {
  389. var input, filter, ul, li, a, i, div;
  390. input = n // .id-myInput
  391. filter = input.value.toUpperCase()
  392. div = n.parentNode // .id-myDropdown
  393. a = div.getElementsByTagName('div')
  394. for (i = 0; i < a.length; i++) {
  395. if (a[i].getAttribute('label') && a[i].getAttribute('label').toUpperCase().indexOf(filter) > -1) {
  396. a[i].style.display = 'block'
  397. } else {
  398. a[i].style.display = 'none'
  399. }
  400. }
  401. }
  402. ");
  403. echo '<hr style="margin-top:300px">';
  404. exit;
  405. }
  406. echo $tbl->render();
  407. if (DBG::isActive() && V::get('DBG_ACL', '', $_GET)) {// test load perms
  408. Lib::loadClass('DebugExecutionTime');
  409. $dbgExecTime = new DebugExecutionTime();
  410. $dbgExecTime->activate();
  411. $dbgExecTime->log('start');
  412. UI::startContainer(['style'=>'border:1px solid red']);
  413. UI::tag('p', null, "TEST - load perms from db");
  414. $idTable = $acl->getID();
  415. UI::tag('p', null, "DBG idTable({$idTable})");
  416. if ($idTable > 0) {
  417. $dbgExecTime->log('before sql');
  418. $aclTableRows = DB::getPDO()->fetchAll("select * from `CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW` where ID_TABLE = {$idTable}");
  419. $dbgExecTime->log('after sql', ['sql']);
  420. UI::table(['caption' => "from CRM_PROCES_idx_TABLE_TO_PROCES_PERMS_VIEW", 'rows' => $aclTableRows]);
  421. $csvIdProces = array();
  422. foreach ($aclTableRows as $row) {
  423. if (!in_array($row['ID_PROCES'], $csvIdProces)) $csvIdProces[] = $row['ID_PROCES'];
  424. }
  425. }
  426. $tableName = $acl->getName();
  427. $databaseName = DB::getPDO()->getDatabaseName();
  428. UI::table([
  429. 'caption' => "Cell to process",
  430. 'rows' => array_map(
  431. function ($row) use ($aclTableRows, $idTable) {
  432. $row['proces'] = array();
  433. $row['id_zasob'] = 0;
  434. $row['PERM_R'] = 0;
  435. $row['PERM_W'] = 0;
  436. $row['PERM_X'] = 0;
  437. $row['PERM_C'] = 0;
  438. $row['PERM_S'] = 0;
  439. $row['PERM_O'] = 0;
  440. $row['PERM_V'] = 0;
  441. $row['PERM_E'] = 0;
  442. foreach ($aclTableRows as $aclInfo) {
  443. if (strtolower($aclInfo['CELL_NAME']) == strtolower($row['COLUMN_NAME'])) {
  444. $row['proces'][] = $aclInfo['ID_PROCES'];
  445. $row['id_zasob'] = $aclInfo['ID_CELL'];
  446. $row['PERM_R'] += $aclInfo['PERM_R'];
  447. $row['PERM_W'] += $aclInfo['PERM_W'];
  448. $row['PERM_X'] += $aclInfo['PERM_X'];
  449. $row['PERM_C'] += $aclInfo['PERM_C'];
  450. $row['PERM_S'] += $aclInfo['PERM_S'];
  451. $row['PERM_O'] += $aclInfo['PERM_O'];
  452. $row['PERM_V'] += $aclInfo['PERM_V'];
  453. $row['PERM_E'] += $aclInfo['PERM_E'];
  454. }
  455. }
  456. $row['proces'] = (empty($row['proces']))
  457. ? "<i style=\"color:red\">Brak</i>"
  458. : implode(", ", $row['proces']);
  459. 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");
  460. return $row;
  461. }, DB::getPDO()->fetchAll("
  462. select t.TABLE_NAME, t.COLUMN_NAME, t.DATA_TYPE, t.COLUMN_TYPE
  463. from `information_schema`.`COLUMNS` t
  464. where t.TABLE_SCHEMA = '{$databaseName}'
  465. and t.TABLE_NAME like '{$tableName}'
  466. ")
  467. )
  468. ]);
  469. if (!empty($csvIdProces)) {
  470. $csvIdProces = implode(",", $csvIdProces);
  471. UI::tag('p', null, "DBG csvIdProces({$csvIdProces})");
  472. $userLogin = User::getLogin();
  473. $dbgExecTime->log('before sql');
  474. $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");
  475. $dbgExecTime->log('after sql', ['sql']);
  476. UI::table(['caption' => "from CRM_PROCES_idx_USER_to_PROCES_VIEW", 'rows' => $rows]);
  477. $userIdProces = array(); foreach ($rows as $row) $userIdProces[] = $row['ID_PROCES'];
  478. $userTablePerms = array();
  479. foreach ($aclTableRows as $row) {
  480. if (!in_array($row['ID_PROCES'], $userIdProces)) continue;
  481. if (array_key_exists($row['CELL_NAME'], $userTablePerms)) {
  482. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_R' ] += $row['PERM_R'];
  483. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_W' ] += $row['PERM_W'];
  484. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_X' ] += $row['PERM_X'];
  485. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_C' ] += $row['PERM_C'];
  486. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_S' ] += $row['PERM_S'];
  487. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_O' ] += $row['PERM_O'];
  488. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_V' ] += $row['PERM_V'];
  489. $userTablePerms[ $row['CELL_NAME'] ][ 'PERM_E' ] += $row['PERM_E'];
  490. } else {
  491. $userTablePerms[ $row['CELL_NAME'] ] = $row;
  492. unset($userTablePerms[ $row['CELL_NAME'] ][ 'TABLE_DESCRIPTION' ]);
  493. unset($userTablePerms[ $row['CELL_NAME'] ][ 'ID_PROCES' ]);
  494. unset($userTablePerms[ $row['CELL_NAME'] ][ 'FORM_TREAT' ]);
  495. }
  496. }
  497. UI::table(['caption' => "\$userTablePerms", 'rows' => $userTablePerms]);
  498. } else UI::alert('warning', "brak \$csvIdProces");
  499. $dbgExecTime->printDebug();
  500. UI::endContainer();
  501. }
  502. } catch (Exception $e) {
  503. UI::startContainer();
  504. UI::alert('danger', "<strong>Wystąpiły błędy!</strong> " . $e->getMessage());
  505. UI::endContainer();
  506. DBG::log($e);
  507. }
  508. UI::dol();
  509. }
  510. public function setInstanceAjaxAction() {
  511. Response::sendTryCatchJson(array($this, 'setInstanceAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  512. }
  513. public function setInstanceAjax($args) {
  514. // namespace => default_db/CRM_PROCES/PROCES_INIT
  515. // primaryKey => 6
  516. // instance => default_db/CRM_PROCES/PROCES_TREE
  517. // toConnect => yes
  518. if (empty($args['namespace'])) throw new Exception("Missing namespace");
  519. if (empty($args['primaryKey'])) throw new Exception("Missing primaryKey");
  520. if (empty($args['instance'])) throw new Exception("Missing instance");
  521. if (empty($args['toConnect'])) throw new Exception("Missing toConnect");
  522. if (!in_array($args['toConnect'], ['yes', 'no'])) throw new Exception("Wrong param toConnect");
  523. $idInstance = ACL::getInstanceId($args['instance']);
  524. $instanceTable = ACL::getInstanceTable($args['namespace']);
  525. switch ($args['toConnect']) {
  526. case 'yes': {
  527. // TODO: _HIST info - waiting
  528. $ret = DB::getPDO()->execSql("
  529. insert into `{$instanceTable}` (`pk`, `idInstance`)
  530. values ( :pk , :idInstance )
  531. ", [
  532. 'pk' => $args['primaryKey'],
  533. 'idInstance' => $idInstance,
  534. ]);
  535. // TODO: _HIST info - mark as done
  536. } break;
  537. case 'no': {
  538. // TODO: _HIST info - waiting
  539. $ret = DB::getPDO()->execSql("
  540. delete from `{$instanceTable}`
  541. where `pk` = :pk
  542. and `idInstance` = :idInstance
  543. ", [
  544. 'pk' => $args['primaryKey'],
  545. 'idInstance' => $idInstance,
  546. ]);
  547. // TODO: _HIST info - mark as done
  548. } break;
  549. }
  550. return [
  551. 'type' => 'success',
  552. 'msg' => "Wprowadzono zmiany ({$ret})",
  553. // TODO: 'data' => all instances for pk
  554. ];
  555. }
  556. public function rmUserTableFilterAjaxAction() {
  557. Response::sendTryCatchJson(array($this, 'rmUserTableFilterAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  558. }
  559. public function rmUserTableFilterAjax($args) {
  560. $namespace = V::get('namespace', '', $args);
  561. $filtrName = V::get('filtrName', '', $args);
  562. if (!$namespace) throw new Exception("Missing namespace");
  563. if (!$filtrName) throw new Exception("Missing filtrName");
  564. $userFltrConfKey = "tableColFilters__" . User::getLogin();
  565. $currentFilters = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = '{$userFltrConfKey}' ");
  566. if (!$currentFilters) return [
  567. 'type' => 'warning',
  568. 'msg' => "Brak filtrów w bazie",
  569. ];
  570. $currentFilters = json_decode($currentFilters, 'assoc');
  571. unset($currentFilters[$namespace][$filtrName]);
  572. $affeced = DB::getPDO()->update('CRM_CONFIG', 'CONF_KEY', $userFltrConfKey, [
  573. 'CONF_VAL' => json_encode($currentFilters)
  574. ]);
  575. return [
  576. 'type' => 'success',
  577. 'msg' => 'Zapisano nowy filtr',
  578. 'data' => $currentFilters[$namespace]
  579. ];
  580. }
  581. public function addUserTableFilterAjaxAction() {
  582. Response::sendTryCatchJson(array($this, 'addUserTableFilterAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  583. }
  584. public function addUserTableFilterAjax($args) {
  585. $namespace = V::get('namespace', '', $args);
  586. $filtrName = V::get('filtrName', '', $args);
  587. $visibleCols = V::get('visibleCols', '', $args);
  588. if (!$namespace) throw new Exception("Missing namespace");
  589. if (!$filtrName) throw new Exception("Missing filtrName");
  590. if (!$visibleCols) throw new Exception("Missing visibleCols");
  591. $userFltrConfKey = "tableColFilters__" . User::getLogin();
  592. $currentFilters = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = '{$userFltrConfKey}' ");
  593. $currentFilters = ($currentFilters) ? json_decode($currentFilters, 'assoc') : [];
  594. $currentFilters[$namespace][$filtrName] = $visibleCols;
  595. $sqlFltr = json_encode($currentFilters);
  596. DB::getPDO()->execSql("
  597. insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
  598. values ('$userFltrConfKey', '{$sqlFltr}')
  599. on duplicate key update CONF_VAL = '{$sqlFltr}'
  600. ");
  601. return [
  602. 'type' => 'success',
  603. 'msg' => 'Zapisano nowy filtr',
  604. 'data' => $currentFilters[$namespace]
  605. ];
  606. }
  607. public function getUserTableFilterAjaxAction() {
  608. Response::sendTryCatchJson(array($this, 'getUserTableFilterAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  609. }
  610. public function getUserTableFilterAjax($args) {
  611. $namespace = V::get('namespace', '', $args);
  612. if (!$namespace) throw new Exception("Missing namespace");
  613. $userFltrConfKey = "tableColFilters__" . User::getLogin();
  614. $currentFilters = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = '{$userFltrConfKey}' ");
  615. $currentFilters = ($currentFilters) ? json_decode($currentFilters, 'assoc') : [];
  616. return [
  617. 'type' => 'success',
  618. 'msg' => 'Odczytano filtry użytkownika',
  619. 'data' => (!empty($currentFilters[$namespace])) ? $currentFilters[$namespace] : []
  620. ];
  621. }
  622. public function revertFromHistAjaxAction() {
  623. Response::sendTryCatchJson(array($this, 'revertFromHistAjax'));
  624. }
  625. public function revertFromHistAjax() {
  626. $typeName = V::get('typeName', '', $_REQUEST, 'word');
  627. if (!$typeName) throw new Exception("Wrong param typeName");
  628. // TODO: use namespace from url
  629. // $namespace = V::get('namespace', '', $_GET, 'word');
  630. // if (!$namespace) {
  631. // $typeName = V::get('typeName', '', $_GET, 'word');
  632. // if (!$typeName) throw new Exception("Wrong param typeName");
  633. // $namespace = Api_WfsNs::getBaseWfsUri() . '/' . str_replace(':', '/', $typeName);
  634. // }
  635. // $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  636. $id = V::get('ID', '', $_REQUEST, 'word');
  637. if (!$id) throw new Exception("Wrong param ID");
  638. $idHist = V::get('idHist', '', $_REQUEST, 'word');
  639. if (!$idHist) throw new Exception("Wrong param idHist");
  640. $fieldName = V::get('fieldName', '', $_REQUEST, 'word');
  641. if (!$fieldName) throw new Exception("Wrong param fieldName");
  642. $acl = Core_AclHelper::getAclByTypeName($typeName);
  643. $item = $acl->getItem($id);
  644. if (!$item) throw new HttpException("Item not found", 404);
  645. if (!$acl->canWriteObjectField($fieldName, $record)) throw new Exception("Missing perm Write for field {$fieldName}");
  646. $histItem = $acl->getHistItem($id, $idHist);
  647. if (!$histItem) throw new HttpException("Hist Item not found", 404);
  648. $histValue = V::get($fieldName, 'N/S;', $histItem);
  649. if ('N/S;' == $histValue) throw new Exception("Missing field value in hist[{$idHist}] for field({$fieldName}) from item[{$id}]");
  650. if ($acl->isGeomField($fieldName)) {
  651. $wktType = strtoupper($acl->getGeomFieldType($fieldName));
  652. if (!$wktType) throw new Exception("Wrong geometry type for field {$fieldName}");
  653. if ($wktType != strtoupper(substr($histValue, 0, strlen($wktType)))) throw new Exception("Wrong geometry type for field {$fieldName} in hist value");
  654. $coords = trim(substr($histValue, strlen($wktType)), '()');
  655. $wktValue = $acl->convertGmlCoordsToWkt($wktType, $coords, ['cs'=>' ', 'ts'=>',']);
  656. if (!$wktValue) throw new Exception("BUG in hist record");
  657. $sqlObj = array();
  658. $sqlObj['ID'] = $id;
  659. $sqlObj[$fieldName] = "GeomFromText('{$wktValue}')";
  660. $affected = DB::getDB()->UPDATE_OBJ($acl->getName(), (object)$sqlObj);
  661. if (0 == $affected) throw new AlertInfoException("Nie wprowadzono żadnych zmian");
  662. else if ($affected < 0) throw new Exception("Wystąpiły błędy podczas aktualizacji rekordu [{$id}]");
  663. $jsonResponse = array();
  664. $jsonResponse['type'] = 'success';
  665. $jsonResponse['msg'] = "Zaktualizowano dane na podstawie wcześniejszej wartości dla rekordu [{$id}]";
  666. $jsonResponse['actions'] = array();
  667. $jsonResponse['actions'][] = ['jsFunction'=>'TableAjax__HIST_Route', 'args'=>[$id]];
  668. return $jsonResponse;
  669. } else {
  670. throw new HttpException("Not implemented - update from hist only for the geom field", 501);
  671. }
  672. throw new Exception("BUG: update field '{$fieldName}' in item[{$id}] from hist[{$idHist}]", 501);
  673. }
  674. public function removeTheGeomAjaxAction() {
  675. Response::sendTryCatchJson(array($this, 'removeTheGeomAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  676. }
  677. public function removeTheGeomAjax($args) {
  678. $namespace = V::get('namespace', '', $args, 'word');
  679. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  680. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
  681. $primaryKeyField = $acl->getPrimaryKeyField();
  682. $primaryKey = V::get($primaryKeyField, 0, $args, 'int');
  683. $geomFieldName = 'the_geom';
  684. $response = new stdClass();
  685. if ($primaryKey <= 0) throw new HttpException("Bad Request - Wrong param ID", 400);
  686. $record = $acl->getItem($primaryKey);
  687. if (!$record) throw new HttpException("Nie odnaleziono rekordu nr {$primaryKey}", 404);
  688. if (!$acl->canWriteObjectField($geomFieldName, $record)) throw new HttpException("Brak dostępu do zapisu dla pola {$geomFieldName}", 403);
  689. if (empty($record->{$geomFieldName})) {
  690. $response->type = 'info';
  691. $response->msg = "Rekord nie jest powiązany z żadnym obiektem na mapie";
  692. $response->record = $record;
  693. return $response;
  694. }
  695. $itemPatch = array();
  696. $itemPatch[$geomFieldName] = "NULL";
  697. $itemPatch[$primaryKeyField] = $primaryKey;
  698. $response = new stdClass();
  699. try {
  700. $affected = $acl->updateItem($itemPatch);
  701. if ($affected > 0) {
  702. $response->type = 'success';
  703. $response->msg = "Usunięto obiekt z mapy dla rekordu {$primaryKey}";// Rekord zapisany pomyślnie
  704. } else if ($affected == 0) {
  705. $response->type = 'info';
  706. $response->msg = "Nie wprowadzono żadnych zmian";
  707. }
  708. $response->record = $acl->getItem($primaryKey);
  709. }
  710. catch (Exception $e) {
  711. $response->type = 'error';
  712. $response->msg = $e->getMessage();
  713. }
  714. return $response;
  715. }
  716. public function moreFunctionsCellAjaxAction() {
  717. Response::sendTryCatchJson(array($this, 'moreFunctionsCell'), $args = $_GET);
  718. }
  719. public function moreFunctionsCell($args) {// ajax task 'MORE_FUNCTIONS_CELL'
  720. $id = V::get('ID', 0, $args, 'int');
  721. if ($id <= 0) throw new HttpException("404", 404);
  722. $namespace = V::get('namespace', '', $args, 'word');
  723. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  724. $acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $args)));
  725. $response = new stdClass();
  726. $response->type = 'success';
  727. $response->msg = 'Funkcje';
  728. $response->rowFunctions = Core_AclHelper::getMoreFunctionsCell($acl, array('primary_key' => $id));
  729. return $response;
  730. }
  731. public function editFormAction() {// namespace, _hash, _primaryKey
  732. $args = $_REQUEST;
  733. $id = V::get('_primaryKey', 0, $args, 'int');
  734. if ($id <= 0) throw new HttpException("Bad Request - missing primaryKey", 400);
  735. $namespace = V::get('namespace', '', $args, 'word');
  736. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  737. $acl = Core_AclHelper::getAclByNamespace($namespace);
  738. $tbl = $this->getTableAjaxWidget($acl);
  739. $tbl->sendAjaxEdit($id, $args);
  740. }
  741. public function editSaveAjaxAction() {
  742. Response::sendTryCatchJson(array($this, 'editSaveAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  743. }
  744. public function editSaveAjax($args) {
  745. $namespace = V::get('namespace', '', $args, 'word');
  746. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  747. $acl = Core_AclHelper::getAclByNamespace($namespace);
  748. $primaryKeyField = $acl->getPrimaryKeyField();
  749. $primaryKey = V::get('primaryKey', 0, $args, 'int');
  750. if (empty($primaryKey)) throw new HttpException("Bad Request - missing primaryKey!", 400);
  751. $item = $acl->getItem($primaryKey);
  752. if (!$item) throw new HttpException("Item not exists!", 404);
  753. $itemFromUser = $acl->convertObjectFromUserInput($args['form'], $type = 'array_by_id', $prefix = 'f');
  754. $response = new stdClass();
  755. $response->primaryKey = $primaryKey;
  756. try {
  757. $itemFromUser[$primaryKeyField] = $primaryKey;
  758. $affected = $acl->updateItem($itemFromUser);
  759. if ($affected > 0) {
  760. $response->type = 'success';
  761. $response->msg = "Rekord zapisany pomyślnie";//"Record saved successfully";
  762. } else if ($affected == 0) {
  763. $response->type = 'info';
  764. $response->msg = "Nie wprowadzono żadnych zmian";
  765. }
  766. $response->record = $acl->getItem($primaryKey);
  767. $rowFunList = Core_AclHelper::getMoreFunctionsCell($acl, array('primary_key'=>$primaryKey, 'record'=>$response->record));
  768. if (!empty($rowFunList)) $response->rowFunctions = $rowFunList;
  769. }
  770. catch (Exception $e) {
  771. $response->type = 'error';
  772. $response->msg = "Wystąpiły błędy!";
  773. $response->msg .= $e->getMessage();
  774. }
  775. return $response;
  776. }
  777. public function typeSpecialCellAction() {
  778. Response::sendTryCatchJson(array($this, 'typeSpecialCell'), $args = $_REQUEST);
  779. }
  780. public function typeSpecialCell($args) {
  781. $namespace = V::get('namespace', '', $args, 'word');
  782. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  783. $acl = Core_AclHelper::getAclByNamespace($namespace);
  784. $id = V::get('ID', 0, $args, 'int');
  785. $fieldName = V::get('col', '', $args);
  786. if ($id <= 0 || empty($fieldName)) throw new HttpException("Bad Request - missing id or col", 400);
  787. $col = $fieldName;// TODO: RM $col
  788. $jsonData = new stdClass();
  789. $idField = $acl->getFieldIdByName($fieldName);
  790. if (!$idField) throw new Exception("Wrong field");
  791. $item = $acl->getItem($id);
  792. if (!$acl->canReadObjectField($fieldName, $item)) throw new Exception("Brak dostępu");
  793. $typeSpecial = Typespecial::getInstance($idField, $fieldName);
  794. if ($typeSpecial) {
  795. $jsonData->data = $typeSpecial->getReturnData($acl->getID(), $id, $fieldName, '');
  796. $jsonData->namespace = 'default_db/' . V::get('tbl_name', '', $jsonData->data);
  797. }
  798. return $jsonData;
  799. }
  800. /**
  801. * @param $_GET['namespace'] = AclNamespace
  802. * @param $_GET['format'] = 'csv' | 'html'
  803. * @param $_GET['flds'] = csv - coma separated field names
  804. * @param $_GET['sortCol'] = FieldName
  805. * @param $_GET['sortDir'] = SortDir ('desc' | 'asc')
  806. * @param $_GET['f_{$fieldName}'] = filter
  807. * @param $_GET['sf_{$fieldName}'] = force filter
  808. */
  809. public function exportAction() {
  810. $args = $_GET;
  811. $namespace = V::get('namespace', '', $args, 'word');
  812. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  813. $acl = Core_AclHelper::getAclByNamespace($namespace);
  814. $exportLimit = 10000;
  815. $params = array();
  816. $params['limit'] = $exportLimit;
  817. // $params['limitstart'] = 0;
  818. $params['order_by'] = V::get('sortCol', '', $args);
  819. $params['order_dir'] = V::get('sortDir', '', $args);
  820. $params['cols'] = array($acl->getPrimaryKeyField());
  821. $toExportFields = explode(',', V::get('flds', '', $_GET));
  822. if (empty($toExportFields)) throw new Exception("Nie wybrano żandych pól do exportu.");
  823. $allowedExportFieldList = Core_AclHelper::getExportFieldList($acl);
  824. foreach ($toExportFields as $fieldName) {
  825. if ($fieldName == $acl->getPrimaryKeyField()) continue;
  826. if (!in_array($fieldName, $allowedExportFieldList)) throw new Exception("Brak uprawnień do exportu pola '{$fieldName}'");
  827. $params['cols'][] = $fieldName;
  828. }
  829. $labels = array();
  830. foreach ($toExportFields as $fieldName) {
  831. $labels[ $fieldName ] = $acl->getFieldLabel($fieldName);
  832. }
  833. foreach ($args as $k => $v) {
  834. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && strlen($v) > 0) {// filter prefix
  835. $params[$k] = $v;
  836. }
  837. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && strlen($v) > 0) {// special filter prefix
  838. $params[$k] = $v;
  839. }
  840. }
  841. $total = $acl->getTotal($params);
  842. // if ($total > $exportLimit) $params['limit'] = $exportLimit;
  843. $items = $acl->getItems($params);
  844. $format = V::get('format', 'html', $_GET);
  845. if ('html' == $format) {
  846. UI::gora();
  847. UI::startTag('table', ['class'=>'table table-bordered table-hover']);
  848. UI::startTag('thead');
  849. UI::startTag('tr');
  850. foreach ($labels as $fldName => $label) {
  851. UI::tag('th', [], $label);
  852. }
  853. UI::endTag('tr');
  854. UI::endTag('thead');
  855. UI::startTag('tbody');
  856. foreach ($items as $item) :
  857. UI::startTag('tr');
  858. foreach ($labels as $fldName => $label) :
  859. UI::tag('td', [], $item->{$fldName});
  860. endforeach;
  861. UI::endTag('tr');
  862. endforeach;
  863. UI::endTag('tbody');
  864. UI::endTag('table');
  865. UI::dol();
  866. }
  867. else if ('csv' == $format) {
  868. $csvFileName = "Tabela-" . $acl->getName() . "-" . date("Y-m-d_H_s");
  869. header('Content-Type: text/csv; charset=utf-8');
  870. header("Content-Disposition: attachment; filename={$csvFileName}.csv");
  871. $csvSeparator = ';';
  872. $labelsLine = array();
  873. foreach ($labels as $fldName => $label) {
  874. $labelsLine[] = '"' . addslashes($label) . '"';
  875. }
  876. echo implode($csvSeparator, $labelsLine) . "\n";
  877. foreach ($items as $item) {
  878. $itemLine = array();
  879. foreach ($labels as $fldName => $label) {
  880. $itemLine[] = '"' . addslashes($item->{$fldName}) . '"';
  881. }
  882. echo implode($csvSeparator, $itemLine) . "\n";
  883. }
  884. }
  885. else {
  886. die("Nieobsługiwany format danych.");
  887. }
  888. }
  889. public function loadDataAjaxAction() {
  890. // $tbl = $this->getTableAjaxWidget($acl);
  891. Response::sendTryCatchJson(array($this, 'loadDataAjax'), $args = $_GET);
  892. }
  893. public function loadDataAjax($args) {
  894. $namespace = V::get('namespace', '', $args, 'word');
  895. if (!$namespace) throw new HttpException("Bad Request - missing namespace", 400);
  896. $acl = P5::getAclByNamespace($namespace);
  897. $uiConf = [
  898. 'pageSize' => 10
  899. ];
  900. $DBG = ('1' == V::get('DBG', '', $args));
  901. $pageSize = V::get('pageSize', $uiConf['pageSize'], $args, 'int');
  902. $page = V::get('page', 0, $args, 'int');
  903. $currSortCol = V::get('currSortCol', '', $args);
  904. $currSortFlip = V::get('currSortFlip', '', $args);
  905. if ($page > 0) {
  906. $page -= 1;
  907. }
  908. $params = array();
  909. $params['limit'] = $pageSize;
  910. $params['limitstart'] = $page * $params['limit'];
  911. $params['order_by'] = ($currSortCol)? $currSortCol : '';
  912. $params['order_dir'] = $currSortFlip;
  913. $filters = new stdClass();
  914. $filters->currSortCol = $currSortCol;
  915. $filters->currSortFlip = $currSortFlip;
  916. foreach ($args as $k => $v) {
  917. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && strlen($v) > 0) {// filter prefix
  918. $params[$k] = $v;
  919. $filters->{$k} = $v;
  920. }
  921. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && strlen($v) > 0) {// special filter prefix
  922. $params[$k] = $v;
  923. $filters->{$k} = $v;
  924. }
  925. }
  926. // TODO: $this->setFilters($filters);
  927. $vCols = $acl->getVirtualFieldListByIdZasob();
  928. if (!empty($vCols)) {
  929. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">vCols (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($vCols);echo'</pre>';}
  930. }
  931. $visibleCols = $acl->getVisibleFieldListByIdZasob();
  932. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">visibleCols (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($visibleCols);echo'</pre>';}
  933. $jsonData = new stdClass();
  934. $jsonData->page = $page + 1;
  935. $jsonData->pageSize = $pageSize;
  936. $jsonData->filters = $filters;
  937. $jsonData->cols = new stdClass();
  938. $jsonData->uniqueCol = $acl->getPrimaryKeyField();
  939. $ind = 0;
  940. foreach ($visibleCols as $fieldID => $col) {
  941. $ind++;
  942. $columnConfig = (object)array('index'=>$ind);
  943. if (in_array($col, array('A_STATUS','A_STATUS_CURRENT','A_SERVICES_STATUS_CURRENT'))) {
  944. $columnConfig->format = '<div class="cell-A_STATUS-{0}">{0}</div>';
  945. }
  946. else if (in_array($col, array('Status'))) {
  947. // Ahmes problems Status colors
  948. $columnConfig->format = '<div class="cell-Status-{0}">{0}</div>';
  949. }
  950. else if ($acl->isGeomField($col)) {
  951. $columnConfig->type = 'geom';
  952. }
  953. if ('' !== ($label = $acl->getFieldLabel($col))) {
  954. $columnConfig->friendly = $label;
  955. }
  956. $colType = $acl->getFieldType($col);
  957. if ($colType) {// @see MarkTableAjaxFilterColType
  958. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">field('.$col.') $colType (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($colType);echo'</pre>';}
  959. if ($colType['type'] == 'date') {
  960. //$columnConfig->type = 'date';// TODO: require datetimepicker
  961. }
  962. if (!empty($colType['simpleType'])) $columnConfig->type = $colType['simpleType'];
  963. } else {// typespecial - no type
  964. $columnConfig->type = 'special';
  965. }
  966. $columnConfig->xsdType = $acl->getXsdFieldType($col);
  967. if ($columnConfig->xsdType) {
  968. $ex = explode(":", $columnConfig->xsdType);
  969. switch ($ex[0]) {
  970. case 'ref':
  971. case 'alias_ref':
  972. if (3 != count($ex)) throw new HttpException("Schema Error for field({$col}) xsdType({$columnConfig->xsdType})", 500);
  973. $columnConfig->type = 'ref';
  974. $columnConfig->xsdRefUri = Api_WfsNs::getNsUri($ex[1]);
  975. $columnConfig->xsdRefType = $ex[2];
  976. $columnConfig->xsdRefNsPrefix = $ex[1];
  977. break;
  978. case 'xsd':
  979. switch ($ex[1]) {
  980. case 'string': $columnConfig->type = 'string'; break;
  981. case 'ind': $columnConfig->type = 'number'; break;// TODO: bug 'ind'?
  982. }
  983. break;
  984. case 'p5':
  985. switch ($ex[1]) {
  986. case 'alias': {
  987. $format = $acl->getXsdFieldParam($col, 'format');
  988. if ($format) $columnConfig->format = $format;
  989. } break;
  990. case 'string': {
  991. $columnConfig->type = 'p5:string';
  992. $columnConfig->formatByValue = $acl->getXsdFieldParam($col, 'formatByValue');
  993. } break;
  994. }
  995. break;
  996. }
  997. }
  998. $typeSpecial = Typespecial::getInstance($fieldID, $col);
  999. if ($typeSpecial) {
  1000. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">typeSpecial (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($typeSpecial);echo'</pre>';}
  1001. $columnConfig->_tsRetId = $typeSpecial->getReturnId();
  1002. if ($columnConfig->_tsRetId == 0) {
  1003. $tsParamOut = V::get('param_out', null, $typeSpecial);
  1004. if ($tsParamOut) {
  1005. $tsFormat = V::get('format', null, $tsParamOut);
  1006. $tsValues = V::get('values', null, $tsParamOut);
  1007. $tsAliases = V::get('alias', null, $tsParamOut);
  1008. if (false !== strpos($tsFormat, '<a') && !empty($tsValues) && !empty($tsAliases)) {
  1009. $tsAliasMap = array();
  1010. /* [values] => Array([ID] => 1467), [alias] => Array([1467] => ID) */
  1011. $bugTsColsNotVisible = array();
  1012. foreach ($tsValues as $kVarName => $vIdZasob) {
  1013. if (array_key_exists($vIdZasob, $tsAliases)) {
  1014. $tsAliasMap[$kVarName] = $tsAliases[$vIdZasob];
  1015. } else {
  1016. $bugTsColsNotVisible[] = "noAliasFor {$vIdZasob}";
  1017. }
  1018. if (!array_key_exists($vIdZasob, $visibleCols)) {
  1019. $bugTsColsNotVisible[] = $vIdZasob;
  1020. }
  1021. }
  1022. if (empty($bugTsColsNotVisible)) {
  1023. $tsSimpleLink = new stdClass();
  1024. $tsSimpleLink->format = $tsFormat;
  1025. $tsSimpleLink->aliasMap = $tsAliasMap;
  1026. $columnConfig->_tsSimpleLink = $tsSimpleLink;
  1027. $columnConfig->type = 'simpleLink';
  1028. } else {
  1029. $columnConfig->_tsSimpleLinkBug = $bugTsColsNotVisible;
  1030. }
  1031. }
  1032. }
  1033. }
  1034. }
  1035. if ($columnConfig->xsdType) {// fix fields type p5:typeSpecialSimpleLink (previously defined by Typespecial)
  1036. switch ($columnConfig->xsdType) {
  1037. case 'p5:typeSpecialSimpleLink': {
  1038. $columnConfig->type = 'simpleLink';
  1039. $columnConfig->_tsRetId = 0;
  1040. $columnConfig->_tsSimpleLink = new stdClass();
  1041. $columnConfig->_tsSimpleLink->format = $acl->getXsdFieldParam($col, 'format');
  1042. $columnConfig->_tsSimpleLink->aliasMap = $acl->getXsdFieldParam($col, 'aliasMap');
  1043. } break;
  1044. }
  1045. }
  1046. // @see ajaxHiddenColsSave
  1047. if (UserProfile::isHiddenColumn($acl->getID(), $fieldID)) {
  1048. $columnConfig->hidden = true;
  1049. }
  1050. $columnConfig->description = $acl->getFieldOpis($col);
  1051. $jsonData->cols->{$col} = $columnConfig;
  1052. }
  1053. $jsonData->rows = array();
  1054. $jsonData->total = $acl->getTotal($params);
  1055. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">get_total (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($jsonData->total);echo'</pre>';}
  1056. $items = $acl->getItems($params);
  1057. foreach ($items as $idx => $item) $items[$idx] = (array)$item;
  1058. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">items (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($items);echo'</pre>';}
  1059. // TODO: add virtual data by Typespecial
  1060. if (!empty($vCols) && !empty($items)) {
  1061. foreach ($vCols as $vColID => $vCol) {
  1062. $colType = $acl->getFieldTypeById($vColID);
  1063. if ($colType) continue;// pomin Typespecial dla realnych komorek w bazie danych
  1064. $typeSpecial = Typespecial::getInstance($vColID, $vCol);
  1065. if ($typeSpecial) {
  1066. $columnConfig = V::get($vCol, null, $jsonData->cols);
  1067. if ($columnConfig && !empty($columnConfig->_tsSimpleLink)) {
  1068. // pomin simple link values - mved to js render
  1069. } else {
  1070. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$vColID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($typeSpecial);echo'</pre>';}
  1071. $ids = array_keys($items);
  1072. $specialValues = $typeSpecial->getValuesByIds($this->_zasobID, $ids);
  1073. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$vCol.') specialValues (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($specialValues);echo'</pre>';}
  1074. if (!empty($specialValues)) foreach ($specialValues as $kItemID => $vValues) {
  1075. $tsValue = implode('<br>', $vValues);
  1076. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Item['.$kItemID.'].'.$vCol.' specialValues (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($items[$kItemID]);echo'</pre>';}
  1077. if (!empty($items[$kItemID][$vCol]) && !empty($tsValue)) {
  1078. $items[$kItemID][$vCol] .= ": {$tsValue}";
  1079. } else {
  1080. $items[$kItemID][$vCol] = $tsValue;
  1081. }
  1082. }
  1083. }
  1084. }
  1085. }
  1086. }
  1087. {
  1088. if(V::get('DBG', '', $_GET)){$jsonData->__DBG__ = [];}
  1089. $p5Alias = [];
  1090. foreach ((array)$jsonData->cols as $fieldName => $conf) {
  1091. if ('p5:alias' == V::get('xsdType', '', $conf)) {
  1092. if(V::get('DBG', '', $_GET)){$jsonData->__DBG__[$fieldName] = $conf;}
  1093. $aliasType = $acl->getXsdFieldParam($fieldName, 'type');// 'type' => 'sql_table_alias',
  1094. if ('sql_table_alias' == $aliasType) {
  1095. $p5Alias[$fieldName] = (array)$conf;
  1096. $p5Alias[$fieldName]['type'] = 'sql_table_alias';
  1097. $p5Alias[$fieldName]['local_join_key'] = $acl->getXsdFieldParam($fieldName, 'local_join_key');// 'local_join_key' => 'ID',
  1098. $p5Alias[$fieldName]['remote_table_name'] = $acl->getXsdFieldParam($fieldName, 'remote_table_name');// 'remote_table_name' => 'CRM_LISTA_ZASOBOW_ORDERS_summary_view',
  1099. $p5Alias[$fieldName]['remote_join_key'] = $acl->getXsdFieldParam($fieldName, 'remote_join_key');// 'remote_join_key' => 'ID',
  1100. $p5Alias[$fieldName]['remote_column_value'] = $acl->getXsdFieldParam($fieldName, 'remote_column_value');// 'remote_column_value' => 'SUM_POS',
  1101. } else if ('sql_query_alias' == $aliasType) {
  1102. $p5Alias[$fieldName] = (array)$conf;
  1103. $p5Alias[$fieldName]['type'] = 'sql_query_alias';
  1104. $p5Alias[$fieldName]['local_join_key'] = $acl->getXsdFieldParam($fieldName, 'local_join_key');
  1105. $p5Alias[$fieldName]['join_query_format'] = $acl->getXsdFieldParam($fieldName, 'join_query_format');
  1106. }
  1107. }
  1108. }
  1109. if(V::get('DBG', '', $_GET)){$jsonData->__DBG__['$p5Alias'] = $p5Alias;}
  1110. if(V::get('DBG', '', $_GET)){$jsonData->__DBG__['$aliasValuesRaw'] = [];}
  1111. if ($p5Alias) {
  1112. $sqlLocalTableName = $acl->getRootTableName();
  1113. $primaryKeyField = $acl->getPrimaryKeyField();
  1114. foreach ($p5Alias as $fieldName => $conf) {
  1115. $aliasValuesRaw = [];
  1116. if ('sql_table_alias' == $conf['type']) {
  1117. $localKeys = [];
  1118. $localKeyName = V::get('local_join_key', '', $conf);
  1119. if ($localKeyName) {
  1120. if (!array_key_exists($localKeyName, $localKeys)) {
  1121. $localKeys[$localKeyName] = [];
  1122. foreach ($items as $item) {
  1123. $keyLocal = V::get($localKeyName, 0, $item);
  1124. if ($keyLocal) $localKeys[$localKeyName][] = DB::getPDO()->quote($keyLocal, PDO::PARAM_STR);
  1125. }
  1126. }
  1127. if(V::get('DBG', '', $_GET)){$jsonData->__DBG__['$localKeys'] = $localKeys;}
  1128. if (!empty($localKeys[$localKeyName])) {
  1129. $sqlRemoteTable = V::get('remote_table_name', '', $conf);
  1130. $sqlRemoteValueFieldName = V::get('remote_column_value', '', $conf);
  1131. $sqlRemoteKeyName = V::get('remote_join_key', '', $conf);
  1132. $sqlLocalKeyName = $acl->getSqlFieldName($localKeyName);
  1133. $aliasValuesRaw = DB::getPDO()->fetchAllByKey("
  1134. select r.{$sqlRemoteKeyName} as {$sqlLocalKeyName}, r.{$sqlRemoteValueFieldName} as remote_value
  1135. from {$sqlRemoteTable} r
  1136. where r.{$sqlRemoteKeyName} in(" . implode(",", $localKeys[$localKeyName]) . ")
  1137. ", $sqlLocalKeyName);
  1138. if(V::get('DBG', '', $_GET)){$jsonData->__DBG__['$aliasValuesRaw'][$fieldName] = $aliasValuesRaw;}
  1139. }
  1140. }
  1141. } else if ('sql_query_alias' == $conf['type']) {
  1142. $localKeys = [];
  1143. $localKeyName = V::get('local_join_key', '', $conf);
  1144. $sqlFormat = V::get('join_query_format', '', $conf);
  1145. if ($localKeyName && $sqlFormat) {
  1146. if (!array_key_exists($localKeyName, $localKeys)) {
  1147. $localKeys[$localKeyName] = [];
  1148. foreach ($items as $item) {
  1149. $keyLocal = V::get($localKeyName, 0, $item);
  1150. if ($keyLocal) $localKeys[$localKeyName][] = DB::getPDO()->quote($keyLocal, PDO::PARAM_STR);
  1151. }
  1152. }
  1153. }
  1154. if (!empty($localKeys[$localKeyName])) {
  1155. $sql = str_replace('{sql_in_local_join_key}', implode(",", $localKeys[$localKeyName]), $sqlFormat);
  1156. $sqlLocalKeyName = $acl->getSqlFieldName($localKeyName);
  1157. $aliasValuesRaw = DB::getPDO()->fetchAllByKey($sql, $sqlLocalKeyName);
  1158. }
  1159. }
  1160. DBG::log($aliasValuesRaw, 'array', '$aliasValuesRaw');
  1161. if (!empty($aliasValuesRaw)) {
  1162. if(V::get('DBG_P5', '', $_GET)){echo '{ "$aliasValuesRaw": '.json_encode($aliasValuesRaw).', "dbg": [' . "\n";}
  1163. array_walk($items, function (&$item) use ($fieldName, $sqlLocalKeyName, $aliasValuesRaw) {
  1164. $sqlValue = V::get($sqlLocalKeyName, null, $item);
  1165. if(V::get('DBG_P5', '', $_GET)){echo '{ "sqlValue": "'.$sqlValue.'", "remote_value": "'.$aliasValuesRaw[$sqlValue]['remote_value'].'", "item": '.json_encode($item) . '},' . "\n";}
  1166. if ($sqlValue !== null && array_key_exists($sqlValue, $aliasValuesRaw)) {
  1167. if(V::get('DBG_P5', '', $_GET)){echo '{ "TODO_update_value": "'.$aliasValuesRaw[$sqlValue]['remote_value'].'"},' . "\n";}
  1168. $item[$fieldName] = $aliasValuesRaw[$sqlValue]['remote_value'];
  1169. if(V::get('DBG_P5', '', $_GET)){echo '{ "TODO_updated_value": "'.$item[$fieldName].'"},' . "\n";}
  1170. }
  1171. });
  1172. if(V::get('DBG_P5', '', $_GET)){echo "\n".'{}]}';die();}
  1173. }
  1174. }
  1175. }
  1176. }
  1177. foreach ($items as $item) {
  1178. // TODO: hide items without 'R'
  1179. foreach ($visibleCols as $fieldName) {
  1180. // TODO: ID default 'R'
  1181. if (!$acl->canReadObjectField($fieldName, $item)) $item[$fieldName] = '*****';
  1182. // null => empty string
  1183. if (!isset($item[$fieldName]) || (!$item[$fieldName] && $item[$fieldName] !== '0')) {
  1184. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">isEmptyString['.$fieldName.'] (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($item[$fieldName]);echo'</pre>';}
  1185. $item[$fieldName] = '';
  1186. }
  1187. }
  1188. $jsonData->rows[] = $item;
  1189. }
  1190. $jsonData->type = 'success';
  1191. $jsonData->msg = 'pobrano nowe dane';
  1192. return $jsonData;
  1193. }
  1194. }