ViewTableAjax.php 66 KB

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