ViewObject.php 47 KB

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