UserMsgs.php 28 KB

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