TableMsgs.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. Lib::loadClass('Router');
  3. Lib::loadClass('RouteBase');
  4. Lib::loadClass('TypespecialVariable');
  5. Lib::loadClass('ProcesHelper');
  6. class Route_TableMsgs extends RouteBase {
  7. public function handleAuth() {
  8. if (!User::logged()) {
  9. User::authByRequest();
  10. }
  11. }
  12. public function defaultAction() {
  13. SE_Layout::gora();
  14. SE_Layout::menu();
  15. $this->menu();
  16. SE_Layout::dol();
  17. }
  18. public function menu() {
  19. $usrLogin = User::getLogin();
  20. ?>
  21. <ul>
  22. <li>TODO: ...</li>
  23. </ul>
  24. <?php
  25. }
  26. public function tableRowAction() {
  27. $idTable = V::get('idTable', 0, $_REQUEST, 'int');
  28. $idRow = V::get('idRow', 0, $_REQUEST, 'int');
  29. if ($idTable <= 0) throw new HttpException("Błęny numer tabeli", 400);
  30. if ($idRow <= 0) throw new HttpException("Błęny numer tabeli", 400);
  31. SE_Layout::gora();
  32. SE_Layout::menu();
  33. try {
  34. $this->tableRowMsgs($idTable, $idRow);
  35. } catch (Exception $e) {
  36. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  37. }
  38. SE_Layout::dol();
  39. }
  40. public function tableRowMsgs($idTable, $idRow) {
  41. $tblAcl = User::getAcl()->getTableAcl($idTable);
  42. $tableName = $tblAcl->getName();
  43. $record = $tblAcl->getItem($idRow);
  44. $args = array();
  45. $args['to_type'] = V::get('to_type', '', $_POST);
  46. $args['to'] = V::get("to-{$args['to_type']}", '', $_POST);
  47. $args['msg'] = V::get('msg', '', $_POST);
  48. $arrorsList = array();
  49. $createdId = 0;
  50. if (!empty($_POST)) {
  51. try {
  52. $this->_validate($args);
  53. $createdId = $this->_create($args, $tableName, $idRow);
  54. } catch(Exception $e) {
  55. $arrorsList[] = $e->getMessage();
  56. }
  57. }
  58. $msgsList = $this->_getMsgs($tableName, $idRow);
  59. $totalReadMsgs = 0;
  60. $totalUnreadMsgs = 0;
  61. foreach ($msgsList as $ind => $msg) {
  62. if ($msg['_read']) {
  63. $totalReadMsgs++;
  64. } else {
  65. $totalUnreadMsgs++;
  66. }
  67. }
  68. $sentMsgsList = $this->_getSentMsgs($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. </ul>
  88. <div class="tab-content" style="margin-bottom:15px">
  89. <div role="tabpanel" class="tab-pane active" id="odebrane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  90. <?php $this->_printTableMsgsList($msgsList, 'read'); ?>
  91. </div>
  92. <div role="tabpanel" class="tab-pane" id="wyslane" style="border-style:none solid solid solid; border-width:1px; border-color:#ddd;">
  93. <?php $this->_printTableMsgsList($sentMsgsList, 'view'); ?>
  94. </div>
  95. </div>
  96. </div>
  97. <div class="panel panel-default" id="tbl-msgs-compose">
  98. <div class="panel-heading">Wyślij nową wiadomość</div>
  99. <div class="panel-body">
  100. <?php if (!empty($arrorsList)) : ?>
  101. <?php foreach ($arrorsList as $errMsg) : ?>
  102. <div class="alert alert-danger"><?php echo $errMsg; ?></div>
  103. <?php endforeach; ?>
  104. <?php endif; ?>
  105. <?php $this->_printMsgForm($args); ?>
  106. </div>
  107. </div>
  108. </div>
  109. <?php
  110. //DBG::_(true, true, "_POST", $_POST, __CLASS__, __FUNCTION__, __LINE__);
  111. //DBG::_(true, true, "tblAcl", $tblAcl, __CLASS__, __FUNCTION__, __LINE__);
  112. //DBG::_(true, true, "record", $record, __CLASS__, __FUNCTION__, __LINE__);
  113. //DBG::_(true, true, "msgsList", $msgsList, __CLASS__, __FUNCTION__, __LINE__);
  114. //throw new Exception("TODO: ...");
  115. }
  116. public function _printTableMsgsList($msgsList, $actionTask = null) {
  117. $msgsTotal = count($msgsList);
  118. ?>
  119. <table class="tblMsgsList table table-hovered" style="margin-bottom:0; table-layout:fixed;">
  120. <thead>
  121. <tr>
  122. <th style="width:60px">#</th>
  123. <th>wiadomość</th>
  124. <th style="width:130px">data</th>
  125. </tr>
  126. </thead>
  127. <tbody>
  128. <?php if ($msgsTotal <= 0) : ?>
  129. <tr>
  130. <td colspan="3"><em class="text-muted" style="padding-left:60px;">Brak wiadomości</em></td>
  131. </tr>
  132. <?php else : ?>
  133. <?php foreach ($msgsList as $msg) : ?>
  134. <?php
  135. $onClick = '';
  136. $msgLink = Request::getPathUri() . 'index.php?_route=TableMsgs&id=' . $msg['_raw']->ID;
  137. if ('read' == $actionTask || 'view' == $actionTask) {
  138. $msgLink .= '&_task=' . $actionTask;
  139. } else {
  140. $msgLink = null;
  141. }
  142. if ($msgLink) {
  143. $jsOnClick = "window.location.href='{$msgLink}'";
  144. $onClick = 'onclick="' . $jsOnClick . '"';
  145. }
  146. ?>
  147. <tr <?php echo $onClick; ?>
  148. class="tblMsgsListItem <?php echo ($msg['_read'])? 'active' : ''; ?>">
  149. <td><?php echo $msg['_raw']->ID; ?></td>
  150. <td>
  151. <div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;"><?php echo htmlspecialchars($msg['message']); ?></div>
  152. <div class="text-muted" style="font-style:italic;">
  153. od <?php echo $msg['_raw']->A_RECORD_CREATE_AUTHOR; ?> do <?php
  154. if ('everyone' == $msg['_raw']->userTargetType) {
  155. echo "wszystkich";
  156. } else if ('user' == $msg['_raw']->userTargetType) {
  157. echo "{$msg['_raw']->userTargetName}";
  158. } else if ('group' == $msg['_raw']->userTargetType) {
  159. echo "grupy {$msg['_raw']->userTargetName}";
  160. }
  161. ?>
  162. </div>
  163. </td>
  164. <td style="white-space:nowrap;"><?php echo $msg['_raw']->A_RECORD_CREATE_DATE; ?></td>
  165. </tr>
  166. <?php endforeach; ?>
  167. <?php endif; ?>
  168. </tbody>
  169. </table>
  170. <?php
  171. }
  172. public function _getMsgs($tableName, $idRow) {
  173. $msgsRoute = Router::getRoute('Msgs');
  174. $msgsList = $msgsRoute->getMessagesForTableRecord($tableName, $idRow);
  175. foreach ($msgsList as $ind => $msg) {
  176. $msgsList[$ind]['_read'] = ('WAITING' != $msg['_raw']->A_STATUS);
  177. }
  178. return $msgsList;
  179. }
  180. public function _getSentMsgs($tableName, $idRow) {
  181. $msgsRoute = Router::getRoute('Msgs');
  182. $msgsList = $msgsRoute->getSentMessagesForTableRecord($tableName, $idRow);
  183. foreach ($msgsList as $ind => $msg) {
  184. $msgsList[$ind]['_read'] = ('WAITING' != $msg['_raw']->A_STATUS);
  185. }
  186. return $msgsList;
  187. }
  188. public function _validate($args) {
  189. $toType = V::get('to_type', '', $args);
  190. $to = V::get('to', '', $args);
  191. $msg = V::get('msg', '', $args);
  192. if (!in_array($toType, array('everyone', 'user', 'group'))) {
  193. throw new Exception("Niedozwolony typ odbiorcy");
  194. }
  195. if (empty($to) && 'everyone' != $toType) {
  196. throw new Exception("Proszę podać odbiorcę wiadomości");
  197. }
  198. if (empty($msg)) {
  199. throw new Exception("Proszę podać treść wiadomości");
  200. }
  201. }
  202. public function _create($args, $tableName, $idRow) {
  203. $toType = V::get('to_type', '', $args);
  204. $to = V::get('to', '', $args);
  205. $msg = V::get('msg', '', $args);
  206. $usrLogin = User::getLogin();
  207. $db = DB::getDB();
  208. if (!$db) throw new Exception("Brak dazy danych!");
  209. if ($db->has_errors()) throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  210. $item = array();
  211. $item['`uiTargetType`'] = "'default_db_table_record'";
  212. $item['`uiTargetName`'] = "'{$tableName}.{$idRow}'";
  213. $item['`userTargetType`'] = "'{$toType}'";
  214. $item['`userTargetName`'] = "'{$to}'";
  215. $item['`msg`'] = "'" . $db->_($msg) . "'";
  216. $item['`A_RECORD_CREATE_DATE`'] = "NOW()";
  217. $item['`A_RECORD_CREATE_AUTHOR`'] = "'{$usrLogin}'";
  218. $item['`A_STATUS`'] = "'WAITING'";
  219. $item['`app_className`'] = "'TableMsgs'";
  220. $sql = "insert into `CRM_UI_MSGS` (" . implode(",", array_keys($item)) . ")
  221. values (" . implode(",", array_values($item)) . ")
  222. ";
  223. $res = $db->query($sql);
  224. 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()));
  225. $createdId = $db->insert_id();
  226. if ($createdId <= 0) throw new Exception("Nie udało się zapisać wiadomości.");
  227. return $createdId;
  228. }
  229. public function _printMsgForm($args) {
  230. $toType = V::get('to_type', '', $args);
  231. $to = V::get('to', '', $args);
  232. $msg = V::get('msg', '', $args);
  233. $listTo = array();
  234. $listTo['everyone'] = 'Wszyscy';
  235. $listTo['user'] = 'Użytkownik';
  236. //$listTo['group'] = 'Grupa';
  237. $toType = (array_key_exists($toType, $listTo))? $toType : 'everyone';
  238. $typeSpecialGroupId = TypespecialVariable::getInstance(-1, '__ZASOB');
  239. $typeSpecialUserLogin = TypespecialVariable::getInstance(-1, '__USER_LOGIN');
  240. $selectedLogin = ('user' == $toType)? $to : '';
  241. $selectedGroupId = ('group' == $toType)? $to : '';
  242. ?>
  243. <form class="form-horizontal" action="" method="post">
  244. <div class="form-group">
  245. <label class="col-sm-2 control-label" for="to">Do:</label>
  246. <div class="col-sm-3">
  247. <select name="to_type" class="form-control" onChange="return selectTblMsgsToType(this);">
  248. <?php foreach ($listTo as $type => $typeLabel) : ?>
  249. <option <?php echo ($type == $toType)? 'selected' : ''; ?>
  250. value="<?php echo $type; ?>"><?php echo $typeLabel; ?></option>
  251. <?php endforeach; ?>
  252. </select>
  253. </div>
  254. <div class="col-sm-7">
  255. <div id="tblMsgsTo-everyone" style="<?php echo ('everyone' == $toType)? '' : 'display:none'; ?>">
  256. <input name="to-everyone" type="text" class="form-control" disabled>
  257. </div>
  258. <div id="tblMsgsTo-group" style="<?php echo ('group' == $toType)? '' : 'display:none'; ?>">
  259. <?php if ($typeSpecialGroupId) : ?>
  260. <?php
  261. $fldName = 'to-group';
  262. $fldParams = array();
  263. $fldParams['allowCreate'] = false;
  264. $fldParams['ajaxDataUrlBase'] = "index.php?_route=TableMsgs&_task=typeSpecialGroupId";
  265. $fldParams['placeholder'] = 'Grupa...';
  266. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  267. echo $typeSpecialUserLogin->showFormItem($tblID = -1, $fldName, $selectedGroupId, $fldParams);
  268. ?>
  269. <?php else : ?>
  270. <input name="to-group" type="text" class="form-control" placeholder="Grupa">
  271. <?php endif; ?>
  272. </div>
  273. <div id="tblMsgsTo-user" style="<?php echo ('user' == $toType)? '' : 'display:none'; ?>">
  274. <?php if ($typeSpecialUserLogin) : ?>
  275. <?php
  276. $fldName = 'to-user';
  277. $fldParams = array();
  278. $fldParams['allowCreate'] = false;
  279. $fldParams['ajaxDataUrlBase'] = "index.php?_route=TableMsgs&_task=typeSpecialUserLogin";
  280. $fldParams['placeholder'] = 'Użytkownik...';
  281. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  282. echo $typeSpecialUserLogin->showFormItem($tblID = -1, $fldName, $selectedLogin, $fldParams);
  283. ?>
  284. <?php else : ?>
  285. <input name="to-user" type="text" class="form-control" placeholder="Użytkownik">
  286. <?php endif; ?>
  287. </div>
  288. </div>
  289. </div>
  290. <div class="form-group">
  291. <label for="to" class="col-sm-2 control-label">Wiadomość:</label>
  292. <div class="col-sm-10">
  293. <textarea name="msg" class="form-control"><?php echo htmlspecialchars($msg); ?></textarea>
  294. </div>
  295. </div>
  296. <div class="form-group">
  297. <div class="col-sm-10 col-sm-offset-2">
  298. <input class="btn btn-primary" type="submit" value="Wyślij">
  299. </div>
  300. </div>
  301. </form>
  302. <script>
  303. function selectTblMsgsToType(n) {
  304. var toTypes = <?php echo json_encode(array_keys($listTo)); ?>,
  305. selectedType = n.value
  306. ;
  307. if (-1 !== toTypes.indexOf(n.value)) {
  308. toTypes.forEach(function(type) {
  309. if (type == selectedType) {
  310. document.getElementById('tblMsgsTo-' + type).style.display = 'block';
  311. } else {
  312. document.getElementById('tblMsgsTo-' + type).style.display = 'none';
  313. }
  314. });
  315. }
  316. }
  317. </script>
  318. <?php
  319. }
  320. public function typeSpecialUserLoginAction() {
  321. header("Content-type: application/json");
  322. $typeSpecialUserId = TypespecialVariable::getInstance(-1, '__USER_LOGIN');
  323. if (!$typeSpecialUserId) {
  324. $jsonData = new stdClass();
  325. $jsonData->message = "TypeSpecial '__USER_LOGIN' not exists";
  326. echo json_encode($jsonData);
  327. exit;
  328. }
  329. $query = V::get('q', '', $_REQUEST);
  330. $rawRows = null;
  331. $jsonData = array();
  332. $queryParams = array();
  333. $rows = $typeSpecialUserId->getValuesWithExports($query, $queryParams);
  334. foreach ($rows as $kID => $vItem) {
  335. $itemJson = new stdClass();
  336. $itemJson->id = $vItem->id;
  337. $itemJson->name = $vItem->param_out;
  338. if (!empty($vItem->exports)) {
  339. $itemJson->exports = $vItem->exports;
  340. }
  341. $jsonData[] = $itemJson;
  342. }
  343. echo json_encode($jsonData);
  344. }
  345. public function typeSpecialGroupIdAction() {
  346. header("Content-type: application/json");
  347. Lib::loadClass('TypespecialVariable');
  348. $typeSpecialZasob = TypespecialVariable::getInstance(-1, '__ZASOB');
  349. if (!$typeSpecialZasob) {
  350. $jsonData = new stdClass();
  351. $jsonData->message = "TypeSpecial '__ZASOB' not exists";
  352. echo json_encode($jsonData);
  353. exit;
  354. }
  355. $query = V::get('q', '', $_REQUEST);
  356. $rawRows = null;
  357. $jsonData = array();
  358. $queryParams = array();
  359. $queryParams['zasob_type_in'] = array('STANOWISKO', 'PODMIOT', 'DZIAL');
  360. $rows = $typeSpecialZasob->getValuesWithExports($query, $queryParams);
  361. DBG::_('DBG_TS', '>1', "rows({$query})", $rows, __CLASS__, __FUNCTION__, __LINE__);
  362. foreach ($rows as $kID => $vItem) {
  363. $itemJson = new stdClass();
  364. $itemJson->id = $vItem->id;
  365. $itemJson->name = $vItem->param_out;
  366. if (!empty($vItem->exports)) {
  367. $itemJson->exports = $vItem->exports;
  368. }
  369. $jsonData[] = $itemJson;
  370. }
  371. echo json_encode($jsonData);
  372. }
  373. public function readAction() {
  374. $msgId = V::get('id', 0, $_GET, 'int');
  375. if ($msgId <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  376. SE_Layout::gora();
  377. SE_Layout::menu();
  378. try {
  379. $msg = $this->_getMsg($msgId);
  380. $this->_markAsRead($msg);
  381. $this->tableRowMsg($msg);
  382. } catch (Exception $e) {
  383. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  384. }
  385. SE_Layout::dol();
  386. }
  387. public function viewAction() {
  388. $msgId = V::get('id', 0, $_GET, 'int');
  389. if ($msgId <= 0) throw new HttpException("Wiadomość nie istnieje!", 404);
  390. SE_Layout::gora();
  391. SE_Layout::menu();
  392. try {
  393. $msg = $this->_getMsg($msgId);
  394. $this->tableRowMsg($msg);
  395. } catch (Exception $e) {
  396. SE_Layout::alert('danger', $e->getMessage() . ' #' . $e->getLine());
  397. }
  398. SE_Layout::dol();
  399. }
  400. public function _getMsg($msgId) {
  401. $msgsRoute = Router::getRoute('Msgs');
  402. $msg['_raw'] = $msgsRoute->getMessage($msgId);
  403. $msg['message'] = $msg['_raw']->msg;
  404. $msg['type'] = $msg['_raw']->msgType;
  405. $msg['_read'] = ('WAITING' != $msg['_raw']->A_STATUS);
  406. // $msg['_raw']->uiTargetType => default_db_table_record
  407. // $msg['_raw']->uiTargetName => TEST_PERMS.31
  408. if ('default_db_table_record' !== $msg['_raw']->uiTargetType) {
  409. throw new Exception("Parse message target type error!");
  410. }
  411. $parts = explode('.', $msg['_raw']->uiTargetName);
  412. if (2 !== count($parts)) throw new Exception("Parse message target type error!");
  413. $msg['tblName'] = $parts[0];
  414. $msg['idRow'] = $parts[1];
  415. if (!is_numeric($msg['idRow'])) throw new Exception("Parse message target type - id row error!");
  416. return $msg;
  417. }
  418. public function _markAsRead($msg) {
  419. if ($msg['_read']) return;
  420. $usrLogin = User::getLogin();
  421. $db = DB::getDB();
  422. if (!$db) throw new Exception("Brak dazy danych!");
  423. if ($db->has_errors()) throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  424. $sql = "update `CRM_UI_MSGS`
  425. set `A_STATUS`='NORMAL'
  426. , `A_RECORD_UPDATE_AUTHOR`='{$usrLogin}'
  427. , `A_RECORD_UPDATE_DATE`=NOW()
  428. where `ID`='{$msg['_raw']->ID}'
  429. ";
  430. $res = $db->query($sql);
  431. 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()));
  432. }
  433. public function tableRowMsg($msg) {
  434. $idTable = ProcesHelper::getZasobTableID($msg['tblName']);
  435. $usrAcl = User::getAcl();
  436. $tblAcl = $usrAcl->getTableAcl($idTable);
  437. $idRow = $msg['idRow'];
  438. ?>
  439. <div class="container">
  440. <h3><i class="glyphicon glyphicon-envelope"></i> <a href="index.php?_route=TableMsgs&_task=tableRow&idTable=<?php echo $idTable; ?>&idRow=<?php echo $idRow; ?>">Wiadomości powiązane z rekordem nr <?php echo $idRow; ?></a>
  441. &raquo; Wiadomość nr <code><?php echo $msg['_raw']->ID; ?></code>
  442. <br><small>z tabeli <a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=13051">Test permy</a></small>
  443. </h3>
  444. <div class="panel panel-<?php echo $msg['type']; ?>">
  445. <div class="panel-heading">
  446. <h3 class="panel-title">Wiadomość wysłana przez <?php echo $msg['_raw']->A_RECORD_CREATE_AUTHOR; ?>
  447. <span class="pull-right"><?php echo $msg['_raw']->A_RECORD_CREATE_DATE; ?></span></h3>
  448. </div>
  449. <div class="panel-body">
  450. <?php echo htmlspecialchars($msg['message']); ?>
  451. </div>
  452. </div>
  453. </div>
  454. <?php
  455. // TODO: odpisz
  456. // TODO: do kosza - add trigger to insert into `CRM_UI_MSGS__TRASH` after DELETE on `CRM_UI_MSGS`
  457. }
  458. }