UserMsgs.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. and `A_STATUS`='WAITING'
  603. and `A_RECORD_UPDATE_AUTHOR`=''
  604. and `A_RECORD_UPDATE_DATE` is null
  605. and (
  606. ('{$usrLogin}'!=`A_RECORD_CREATE_AUTHOR`)
  607. or ('{$usrLogin}'=`A_RECORD_CREATE_AUTHOR`
  608. and 'user'=`userTargetType`
  609. and '{$usrLogin}'=`userTargetName`
  610. )
  611. )
  612. ";
  613. $res = $db->query($sql);
  614. 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()));
  615. }
  616. public function viewMsg($msg) {
  617. $usrLogin = User::getLogin();
  618. $idTable = 0;
  619. if (!empty($msg['tblName'])) {
  620. $tblName = $msg['tblName'];
  621. $idTable = ProcesHelper::getZasobTableID($tblName);
  622. if (!$idTable) throw new Exception("Nie udało się odnaleźć nr tabeli '{$tblName}'");
  623. $idRow = $msg['idRow'];
  624. if (!$idRow) throw new Exception("Brak numeru powiązanego rekordu!");
  625. $usrAcl = User::getAcl();
  626. $tblAcl = $usrAcl->getTableAcl($idTable);
  627. }
  628. $linkBase = "index.php?_route=UserMsgs";
  629. $linkBase .= "&usrLogin={$usrLogin}";
  630. //$rmMsgLink = "{$linkBase}&_task=removeMsg&id={$msg['_raw']->ID}";
  631. $backLink = "{$linkBase}";
  632. ?>
  633. <div class="container">
  634. <h3><i class="glyphicon glyphicon-envelope"></i> <a href="<?php echo $backLink; ?>">Wiadomości <?php echo $usrLogin; ?></a>
  635. &raquo; Wiadomość nr <?php echo $msg['_raw']->ID; ?>
  636. <?php if ($idTable > 0) : ?>
  637. <!-- <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>
  638. z tabeli <a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $idTable; ?>"><?php echo $tblAcl->getLabel(); ?></a>
  639. </small>
  640. -->
  641. <small style="display:block; text-align:right">
  642. <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>
  643. <span style="font-size:12px; line-height:15px; vertical-align:text-bottom;">z tabeli</span>
  644. <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>
  645. </small>
  646. <?php endif; ?>
  647. </h3>
  648. </div>
  649. <?php $this->printWidgetViewMsg($msg); ?>
  650. <?php
  651. }
  652. public function printWidgetViewMsg($msg) {
  653. $messageList = array();
  654. $uiTargetName = $msg['_raw']->uiTargetName;
  655. $uiTargetType = $msg['_raw']->uiTargetType;
  656. $replyLink = "index.php?_route=UserMsgs&_task=reply&uiTargetName={$uiTargetName}&uiTargetType={$uiTargetType}";
  657. $markAsReadLink = "index.php?_route=UserMsgs&_task=markAsRead";
  658. $message = $this->_convertMessageToJson($msg['_raw']);
  659. {//if ($message->idThread > 0) {
  660. $sqlLimit = 100;
  661. $ds = DB::getDataSource();
  662. $sqlIdThread = ($message->idThread > 0)? $message->idThread : $message->id;
  663. $sql = "
  664. select m.*
  665. from `CRM_UI_MSGS` m
  666. where (m.`idThread` = {$sqlIdThread} or m.`ID` = {$sqlIdThread})
  667. -- and m.`ID` < {$message->id}
  668. order by m.`ID` asc
  669. limit {$sqlLimit}
  670. ";
  671. $moreMsgs = $ds->getListByQuery($sql);
  672. foreach ($moreMsgs as $msg) $messageList[] = $this->_convertMessageToJson($msg);
  673. }
  674. //$messageList[] = $message;
  675. ?>
  676. <link rel="stylesheet" href="./stuff/widget-select.css">
  677. <style type="text/css">
  678. .user_avatar {
  679. display:block;
  680. width:40px;
  681. height:40px;
  682. margin:0 auto 10px auto;
  683. padding:0;
  684. border:1px solid #ddd;
  685. line-height: 38px;
  686. font-size:16px;
  687. text-align:center;
  688. vertical-align:middle;
  689. color:#aaa;
  690. }
  691. </style>
  692. <div id="widget-msg-tree" style="max-width:1000px; margin:0 auto;"></div>
  693. <script src="stuff/vendors.js"></script>
  694. <script src="stuff/bundle.se_route_user_msgs.js"></script>
  695. <script>
  696. var testNewRecordCounter = 0;
  697. jQuery("#widget-msg-tree").MsgThread({
  698. usrLogin: '<?php echo User::getLogin(); ?>',
  699. idThread: <?php echo $message->idThread; ?>,
  700. msgs: <?php echo json_encode($messageList); ?>,
  701. fetchMessages: (function() {
  702. var _msgsXhr = null;
  703. return function(reqData, callback) {
  704. if (_msgsXhr && _msgsXhr.state() === 'pending') {
  705. _msgsXhr.abort();
  706. _msgsXhr = null;
  707. }
  708. _msgsXhr = $.ajax({
  709. url: 'index.php?_route=UserMsgs&_task=getMessagesById&idThread=<?php echo $message->idThread; ?>',
  710. data: reqData,
  711. dataType: 'json'
  712. });
  713. _msgsXhr.done(function(data, textStatus, jqXHR) {
  714. if (data && data.msgs && data.msgs.length > 0) {
  715. callback(null, {msgs: data.msgs});
  716. } else {
  717. callback(null, {msgs: []});//"Error no data!");
  718. }
  719. });
  720. _msgsXhr.fail(function() {
  721. callback(null, {options: []});//"Error no data!");
  722. });
  723. _msgsXhr.always(function() {
  724. _msgsXhr = null;
  725. });
  726. };
  727. })(),
  728. fetchOptionsForGroup: (function() {
  729. var _groupXhr = null;
  730. return function(input, callback) {
  731. if (_groupXhr && _groupXhr.state() === 'pending') {
  732. _groupXhr.abort();
  733. _groupXhr = null;
  734. }
  735. _groupXhr = $.ajax({
  736. url: 'index.php?_route=UserMsgs&_task=typeSpecialGroupId&q=' + input,
  737. dataType: 'json'
  738. });
  739. _groupXhr.done(function(data, textStatus, jqXHR) {
  740. if (data && data.length > 0) {
  741. var options = [];
  742. data.forEach(function(item) {
  743. options.push({value: item.id, label: item.name});
  744. });
  745. callback(null, {options: options});
  746. } else {
  747. callback(null, {options: []});//"Error no data!");
  748. }
  749. });
  750. _groupXhr.fail(function() {
  751. callback(null, {options: []});//"Error no data!");
  752. });
  753. _groupXhr.always(function() {
  754. _groupXhr = null;
  755. });
  756. };
  757. })(),
  758. fetchOptionsForUser: (function() {
  759. var _userXhr = null;
  760. return function(input, callback) {
  761. if (_userXhr && _userXhr.state() === 'pending') {
  762. _userXhr.abort();
  763. _userXhr = null;
  764. }
  765. _userXhr = $.ajax({
  766. url: 'index.php?_route=UserMsgs&_task=typeSpecialUserLogin&q=' + input,
  767. dataType: 'json'
  768. });
  769. _userXhr.done(function(data, textStatus, jqXHR) {
  770. if (data && data.length > 0) {
  771. var options = [];
  772. data.forEach(function(item) {
  773. options.push({value: item.id, label: item.name});
  774. });
  775. callback(null, {options: options});
  776. } else {
  777. callback(null, {options: []});//"Error no data!");
  778. }
  779. });
  780. _userXhr.fail(function() {
  781. callback(null, {options: []});//"Error no data!");
  782. });
  783. _userXhr.always(function() {
  784. _userXhr = null;
  785. });
  786. };
  787. })(),
  788. saveReply: function(data, callback) {
  789. //console.log('#widget-msg-tree/MsgThread::saveReply: data:', data, 'callback', callback);
  790. $.ajax({
  791. url: '<?php echo $replyLink; ?>',
  792. method: 'POST',
  793. data: data,
  794. dataType: 'json'
  795. })
  796. .done(function(data, textStatus, jqXHR) {
  797. var returnData = {message: '', type: 'danger'};
  798. if (data && data.record) {
  799. returnData.msg = data.msg || 'Wysłano wiadomość';
  800. returnData.record = data.record;
  801. returnData.type = 'success';
  802. } else if (data.validateErrors) {
  803. returnData.msg = data.msg || 'Wystąpiły błędy w formularzu';
  804. returnData.type = 'warning';
  805. returnData.validateErrors = data.validateErrors;
  806. } else {
  807. returnData.msg = data.msg || 'Nie udało się wysłać wiadomości!';
  808. returnData.type = 'danger';
  809. }
  810. callback(null, returnData);
  811. })
  812. .fail(function() {
  813. callback(null, {message: 'Nie udało się wysłać wiadomości!', type: 'danger'});
  814. });
  815. },
  816. markAsRead: function(idMsg, callback) {
  817. //console.log('#widget-msg-tree/MsgThread::markAsRead: idMsg:', idMsg);
  818. $.ajax({
  819. url: '<?php echo $markAsReadLink; ?>&idMsg=' + idMsg,
  820. method: 'GET',
  821. dataType: 'json'
  822. })
  823. .done(function(data, textStatus, jqXHR) {
  824. var returnData = {message: '', type: 'danger'};
  825. if (data && data.record) {
  826. returnData.msg = data.msg || 'Oznaczono wiadomość jako odczytaną';
  827. returnData.record = data.record;
  828. returnData.type = 'success';
  829. } else {
  830. returnData.msg = data.msg || 'Nie udało się oznaczyć wiadomości jako odczytanej!';
  831. returnData.type = 'danger';
  832. }
  833. callback(null, returnData);
  834. })
  835. .fail(function() {
  836. callback(null, {message: 'Nie udało się oznaczyć wiadomości jako odczytanej!', type: 'danger'});
  837. });
  838. },
  839. dbg: false
  840. });
  841. // jQuery("#widget-msg-tree").on('change', function(e, data) {
  842. // console.log('#widget-msg-tree/MsgThread::onChange: data:', data);
  843. // });
  844. </script>
  845. <?php
  846. }
  847. public function _convertMessageToJson($rawMsg) {
  848. /* $msg = {_raw: {A_RECORD_CREATE_AUTHOR: "plabudda",
  849. A_RECORD_CREATE_DATE: "2015-10-26 12:20:05",
  850. A_RECORD_DELETE_AUTHOR: "",
  851. A_RECORD_DELETE_DATE: null,
  852. A_RECORD_UPDATE_AUTHOR: "plabudda",
  853. A_RECORD_UPDATE_DATE: "2015-11-02 12:44:59",
  854. A_STATUS: "NORMAL",
  855. ID: "67",
  856. actionExecutedTime: "2015-11-02 12:44:59",
  857. actionNotes: "",
  858. app_className: "TableMsgs",
  859. msg: "test Y",
  860. msgType: "info",
  861. uiTargetName: "TEST_PERMS.31",
  862. uiTargetType: "default_db_table_record",
  863. userTargetName: "plabudda",
  864. userTargetType: "user"}
  865. _read: true,
  866. idRow: "31",
  867. message: "test Y",
  868. tblName: "TEST_PERMS",
  869. type: "info",
  870. usrLogin: "plabudda"} */
  871. $usrLogin = User::getLogin();
  872. $message = new stdClass();
  873. $message->id = $rawMsg->ID;
  874. $message->idThread = $rawMsg->idThread;// TODO: ID_THREAD
  875. $message->idReplyTo = $rawMsg->idReplyTo;// TODO: ID_REPLY_TO
  876. $message->message = $rawMsg->msg;
  877. $message->type = $rawMsg->msgType;
  878. $message->to = $rawMsg->userTargetName;
  879. $message->toType = $rawMsg->userTargetType;
  880. $message->author = $rawMsg->A_RECORD_CREATE_AUTHOR;
  881. $message->created = $rawMsg->A_RECORD_CREATE_DATE;
  882. $message->_read = ('WAITING' != $rawMsg->A_STATUS);
  883. $message->_readByUser = ('WAITING' != $rawMsg->A_STATUS);
  884. if ('WAITING' == $rawMsg->A_STATUS
  885. && $usrLogin == $rawMsg->A_RECORD_CREATE_AUTHOR) {
  886. if ('user' == $rawMsg->userTargetType
  887. && $usrLogin == $rawMsg->userTargetName) {
  888. $message->_readByUser = false;
  889. } else {
  890. $message->_readByUser = true;
  891. }
  892. }
  893. if ($message->_read) {
  894. if (!empty($rawMsg->A_RECORD_UPDATE_DATE)) $message->_readDate = $rawMsg->A_RECORD_UPDATE_DATE;
  895. if (!empty($rawMsg->A_RECORD_UPDATE_AUTHOR)) $message->_readBy = $rawMsg->A_RECORD_UPDATE_AUTHOR;
  896. }
  897. return $message;
  898. }
  899. public function getMessagesByIdAction() {
  900. try {
  901. $idThread = V::get('idThread', '', $_GET, 'int');
  902. $idLastMsg = V::get('idLastMsg', '', $_GET, 'int');
  903. if ($idThread <= 0) throw new Exception("Wrong param id!");
  904. $sqlLimit = 10;// TODO: 100?
  905. $ds = DB::getDataSource();
  906. $sql = "
  907. select m.*
  908. from `CRM_UI_MSGS` m
  909. where m.`idThread` = {$idThread}
  910. and m.`ID` > {$idLastMsg}
  911. order by m.`ID` asc
  912. limit {$sqlLimit}
  913. ";
  914. $moreMsgs = $ds->getListByQuery($sql);
  915. $response = new stdClass();
  916. $response->msg = "Nowe wiadomości";
  917. $response->type = 'success';
  918. $response->msgs = array();
  919. foreach ($moreMsgs as $msg) {
  920. $response->msgs[] = $this->_convertMessageToJson($msg);
  921. }
  922. } catch (Exception $e) {
  923. $response = new stdClass();
  924. $response->msg = "Wystąpiły błędy: " . $e->getMessage();
  925. $response->type = 'danger';
  926. }
  927. echo json_encode($response);
  928. }
  929. public function replyAction() {
  930. try {
  931. $uiTargetType = V::get('uiTargetType', '', $_GET);
  932. $uiTargetName = V::get('uiTargetName', '', $_GET);
  933. $response = $this->_reply($uiTargetType, $uiTargetName, $_POST);
  934. } catch (Exception $e) {
  935. $response = new stdClass();
  936. $response->msg = "Wystąpiły błędy: " . $e->getMessage();
  937. $response->type = 'danger';
  938. }
  939. echo json_encode($response);
  940. }
  941. public function markAsReadAction() {
  942. $usrLogin = User::getLogin();
  943. try {
  944. $idMsg = V::get('idMsg', '', $_GET, 'int');
  945. $msg = $this->_getMsg($idMsg, $usrLogin);
  946. $this->_markAsRead($msg);
  947. $response = new stdClass();
  948. //$response->msg = "";
  949. $response->type = "success";
  950. $msg = $this->_getMsg($idMsg, $usrLogin);
  951. $response->record = $this->_convertMessageToJson($msg['_raw']);
  952. } catch (Exception $e) {
  953. $response = new stdClass();
  954. $response->msg = "Wystąpiły błędy: " . $e->getMessage();
  955. $response->type = 'danger';
  956. }
  957. echo json_encode($response);
  958. }
  959. public function _reply($uiTargetType, $uiTargetName, $args) {
  960. $ds = DB::getDataSource();
  961. $newMsg = array();
  962. $newMsg['idReplyTo'] = V::get('idReplyTo', '', $args, 'int');
  963. $newMsg['msg'] = V::get('message', '', $args);
  964. $newMsg['msgType'] = V::get('msgType', 'info', $args);
  965. $newMsg['userTargetType'] = V::get('toType', '', $args);
  966. $newMsg['userTargetName'] = V::get('to', '', $args);
  967. $newMsg['A_RECORD_CREATE_DATE'] = 'NOW()';
  968. $newMsg['A_RECORD_CREATE_AUTHOR'] = User::getLogin();
  969. $newMsg['app_className'] = 'TableMsgs';
  970. //DBG::_(true, true, "newMsg", $newMsg, __CLASS__, __FUNCTION__, __LINE__);
  971. if ($newMsg['idReplyTo'] <= 0) throw new Exception("Wrong id reply to msg");
  972. $parentMsg = $ds->getById('CRM_UI_MSGS', $newMsg['idReplyTo']);
  973. if (!$parentMsg) throw new Exception("Nie znaleziono wiadomości");
  974. $newMsg['idThread'] = ($parentMsg->idThread > 0)? $parentMsg->idThread : $parentMsg->ID;
  975. $newMsg['uiTargetType'] = $uiTargetType;// TODO:? $parentMsg->uiTargetType
  976. $newMsg['uiTargetName'] = $uiTargetName;// TODO:? $parentMsg->uiTargetName
  977. $insertedId = $ds->insert('CRM_UI_MSGS', $newMsg);
  978. if (!$insertedId) throw new Exception("Nie udało się utworzyć rekordu");
  979. $msgAdded = $ds->getById('CRM_UI_MSGS', $insertedId);
  980. $response = new stdClass();
  981. $response->msg = "Wysłano wiadomość";
  982. $response->type = 'success';
  983. $response->record = $this->_convertMessageToJson($msgAdded);
  984. return $response;
  985. }
  986. public function removeMsgAction() {
  987. $idMsg = V::get('id', 0, $_GET, 'int');
  988. $usrLogin = V::get('usrLogin', 0, $_REQUEST, 'word');
  989. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  990. if (empty($usrLogin)) throw new HttpException("Błęny user login", 404);
  991. SE_Layout::gora();
  992. SE_Layout::menu();
  993. try {
  994. $msg = $this->_getMsg($idMsg, $usrLogin);
  995. $msgsRoute = Router::getRoute('Msgs');
  996. $msgsRoute->removeTableRecordMsg($idMsg);
  997. } catch (Exception $e) {
  998. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  999. SE_Layout::dol();
  1000. exit;
  1001. }
  1002. ?>
  1003. <div class="container">
  1004. <div class="alert alert-success">
  1005. Wiadomość została usunięta <a class="btn btn-xs btn-default" href="index.php?_route=UserMsgs&usrLogin=<?php echo $usrLogin; ?>">wróć</a>
  1006. </div>
  1007. </div>
  1008. <?php
  1009. SE_Layout::dol();
  1010. }
  1011. }