UserMsgs.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. <?php
  2. Lib::loadClass('Router');
  3. Lib::loadClass('RouteBase');
  4. Lib::loadClass('TypespecialVariable');
  5. Lib::loadClass('ProcesHelper');
  6. Lib::loadClass('UsersHelper');
  7. Lib::loadClass('UI');
  8. class Route_UserMsgs extends RouteBase {
  9. var $_listLimit = 20;
  10. function handleAuth() {
  11. if (!User::logged()) {
  12. User::authByRequest();
  13. }
  14. }
  15. function defaultAction() { UI::layout([ $this, 'defaultView' ]); }
  16. function defaultView() {
  17. $usrLogin = User::getLogin();
  18. //$this->menu();
  19. $this->userMsgs($usrLogin);
  20. }
  21. function menu() {
  22. $usrLogin = User::getLogin();
  23. echo UI::h('ul', [], [
  24. UI::h('li', [], "TODO: ..."),
  25. ]);
  26. }
  27. function userMsgs($usrLogin) {
  28. $msgsList = $this->getMsgs('inbox', $usrLogin);
  29. $totalReadMsgs = 0;
  30. $totalUnreadMsgs = 0;
  31. foreach ($msgsList as $ind => $msg) {
  32. if ($msg['_read']) {
  33. $totalReadMsgs++;
  34. } else {
  35. $totalUnreadMsgs++;
  36. }
  37. }
  38. $sentMsgsList = $this->getMsgs('sent', $usrLogin);
  39. $removedMsgsList = $this->getMsgs('removed', $usrLogin);
  40. ?>
  41. <style type="text/css">
  42. .tblMsgsListItem { cursor:pointer; }
  43. </style>
  44. <div class="container">
  45. <h3><i class="glyphicon glyphicon-envelope"></i> Wiadomości <code><?php echo $usrLogin; ?></code></h3>
  46. <div>
  47. <ul class="nav nav-tabs" role="tablist">
  48. <!--
  49. <li>
  50. <a href="#tbl-msgs-compose"><i class="glyphicon glyphicon-plus"></i> Nowa wiadomość</a>
  51. </li>
  52. -->
  53. <li role="presentation" class="active"><a href="#odebrane" aria-controls="odebrane" role="tab" data-toggle="tab">Odebrane <em>(<?php echo $totalUnreadMsgs; ?>)</em></a></li>
  54. <li role="presentation"><a href="#wyslane" aria-controls="wyslane" role="tab" data-toggle="tab">Wysłane</em></a></li>
  55. <li role="presentation"><a href="#kosz" aria-controls="kosz" role="tab" data-toggle="tab">Kosz</em></a></li>
  56. </ul>
  57. <div class="tab-content" style="margin-bottom:15px">
  58. <div role="tabpanel" class="tab-pane active" id="odebrane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  59. <?php $this->_printUserMsgsList('inbox', $msgsList, $usrLogin); ?>
  60. </div>
  61. <div role="tabpanel" class="tab-pane" id="wyslane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  62. <?php $this->_printUserMsgsList('sent', $sentMsgsList, $usrLogin); ?>
  63. </div>
  64. <div role="tabpanel" class="tab-pane" id="kosz" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  65. <?php $this->_printUserMsgsList('removed', $removedMsgsList, $usrLogin); ?>
  66. </div>
  67. </div>
  68. </div>
  69. <!--
  70. <div class="panel panel-default" id="tbl-msgs-compose">
  71. <div class="panel-heading">Wyślij nową wiadomość</div>
  72. <div class="panel-body">
  73. <?php if (!empty($arrorsList)) : ?>
  74. <?php foreach ($arrorsList as $errMsg) : ?>
  75. <div class="alert alert-danger"><?php echo $errMsg; ?></div>
  76. <?php endforeach; ?>
  77. <?php endif; ?>
  78. <?php $this->_printMsgForm($args); ?>
  79. </div>
  80. </div>
  81. -->
  82. </div>
  83. <script>
  84. function tblMsgsLoadMoreRows(n) {
  85. var nNode = jQuery(n),
  86. lastMsgId = nNode.data('last_msg_id'),
  87. listType = nNode.data('list_type')
  88. ;
  89. nNode.blur();
  90. function tblMsgsSetNoMoreRows(btnLoadMoreNode) {
  91. btnLoadMoreNode.closest('td').css({color:'silver'}).html('Brak starszych wiadomości');
  92. }
  93. if (lastMsgId <= 0) {
  94. tblMsgsSetNoMoreRows(nNode);
  95. }
  96. function tblMsgsAddMsgToList(msg, btnLoadMoreNode, listType) {
  97. var tbodyNode = btnLoadMoreNode.closest('tfoot').prev('tbody'),
  98. trNode = jQuery('<tr></tr>'),
  99. tdIdNode = jQuery('<td></td>'),
  100. tdMsgNode = jQuery('<td></td>'),
  101. tdDateNode = jQuery('<td style="white-space:nowrap;"></td>'),
  102. actionTask = (listType == 'inbox')? 'read' : 'view',
  103. msgLink = ''
  104. ;
  105. trNode.addClass('tblMsgsListItem');
  106. if (msg['_read']) trNode.addClass('active');
  107. if ('read' === actionTask || 'view' === actionTask) {
  108. msgLink = '<?php echo Request::getPathUri() . 'index.php?_route=UserMsgs'; ?>';
  109. msgLink += '&usrLogin=<?php echo $usrLogin; ?>';
  110. msgLink += '&id=' + msg['_raw']['ID'];
  111. msgLink += '&_task=' + actionTask;
  112. trNode.attr('onclick', "window.location.href='" + msgLink + "'");
  113. }
  114. tdIdNode.append(msg['_raw']['ID']);
  115. tdIdNode.appendTo(trNode);
  116. tdMsgNode.append('<div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;">' + msg['message'] + '</div>');
  117. msgMetaInfo = 'od ' + msg['_raw']['A_RECORD_CREATE_AUTHOR'] + ' do ';
  118. if ('everyone' == msg['_raw']['userTargetType']) {
  119. msgMetaInfo += 'wszystkich';
  120. } else if ('user' == msg['_raw']['userTargetType']) {
  121. msgMetaInfo += msg['_raw']['userTargetName'];
  122. } else if ('group' == msg['_raw']['userTargetType']) {
  123. msgMetaInfo += 'grupy ' + msg['_raw']['userTargetName'];
  124. }
  125. tdMsgNode.append('<div class="text-muted" style="font-style:italic;">' + msgMetaInfo + '</div>');
  126. tdMsgNode.appendTo(trNode);
  127. tdDateNode.append(msg['_raw']['A_RECORD_CREATE_DATE']);
  128. if (msg['_readDate']) {
  129. tdDateNode.append('<div class="text-muted" style="font-style:italic" title="Przeczytano ' + msg['_readDate'] + '">' + msg['_readDate'] + '</div>');
  130. } else {
  131. tdDateNode.append('<div class="text-muted" style="font-style:italic" title="Wiadomość nie została jeszcze odczytana">nieodczytana</div>');
  132. }
  133. tdDateNode.appendTo(trNode);
  134. trNode.hide();
  135. trNode.appendTo(tbodyNode);
  136. trNode.show('slow');
  137. };
  138. jQuery.ajax({
  139. data: {},
  140. dataType: 'json',
  141. type: "POST",
  142. url: 'index.php?_route=UserMsgs&_task=loadMoreRows&listType=' + listType + '&lastMsgId=' + lastMsgId + '&usrLogin=<?php echo $usrLogin; ?>'
  143. })
  144. .done(function(data, textStatus, jqXHR) {
  145. var listLimit = <?php echo $this->_listLimit; ?>,
  146. i = 0,
  147. lastMsgId = 0,
  148. hasMore = false
  149. ;
  150. if (!data || !data.msgs || !data.keysOrder) {
  151. jQuery.notify('Wystąpiły błędy podczas pobierania listy wiadomości', 'error');
  152. return false;
  153. }
  154. data.keysOrder.forEach(function(key) {
  155. if (i < listLimit) {
  156. lastMsgId = key;
  157. tblMsgsAddMsgToList(data.msgs[key], nNode, listType);
  158. } else {
  159. hasMore = true;
  160. }
  161. i++;
  162. });
  163. if (!hasMore) {
  164. tblMsgsSetNoMoreRows(nNode);
  165. }
  166. nNode.data('last_msg_id', lastMsgId);
  167. })
  168. .fail(function(jqXHR) {
  169. if (jqXHR.responseJSON) {
  170. jQuery.notify('Nie udało się pobrać listy wiadomości', 'error');
  171. }
  172. else {
  173. var txt = jqXHR.responseText || 'Nie udało się pobrać listy wiadomości';
  174. if (jqXHR.status == 404) {
  175. jQuery.notify(jqXHR.responseText, 'error');
  176. } else {
  177. jQuery.notify(jqXHR.responseText, 'warn');
  178. }
  179. }
  180. });
  181. }
  182. </script>
  183. <?php
  184. //DBG::_(true, true, "_POST", $_POST, __CLASS__, __FUNCTION__, __LINE__);
  185. //DBG::_(true, true, "tblAcl", $tblAcl, __CLASS__, __FUNCTION__, __LINE__);
  186. //DBG::_(true, true, "record", $record, __CLASS__, __FUNCTION__, __LINE__);
  187. //DBG::_(true, true, "msgsList", $msgsList, __CLASS__, __FUNCTION__, __LINE__);
  188. //throw new Exception("TODO: ...");
  189. }
  190. function loadMoreRowsAction() {
  191. $usrLogin = V::get('usrLogin', '', $_GET, 'word');
  192. $lastMsgId = V::get('lastMsgId', 0, $_GET, 'int');
  193. $listType = V::get('listType', '', $_GET, 'word');
  194. if (!$usrLogin) throw new HttpException("Wrong param login", 404);
  195. if ($lastMsgId <= 0) throw new HttpException("Wrong param lastMsgId", 404);
  196. if (!in_array($listType, array('inbox','sent','removed'))) throw new HttpException("Wrong param listType", 404);
  197. $resultData = new stdClass();
  198. $resultData->msgs = $this->getMsgs($listType, $usrLogin, $lastMsgId);
  199. $resultData->keysOrder = array_keys($resultData->msgs);
  200. echo json_encode($resultData);
  201. }
  202. function _printUserMsgsList($listType, $msgsList, $usrLogin) {
  203. $msgsTotal = count($msgsList);
  204. $listLimit = $this->_listLimit;
  205. $lastMsgId = 0;
  206. $actionTask = ($listType == 'inbox')? 'read' : 'view';
  207. ?>
  208. <table class="tblMsgsList table table-hovered" style="margin-bottom:0; table-layout:fixed;">
  209. <thead>
  210. <tr>
  211. <th style="width:60px">#</th>
  212. <th>wiadomość</th>
  213. <th style="width:130px">data</th>
  214. </tr>
  215. </thead>
  216. <tbody>
  217. <?php if ($msgsTotal <= 0) : ?>
  218. <tr>
  219. <td colspan="3"><em class="text-muted" style="padding-left:60px;">Brak wiadomości</em></td>
  220. </tr>
  221. <?php else : ?>
  222. <?php $i = 0; foreach ($msgsList as $idMsg => $msg) : $i++; if ($i > $listLimit) break; $lastMsgId = $idMsg; ?>
  223. <?php
  224. $onClick = '';
  225. $msgLink = Request::getPathUri() . 'index.php?_route=UserMsgs&id=' . $msg['_raw']->ID;
  226. $msgLink .= "&usrLogin={$usrLogin}";
  227. if ('read' == $actionTask || 'view' == $actionTask) {
  228. $msgLink .= '&_task=' . $actionTask;
  229. } else {
  230. $msgLink = null;
  231. }
  232. if ($msgLink) {
  233. $jsOnClick = "window.location.href='{$msgLink}'";
  234. $onClick = 'onclick="' . $jsOnClick . '"';
  235. }
  236. ?>
  237. <tr <?php echo $onClick; ?>
  238. class="tblMsgsListItem <?php echo ($msg['_read'])? 'active' : ''; ?>">
  239. <td><?php echo $msg['_raw']->ID; ?></td>
  240. <td>
  241. <div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;"><?php echo htmlspecialchars($msg['message']); ?></div>
  242. <div class="text-muted" style="font-style:italic;">
  243. od <?php echo $msg['_raw']->A_RECORD_CREATE_AUTHOR; ?> do <?php
  244. if ('everyone' == $msg['_raw']->userTargetType) {
  245. echo "wszystkich";
  246. } else if ('user' == $msg['_raw']->userTargetType) {
  247. echo "{$msg['_raw']->userTargetName}";
  248. } else if ('group' == $msg['_raw']->userTargetType) {
  249. echo "grupy {$msg['_raw']->userTargetName}";
  250. }
  251. ?>
  252. </div>
  253. </td>
  254. <td style="white-space:nowrap;">
  255. <?php echo $msg['_raw']->A_RECORD_CREATE_DATE; ?>
  256. <?php if ($msg['_readDate']) : ?>
  257. <div class="text-muted" style="font-style:italic" title="Przeczytano <?php echo $msg['_readDate']; ?>"><?php echo $msg['_readDate']; ?></div>
  258. <?php else : ?>
  259. <div class="text-muted" style="font-style:italic" title="Wiadomość nie została jeszcze odczytana">nieodczytana</div>
  260. <?php endif; ?>
  261. </td>
  262. </tr>
  263. <?php endforeach; ?>
  264. <?php endif; ?>
  265. </tbody>
  266. <tfoot>
  267. <?php if ($msgsTotal > $listLimit) : ?>
  268. <tr class="active">
  269. <td colspan="3" style="text-align:center">
  270. <button class="btn btn-link"
  271. data-last_msg_id="<?php echo $lastMsgId; ?>"
  272. data-list_type="<?php echo $listType; ?>"
  273. onclick="return tblMsgsLoadMoreRows(this);">pobierz starsze wiadomości ...</button>
  274. </td>
  275. </tr>
  276. <?php endif; ?>
  277. </tfoot>
  278. </table>
  279. <?php
  280. }
  281. function getMsgs($filterType, $usrLogin, $lastMsgId = null, $fromTime = null) {
  282. $lastMsgId = (int)$lastMsgId;
  283. $msgsRoute = Router::getRoute('Msgs');
  284. $msgsList = array();
  285. if (empty($usrLogin)) throw new Exception("No user login!");
  286. $sqlWhereAddFilter = "";
  287. if ($usrLogin == User::getLogin()) {
  288. $userGroupIds = User::getGroupsIds();
  289. } else {
  290. $userGroup = UsersHelper::getGroupByUserName($usrLogin);
  291. $userGroupIds = array_keys($userGroup);
  292. }
  293. $sqlFilerMsgsForUser = "
  294. m.`userTargetType` in('everyone')
  295. or (m.`userTargetType`='user' and m.`userTargetName`='{$usrLogin}')
  296. or (m.`userTargetType`='group' and m.`userTargetName` in(" . implode(",", $userGroupIds) . "))
  297. ";
  298. switch ($filterType) {
  299. case 'inbox':
  300. $sqlWhereAddFilter = "
  301. and ({$sqlFilerMsgsForUser})
  302. and m.`A_STATUS` in('WAITING', 'NORMAL')
  303. ";
  304. break;
  305. case 'unread':
  306. $sqlWhereAddFilter = "
  307. and ({$sqlFilerMsgsForUser})
  308. and m.`A_STATUS` in('WAITING')
  309. ";
  310. break;
  311. case 'sent':
  312. $sqlWhereAddFilter = "
  313. and m.`A_RECORD_CREATE_AUTHOR`='{$usrLogin}'
  314. and (m.`A_STATUS` in('WAITING', 'NORMAL')
  315. or (m.`A_STATUS`='OFF_HARD' and m.`A_RECORD_DELETE_AUTHOR`!='{$usrLogin}')
  316. )
  317. ";
  318. break;
  319. case 'removed':
  320. $sqlWhereAddFilter = "
  321. and (m.`A_RECORD_CREATE_AUTHOR`='{$usrLogin}'
  322. or ({$sqlFilerMsgsForUser})
  323. )
  324. and m.`A_STATUS` in('OFF_HARD', 'DELETED')
  325. ";
  326. break;
  327. default: throw new Exception("Unknown filter type");
  328. }
  329. $db = DB::getDB();
  330. $tableName = $db->_($tableName);
  331. if ($lastMsgId > 0) {
  332. $sqlWhereAddFilter .= "\n and m.`ID`<{$lastMsgId}";
  333. }
  334. if (!empty($fromTime)) {
  335. $sqlWhereAddFilter .= "\n and m.`A_RECORD_CREATE_DATE`>='{$fromTime}'";
  336. }
  337. $sqlLimit = $this->_listLimit + 1;
  338. $sql = "select m.*
  339. from `CRM_UI_MSGS` m
  340. where m.`uiTargetType`='default_db_table_record'
  341. -- and m.`uiTargetName`='{$tableName}.{$idRow}'
  342. {$sqlWhereAddFilter}
  343. order by m.`ID` DESC
  344. limit {$sqlLimit}
  345. ";
  346. DBG::_('DBG_MSGS', '>1', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
  347. $db = DB::getDB();
  348. $res = $db->query($sql);
  349. while ($r = $db->fetch($res)) {
  350. $msg['message'] = $r->msg;
  351. $msg['type'] = $r->msgType;
  352. $msg['_raw'] = $r;
  353. $msg['_read'] = ('WAITING' != $r->A_STATUS);
  354. $msg['_readDate'] = $r->actionExecutedTime;
  355. $msgsList[$r->ID] = $msg;
  356. }
  357. return $msgsList;
  358. }
  359. function _validate($args) {
  360. $toType = V::get('to_type', '', $args);
  361. $to = V::get('to', '', $args);
  362. $msg = V::get('msg', '', $args);
  363. if (!in_array($toType, array('everyone', 'user', 'group'))) {
  364. throw new Exception("Niedozwolony typ odbiorcy");
  365. }
  366. if (empty($to) && 'everyone' != $toType) {
  367. throw new Exception("Proszę podać odbiorcę wiadomości");
  368. }
  369. if (empty($msg)) {
  370. throw new Exception("Proszę podać treść wiadomości");
  371. }
  372. }
  373. function _create($args, $tableName, $idRow) {
  374. $toType = V::get('to_type', '', $args);
  375. $to = V::get('to', '', $args);
  376. $msg = V::get('msg', '', $args);
  377. $usrLogin = User::getLogin();
  378. $db = DB::getDB();
  379. if (!$db) throw new Exception("Brak dazy danych!");
  380. if ($db->has_errors()) throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  381. $item = array();
  382. $item['`uiTargetType`'] = "'default_db_table_record'";
  383. $item['`uiTargetName`'] = "'{$tableName}.{$idRow}'";
  384. $item['`userTargetType`'] = "'{$toType}'";
  385. $item['`userTargetName`'] = "'{$to}'";
  386. $item['`msg`'] = "'" . $db->_($msg) . "'";
  387. $item['`A_RECORD_CREATE_DATE`'] = "NOW()";
  388. $item['`A_RECORD_CREATE_AUTHOR`'] = "'{$usrLogin}'";
  389. $item['`A_STATUS`'] = "'WAITING'";
  390. $item['`app_className`'] = "'TableMsgs'";
  391. $sql = "insert into `CRM_UI_MSGS` (" . implode(",", array_keys($item)) . ")
  392. values (" . implode(",", array_values($item)) . ")
  393. ";
  394. $res = $db->query($sql);
  395. 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()));
  396. $createdId = $db->insert_id();
  397. if ($createdId <= 0) throw new Exception("Nie udało się zapisać wiadomości.");
  398. return $createdId;
  399. }
  400. function _printMsgForm($args) {
  401. $toType = V::get('to_type', '', $args);
  402. $to = V::get('to', '', $args);
  403. $msg = V::get('msg', '', $args);
  404. $listTo = array();
  405. $listTo['everyone'] = 'Wszyscy';
  406. $listTo['user'] = 'Użytkownik';
  407. $listTo['group'] = 'Grupa';
  408. $toType = (array_key_exists($toType, $listTo))? $toType : 'everyone';
  409. $typeSpecialGroupId = TypespecialVariable::getInstance(-1, '__ZASOB');
  410. $typeSpecialUserLogin = TypespecialVariable::getInstance(-1, '__USER_LOGIN');
  411. $selectedLogin = ('user' == $toType)? $to : '';
  412. $selectedGroupId = ('group' == $toType)? $to : '';
  413. ?>
  414. <form class="form-horizontal" action="" method="post">
  415. <div class="form-group">
  416. <label class="col-sm-2 control-label" for="to">Do:</label>
  417. <div class="col-sm-3">
  418. <select name="to_type" class="form-control" onChange="return selectTblMsgsToType(this);">
  419. <?php foreach ($listTo as $type => $typeLabel) : ?>
  420. <option <?php echo ($type == $toType)? 'selected' : ''; ?>
  421. value="<?php echo $type; ?>"><?php echo $typeLabel; ?></option>
  422. <?php endforeach; ?>
  423. </select>
  424. </div>
  425. <div class="col-sm-7">
  426. <div id="tblMsgsTo-everyone" style="<?php echo ('everyone' == $toType)? '' : 'display:none'; ?>">
  427. <input name="to-everyone" type="text" class="form-control" disabled>
  428. </div>
  429. <div id="tblMsgsTo-group" style="<?php echo ('group' == $toType)? '' : 'display:none'; ?>">
  430. <?php if ($typeSpecialGroupId) : ?>
  431. <?php
  432. $fldName = 'to-group';
  433. $fldParams = array();
  434. $fldParams['allowCreate'] = false;
  435. $fldParams['ajaxDataUrlBase'] = "index.php?_route=TableMsgs&_task=typeSpecialGroupId";
  436. $fldParams['placeholder'] = 'Grupa...';
  437. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  438. echo $typeSpecialUserLogin->showFormItem($tblID = -1, $fldName, $selectedGroupId, $fldParams);
  439. ?>
  440. <?php else : ?>
  441. <input name="to-group" type="text" class="form-control" placeholder="Grupa">
  442. <?php endif; ?>
  443. </div>
  444. <div id="tblMsgsTo-user" style="<?php echo ('user' == $toType)? '' : 'display:none'; ?>">
  445. <?php if ($typeSpecialUserLogin) : ?>
  446. <?php
  447. $fldName = 'to-user';
  448. $fldParams = array();
  449. $fldParams['allowCreate'] = false;
  450. $fldParams['ajaxDataUrlBase'] = "index.php?_route=TableMsgs&_task=typeSpecialUserLogin";
  451. $fldParams['placeholder'] = 'Użytkownik...';
  452. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  453. echo $typeSpecialUserLogin->showFormItem($tblID = -1, $fldName, $selectedLogin, $fldParams);
  454. ?>
  455. <?php else : ?>
  456. <input name="to-user" type="text" class="form-control" placeholder="Użytkownik">
  457. <?php endif; ?>
  458. </div>
  459. </div>
  460. </div>
  461. <div class="form-group">
  462. <label for="to" class="col-sm-2 control-label">Wiadomość:</label>
  463. <div class="col-sm-10">
  464. <textarea name="msg" class="form-control"><?php echo htmlspecialchars($msg); ?></textarea>
  465. </div>
  466. </div>
  467. <div class="form-group">
  468. <div class="col-sm-10 col-sm-offset-2">
  469. <input class="btn btn-primary" type="submit" value="Wyślij">
  470. </div>
  471. </div>
  472. </form>
  473. <script>
  474. function selectTblMsgsToType(n) {
  475. var toTypes = <?php echo json_encode(array_keys($listTo)); ?>,
  476. selectedType = n.value
  477. ;
  478. if (-1 !== toTypes.indexOf(n.value)) {
  479. toTypes.forEach(function(type) {
  480. if (type == selectedType) {
  481. document.getElementById('tblMsgsTo-' + type).style.display = 'block';
  482. } else {
  483. document.getElementById('tblMsgsTo-' + type).style.display = 'none';
  484. }
  485. });
  486. }
  487. }
  488. </script>
  489. <?php
  490. }
  491. function typeSpecialUserLoginAction() {
  492. header("Content-type: application/json");
  493. $typeSpecialUserId = TypespecialVariable::getInstance(-1, '__USER_LOGIN');
  494. if (!$typeSpecialUserId) {
  495. $jsonData = new stdClass();
  496. $jsonData->message = "TypeSpecial '__USER_LOGIN' not exists";
  497. echo json_encode($jsonData);
  498. exit;
  499. }
  500. $query = V::get('q', '', $_REQUEST);
  501. $rawRows = null;
  502. $jsonData = array();
  503. $queryParams = array();
  504. $rows = $typeSpecialUserId->getValuesWithExports($query, $queryParams);
  505. foreach ($rows as $kID => $vItem) {
  506. $itemJson = new stdClass();
  507. $itemJson->id = $vItem->id;
  508. $itemJson->name = $vItem->param_out;
  509. if (!empty($vItem->exports)) {
  510. $itemJson->exports = $vItem->exports;
  511. }
  512. $jsonData[] = $itemJson;
  513. }
  514. echo json_encode($jsonData);
  515. }
  516. function typeSpecialGroupIdAction() {
  517. header("Content-type: application/json");
  518. Lib::loadClass('TypespecialVariable');
  519. $typeSpecialZasob = TypespecialVariable::getInstance(-1, '__ZASOB');
  520. if (!$typeSpecialZasob) {
  521. $jsonData = new stdClass();
  522. $jsonData->message = "TypeSpecial '__ZASOB' not exists";
  523. echo json_encode($jsonData);
  524. exit;
  525. }
  526. $query = V::get('q', '', $_REQUEST);
  527. $rawRows = null;
  528. $jsonData = array();
  529. $queryParams = array();
  530. $queryParams['zasob_type_in'] = array('STANOWISKO', 'PODMIOT', 'DZIAL');
  531. $rows = $typeSpecialZasob->getValuesWithExports($query, $queryParams);
  532. DBG::_('DBG_TS', '>1', "rows({$query})", $rows, __CLASS__, __FUNCTION__, __LINE__);
  533. foreach ($rows as $kID => $vItem) {
  534. $itemJson = new stdClass();
  535. $itemJson->id = $vItem->id;
  536. $itemJson->name = $vItem->param_out;
  537. if (!empty($vItem->exports)) {
  538. $itemJson->exports = $vItem->exports;
  539. }
  540. $jsonData[] = $itemJson;
  541. }
  542. echo json_encode($jsonData);
  543. }
  544. function readAction() {
  545. $idMsg = V::get('id', 0, $_GET, 'int');
  546. $usrLogin = V::get('usrLogin', '', $_REQUEST, 'word');
  547. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  548. if (empty($usrLogin)) throw new HttpException("Błęny user login!", 404);
  549. SE_Layout::gora();
  550. SE_Layout::menu();
  551. try {
  552. $msg = $this->_getMsg($idMsg, $usrLogin);
  553. $this->_markAsRead($msg);
  554. $this->viewMsg($msg);
  555. } catch (Exception $e) {
  556. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  557. }
  558. SE_Layout::dol();
  559. }
  560. function viewAction() {
  561. $idMsg = V::get('id', 0, $_GET, 'int');
  562. $usrLogin = V::get('usrLogin', 0, $_REQUEST, 'word');
  563. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  564. if (empty($usrLogin)) throw new HttpException("Błęny user login", 404);
  565. SE_Layout::gora();
  566. SE_Layout::menu();
  567. try {
  568. $msg = $this->_getMsg($idMsg, $usrLogin);
  569. $this->viewMsg($msg);
  570. } catch (Exception $e) {
  571. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  572. }
  573. SE_Layout::dol();
  574. }
  575. function _getMsg($idMsg, $usrLogin) {
  576. $msgsRoute = Router::getRoute('Msgs');
  577. $msg['_raw'] = $msgsRoute->getMessage($idMsg);
  578. if (!$msg['_raw']) throw new HttpException("Wiadomość nie istnieje!", 404);
  579. $msg['usrLogin'] = $usrLogin;
  580. $msg['message'] = $msg['_raw']->msg;
  581. $msg['type'] = $msg['_raw']->msgType;
  582. $msg['_read'] = ('WAITING' != $msg['_raw']->A_STATUS);
  583. // $msg['_raw']->uiTargetType => default_db_table_record
  584. // $msg['_raw']->uiTargetName => TEST_PERMS.31
  585. if ('default_db_table_record' !== $msg['_raw']->uiTargetType) {
  586. throw new Exception("Parse message target type error!");
  587. }
  588. $parts = explode('.', $msg['_raw']->uiTargetName);
  589. if (2 !== count($parts)) throw new Exception("Parse message target type error!");
  590. $msg['tblName'] = $parts[0];
  591. $msg['idRow'] = $parts[1];
  592. if (!is_numeric($msg['idRow'])) throw new Exception("Parse message target type - id row type error!");
  593. return $msg;
  594. }
  595. function _markAsRead($msg) {
  596. if ($msg['_read']) return;
  597. $usrLogin = User::getLogin();
  598. $db = DB::getDB();
  599. if (!$db) throw new Exception("Brak dazy danych!");
  600. if ($db->has_errors()) throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  601. $sql = "update `CRM_UI_MSGS`
  602. set `A_STATUS`='NORMAL'
  603. , `A_RECORD_UPDATE_AUTHOR`='{$usrLogin}'
  604. , `A_RECORD_UPDATE_DATE`=NOW()
  605. , `actionExecutedTime`=NOW()
  606. where `ID`='{$msg['_raw']->ID}'
  607. and `A_STATUS`='WAITING'
  608. and `A_RECORD_UPDATE_AUTHOR`=''
  609. and `A_RECORD_UPDATE_DATE` is null
  610. and (
  611. ('{$usrLogin}'!=`A_RECORD_CREATE_AUTHOR`)
  612. or ('{$usrLogin}'=`A_RECORD_CREATE_AUTHOR`
  613. and 'user'=`userTargetType`
  614. and '{$usrLogin}'=`userTargetName`
  615. )
  616. )
  617. ";
  618. $res = $db->query($sql);
  619. 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()));
  620. }
  621. function viewMsg($msg) {
  622. $usrLogin = User::getLogin();
  623. $idTable = 0;
  624. if (!empty($msg['tblName'])) {
  625. $tblName = $msg['tblName'];
  626. $idTable = ProcesHelper::getZasobTableID($tblName);
  627. if (!$idTable) throw new Exception("Nie udało się odnaleźć nr tabeli '{$tblName}'");
  628. $idRow = $msg['idRow'];
  629. if (!$idRow) throw new Exception("Brak numeru powiązanego rekordu!");
  630. $usrAcl = User::getAcl();
  631. $tblAcl = $usrAcl->getTableAcl($idTable);
  632. }
  633. $linkBase = "index.php?_route=UserMsgs";
  634. $linkBase .= "&usrLogin={$usrLogin}";
  635. //$rmMsgLink = "{$linkBase}&_task=removeMsg&id={$msg['_raw']->ID}";
  636. $backLink = "{$linkBase}";
  637. ?>
  638. <div class="container">
  639. <h3><i class="glyphicon glyphicon-envelope"></i> <a href="<?php echo $backLink; ?>">Wiadomości <?php echo $usrLogin; ?></a>
  640. &raquo; Wiadomość nr <?php echo $msg['_raw']->ID; ?>
  641. <?php if ($idTable > 0) : ?>
  642. <!-- <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>
  643. z tabeli <a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $idTable; ?>"><?php echo $tblAcl->getLabel(); ?></a>
  644. </small>
  645. -->
  646. <small style="display:block; text-align:right">
  647. <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>
  648. <span style="font-size:12px; line-height:15px; vertical-align:text-bottom;">z tabeli</span>
  649. <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>
  650. </small>
  651. <?php endif; ?>
  652. </h3>
  653. </div>
  654. <?php $this->printWidgetViewMsg($msg); ?>
  655. <?php
  656. }
  657. function printWidgetViewMsg($msg) {
  658. $messageList = array();
  659. $uiTargetName = $msg['_raw']->uiTargetName;
  660. $uiTargetType = $msg['_raw']->uiTargetType;
  661. $replyLink = "index.php?_route=UserMsgs&_task=reply&uiTargetName={$uiTargetName}&uiTargetType={$uiTargetType}";
  662. $markAsReadLink = "index.php?_route=UserMsgs&_task=markAsRead";
  663. $message = $this->_convertMessageToJson($msg['_raw']);
  664. {//if ($message->idThread > 0) {
  665. $sqlLimit = 100;
  666. $ds = DB::getDataSource();
  667. $sqlIdThread = ($message->idThread > 0)? $message->idThread : $message->id;
  668. $sql = "
  669. select m.*
  670. from `CRM_UI_MSGS` m
  671. where (m.`idThread` = {$sqlIdThread} or m.`ID` = {$sqlIdThread})
  672. -- and m.`ID` < {$message->id}
  673. order by m.`ID` asc
  674. limit {$sqlLimit}
  675. ";
  676. $moreMsgs = $ds->getListByQuery($sql);
  677. foreach ($moreMsgs as $msg) $messageList[] = $this->_convertMessageToJson($msg);
  678. }
  679. //$messageList[] = $message;
  680. ?>
  681. <link rel="stylesheet" href="./stuff/widget-select.css">
  682. <style type="text/css">
  683. .user_avatar {
  684. display:block;
  685. width:40px;
  686. height:40px;
  687. margin:0 auto 10px auto;
  688. padding:0;
  689. border:1px solid #ddd;
  690. line-height: 38px;
  691. font-size:16px;
  692. text-align:center;
  693. vertical-align:middle;
  694. color:#aaa;
  695. }
  696. </style>
  697. <div id="widget-msg-tree" style="max-width:1000px; margin:0 auto;"></div>
  698. <script src="stuff/vendors.js"></script>
  699. <script src="stuff/bundle.se_route_user_msgs.js"></script>
  700. <script>
  701. var testNewRecordCounter = 0;
  702. jQuery("#widget-msg-tree").MsgThread({
  703. usrLogin: '<?php echo User::getLogin(); ?>',
  704. idThread: <?php echo $message->idThread; ?>,
  705. msgs: <?php echo json_encode($messageList); ?>,
  706. fetchMessages: (function() {
  707. var _msgsXhr = null;
  708. return function(reqData, callback) {
  709. if (_msgsXhr && _msgsXhr.state() === 'pending') {
  710. _msgsXhr.abort();
  711. _msgsXhr = null;
  712. }
  713. _msgsXhr = $.ajax({
  714. url: 'index.php?_route=UserMsgs&_task=getMessagesById&idThread=<?php echo $message->idThread; ?>',
  715. data: reqData,
  716. dataType: 'json'
  717. });
  718. _msgsXhr.done(function(data, textStatus, jqXHR) {
  719. if (data && data.msgs && data.msgs.length > 0) {
  720. callback(null, {msgs: data.msgs});
  721. } else {
  722. callback(null, {msgs: []});//"Error no data!");
  723. }
  724. });
  725. _msgsXhr.fail(function() {
  726. callback(null, {options: []});//"Error no data!");
  727. });
  728. _msgsXhr.always(function() {
  729. _msgsXhr = null;
  730. });
  731. };
  732. })(),
  733. fetchOptionsForGroup: (function() {
  734. var _groupXhr = null;
  735. return function(input, callback) {
  736. if (_groupXhr && _groupXhr.state() === 'pending') {
  737. _groupXhr.abort();
  738. _groupXhr = null;
  739. }
  740. _groupXhr = $.ajax({
  741. url: 'index.php?_route=UserMsgs&_task=typeSpecialGroupId&q=' + input,
  742. dataType: 'json'
  743. });
  744. _groupXhr.done(function(data, textStatus, jqXHR) {
  745. if (data && data.length > 0) {
  746. var options = [];
  747. data.forEach(function(item) {
  748. options.push({value: item.id, label: item.name});
  749. });
  750. callback(null, {options: options});
  751. } else {
  752. callback(null, {options: []});//"Error no data!");
  753. }
  754. });
  755. _groupXhr.fail(function() {
  756. callback(null, {options: []});//"Error no data!");
  757. });
  758. _groupXhr.always(function() {
  759. _groupXhr = null;
  760. });
  761. };
  762. })(),
  763. fetchOptionsForUser: (function() {
  764. var _userXhr = null;
  765. return function(input, callback) {
  766. if (_userXhr && _userXhr.state() === 'pending') {
  767. _userXhr.abort();
  768. _userXhr = null;
  769. }
  770. _userXhr = $.ajax({
  771. url: 'index.php?_route=UserMsgs&_task=typeSpecialUserLogin&q=' + input,
  772. dataType: 'json'
  773. });
  774. _userXhr.done(function(data, textStatus, jqXHR) {
  775. if (data && data.length > 0) {
  776. var options = [];
  777. data.forEach(function(item) {
  778. options.push({value: item.id, label: item.name});
  779. });
  780. callback(null, {options: options});
  781. } else {
  782. callback(null, {options: []});//"Error no data!");
  783. }
  784. });
  785. _userXhr.fail(function() {
  786. callback(null, {options: []});//"Error no data!");
  787. });
  788. _userXhr.always(function() {
  789. _userXhr = null;
  790. });
  791. };
  792. })(),
  793. saveReply: function(data, callback) {
  794. //console.log('#widget-msg-tree/MsgThread::saveReply: data:', data, 'callback', callback);
  795. $.ajax({
  796. url: '<?php echo $replyLink; ?>',
  797. method: 'POST',
  798. data: data,
  799. dataType: 'json'
  800. })
  801. .done(function(data, textStatus, jqXHR) {
  802. var returnData = {message: '', type: 'danger'};
  803. if (data && data.record) {
  804. returnData.msg = data.msg || 'Wysłano wiadomość';
  805. returnData.record = data.record;
  806. returnData.type = 'success';
  807. } else if (data.validateErrors) {
  808. returnData.msg = data.msg || 'Wystąpiły błędy w formularzu';
  809. returnData.type = 'warning';
  810. returnData.validateErrors = data.validateErrors;
  811. } else {
  812. returnData.msg = data.msg || 'Nie udało się wysłać wiadomości!';
  813. returnData.type = 'danger';
  814. }
  815. callback(null, returnData);
  816. })
  817. .fail(function() {
  818. callback(null, {message: 'Nie udało się wysłać wiadomości!', type: 'danger'});
  819. });
  820. },
  821. markAsRead: function(idMsg, callback) {
  822. //console.log('#widget-msg-tree/MsgThread::markAsRead: idMsg:', idMsg);
  823. $.ajax({
  824. url: '<?php echo $markAsReadLink; ?>&idMsg=' + idMsg,
  825. method: 'GET',
  826. dataType: 'json'
  827. })
  828. .done(function(data, textStatus, jqXHR) {
  829. var returnData = {message: '', type: 'danger'};
  830. if (data && data.record) {
  831. returnData.msg = data.msg || 'Oznaczono wiadomość jako odczytaną';
  832. returnData.record = data.record;
  833. returnData.type = 'success';
  834. } else {
  835. returnData.msg = data.msg || 'Nie udało się oznaczyć wiadomości jako odczytanej!';
  836. returnData.type = 'danger';
  837. }
  838. callback(null, returnData);
  839. })
  840. .fail(function() {
  841. callback(null, {message: 'Nie udało się oznaczyć wiadomości jako odczytanej!', type: 'danger'});
  842. });
  843. },
  844. dbg: false
  845. });
  846. // jQuery("#widget-msg-tree").on('change', function(e, data) {
  847. // console.log('#widget-msg-tree/MsgThread::onChange: data:', data);
  848. // });
  849. </script>
  850. <?php
  851. }
  852. function _convertMessageToJson($rawMsg) {
  853. /* $msg = {_raw: {A_RECORD_CREATE_AUTHOR: "plabudda",
  854. A_RECORD_CREATE_DATE: "2015-10-26 12:20:05",
  855. A_RECORD_DELETE_AUTHOR: "",
  856. A_RECORD_DELETE_DATE: null,
  857. A_RECORD_UPDATE_AUTHOR: "plabudda",
  858. A_RECORD_UPDATE_DATE: "2015-11-02 12:44:59",
  859. A_STATUS: "NORMAL",
  860. ID: "67",
  861. actionExecutedTime: "2015-11-02 12:44:59",
  862. actionNotes: "",
  863. app_className: "TableMsgs",
  864. msg: "test Y",
  865. msgType: "info",
  866. uiTargetName: "TEST_PERMS.31",
  867. uiTargetType: "default_db_table_record",
  868. userTargetName: "plabudda",
  869. userTargetType: "user"}
  870. _read: true,
  871. idRow: "31",
  872. message: "test Y",
  873. tblName: "TEST_PERMS",
  874. type: "info",
  875. usrLogin: "plabudda"} */
  876. $usrLogin = User::getLogin();
  877. $message = new stdClass();
  878. $message->id = $rawMsg->ID;
  879. $message->idThread = $rawMsg->idThread;// TODO: ID_THREAD
  880. $message->idReplyTo = $rawMsg->idReplyTo;// TODO: ID_REPLY_TO
  881. $message->message = $rawMsg->msg;
  882. $message->type = $rawMsg->msgType;
  883. $message->to = $rawMsg->userTargetName;
  884. $message->toType = $rawMsg->userTargetType;
  885. $message->author = $rawMsg->A_RECORD_CREATE_AUTHOR;
  886. $message->created = $rawMsg->A_RECORD_CREATE_DATE;
  887. $message->_read = ('WAITING' != $rawMsg->A_STATUS);
  888. $message->_readByUser = ('WAITING' != $rawMsg->A_STATUS);
  889. if ('WAITING' == $rawMsg->A_STATUS
  890. && $usrLogin == $rawMsg->A_RECORD_CREATE_AUTHOR) {
  891. if ('user' == $rawMsg->userTargetType
  892. && $usrLogin == $rawMsg->userTargetName) {
  893. $message->_readByUser = false;
  894. } else {
  895. $message->_readByUser = true;
  896. }
  897. }
  898. if ($message->_read) {
  899. if (!empty($rawMsg->A_RECORD_UPDATE_DATE)) $message->_readDate = $rawMsg->A_RECORD_UPDATE_DATE;
  900. if (!empty($rawMsg->A_RECORD_UPDATE_AUTHOR)) $message->_readBy = $rawMsg->A_RECORD_UPDATE_AUTHOR;
  901. }
  902. return $message;
  903. }
  904. function getMessagesByIdAction() {
  905. try {
  906. $idThread = V::get('idThread', '', $_GET, 'int');
  907. $idLastMsg = V::get('idLastMsg', '', $_GET, 'int');
  908. if ($idThread <= 0) throw new Exception("Wrong param id!");
  909. $sqlLimit = 10;// TODO: 100?
  910. $ds = DB::getDataSource();
  911. $sql = "
  912. select m.*
  913. from `CRM_UI_MSGS` m
  914. where m.`idThread` = {$idThread}
  915. and m.`ID` > {$idLastMsg}
  916. order by m.`ID` asc
  917. limit {$sqlLimit}
  918. ";
  919. $moreMsgs = $ds->getListByQuery($sql);
  920. $response = new stdClass();
  921. $response->msg = "Nowe wiadomości";
  922. $response->type = 'success';
  923. $response->msgs = array();
  924. foreach ($moreMsgs as $msg) {
  925. $response->msgs[] = $this->_convertMessageToJson($msg);
  926. }
  927. } catch (Exception $e) {
  928. $response = new stdClass();
  929. $response->msg = "Wystąpiły błędy: " . $e->getMessage();
  930. $response->type = 'danger';
  931. }
  932. echo json_encode($response);
  933. }
  934. function replyAction() {
  935. try {
  936. $uiTargetType = V::get('uiTargetType', '', $_GET);
  937. $uiTargetName = V::get('uiTargetName', '', $_GET);
  938. $response = $this->_reply($uiTargetType, $uiTargetName, $_POST);
  939. } catch (Exception $e) {
  940. $response = new stdClass();
  941. $response->msg = "Wystąpiły błędy: " . $e->getMessage();
  942. $response->type = 'danger';
  943. }
  944. echo json_encode($response);
  945. }
  946. function markAsReadAction() {
  947. $usrLogin = User::getLogin();
  948. try {
  949. $idMsg = V::get('idMsg', '', $_GET, 'int');
  950. $msg = $this->_getMsg($idMsg, $usrLogin);
  951. $this->_markAsRead($msg);
  952. $response = new stdClass();
  953. //$response->msg = "";
  954. $response->type = "success";
  955. $msg = $this->_getMsg($idMsg, $usrLogin);
  956. $response->record = $this->_convertMessageToJson($msg['_raw']);
  957. } catch (Exception $e) {
  958. $response = new stdClass();
  959. $response->msg = "Wystąpiły błędy: " . $e->getMessage();
  960. $response->type = 'danger';
  961. }
  962. echo json_encode($response);
  963. }
  964. function _reply($uiTargetType, $uiTargetName, $args) {
  965. $ds = DB::getDataSource();
  966. $newMsg = array();
  967. $newMsg['idReplyTo'] = V::get('idReplyTo', '', $args, 'int');
  968. $newMsg['msg'] = V::get('message', '', $args);
  969. $newMsg['msgType'] = V::get('msgType', 'info', $args);
  970. $newMsg['userTargetType'] = V::get('toType', '', $args);
  971. $newMsg['userTargetName'] = V::get('to', '', $args);
  972. $newMsg['A_RECORD_CREATE_DATE'] = 'NOW()';
  973. $newMsg['A_RECORD_CREATE_AUTHOR'] = User::getLogin();
  974. $newMsg['app_className'] = 'TableMsgs';
  975. //DBG::_(true, true, "newMsg", $newMsg, __CLASS__, __FUNCTION__, __LINE__);
  976. if ($newMsg['idReplyTo'] <= 0) throw new Exception("Wrong id reply to msg");
  977. $parentMsg = $ds->getById('CRM_UI_MSGS', $newMsg['idReplyTo']);
  978. if (!$parentMsg) throw new Exception("Nie znaleziono wiadomości");
  979. $newMsg['idThread'] = ($parentMsg->idThread > 0)? $parentMsg->idThread : $parentMsg->ID;
  980. $newMsg['uiTargetType'] = $uiTargetType;// TODO:? $parentMsg->uiTargetType
  981. $newMsg['uiTargetName'] = $uiTargetName;// TODO:? $parentMsg->uiTargetName
  982. $insertedId = $ds->insert('CRM_UI_MSGS', $newMsg);
  983. if (!$insertedId) throw new Exception("Nie udało się utworzyć rekordu");
  984. $msgAdded = $ds->getById('CRM_UI_MSGS', $insertedId);
  985. $response = new stdClass();
  986. $response->msg = "Wysłano wiadomość";
  987. $response->type = 'success';
  988. $response->record = $this->_convertMessageToJson($msgAdded);
  989. return $response;
  990. }
  991. function removeMsgAction() {
  992. $idMsg = V::get('id', 0, $_GET, 'int');
  993. $usrLogin = V::get('usrLogin', 0, $_REQUEST, 'word');
  994. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  995. if (empty($usrLogin)) throw new HttpException("Błęny user login", 404);
  996. SE_Layout::gora();
  997. SE_Layout::menu();
  998. try {
  999. $msg = $this->_getMsg($idMsg, $usrLogin);
  1000. $msgsRoute = Router::getRoute('Msgs');
  1001. $msgsRoute->removeTableRecordMsg($idMsg);
  1002. } catch (Exception $e) {
  1003. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  1004. SE_Layout::dol();
  1005. exit;
  1006. }
  1007. ?>
  1008. <div class="container">
  1009. <div class="alert alert-success">
  1010. Wiadomość została usunięta <a class="btn btn-xs btn-default" href="index.php?_route=UserMsgs&usrLogin=<?php echo $usrLogin; ?>">wróć</a>
  1011. </div>
  1012. </div>
  1013. <?php
  1014. SE_Layout::dol();
  1015. }
  1016. }