Status.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('UI');
  4. Lib::loadClass('Response');
  5. class Route_Status extends RouteBase {
  6. public function defaultAction() {
  7. UI::gora();
  8. UI::startTag('div', ['class' => "container"]);
  9. echo UI::h('h1', [], [
  10. UI::h('a', ['href'=>"index.php"], "SE"),
  11. " &raquo; ",
  12. " Status systemu procesy5"
  13. ]);
  14. try {
  15. DB::getPDO();
  16. $this->viewStatusDatabase();
  17. if (in_array(User::get('ADM_ADMIN_LEVEL'), ['0', '1'])) {
  18. $this->viewStatusUsers();
  19. }
  20. // UI::table([
  21. // 'caption' => 'Baza danych',
  22. // 'rows' => DB::getPDO()->fetchAll(" SHOW VARIABLES ")
  23. // ]);
  24. } catch (Exception $e) {
  25. UI::alert('danger', $e->getMessage());
  26. }
  27. UI::endTag('div');// .container
  28. UI::dol();
  29. }
  30. public function viewStatusDatabase() {
  31. if (1 == V::get('event_sheduler_on', '', $_POST)) {
  32. $this->fixEventSheduler();
  33. }
  34. $dbEvents = DB::getPDO()->fetchFirst(" SHOW VARIABLES WHERE VARIABLE_NAME = 'event_scheduler' ");
  35. // DBG::nicePrint($dbEvents, '$dbEvents');
  36. // [Variable_name] => event_scheduler
  37. // [Value] => ON
  38. UI::table([
  39. 'caption' => UI::h('b', ['style' => "color:#000"], 'Baza danych'),
  40. 'rows' => [
  41. [
  42. 'nazwa' => 'Event Scheduler (generowanie Grafika, itp.)',
  43. 'wartość' => ('ON' == $dbEvents['Value'])
  44. ? UI::h('span', ['class' => "label label-success"], "ON")
  45. : UI::h('span', ['class' => "label label-danger"], "OFF"),
  46. '#' => UI::hButtonPost("Włącz", [
  47. 'class' => "btn btn-xs btn-default",
  48. 'data' => [
  49. 'event_sheduler_on' => 1
  50. ]
  51. ])
  52. ]
  53. ]
  54. ]);
  55. }
  56. public function fixEventSheduler() {
  57. DB::getPDO()->execSql(" SET GLOBAL event_scheduler='ON' ");
  58. }
  59. public function viewStatusUsers() {
  60. $nowMinus3Months = date("Y-m-d", mktime(0,0,0, date('m') - 1, date('d'), date('Y')));
  61. $sesUsers = DB::getPDO()->fetchAll("
  62. select c.ID, c.CONF_KEY, c.CONF_VAL
  63. from CRM_CONFIG c
  64. where c.CONF_KEY like 'acl_user_%_%_cache_update'
  65. and c.CONF_VAL > '{$nowMinus3Months}'
  66. ");
  67. $sesUsers = array_reduce( $sesUsers, function ($ret, $record) {
  68. // $format = "acl_user_{ID_USERS}_{ID_PROCES}_cache_update";
  69. list($idUser, $idProces) = explode('_', substr($record['CONF_KEY'], strlen('acl_user_'), -1 * strlen('_cache_update')));
  70. if (!array_key_exists($idUser, $ret)) {
  71. $ret[$idUser] = [
  72. 'idUser' => $idUser,
  73. 'lastUpdateAclCache' => $record['CONF_VAL'],
  74. 'log' => []
  75. ];
  76. } else {
  77. $ret[$idUser]['lastUpdateAclCache'] = ($record['CONF_VAL'] > $ret[$idUser]['lastUpdateAclCache'])
  78. ? $record['CONF_VAL']
  79. : $ret[$idUser]['lastUpdateAclCache'];
  80. }
  81. $ret[$idUser]['log'][] = [ 'ID_PROCES' => $idProces, 'data' => $record['CONF_VAL'] ];
  82. return $ret;
  83. }, [] );
  84. $sesUsers = array_map(function ($sesGroup) {
  85. return [
  86. 'idUser' => $sesGroup['idUser'],
  87. 'user' => DB::getPDO()->fetchValue("select u.ADM_ACCOUNT from ADMIN_USERS u where u.ID = {$sesGroup['idUser']}"),
  88. 'lastUpdateAclCache' => $sesGroup['lastUpdateAclCache'],
  89. 'log' => UI::h('span', [ 'style' => "color:#bbb" ], implode('<br>', array_map(function ($log) {
  90. return ($log['ID_PROCES'] > 0)
  91. ? "Filtr procesu {$log['ID_PROCES']} uruchomiony {$log['data']}"
  92. : "Filtr ogólny uruchomiony {$log['data']}";
  93. }, $sesGroup['log']))),
  94. ];
  95. }, $sesUsers);
  96. usort($sesUsers, function ($ua, $ub) {
  97. $a = $ua['lastUpdateAclCache'];
  98. $b = $ub['lastUpdateAclCache'];
  99. return ($a === $b)
  100. ? 0
  101. : ( ($a < $b)
  102. ? 1
  103. : -1
  104. ) ;
  105. });
  106. if ('0' == User::get('ADM_ADMIN_LEVEL')) {
  107. $sesUsers = array_map(function ($row) {
  108. $row['#'] = UI::h('div', [ 'style' => "display:inline-block", 'class' => "activateUserDebugBtn" . ( DBG::hasUserDebug($row['idUser'], User::getID()) ? ' active' : '' ) ], [
  109. UI::h('label', ['style' => "padding-right:6px; font-weight:normal"], "Debug"),
  110. UI::hButtonAjax("Włącz", "activateUserDebug", [
  111. 'class' => "btn btn-xs btn-default activateUserDebugBtn-activate",
  112. 'href' => $this->getLink('startUserDebugAjax'),
  113. 'data' => [ 'idUser' => $row['idUser'], 'do' => 'activate' ]
  114. ]),
  115. UI::hButtonAjax("Wyłącz", "activateUserDebug", [
  116. 'class' => "btn btn-xs btn-default activateUserDebugBtn-deactivate",
  117. 'href' => $this->getLink('startUserDebugAjax'),
  118. 'data' => [ 'idUser' => $row['idUser'], 'do' => 'deactivate' ]
  119. ]),
  120. ]);
  121. return $row;
  122. }, $sesUsers);
  123. }
  124. UI::table([
  125. 'caption' => UI::h('b', ['style' => "color:#000"], "Ostatnie logowania do aplikacji") . " <small>(update acl cache)</small>", // TODO: Aktualnie zalogowani użytkownicy
  126. 'rows' => $sesUsers
  127. ]);
  128. echo UI::h('style', ['type' => "text/css"], "
  129. .activateUserDebugBtn .activateUserDebugBtn-activate { display:inline }
  130. .activateUserDebugBtn .activateUserDebugBtn-deactivate { display:none }
  131. .activateUserDebugBtn.active .activateUserDebugBtn-activate { display:none }
  132. .activateUserDebugBtn.active .activateUserDebugBtn-deactivate { display:inline }
  133. ");
  134. echo UI::h('script', ['src'=>"static/vendor.js?_v=b636cab1", 'type'=>"text/javascript"]);
  135. echo UI::h('script', [], "
  136. (function (global) {
  137. if (!global.p5VendorJs.React) throw 'Missing p5VendorJs.React'
  138. if (!global.p5VendorJs.ReactDOM) throw 'Missing p5VendorJs.ReactDOM'
  139. if (!global.p5VendorJs.ToggleButton) throw 'Missing p5VendorJs.ToggleButton'
  140. if (!global.fetch) throw 'Missing global.fetch'
  141. var React = global.p5VendorJs.React
  142. var ReactDOM = global.p5VendorJs.ReactDOM
  143. var h = React.createElement
  144. function convertToReactToggle(n, activateBtn, deactivateBtn, isActive) {
  145. var toggleReact = document.createElement('div')
  146. toggleReact.style.display = 'inline-block'
  147. n.parentNode.insertBefore(toggleReact, n.nextSibling)
  148. deactivateBtn.style.display = 'none'
  149. activateBtn.style.display = 'none'
  150. function reactToggleBtnOnToggle(value) {
  151. if (value) {
  152. deactivateBtn.onclick()
  153. } else {
  154. activateBtn.onclick()
  155. }
  156. render_reactToggleBtn(!value)
  157. }
  158. function render_reactToggleBtn(state) {
  159. ReactDOM.render(
  160. h(global.p5VendorJs.ToggleButton, {
  161. value: state,
  162. onToggle: reactToggleBtnOnToggle
  163. })
  164. , toggleReact
  165. )
  166. }
  167. render_reactToggleBtn(isActive)
  168. }
  169. var toggles = document.querySelectorAll('.activateUserDebugBtn')
  170. for (var i = 0; i < toggles.length; i++) {
  171. var btns = toggles[i].querySelectorAll('a')
  172. if (2 !== btns.length) contiune;
  173. var activateBtn = btns[0]
  174. var deactivateBtn = btns[1]
  175. convertToReactToggle(toggles[i], activateBtn, deactivateBtn, toggles[i].classList.contains('active'))
  176. }
  177. })(window)
  178. ");
  179. UI::hButtonAjaxOnResponse("activateUserDebug", /* payload, n */ "
  180. p5UI__notifyAjaxCallback(payload)
  181. console.log('activateUserDebug :: payload', payload)
  182. if ('success' !== payload.type) return false
  183. if (payload.body && 'active' in payload.body) {
  184. if (payload.body.active) {
  185. n.parentNode.classList.add('active')
  186. } else {
  187. n.parentNode.classList.remove('active')
  188. }
  189. // if (!n.parentNode.classList.contains('active')) {
  190. // n.parentNode.classList.add('active')
  191. // } else {
  192. // n.parentNode.classList.remove('active')
  193. // }
  194. }
  195. ");
  196. }
  197. public function startUserDebugAjaxAction() {
  198. Response::sendTryCatchJson(array($this, 'startUserDebugAjax'), $_POST);
  199. }
  200. public function startUserDebugAjax($args) {
  201. if ('0' !== (string)User::get('ADM_ADMIN_LEVEL')) throw new Exception("Access Denied");
  202. $idUser = V::get('idUser', 0, $args);
  203. if ($idUser <= 0) throw new Exception("Missing idUser");
  204. $do = V::get('do', 0, $args);
  205. if (!in_array($do, ['activate', 'deactivate'])) throw new Exception("Missing do");
  206. $hasDebug = DBG::hasUserDebug($idUser, User::getID());
  207. switch ($do) {
  208. case 'activate': DBG::startUserDebug($idUser, User::getID()); break;
  209. case 'deactivate': DBG::stopUserDebug($idUser, User::getID()); break;
  210. }
  211. return [
  212. 'type' => 'success',
  213. '__args' => $args,
  214. '__$hasDebug' => $hasDebug,
  215. 'body' => [
  216. 'active' => DBG::hasUserDebug($idUser, User::getID())
  217. ]
  218. ];
  219. }
  220. }