TableMsgs.php 17 KB

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