TableMsgs.php 23 KB

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