TableMsgs.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. <?php
  2. Lib::loadClass('Router');
  3. Lib::loadClass('RouteBase');
  4. Lib::loadClass('TypespecialVariable');
  5. class Route_TableMsgs extends RouteBase {
  6. var $_listLimit = 20;
  7. public function handleAuth() {
  8. if (!User::logged()) {
  9. User::authByRequest();
  10. }
  11. }
  12. public function defaultAction() {
  13. SE_Layout::gora();
  14. SE_Layout::menu();
  15. $this->menu();
  16. SE_Layout::dol();
  17. }
  18. public function menu() {
  19. $usrLogin = User::getLogin();
  20. ?>
  21. <ul>
  22. <li>TODO: ...</li>
  23. </ul>
  24. <?php
  25. }
  26. public function tableRowAction() {
  27. $idTable = V::get('idTable', 0, $_REQUEST, 'int');
  28. $idRow = V::get('idRow', 0, $_REQUEST, 'int');
  29. if ($idTable <= 0) throw new HttpException("Błęny numer tabeli", 400);
  30. if ($idRow <= 0) throw new HttpException("Błęny numer tabeli", 400);
  31. SE_Layout::gora();
  32. SE_Layout::menu();
  33. try {
  34. $this->tableRowMsgs($idTable, $idRow);
  35. } catch (Exception $e) {
  36. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  37. }
  38. SE_Layout::dol();
  39. }
  40. public function tableRowMsgs($idTable, $idRow) {
  41. $tblAcl = User::getAcl()->getTableAcl($idTable);
  42. $tableName = $tblAcl->getName();
  43. $record = $tblAcl->getItem($idRow);
  44. $args = array();
  45. $args['to_type'] = V::get('to_type', '', $_POST);
  46. $args['to'] = V::get("to-{$args['to_type']}", '', $_POST);
  47. $args['msg'] = V::get('msg', '', $_POST);
  48. $arrorsList = array();
  49. $createdId = 0;
  50. if (!empty($_POST)) {
  51. try {
  52. $this->_validate($args);
  53. $createdId = $this->_create($args, $tableName, $idRow);
  54. if ($createdId > 0) {
  55. $args['to'] = '';
  56. $args['msg'] = '';
  57. }
  58. } catch(Exception $e) {
  59. $arrorsList[] = $e->getMessage();
  60. }
  61. }
  62. $msgsList = $this->getMsgs('inbox', $tableName, $idRow);
  63. $totalReadMsgs = 0;
  64. $totalUnreadMsgs = 0;
  65. foreach ($msgsList as $ind => $msg) {
  66. if ($msg['_read']) {
  67. $totalReadMsgs++;
  68. } else {
  69. $totalUnreadMsgs++;
  70. }
  71. }
  72. $sentMsgsList = $this->getMsgs('sent', $tableName, $idRow);
  73. $removedMsgsList = $this->getMsgs('removed', $tableName, $idRow);
  74. ?>
  75. <style type="text/css">
  76. .tblMsgsListItem { cursor:pointer; }
  77. </style>
  78. <div class="container">
  79. <h3 style="margin-bottom:20px; border-bottom:1px solid #e5e5e5;"><i class="glyphicon glyphicon-envelope"></i> Wiadomości powiązane z rekordem nr <code><?php echo $idRow; ?></code>
  80. <br>
  81. <!-- <small>z tabeli <a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $idTable; ?>"><?php echo $tblAcl->getRawLabel(); ?></a></small> -->
  82. <small style="display:block; text-align:right">
  83. <a style="font-size:12px; line-height:15px; vertical-align:text-bottom;" title="Edytuj rekord" href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $idTable; ?>#EDIT/<?php echo $idRow; ?>"><i class="glyphicon glyphicon-pencil"></i> Edytuj rekord <?php echo $idRow; ?></a>
  84. </small>
  85. </h3>
  86. <?php if ($createdId > 0) : ?>
  87. <?php echo SE_Layout::alert('info', "Wysłano wiadomość nr '{$createdId}'"); ?>
  88. <?php endif; ?>
  89. <div>
  90. <ul class="nav nav-tabs" role="tablist">
  91. <li>
  92. <a href="#tbl-msgs-compose"><i class="glyphicon glyphicon-plus"></i> Nowa wiadomość</a>
  93. </li>
  94. <li role="presentation" class="active"><a href="#odebrane" aria-controls="odebrane" role="tab" data-toggle="tab">Odebrane <em>(<?php echo $totalUnreadMsgs; ?>)</em></a></li>
  95. <li role="presentation"><a href="#wyslane" aria-controls="wyslane" role="tab" data-toggle="tab">Wysłane</em></a></li>
  96. <li role="presentation"><a href="#kosz" aria-controls="kosz" role="tab" data-toggle="tab">Kosz</em></a></li>
  97. </ul>
  98. <div class="tab-content" style="margin-bottom:15px">
  99. <div role="tabpanel" class="tab-pane active" id="odebrane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  100. <?php $this->_printTableMsgsList('inbox', $msgsList, $idTable, $idRow); ?>
  101. </div>
  102. <div role="tabpanel" class="tab-pane" id="wyslane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  103. <?php $this->_printTableMsgsList('sent', $sentMsgsList, $idTable, $idRow); ?>
  104. </div>
  105. <div role="tabpanel" class="tab-pane" id="kosz" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  106. <?php $this->_printTableMsgsList('removed', $removedMsgsList, $idTable, $idRow); ?>
  107. </div>
  108. </div>
  109. </div>
  110. <div class="panel panel-default" id="tbl-msgs-compose">
  111. <div class="panel-heading">Wyślij nową wiadomość</div>
  112. <div class="panel-body">
  113. <?php if (!empty($arrorsList)) : ?>
  114. <?php foreach ($arrorsList as $errMsg) : ?>
  115. <div class="alert alert-danger"><?php echo $errMsg; ?></div>
  116. <?php endforeach; ?>
  117. <?php endif; ?>
  118. <?php $this->_printMsgForm($args); ?>
  119. </div>
  120. </div>
  121. </div>
  122. <script>
  123. function tblMsgsLoadMoreRows(n) {
  124. var nNode = jQuery(n),
  125. lastMsgId = nNode.data('last_msg_id'),
  126. listType = nNode.data('list_type')
  127. ;
  128. nNode.blur();
  129. function tblMsgsSetNoMoreRows(btnLoadMoreNode) {
  130. btnLoadMoreNode.closest('td').css({color:'silver'}).html('Brak starszych wiadomości');
  131. }
  132. if (lastMsgId <= 0) {
  133. tblMsgsSetNoMoreRows(nNode);
  134. }
  135. function tblMsgsAddMsgToList(msg, btnLoadMoreNode, listType) {
  136. var tbodyNode = btnLoadMoreNode.closest('tfoot').prev('tbody'),
  137. trNode = jQuery('<tr></tr>'),
  138. tdIdNode = jQuery('<td></td>'),
  139. tdMsgNode = jQuery('<td></td>'),
  140. tdDateNode = jQuery('<td style="white-space:nowrap;"></td>'),
  141. actionTask = (listType == 'inbox')? 'read' : 'view',
  142. msgLink = ''
  143. ;
  144. trNode.addClass('tblMsgsListItem');
  145. if (msg['_read']) trNode.addClass('active');
  146. if ('read' === actionTask || 'view' === actionTask) {
  147. msgLink = '<?php echo Request::getPathUri() . 'index.php?_route=TableMsgs'; ?>';
  148. msgLink += '&idTable=<?php echo $idTable; ?>&idRow=<?php echo $idRow; ?>';
  149. msgLink += '&id=' + msg['_raw']['ID'];
  150. msgLink += '&_task=' + actionTask;
  151. trNode.attr('onclick', "window.location.href='" + msgLink + "'");
  152. }
  153. tdIdNode.append(msg['_raw']['ID']);
  154. tdIdNode.appendTo(trNode);
  155. tdMsgNode.append('<div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;">' + msg['message'] + '</div>');
  156. msgMetaInfo = 'od ' + msg['_raw']['A_RECORD_CREATE_AUTHOR'] + ' do ';
  157. if ('everyone' == msg['_raw']['userTargetType']) {
  158. msgMetaInfo += 'wszystkich';
  159. } else if ('user' == msg['_raw']['userTargetType']) {
  160. msgMetaInfo += msg['_raw']['userTargetName'];
  161. } else if ('group' == msg['_raw']['userTargetType']) {
  162. msgMetaInfo += 'grupy ' + msg['_raw']['userTargetName'];
  163. }
  164. tdMsgNode.append('<div class="text-muted" style="font-style:italic;">' + msgMetaInfo + '</div>');
  165. tdMsgNode.appendTo(trNode);
  166. tdDateNode.append(msg['_raw']['A_RECORD_CREATE_DATE']);
  167. if (msg['_readDate']) {
  168. tdDateNode.append('<div class="text-muted" style="font-style:italic" title="Przeczytano ' + msg['_readDate'] + '">' + msg['_readDate'] + '</div>');
  169. } else {
  170. tdDateNode.append('<div class="text-muted" style="font-style:italic" title="Wiadomość nie została jeszcze odczytana">nieodczytana</div>');
  171. }
  172. tdDateNode.appendTo(trNode);
  173. trNode.hide();
  174. trNode.appendTo(tbodyNode);
  175. trNode.show('slow');
  176. };
  177. jQuery.ajax({
  178. data: {},
  179. dataType: 'json',
  180. type: "POST",
  181. url: 'index.php?_route=TableMsgs&_task=loadMoreRows&listType=' + listType + '&lastMsgId=' + lastMsgId + '&tableName=<?php echo $tableName; ?>' + '&idRow=<?php echo $idRow; ?>'
  182. })
  183. .done(function(data, textStatus, jqXHR) {
  184. var listLimit = <?php echo $this->_listLimit; ?>,
  185. i = 0,
  186. lastMsgId = 0,
  187. hasMore = false
  188. ;
  189. if (!data || !data.msgs || !data.keysOrder) {
  190. jQuery.notify('Wystąpiły błędy podczas pobierania listy wiadomości', 'error');
  191. return false;
  192. }
  193. data.keysOrder.forEach(function(key) {
  194. if (i < listLimit) {
  195. lastMsgId = key;
  196. tblMsgsAddMsgToList(data.msgs[key], nNode, listType);
  197. } else {
  198. hasMore = true;
  199. }
  200. i++;
  201. });
  202. if (!hasMore) {
  203. tblMsgsSetNoMoreRows(nNode);
  204. }
  205. nNode.data('last_msg_id', lastMsgId);
  206. })
  207. .fail(function(jqXHR) {
  208. if (jqXHR.responseJSON) {
  209. jQuery.notify('Nie udało się pobrać listy wiadomości', 'error');
  210. }
  211. else {
  212. var txt = jqXHR.responseText || 'Nie udało się pobrać listy wiadomości';
  213. if (jqXHR.status == 404) {
  214. jQuery.notify(jqXHR.responseText, 'error');
  215. } else {
  216. jQuery.notify(jqXHR.responseText, 'warn');
  217. }
  218. }
  219. });
  220. }
  221. </script>
  222. <?php
  223. //DBG::_(true, true, "_POST", $_POST, __CLASS__, __FUNCTION__, __LINE__);
  224. //DBG::_(true, true, "tblAcl", $tblAcl, __CLASS__, __FUNCTION__, __LINE__);
  225. //DBG::_(true, true, "record", $record, __CLASS__, __FUNCTION__, __LINE__);
  226. //DBG::_(true, true, "msgsList", $msgsList, __CLASS__, __FUNCTION__, __LINE__);
  227. //throw new Exception("TODO: ...");
  228. }
  229. public function loadMoreRowsAction() {
  230. $tableName = V::get('tableName', '', $_GET, 'word');
  231. $idRow = V::get('idRow', 0, $_GET, 'int');
  232. $lastMsgId = V::get('lastMsgId', 0, $_GET, 'int');
  233. $listType = V::get('listType', '', $_GET, 'word');
  234. if ($idRow <= 0) throw new HttpException("Wrong param id row", 404);
  235. if (!$tableName) throw new HttpException("Wrong param table name", 404);
  236. if ($lastMsgId <= 0) throw new HttpException("Wrong param lastMsgId", 404);
  237. if (!in_array($listType, array('inbox','sent','removed'))) throw new HttpException("Wrong param listType", 404);
  238. $resultData = new stdClass();
  239. $resultData->msgs = $this->getMsgs($listType, $tableName, $idRow, $lastMsgId);
  240. $resultData->keysOrder = array_keys($resultData->msgs);
  241. echo json_encode($resultData);
  242. }
  243. public function _printTableMsgsList($listType, $msgsList, $idTable, $idRow) {
  244. $msgsTotal = count($msgsList);
  245. $listLimit = $this->_listLimit;
  246. $lastMsgId = 0;
  247. $actionTask = ($listType == 'inbox')? 'read' : 'view';
  248. ?>
  249. <table class="tblMsgsList table table-hovered" style="margin-bottom:0; table-layout:fixed;">
  250. <thead>
  251. <tr>
  252. <th style="width:60px">#</th>
  253. <th>wiadomość</th>
  254. <th style="width:130px">data</th>
  255. </tr>
  256. </thead>
  257. <tbody>
  258. <?php if ($msgsTotal <= 0) : ?>
  259. <tr>
  260. <td colspan="3"><em class="text-muted" style="padding-left:60px;">Brak wiadomości</em></td>
  261. </tr>
  262. <?php else : ?>
  263. <?php $i = 0; foreach ($msgsList as $idMsg => $msg) : $i++; if ($i > $listLimit) break; $lastMsgId = $idMsg; ?>
  264. <?php
  265. $onClick = '';
  266. $msgLink = Request::getPathUri() . 'index.php?_route=TableMsgs&id=' . $msg['_raw']->ID;
  267. $msgLink .= "&idTable={$idTable}&idRow={$idRow}";
  268. if ('read' == $actionTask || 'view' == $actionTask) {
  269. $msgLink .= '&_task=' . $actionTask;
  270. } else {
  271. $msgLink = null;
  272. }
  273. if ($msgLink) {
  274. $jsOnClick = "window.location.href='{$msgLink}'";
  275. $onClick = 'onclick="' . $jsOnClick . '"';
  276. }
  277. ?>
  278. <tr <?php echo $onClick; ?>
  279. class="tblMsgsListItem <?php echo ($msg['_read'])? 'active' : ''; ?>">
  280. <td><?php echo $msg['_raw']->ID; ?></td>
  281. <td>
  282. <div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;"><?php echo htmlspecialchars($msg['message']); ?></div>
  283. <div class="text-muted" style="font-style:italic;">
  284. od <?php echo $msg['_raw']->A_RECORD_CREATE_AUTHOR; ?> do <?php
  285. if ('everyone' == $msg['_raw']->userTargetType) {
  286. echo "wszystkich";
  287. } else if ('user' == $msg['_raw']->userTargetType) {
  288. echo "{$msg['_raw']->userTargetName}";
  289. } else if ('group' == $msg['_raw']->userTargetType) {
  290. echo "grupy {$msg['_raw']->userTargetName}";
  291. }
  292. ?>
  293. </div>
  294. </td>
  295. <td style="white-space:nowrap;">
  296. <?php echo $msg['_raw']->A_RECORD_CREATE_DATE; ?>
  297. <?php if ($msg['_readDate']) : ?>
  298. <div class="text-muted" style="font-style:italic" title="Przeczytano <?php echo $msg['_readDate']; ?>"><?php echo $msg['_readDate']; ?></div>
  299. <?php else : ?>
  300. <div class="text-muted" style="font-style:italic" title="Wiadomość nie została jeszcze odczytana">nieodczytana</div>
  301. <?php endif; ?>
  302. </td>
  303. </tr>
  304. <?php endforeach; ?>
  305. <?php endif; ?>
  306. </tbody>
  307. <tfoot>
  308. <?php if ($msgsTotal > $listLimit) : ?>
  309. <tr class="active">
  310. <td colspan="3" style="text-align:center">
  311. <button class="btn btn-link"
  312. data-last_msg_id="<?php echo $lastMsgId; ?>"
  313. data-list_type="<?php echo $listType; ?>"
  314. onclick="return tblMsgsLoadMoreRows(this);">pobierz starsze wiadomości ...</button>
  315. </td>
  316. </tr>
  317. <?php endif; ?>
  318. </tfoot>
  319. </table>
  320. <?php
  321. }
  322. public function getMsgs($filterType, $tableName, $idRow, $lastMsgId = null) {
  323. $lastMsgId = (int)$lastMsgId;
  324. $msgsRoute = Router::getRoute('Msgs');
  325. $msgsList = array();
  326. if (empty($tableName)) throw new Exception("No table name!");
  327. $sqlWhereAddFilter = "";
  328. $usrLogin = User::getLogin();
  329. $userGroupIds = User::getGroupsIds();
  330. $sqlFilerMsgsForUser = "
  331. m.`userTargetType` in('everyone')
  332. or (m.`userTargetType`='user' and m.`userTargetName`='{$usrLogin}')
  333. or (m.`userTargetType`='group' and m.`userTargetName` in(" . implode(",", $userGroupIds) . "))
  334. ";
  335. switch ($filterType) {
  336. case 'inbox':
  337. $sqlWhereAddFilter = "
  338. and ({$sqlFilerMsgsForUser})
  339. and m.`A_STATUS` not in('DELETED')
  340. ";
  341. break;
  342. case 'sent':
  343. $sqlWhereAddFilter = "
  344. and m.`A_RECORD_CREATE_AUTHOR`='{$usrLogin}'
  345. and m.`A_STATUS` not in('DELETED')
  346. ";
  347. break;
  348. case 'removed':
  349. $sqlWhereAddFilter = "
  350. and (m.`A_RECORD_CREATE_AUTHOR`='{$usrLogin}'
  351. or ({$sqlFilerMsgsForUser})
  352. )
  353. and m.`A_STATUS` in('DELETED')
  354. ";
  355. break;
  356. default: throw new Exception("Unknown filter type");
  357. }
  358. if ($lastMsgId > 0) {
  359. $sqlWhereAddFilter .= "\n and m.`ID`<{$lastMsgId}";
  360. }
  361. $sqlLimit = $this->_listLimit + 1;
  362. $rawMsgs = DB::getPDO()->fetchAllByKey("
  363. select m.*
  364. from `CRM_UI_MSGS` m
  365. where m.`uiTargetType` = 'default_db_table_record'
  366. and m.`uiTargetName` = :feature_id
  367. {$sqlWhereAddFilter}
  368. order by m.`ID` DESC
  369. limit {$sqlLimit}
  370. ", 'ID', [
  371. ':feature_id' => "{$tableName}.{$idRow}"
  372. ]);
  373. return array_map(function ($msg) {
  374. return [
  375. 'message' => $msg['msg'],
  376. 'type' => $msg['msgType'],
  377. '_raw' => (object)$msg,
  378. '_read' => ('WAITING' != $msg['A_STATUS']),
  379. '_readDate' => $msg['actionExecutedTime'],
  380. ];
  381. }, $rawMsgs);
  382. }
  383. public function _validate($args) {
  384. $toType = V::get('to_type', '', $args);
  385. $to = V::get('to', '', $args);
  386. $msg = V::get('msg', '', $args);
  387. if (!in_array($toType, array('everyone', 'user', 'group'))) {
  388. throw new Exception("Niedozwolony typ odbiorcy");
  389. }
  390. if (empty($to) && 'everyone' != $toType) {
  391. throw new Exception("Proszę podać odbiorcę wiadomości");
  392. }
  393. if (empty($msg)) {
  394. throw new Exception("Proszę podać treść wiadomości");
  395. }
  396. }
  397. public function _create($args, $tableName, $idRow) {
  398. $toType = V::get('to_type', '', $args);
  399. $to = V::get('to', '', $args);
  400. $msg = V::get('msg', '', $args);
  401. $usrLogin = User::getLogin();
  402. $db = DB::getDB();
  403. if (!$db) throw new Exception("Brak dazy danych!");
  404. if ($db->has_errors()) throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  405. $item = array();
  406. $item['`uiTargetType`'] = "'default_db_table_record'";
  407. $item['`uiTargetName`'] = "'{$tableName}.{$idRow}'";
  408. $item['`userTargetType`'] = "'{$toType}'";
  409. $item['`userTargetName`'] = "'{$to}'";
  410. $item['`msg`'] = "'" . $db->_($msg) . "'";
  411. $item['`A_RECORD_CREATE_DATE`'] = "NOW()";
  412. $item['`A_RECORD_CREATE_AUTHOR`'] = "'{$usrLogin}'";
  413. $item['`A_STATUS`'] = "'WAITING'";
  414. $item['`app_className`'] = "'TableMsgs'";
  415. $sql = "insert into `CRM_UI_MSGS` (" . implode(",", array_keys($item)) . ")
  416. values (" . implode(",", array_values($item)) . ")
  417. ";
  418. $res = $db->query($sql);
  419. if (!$res || $db->has_errors()) throw new Exception("Wystąpiły błędy podczas próby zapisu wiadomości: " . implode("\n<br>", $db->get_errors()));
  420. $createdId = $db->insert_id();
  421. if ($createdId <= 0) throw new Exception("Nie udało się zapisać wiadomości.");
  422. return $createdId;
  423. }
  424. public function _printMsgForm($args) {
  425. $toType = V::get('to_type', '', $args);
  426. $to = V::get('to', '', $args);
  427. $msg = V::get('msg', '', $args);
  428. $listTo = array();
  429. $listTo['everyone'] = 'Wszyscy';
  430. $listTo['user'] = 'Użytkownik';
  431. $listTo['group'] = 'Grupa';
  432. $toType = (array_key_exists($toType, $listTo))? $toType : 'everyone';
  433. $typeSpecialGroupId = TypespecialVariable::getInstance(-1, '__ZASOB');
  434. $typeSpecialUserLogin = TypespecialVariable::getInstance(-1, '__USER_LOGIN');
  435. $selectedLogin = ('user' == $toType)? $to : '';
  436. $selectedGroupId = ('group' == $toType)? $to : '';
  437. ?>
  438. <form class="form-horizontal" action="" method="post">
  439. <div class="form-group">
  440. <label class="col-sm-2 control-label" for="to">Do:</label>
  441. <div class="col-sm-3">
  442. <select name="to_type" class="form-control" onChange="return selectTblMsgsToType(this);">
  443. <?php foreach ($listTo as $type => $typeLabel) : ?>
  444. <option <?php echo ($type == $toType)? 'selected' : ''; ?>
  445. value="<?php echo $type; ?>"><?php echo $typeLabel; ?></option>
  446. <?php endforeach; ?>
  447. </select>
  448. </div>
  449. <div class="col-sm-7">
  450. <div id="tblMsgsTo-everyone" style="<?php echo ('everyone' == $toType)? '' : 'display:none'; ?>">
  451. <input name="to-everyone" type="text" class="form-control" disabled>
  452. </div>
  453. <div id="tblMsgsTo-group" style="<?php echo ('group' == $toType)? '' : 'display:none'; ?>">
  454. <?php if ($typeSpecialGroupId) : ?>
  455. <?php
  456. $fldName = 'to-group';
  457. $fldParams = array();
  458. $fldParams['allowCreate'] = false;
  459. $fldParams['ajaxDataUrlBase'] = "index.php?_route=TableMsgs&_task=typeSpecialGroupId";
  460. $fldParams['placeholder'] = 'Grupa...';
  461. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  462. echo $typeSpecialUserLogin->showFormItem($tblID = -1, $fldName, $selectedGroupId, $fldParams);
  463. ?>
  464. <?php else : ?>
  465. <input name="to-group" type="text" class="form-control" placeholder="Grupa">
  466. <?php endif; ?>
  467. </div>
  468. <div id="tblMsgsTo-user" style="<?php echo ('user' == $toType)? '' : 'display:none'; ?>">
  469. <?php if ($typeSpecialUserLogin) : ?>
  470. <?php
  471. $fldName = 'to-user';
  472. $fldParams = array();
  473. $fldParams['allowCreate'] = false;
  474. $fldParams['ajaxDataUrlBase'] = "index.php?_route=TableMsgs&_task=typeSpecialUserLogin";
  475. $fldParams['placeholder'] = 'Użytkownik...';
  476. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  477. echo $typeSpecialUserLogin->showFormItem($tblID = -1, $fldName, $selectedLogin, $fldParams);
  478. ?>
  479. <?php else : ?>
  480. <input name="to-user" type="text" class="form-control" placeholder="Użytkownik">
  481. <?php endif; ?>
  482. </div>
  483. </div>
  484. </div>
  485. <div class="form-group">
  486. <label for="to" class="col-sm-2 control-label">Wiadomość:</label>
  487. <div class="col-sm-10">
  488. <textarea name="msg" class="form-control"><?php echo htmlspecialchars($msg); ?></textarea>
  489. </div>
  490. </div>
  491. <div class="form-group">
  492. <div class="col-sm-10 col-sm-offset-2">
  493. <input class="btn btn-primary" type="submit" value="Wyślij">
  494. </div>
  495. </div>
  496. </form>
  497. <script>
  498. function selectTblMsgsToType(n) {
  499. var toTypes = <?php echo json_encode(array_keys($listTo)); ?>,
  500. selectedType = n.value
  501. ;
  502. if (-1 !== toTypes.indexOf(n.value)) {
  503. toTypes.forEach(function(type) {
  504. if (type == selectedType) {
  505. document.getElementById('tblMsgsTo-' + type).style.display = 'block';
  506. } else {
  507. document.getElementById('tblMsgsTo-' + type).style.display = 'none';
  508. }
  509. });
  510. }
  511. }
  512. </script>
  513. <?php
  514. }
  515. public function typeSpecialUserLoginAction() {
  516. header("Content-type: application/json");
  517. $typeSpecialUserId = TypespecialVariable::getInstance(-1, '__USER_LOGIN');
  518. if (!$typeSpecialUserId) {
  519. $jsonData = new stdClass();
  520. $jsonData->message = "TypeSpecial '__USER_LOGIN' not exists";
  521. echo json_encode($jsonData);
  522. exit;
  523. }
  524. $query = V::get('q', '', $_REQUEST);
  525. $rawRows = null;
  526. $jsonData = array();
  527. $queryParams = array();
  528. $rows = $typeSpecialUserId->getValuesWithExports($query, $queryParams);
  529. foreach ($rows as $kID => $vItem) {
  530. $itemJson = new stdClass();
  531. $itemJson->id = $vItem->id;
  532. $itemJson->name = $vItem->param_out;
  533. if (!empty($vItem->exports)) {
  534. $itemJson->exports = $vItem->exports;
  535. }
  536. $jsonData[] = $itemJson;
  537. }
  538. echo json_encode($jsonData);
  539. }
  540. public function typeSpecialGroupIdAction() {
  541. header("Content-type: application/json");
  542. Lib::loadClass('TypespecialVariable');
  543. $typeSpecialZasob = TypespecialVariable::getInstance(-1, '__ZASOB');
  544. if (!$typeSpecialZasob) {
  545. $jsonData = new stdClass();
  546. $jsonData->message = "TypeSpecial '__ZASOB' not exists";
  547. echo json_encode($jsonData);
  548. exit;
  549. }
  550. $query = V::get('q', '', $_REQUEST);
  551. $rawRows = null;
  552. $jsonData = array();
  553. $queryParams = array();
  554. $queryParams['zasob_type_in'] = array('STANOWISKO', 'PODMIOT', 'DZIAL');
  555. $rows = $typeSpecialZasob->getValuesWithExports($query, $queryParams);
  556. DBG::_('DBG_TS', '>1', "rows({$query})", $rows, __CLASS__, __FUNCTION__, __LINE__);
  557. foreach ($rows as $kID => $vItem) {
  558. $itemJson = new stdClass();
  559. $itemJson->id = $vItem->id;
  560. $itemJson->name = $vItem->param_out;
  561. if (!empty($vItem->exports)) {
  562. $itemJson->exports = $vItem->exports;
  563. }
  564. $jsonData[] = $itemJson;
  565. }
  566. echo json_encode($jsonData);
  567. }
  568. public function readAction() {
  569. $idMsg = V::get('id', 0, $_GET, 'int');
  570. $idTable = V::get('idTable', 0, $_REQUEST, 'int');
  571. $idRow = V::get('idRow', 0, $_REQUEST, 'int');
  572. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  573. if ($idTable <= 0) throw new HttpException("Błęny numer tabeli", 400);
  574. if ($idRow <= 0) throw new HttpException("Błęny numer tabeli", 400);
  575. SE_Layout::gora();
  576. SE_Layout::menu();
  577. try {
  578. $msg = $this->_getMsg($idMsg, $idTable, $idRow);
  579. $this->_markAsRead($msg);
  580. $this->viewMsg($msg);
  581. } catch (Exception $e) {
  582. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  583. }
  584. SE_Layout::dol();
  585. }
  586. public function viewAction() {
  587. $idMsg = V::get('id', 0, $_GET, 'int');
  588. $idTable = V::get('idTable', 0, $_REQUEST, 'int');
  589. $idRow = V::get('idRow', 0, $_REQUEST, 'int');
  590. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  591. if ($idTable <= 0) throw new HttpException("Błęny numer tabeli", 400);
  592. if ($idRow <= 0) throw new HttpException("Błęny numer tabeli", 400);
  593. SE_Layout::gora();
  594. SE_Layout::menu();
  595. try {
  596. $msg = $this->_getMsg($idMsg, $idTable, $idRow);
  597. $this->viewMsg($msg);
  598. } catch (Exception $e) {
  599. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  600. }
  601. SE_Layout::dol();
  602. }
  603. public function _getMsg($idMsg, $idTable, $idRow) {
  604. $msgsRoute = Router::getRoute('Msgs');
  605. $msg['_raw'] = $msgsRoute->getMessage($idMsg);
  606. if (!$msg['_raw']) throw new HttpException("Wiadomość nie istnieje!", 404);
  607. $msg['idTable'] = $idTable;
  608. $msg['idRow'] = $idRow;
  609. $msg['message'] = $msg['_raw']->msg;
  610. $msg['type'] = $msg['_raw']->msgType;
  611. $msg['_read'] = ('WAITING' != $msg['_raw']->A_STATUS);
  612. // $msg['_raw']->uiTargetType => default_db_table_record
  613. // $msg['_raw']->uiTargetName => TEST_PERMS.31
  614. if ('default_db_table_record' !== $msg['_raw']->uiTargetType) {
  615. throw new Exception("Parse message target type error!");
  616. }
  617. $parts = explode('.', $msg['_raw']->uiTargetName);
  618. if (2 !== count($parts)) throw new Exception("Parse message target type error!");
  619. $msg['tblName'] = $parts[0];
  620. if (!is_numeric($msg['idRow'])) throw new Exception("Parse message target type - id row type error!");
  621. if ($msg['idRow'] != $parts[1]) throw new Exception("Parse message target type - id row value error!");
  622. return $msg;
  623. }
  624. public function _markAsRead($msg) {
  625. if ($msg['_read']) return;
  626. $usrLogin = User::getLogin();
  627. $db = DB::getDB();
  628. if (!$db) throw new Exception("Brak dazy danych!");
  629. if ($db->has_errors()) throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  630. $sql = "update `CRM_UI_MSGS`
  631. set `A_STATUS`='NORMAL'
  632. , `A_RECORD_UPDATE_AUTHOR`='{$usrLogin}'
  633. , `A_RECORD_UPDATE_DATE`=NOW()
  634. , `actionExecutedTime`=NOW()
  635. where `ID`='{$msg['_raw']->ID}'
  636. and `A_STATUS`='WAITING'
  637. and `A_RECORD_UPDATE_AUTHOR`=''
  638. and `A_RECORD_UPDATE_DATE` is null
  639. and (
  640. ('{$usrLogin}'!=`A_RECORD_CREATE_AUTHOR`)
  641. or ('{$usrLogin}'=`A_RECORD_CREATE_AUTHOR`
  642. and 'user'=`userTargetType`
  643. and '{$usrLogin}'=`userTargetName`
  644. )
  645. )
  646. ";
  647. $res = $db->query($sql);
  648. if (!$res || $db->has_errors()) throw new Exception("Wystąpiły błędy podczas próby zapisu wiadomości: " . implode("\n<br>", $db->get_errors()));
  649. }
  650. public function viewMsg($msg) {
  651. $usrLogin = User::getLogin();
  652. $idTable = 0;
  653. if (!empty($msg['tblName'])) {
  654. $tblName = $msg['tblName'];
  655. $idTable = (!empty($msg['idTable']))? $msg['idTable'] : ProcesHelper::getZasobTableID($tblName);
  656. if (!$idTable) throw new Exception("Nie udało się odnaleźć nr tabeli '{$tblName}'");
  657. $idRow = $msg['idRow'];
  658. if (!$idRow) throw new Exception("Brak numeru powiązanego rekordu!");
  659. $usrAcl = User::getAcl();
  660. $tblAcl = $usrAcl->getTableAcl($idTable);
  661. }
  662. $userMsgsRoute = Router::getRoute('UserMsgs');
  663. $linkBase = "index.php?_route=TableMsgs";
  664. $linkBase .= "&idTable={$idTable}&idRow={$idRow}";
  665. //$rmMsgLink = "{$linkBase}&_task=removeMsg&id={$msg['_raw']->ID}";
  666. $backLink = "{$linkBase}&_task=tableRow";
  667. ?>
  668. <div class="container">
  669. <h3><i class="glyphicon glyphicon-envelope"></i> <a href="<?php echo $backLink; ?>">Wiadomości powiązane z rekordem nr <?php echo $idRow; ?></a>
  670. &raquo; Wiadomość nr <?php echo $msg['_raw']->ID; ?>
  671. <?php if ($idTable > 0) : ?>
  672. <!-- <br><small>wiadomość powiązana z rekordem <a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $idTable; ?>#EDIT/<?php echo $idRow; ?>"><?php echo $idRow; ?></a>
  673. z tabeli <a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $idTable; ?>"><?php echo $tblAcl->getLabel(); ?></a>
  674. </small>
  675. -->
  676. <small style="display:block; text-align:right">
  677. <a style="font-size:12px; line-height:15px; vertical-align:text-bottom;" title="Edytuj rekord" href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $idTable; ?>#EDIT/<?php echo $idRow; ?>"><i class="glyphicon glyphicon-pencil"></i> Edytuj rekord <?php echo $idRow; ?></a>
  678. <span style="font-size:12px; line-height:15px; vertical-align:text-bottom;">z tabeli</span>
  679. <a style="font-size:12px; line-height:15px; vertical-align:text-bottom;" href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $idTable; ?>"><?php echo $tblAcl->getRawLabel(); ?></a>
  680. </small>
  681. <?php endif; ?>
  682. </h3>
  683. </div>
  684. <?php $userMsgsRoute->printWidgetViewMsg($msg); ?>
  685. <?php
  686. }
  687. public function removeMsgAction() {
  688. $idMsg = V::get('id', 0, $_GET, 'int');
  689. $idTable = V::get('idTable', 0, $_REQUEST, 'int');
  690. $idRow = V::get('idRow', 0, $_REQUEST, 'int');
  691. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  692. if ($idTable <= 0) throw new HttpException("Błęny numer tabeli", 400);
  693. if ($idRow <= 0) throw new HttpException("Błęny numer tabeli", 400);
  694. SE_Layout::gora();
  695. SE_Layout::menu();
  696. try {
  697. $msg = $this->_getMsg($idMsg, $idTable, $idRow);
  698. $msgsRoute = Router::getRoute('Msgs');
  699. $msgsRoute->removeTableRecordMsg($idMsg);
  700. } catch (Exception $e) {
  701. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  702. SE_Layout::dol();
  703. exit;
  704. }
  705. ?>
  706. <div class="container">
  707. <div class="alert alert-success">
  708. Wiadomość została usunięta <a class="btn btn-xs btn-default" href="index.php?_route=TableMsgs&_task=tableRow&idTable=<?php echo $idTable; ?>&idRow=<?php echo $idRow; ?>">wróć</a>
  709. </div>
  710. </div>
  711. <?php
  712. SE_Layout::dol();
  713. }
  714. }