TableMsgs.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. <?php
  2. Lib::loadClass('Router');
  3. Lib::loadClass('RouteBase');
  4. Lib::loadClass('TypespecialVariable');
  5. class Route_TableMsgs extends RouteBase {
  6. public function handleAuth() {
  7. if (!User::logged()) {
  8. User::authByRequest();
  9. }
  10. }
  11. public function defaultAction() {
  12. SE_Layout::gora();
  13. SE_Layout::menu();
  14. $this->menu();
  15. SE_Layout::dol();
  16. }
  17. public function menu() {
  18. $usrLogin = User::getLogin();
  19. ?>
  20. <ul>
  21. <li>TODO: ...</li>
  22. </ul>
  23. <?php
  24. }
  25. public function tableRowAction() {
  26. $idTable = V::get('idTable', 0, $_REQUEST, 'int');
  27. $idRow = V::get('idRow', 0, $_REQUEST, 'int');
  28. if ($idTable <= 0) throw new HttpException("Błęny numer tabeli", 400);
  29. if ($idRow <= 0) throw new HttpException("Błęny numer tabeli", 400);
  30. SE_Layout::gora();
  31. SE_Layout::menu();
  32. try {
  33. $this->tableRowMsgs($idTable, $idRow);
  34. } catch (Exception $e) {
  35. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  36. }
  37. SE_Layout::dol();
  38. }
  39. public function tableRowMsgs($idTable, $idRow) {
  40. $tblAcl = User::getAcl()->getTableAcl($idTable);
  41. $tableName = $tblAcl->getName();
  42. $record = $tblAcl->getItem($idRow);
  43. $args = array();
  44. $args['to_type'] = V::get('to_type', '', $_POST);
  45. $args['to'] = V::get("to-{$args['to_type']}", '', $_POST);
  46. $args['msg'] = V::get('msg', '', $_POST);
  47. $arrorsList = array();
  48. $createdId = 0;
  49. if (!empty($_POST)) {
  50. try {
  51. $this->_validate($args);
  52. $createdId = $this->_create($args, $tableName, $idRow);
  53. } catch(Exception $e) {
  54. $arrorsList[] = $e->getMessage();
  55. }
  56. }
  57. $msgsList = $this->_getMsgs('inbox', $tableName, $idRow);
  58. $totalReadMsgs = 0;
  59. $totalUnreadMsgs = 0;
  60. foreach ($msgsList as $ind => $msg) {
  61. if ($msg['_read']) {
  62. $totalReadMsgs++;
  63. } else {
  64. $totalUnreadMsgs++;
  65. }
  66. }
  67. $sentMsgsList = $this->_getMsgs('sent', $tableName, $idRow);
  68. $removedMsgsList = $this->_getMsgs('removed', $tableName, $idRow);
  69. ?>
  70. <style type="text/css">
  71. .tblMsgsListItem { cursor:pointer; }
  72. </style>
  73. <div class="container">
  74. <h3><i class="glyphicon glyphicon-envelope"></i> Wiadomości powiązane z rekordem nr <code><?php echo $idRow; ?></code>
  75. <br><small>z tabeli <a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $idTable; ?>"><?php echo $tblAcl->getLabel(); ?></a></small>
  76. </h3>
  77. <?php if ($createdId > 0) : ?>
  78. <?php echo SE_Layout::alert('info', "Wysłano wiadomość nr '{$createdId}'"); ?>
  79. <?php endif; ?>
  80. <div>
  81. <ul class="nav nav-tabs" role="tablist">
  82. <li>
  83. <a href="#tbl-msgs-compose"><i class="glyphicon glyphicon-plus"></i> Nowa wiadomość</a>
  84. </li>
  85. <li role="presentation" class="active"><a href="#odebrane" aria-controls="odebrane" role="tab" data-toggle="tab">Odebrane <em>(<?php echo $totalUnreadMsgs; ?>)</em></a></li>
  86. <li role="presentation"><a href="#wyslane" aria-controls="wyslane" role="tab" data-toggle="tab">Wysłane</em></a></li>
  87. <li role="presentation"><a href="#kosz" aria-controls="kosz" role="tab" data-toggle="tab">Kosz</em></a></li>
  88. </ul>
  89. <div class="tab-content" style="margin-bottom:15px">
  90. <div role="tabpanel" class="tab-pane active" id="odebrane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  91. <?php $this->_printTableMsgsList($msgsList, $idTable, $idRow, 'read'); ?>
  92. </div>
  93. <div role="tabpanel" class="tab-pane" id="wyslane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  94. <?php $this->_printTableMsgsList($sentMsgsList, $idTable, $idRow, 'view'); ?>
  95. </div>
  96. <div role="tabpanel" class="tab-pane" id="kosz" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  97. <?php $this->_printTableMsgsList($removedMsgsList, $idTable, $idRow, 'view'); ?>
  98. </div>
  99. </div>
  100. </div>
  101. <div class="panel panel-default" id="tbl-msgs-compose">
  102. <div class="panel-heading">Wyślij nową wiadomość</div>
  103. <div class="panel-body">
  104. <?php if (!empty($arrorsList)) : ?>
  105. <?php foreach ($arrorsList as $errMsg) : ?>
  106. <div class="alert alert-danger"><?php echo $errMsg; ?></div>
  107. <?php endforeach; ?>
  108. <?php endif; ?>
  109. <?php $this->_printMsgForm($args); ?>
  110. </div>
  111. </div>
  112. </div>
  113. <?php
  114. //DBG::_(true, true, "_POST", $_POST, __CLASS__, __FUNCTION__, __LINE__);
  115. //DBG::_(true, true, "tblAcl", $tblAcl, __CLASS__, __FUNCTION__, __LINE__);
  116. //DBG::_(true, true, "record", $record, __CLASS__, __FUNCTION__, __LINE__);
  117. //DBG::_(true, true, "msgsList", $msgsList, __CLASS__, __FUNCTION__, __LINE__);
  118. //throw new Exception("TODO: ...");
  119. }
  120. public function _printTableMsgsList($msgsList, $idTable, $idRow, $actionTask = null) {
  121. $msgsTotal = count($msgsList);
  122. ?>
  123. <table class="tblMsgsList table table-hovered" style="margin-bottom:0; table-layout:fixed;">
  124. <thead>
  125. <tr>
  126. <th style="width:60px">#</th>
  127. <th>wiadomość</th>
  128. <th style="width:130px">data</th>
  129. </tr>
  130. </thead>
  131. <tfoot>
  132. <?php if ($msgsTotal > 20) : ?>
  133. <tr>
  134. <td colspan="3"><em class="text-muted" style="padding-left:60px;">...</em></td>
  135. </tr>
  136. <?php endif; ?>
  137. </tfoot>
  138. <tbody>
  139. <?php if ($msgsTotal <= 0) : ?>
  140. <tr>
  141. <td colspan="3"><em class="text-muted" style="padding-left:60px;">Brak wiadomości</em></td>
  142. </tr>
  143. <?php else : ?>
  144. <?php foreach ($msgsList as $msg) : ?>
  145. <?php
  146. $onClick = '';
  147. $msgLink = Request::getPathUri() . 'index.php?_route=TableMsgs&id=' . $msg['_raw']->ID;
  148. $msgLink .= "&idTable={$idTable}&idRow={$idRow}";
  149. if ('read' == $actionTask || 'view' == $actionTask) {
  150. $msgLink .= '&_task=' . $actionTask;
  151. } else {
  152. $msgLink = null;
  153. }
  154. if ($msgLink) {
  155. $jsOnClick = "window.location.href='{$msgLink}'";
  156. $onClick = 'onclick="' . $jsOnClick . '"';
  157. }
  158. ?>
  159. <tr <?php echo $onClick; ?>
  160. class="tblMsgsListItem <?php echo ($msg['_read'])? 'active' : ''; ?>">
  161. <td><?php echo $msg['_raw']->ID; ?></td>
  162. <td>
  163. <div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;"><?php echo htmlspecialchars($msg['message']); ?></div>
  164. <div class="text-muted" style="font-style:italic;">
  165. od <?php echo $msg['_raw']->A_RECORD_CREATE_AUTHOR; ?> do <?php
  166. if ('everyone' == $msg['_raw']->userTargetType) {
  167. echo "wszystkich";
  168. } else if ('user' == $msg['_raw']->userTargetType) {
  169. echo "{$msg['_raw']->userTargetName}";
  170. } else if ('group' == $msg['_raw']->userTargetType) {
  171. echo "grupy {$msg['_raw']->userTargetName}";
  172. }
  173. ?>
  174. </div>
  175. </td>
  176. <td style="white-space:nowrap;">
  177. <?php echo $msg['_raw']->A_RECORD_CREATE_DATE; ?>
  178. <?php if ($msg['_readDate']) : ?>
  179. <div class="text-muted" style="font-style:italic" title="Przeczytano <?php echo $msg['_readDate']; ?>"><?php echo $msg['_readDate']; ?></div>
  180. <?php endif; ?>
  181. </td>
  182. </tr>
  183. <?php endforeach; ?>
  184. <?php endif; ?>
  185. </tbody>
  186. </table>
  187. <?php
  188. }
  189. public function _getMsgs($filterType, $tableName, $idRow) {
  190. $msgsRoute = Router::getRoute('Msgs');
  191. //$msgsList = $msgsRoute->getMessagesForTableRecord($tableName, $idRow);
  192. $msgsList = array();
  193. if (empty($tableName)) throw new Exception("No table name!");
  194. $sqlWhereAddFilter = "";
  195. $usrLogin = User::getLogin();
  196. switch ($filterType) {
  197. case 'inbox':
  198. $sqlWhereAddFilter = "
  199. and (m.`userTargetType` in('everyone')
  200. or (m.`userTargetType`='user' and m.`userTargetName`='{$usrLogin}')
  201. -- TODO: use group id
  202. )
  203. and m.`A_STATUS` not in('DELETED')
  204. ";
  205. break;
  206. case 'sent':
  207. $sqlWhereAddFilter = "
  208. and m.`A_RECORD_CREATE_AUTHOR`='{$usrLogin}'
  209. and m.`A_STATUS` not in('DELETED')
  210. ";
  211. break;
  212. case 'removed':
  213. $sqlWhereAddFilter = "
  214. and (m.`A_RECORD_CREATE_AUTHOR`='{$usrLogin}'
  215. or (m.`userTargetType` in('everyone')
  216. or (m.`userTargetType`='user' and m.`userTargetName`='{$usrLogin}')
  217. -- TODO: use group id
  218. )
  219. )
  220. and m.`A_STATUS` in('DELETED')
  221. ";
  222. break;
  223. default: throw new Exception("Unknown filter type");
  224. }
  225. $db = DB::getDB();
  226. $tableName = $db->_($tableName);
  227. $sql = "select m.*
  228. from `CRM_UI_MSGS` m
  229. where m.`uiTargetType`='default_db_table_record'
  230. and m.`uiTargetName`='{$tableName}.{$idRow}'
  231. {$sqlWhereAddFilter}
  232. order by m.`ID` DESC
  233. limit 21
  234. ";
  235. $db = DB::getDB();
  236. $res = $db->query($sql);
  237. while ($r = $db->fetch($res)) {
  238. $msg['message'] = $r->msg;
  239. $msg['type'] = $r->msgType;
  240. $msg['_raw'] = $r;
  241. $msg['_read'] = ('WAITING' != $r->A_STATUS);
  242. $msg['_readDate'] = $r->actionExecutedTime;
  243. $msgsList[$r->ID] = $msg;
  244. }
  245. return $msgsList;
  246. }
  247. public function _validate($args) {
  248. $toType = V::get('to_type', '', $args);
  249. $to = V::get('to', '', $args);
  250. $msg = V::get('msg', '', $args);
  251. if (!in_array($toType, array('everyone', 'user', 'group'))) {
  252. throw new Exception("Niedozwolony typ odbiorcy");
  253. }
  254. if (empty($to) && 'everyone' != $toType) {
  255. throw new Exception("Proszę podać odbiorcę wiadomości");
  256. }
  257. if (empty($msg)) {
  258. throw new Exception("Proszę podać treść wiadomości");
  259. }
  260. }
  261. public function _create($args, $tableName, $idRow) {
  262. $toType = V::get('to_type', '', $args);
  263. $to = V::get('to', '', $args);
  264. $msg = V::get('msg', '', $args);
  265. $usrLogin = User::getLogin();
  266. $db = DB::getDB();
  267. if (!$db) throw new Exception("Brak dazy danych!");
  268. if ($db->has_errors()) throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  269. $item = array();
  270. $item['`uiTargetType`'] = "'default_db_table_record'";
  271. $item['`uiTargetName`'] = "'{$tableName}.{$idRow}'";
  272. $item['`userTargetType`'] = "'{$toType}'";
  273. $item['`userTargetName`'] = "'{$to}'";
  274. $item['`msg`'] = "'" . $db->_($msg) . "'";
  275. $item['`A_RECORD_CREATE_DATE`'] = "NOW()";
  276. $item['`A_RECORD_CREATE_AUTHOR`'] = "'{$usrLogin}'";
  277. $item['`A_STATUS`'] = "'WAITING'";
  278. $item['`app_className`'] = "'TableMsgs'";
  279. $sql = "insert into `CRM_UI_MSGS` (" . implode(",", array_keys($item)) . ")
  280. values (" . implode(",", array_values($item)) . ")
  281. ";
  282. $res = $db->query($sql);
  283. 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()));
  284. $createdId = $db->insert_id();
  285. if ($createdId <= 0) throw new Exception("Nie udało się zapisać wiadomości.");
  286. return $createdId;
  287. }
  288. public function _printMsgForm($args) {
  289. $toType = V::get('to_type', '', $args);
  290. $to = V::get('to', '', $args);
  291. $msg = V::get('msg', '', $args);
  292. $listTo = array();
  293. $listTo['everyone'] = 'Wszyscy';
  294. $listTo['user'] = 'Użytkownik';
  295. //$listTo['group'] = 'Grupa';
  296. $toType = (array_key_exists($toType, $listTo))? $toType : 'everyone';
  297. $typeSpecialGroupId = TypespecialVariable::getInstance(-1, '__ZASOB');
  298. $typeSpecialUserLogin = TypespecialVariable::getInstance(-1, '__USER_LOGIN');
  299. $selectedLogin = ('user' == $toType)? $to : '';
  300. $selectedGroupId = ('group' == $toType)? $to : '';
  301. ?>
  302. <form class="form-horizontal" action="" method="post">
  303. <div class="form-group">
  304. <label class="col-sm-2 control-label" for="to">Do:</label>
  305. <div class="col-sm-3">
  306. <select name="to_type" class="form-control" onChange="return selectTblMsgsToType(this);">
  307. <?php foreach ($listTo as $type => $typeLabel) : ?>
  308. <option <?php echo ($type == $toType)? 'selected' : ''; ?>
  309. value="<?php echo $type; ?>"><?php echo $typeLabel; ?></option>
  310. <?php endforeach; ?>
  311. </select>
  312. </div>
  313. <div class="col-sm-7">
  314. <div id="tblMsgsTo-everyone" style="<?php echo ('everyone' == $toType)? '' : 'display:none'; ?>">
  315. <input name="to-everyone" type="text" class="form-control" disabled>
  316. </div>
  317. <div id="tblMsgsTo-group" style="<?php echo ('group' == $toType)? '' : 'display:none'; ?>">
  318. <?php if ($typeSpecialGroupId) : ?>
  319. <?php
  320. $fldName = 'to-group';
  321. $fldParams = array();
  322. $fldParams['allowCreate'] = false;
  323. $fldParams['ajaxDataUrlBase'] = "index.php?_route=TableMsgs&_task=typeSpecialGroupId";
  324. $fldParams['placeholder'] = 'Grupa...';
  325. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  326. echo $typeSpecialUserLogin->showFormItem($tblID = -1, $fldName, $selectedGroupId, $fldParams);
  327. ?>
  328. <?php else : ?>
  329. <input name="to-group" type="text" class="form-control" placeholder="Grupa">
  330. <?php endif; ?>
  331. </div>
  332. <div id="tblMsgsTo-user" style="<?php echo ('user' == $toType)? '' : 'display:none'; ?>">
  333. <?php if ($typeSpecialUserLogin) : ?>
  334. <?php
  335. $fldName = 'to-user';
  336. $fldParams = array();
  337. $fldParams['allowCreate'] = false;
  338. $fldParams['ajaxDataUrlBase'] = "index.php?_route=TableMsgs&_task=typeSpecialUserLogin";
  339. $fldParams['placeholder'] = 'Użytkownik...';
  340. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  341. echo $typeSpecialUserLogin->showFormItem($tblID = -1, $fldName, $selectedLogin, $fldParams);
  342. ?>
  343. <?php else : ?>
  344. <input name="to-user" type="text" class="form-control" placeholder="Użytkownik">
  345. <?php endif; ?>
  346. </div>
  347. </div>
  348. </div>
  349. <div class="form-group">
  350. <label for="to" class="col-sm-2 control-label">Wiadomość:</label>
  351. <div class="col-sm-10">
  352. <textarea name="msg" class="form-control"><?php echo htmlspecialchars($msg); ?></textarea>
  353. </div>
  354. </div>
  355. <div class="form-group">
  356. <div class="col-sm-10 col-sm-offset-2">
  357. <input class="btn btn-primary" type="submit" value="Wyślij">
  358. </div>
  359. </div>
  360. </form>
  361. <script>
  362. function selectTblMsgsToType(n) {
  363. var toTypes = <?php echo json_encode(array_keys($listTo)); ?>,
  364. selectedType = n.value
  365. ;
  366. if (-1 !== toTypes.indexOf(n.value)) {
  367. toTypes.forEach(function(type) {
  368. if (type == selectedType) {
  369. document.getElementById('tblMsgsTo-' + type).style.display = 'block';
  370. } else {
  371. document.getElementById('tblMsgsTo-' + type).style.display = 'none';
  372. }
  373. });
  374. }
  375. }
  376. </script>
  377. <?php
  378. }
  379. public function typeSpecialUserLoginAction() {
  380. header("Content-type: application/json");
  381. $typeSpecialUserId = TypespecialVariable::getInstance(-1, '__USER_LOGIN');
  382. if (!$typeSpecialUserId) {
  383. $jsonData = new stdClass();
  384. $jsonData->message = "TypeSpecial '__USER_LOGIN' not exists";
  385. echo json_encode($jsonData);
  386. exit;
  387. }
  388. $query = V::get('q', '', $_REQUEST);
  389. $rawRows = null;
  390. $jsonData = array();
  391. $queryParams = array();
  392. $rows = $typeSpecialUserId->getValuesWithExports($query, $queryParams);
  393. foreach ($rows as $kID => $vItem) {
  394. $itemJson = new stdClass();
  395. $itemJson->id = $vItem->id;
  396. $itemJson->name = $vItem->param_out;
  397. if (!empty($vItem->exports)) {
  398. $itemJson->exports = $vItem->exports;
  399. }
  400. $jsonData[] = $itemJson;
  401. }
  402. echo json_encode($jsonData);
  403. }
  404. public function typeSpecialGroupIdAction() {
  405. header("Content-type: application/json");
  406. Lib::loadClass('TypespecialVariable');
  407. $typeSpecialZasob = TypespecialVariable::getInstance(-1, '__ZASOB');
  408. if (!$typeSpecialZasob) {
  409. $jsonData = new stdClass();
  410. $jsonData->message = "TypeSpecial '__ZASOB' not exists";
  411. echo json_encode($jsonData);
  412. exit;
  413. }
  414. $query = V::get('q', '', $_REQUEST);
  415. $rawRows = null;
  416. $jsonData = array();
  417. $queryParams = array();
  418. $queryParams['zasob_type_in'] = array('STANOWISKO', 'PODMIOT', 'DZIAL');
  419. $rows = $typeSpecialZasob->getValuesWithExports($query, $queryParams);
  420. DBG::_('DBG_TS', '>1', "rows({$query})", $rows, __CLASS__, __FUNCTION__, __LINE__);
  421. foreach ($rows as $kID => $vItem) {
  422. $itemJson = new stdClass();
  423. $itemJson->id = $vItem->id;
  424. $itemJson->name = $vItem->param_out;
  425. if (!empty($vItem->exports)) {
  426. $itemJson->exports = $vItem->exports;
  427. }
  428. $jsonData[] = $itemJson;
  429. }
  430. echo json_encode($jsonData);
  431. }
  432. public function readAction() {
  433. $idMsg = V::get('id', 0, $_GET, 'int');
  434. $idTable = V::get('idTable', 0, $_REQUEST, 'int');
  435. $idRow = V::get('idRow', 0, $_REQUEST, 'int');
  436. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  437. if ($idTable <= 0) throw new HttpException("Błęny numer tabeli", 400);
  438. if ($idRow <= 0) throw new HttpException("Błęny numer tabeli", 400);
  439. SE_Layout::gora();
  440. SE_Layout::menu();
  441. try {
  442. $msg = $this->_getMsg($idMsg, $idTable, $idRow);
  443. $this->_markAsRead($msg);
  444. $this->tableRowMsg($msg);
  445. } catch (Exception $e) {
  446. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  447. }
  448. SE_Layout::dol();
  449. }
  450. public function viewAction() {
  451. $idMsg = V::get('id', 0, $_GET, 'int');
  452. $idTable = V::get('idTable', 0, $_REQUEST, 'int');
  453. $idRow = V::get('idRow', 0, $_REQUEST, 'int');
  454. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  455. if ($idTable <= 0) throw new HttpException("Błęny numer tabeli", 400);
  456. if ($idRow <= 0) throw new HttpException("Błęny numer tabeli", 400);
  457. SE_Layout::gora();
  458. SE_Layout::menu();
  459. try {
  460. $msg = $this->_getMsg($idMsg, $idTable, $idRow);
  461. $this->tableRowMsg($msg);
  462. } catch (Exception $e) {
  463. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  464. }
  465. SE_Layout::dol();
  466. }
  467. public function _getMsg($idMsg, $idTable, $idRow) {
  468. $msgsRoute = Router::getRoute('Msgs');
  469. $msg['_raw'] = $msgsRoute->getMessage($idMsg);
  470. if (!$msg['_raw']) throw new HttpException("Wiadomość nie istnieje!", 404);
  471. $msg['idTable'] = $idTable;
  472. $msg['idRow'] = $idRow;
  473. $msg['message'] = $msg['_raw']->msg;
  474. $msg['type'] = $msg['_raw']->msgType;
  475. $msg['_read'] = ('WAITING' != $msg['_raw']->A_STATUS);
  476. // $msg['_raw']->uiTargetType => default_db_table_record
  477. // $msg['_raw']->uiTargetName => TEST_PERMS.31
  478. if ('default_db_table_record' !== $msg['_raw']->uiTargetType) {
  479. throw new Exception("Parse message target type error!");
  480. }
  481. $parts = explode('.', $msg['_raw']->uiTargetName);
  482. if (2 !== count($parts)) throw new Exception("Parse message target type error!");
  483. $msg['tblName'] = $parts[0];
  484. if (!is_numeric($msg['idRow'])) throw new Exception("Parse message target type - id row type error!");
  485. if ($msg['idRow'] != $parts[1]) throw new Exception("Parse message target type - id row value error!");
  486. return $msg;
  487. }
  488. public function _markAsRead($msg) {
  489. if ($msg['_read']) return;
  490. $usrLogin = User::getLogin();
  491. $db = DB::getDB();
  492. if (!$db) throw new Exception("Brak dazy danych!");
  493. if ($db->has_errors()) throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  494. $sql = "update `CRM_UI_MSGS`
  495. set `A_STATUS`='NORMAL'
  496. , `A_RECORD_UPDATE_AUTHOR`='{$usrLogin}'
  497. , `A_RECORD_UPDATE_DATE`=NOW()
  498. , `actionExecutedTime`=NOW()
  499. where `ID`='{$msg['_raw']->ID}'
  500. ";
  501. $res = $db->query($sql);
  502. 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()));
  503. }
  504. public function tableRowMsg($msg) {
  505. $idTable = $msg['idTable'];
  506. $idRow = $msg['idRow'];
  507. $usrAcl = User::getAcl();
  508. $tblAcl = $usrAcl->getTableAcl($idTable);
  509. $linkBase = "index.php?_route=TableMsgs";
  510. $linkBase .= "&idTable={$msg['idTable']}";
  511. $linkBase .= "&idRow={$msg['idRow']}";
  512. $rmMsgLink = "{$linkBase}&_task=removeMsg&id={$msg['_raw']->ID}";
  513. $backLink = "{$linkBase}&_task=tableRow";
  514. ?>
  515. <div class="container">
  516. <h3><i class="glyphicon glyphicon-envelope"></i> <a href="<?php echo $backLink; ?>">Wiadomości powiązane z rekordem nr <?php echo $idRow; ?></a>
  517. &raquo; Wiadomość nr <code><?php echo $msg['_raw']->ID; ?></code>
  518. <br><small>z tabeli <a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=13051">Test permy</a></small>
  519. </h3>
  520. <div class="panel panel-<?php echo $msg['type']; ?>">
  521. <div class="panel-heading">
  522. <h3 class="panel-title">Wiadomość wysłana przez <?php echo $msg['_raw']->A_RECORD_CREATE_AUTHOR; ?>
  523. <span class="pull-right"><?php echo $msg['_raw']->A_RECORD_CREATE_DATE; ?></span></h3>
  524. </div>
  525. <div class="panel-body">
  526. <?php echo htmlspecialchars($msg['message']); ?>
  527. </div>
  528. <div class="panel-footer" style="overflow:hidden">
  529. <?php if (!empty($msg['_raw']->A_RECORD_UPDATE_DATE) && !empty($msg['_raw']->A_RECORD_UPDATE_AUTHOR)) : ?>
  530. <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>
  531. <?php endif; ?>
  532. <?php if (!empty($msg['_raw']->A_RECORD_DELETE_DATE) && !empty($msg['_raw']->A_RECORD_DELETE_AUTHOR)) : ?>
  533. <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>
  534. <?php endif; ?>
  535. <?php if ('DELETED' != $msg['_raw']->A_STATUS) : ?>
  536. <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>
  537. <?php endif; ?>
  538. </div>
  539. </div>
  540. </div>
  541. <?php
  542. // TODO: odpisz
  543. }
  544. public function removeMsgAction() {
  545. $idMsg = V::get('id', 0, $_GET, 'int');
  546. $idTable = V::get('idTable', 0, $_REQUEST, 'int');
  547. $idRow = V::get('idRow', 0, $_REQUEST, 'int');
  548. if ($idMsg <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  549. if ($idTable <= 0) throw new HttpException("Błęny numer tabeli", 400);
  550. if ($idRow <= 0) throw new HttpException("Błęny numer tabeli", 400);
  551. SE_Layout::gora();
  552. SE_Layout::menu();
  553. try {
  554. $msg = $this->_getMsg($idMsg, $idTable, $idRow);
  555. $this->_removeRowMsg($msg);
  556. } catch (Exception $e) {
  557. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  558. SE_Layout::dol();
  559. exit;
  560. }
  561. ?>
  562. <div class="container">
  563. <div class="alert alert-success">
  564. Wiadomość została usunięta - <a href="index.php?_route=TableMsgs&_task=tableRow&idTable=<?php echo $idTable; ?>&idRow=<?php echo $idRow; ?>">wróć</a>
  565. </div>
  566. </div>
  567. <?php
  568. SE_Layout::dol();
  569. }
  570. public function _removeRowMsg($msg) {
  571. // IDEA: do kosza - add trigger to insert into `CRM_UI_MSGS__TRASH` after DELETE on `CRM_UI_MSGS`
  572. if (empty($msg['_raw']) || empty($msg['_raw']->ID)) throw new Exception("Brak wiadomości!");
  573. $usrLogin = User::getLogin();
  574. $db = DB::getDB();
  575. if (!$db) throw new Exception("Brak dazy danych!");
  576. if ($db->has_errors()) throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  577. $sqlTODO = "delete `CRM_UI_MSGS` where `ID`='{$msg['_raw']->ID}' ";
  578. $sql = "update `CRM_UI_MSGS`
  579. set `A_STATUS`='DELETED'
  580. , `A_RECORD_DELETE_AUTHOR`='{$usrLogin}'
  581. , `A_RECORD_DELETE_DATE`=NOW()
  582. where `ID`='{$msg['_raw']->ID}'
  583. ";
  584. $res = $db->query($sql);
  585. 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()));
  586. }
  587. }