Msgs.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. /*
  4. ## Flash message system:
  5. System for automatic and custom messages for users.
  6. - `app_className` - for automatic msgs to search for msg text
  7. - `msg` - msg to show or to parse by `app_className`
  8. - `uiTargetType` - where to show this msg eg. 'default_db_table', 'after_login', 'everywhere'
  9. - `uiTargetName` - eg. database table name (if from default_db)
  10. - `userTargetType` - type of users allowed to see this msg eg. 'everyone', 'admin' (ADMIN_LEVEL=0?), 'user', 'group'
  11. - `userTargetName` - login, group name
  12. - `actionExecuted` - execution time if msg require user to run specific action (send msg.id in request)
  13. - `actionNotes` - notes/msgs/dbg for user actions executed from this msg
  14. Messages created by db triggers must define `app_className` that should parse `msg`. For example to use in FixProjectPath and FixZasobPath to keep correct paths.
  15. */
  16. class Route_Msgs extends RouteBase {
  17. public function handleAuth() {
  18. if (!User::logged()) {
  19. throw new HttpException('Unauthorized', 401);
  20. }
  21. }
  22. public function defaultAction() {
  23. SE_Layout::gora();
  24. ?>
  25. <div class="container">
  26. <h1>Messages system</h1>
  27. ...
  28. </div>
  29. <?php
  30. SE_Layout::dol();
  31. }
  32. public function reinstallAction() {
  33. $this->reinstall();
  34. die('OK');
  35. }
  36. public function runAction() {
  37. $msgId = V::get('_msgId', 0, $_REQUEST, 'int');
  38. if ($msgId > 0) {
  39. $this->runByMessageId($msgId);
  40. }
  41. die('OK');
  42. }
  43. public function addTestMsgAction() {
  44. $sql = "INSERT INTO `CRM_UI_MSGS` (`ID`, `app_className`, `msg`, `msgType`, `uiTargetType`, `uiTargetName`, `userTargetType`, `userTargetName`, `actionExecutedTime`, `actionNotes`, `A_RECORD_CREATE_DATE`, `A_RECORD_CREATE_AUTHOR`, `A_RECORD_UPDATE_DATE`, `A_RECORD_UPDATE_AUTHOR`)
  45. VALUES (NULL, 'FixZasobPath', 'Update all paths', 'danger', 'default_db_table', 'CRM_LISTA_ZASOBOW', 'user', 'plabudda', NULL, '', NULL, 'plabudda', NULL, '')";
  46. $db = DB::getDB();
  47. $db->query($sql);
  48. die('OK');
  49. }
  50. public function reinstall() {
  51. $sqlList = array();
  52. $sqlList['RemoveTable'] = "DROP TABLE IF EXISTS `CRM_UI_MSGS`";
  53. $sqlList['InstallTable'] = "
  54. CREATE TABLE IF NOT EXISTS `CRM_UI_MSGS` (
  55. `ID` int(11) NOT NULL AUTO_INCREMENT
  56. -- app_className - for automatic msgs to search for msg text
  57. , `app_className` varchar(255) DEFAULT NULL
  58. -- msg - msg to show or to parse by app_className
  59. , `msg` varchar(255) NOT NULL
  60. , `msgType` enum('info','danger','warning','success') NOT NULL DEFAULT 'info'
  61. -- uiTargetType - where to show this msg eg. 'default_db_table', 'after_login', 'everywhere'
  62. , `uiTargetType` varchar(255) NOT NULL DEFAULT ''
  63. -- uiTargetName - eg. database table name (if from default_db)
  64. , `uiTargetName` varchar(255) NOT NULL DEFAULT ''
  65. -- userTargetType - type of users allowed to see this msg eg. 'everyone', 'admin' (ADMIN_LEVEL=0?), 'user', 'group'
  66. , `userTargetType` enum('none','everyone','admin','user','group') NOT NULL DEFAULT 'none'
  67. -- userTargetName - login, group name
  68. , `userTargetName` varchar(255) NOT NULL DEFAULT ''
  69. -- actionExecutedTime - execution time if msg require user to run specific action (send msg.id in request)
  70. , `actionExecutedTime` datetime DEFAULT NULL
  71. -- actionNotes - notes/msgs/dbg for user actions executed from this msg
  72. , `actionNotes` varchar(255) NOT NULL DEFAULT ''
  73. , `A_STATUS` enum('WAITING','NORMAL','OFF_HARD','DELETED') NOT NULL DEFAULT 'WAITING'
  74. , `A_RECORD_CREATE_DATE` datetime DEFAULT NULL
  75. , `A_RECORD_CREATE_AUTHOR` varchar(40) NOT NULL DEFAULT ''
  76. , `A_RECORD_UPDATE_DATE` datetime DEFAULT NULL
  77. , `A_RECORD_UPDATE_AUTHOR` varchar(40) NOT NULL DEFAULT ''
  78. , PRIMARY KEY (`ID`)
  79. , KEY `app_className` (`app_className`)
  80. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  81. ";
  82. $db = DB::getDB();
  83. if ($db->has_errors()) {
  84. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  85. }
  86. foreach ($sqlList as $sqlName => $sql) {
  87. $res = $db->query($sql);
  88. if ($db->has_errors()) {
  89. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  90. }
  91. }
  92. }
  93. public function getActiveMessagesForTable($tblName) {
  94. if (empty($tblName)) return;
  95. $db = DB::getDB();
  96. $tblName = $db->_($tblName);
  97. $usrLogin = User::getLogin();
  98. $msgs = null;
  99. $sql = "select m.*
  100. from `CRM_UI_MSGS` m
  101. where m.`uiTargetType`='default_db_table'
  102. and m.`A_STATUS`='WAITING'
  103. and m.`uiTargetName`='{$tblName}'
  104. and (m.`userTargetType` in('everyone')
  105. or (m.`userTargetType`='user' and m.`userTargetName`='{$usrLogin}')
  106. -- TODO: use 'admin', 'group'
  107. )
  108. ";
  109. $db = DB::getDB();
  110. $res = $db->query($sql);
  111. while ($r = $db->fetch($res)) {
  112. if ($msg = $this->parseMessage($r)) {
  113. $msg['link'] = 'index.php?_route=Msgs&_task=run&_msgId=' . $r->ID;
  114. $msgs[$r->ID] = $msg;
  115. }
  116. }
  117. return $msgs;
  118. }
  119. public function parseMessage($r) {
  120. $msg = null;// ['type'=>'info', 'message'=>'...']
  121. // $r->app_className - for automatic msgs to search for msg text
  122. // $r->msg - for automatic msgs to search for msg text
  123. // $r->msgType - 'info','danger','warning','success'
  124. if (!empty($r->app_className)) {
  125. $route = Router::getRoute($r->app_className);
  126. $msg = array();
  127. $msg['message'] = $route->parseMessageFromMsgsSystem($r->msg);
  128. $msg['type'] = $r->msgType;
  129. } else {
  130. $msg = array();
  131. $msg['message'] = $this->parseMessageFromMsgsSystem($r->msg);
  132. $msg['type'] = $r->msgType;
  133. }
  134. return $msg;
  135. }
  136. public function parseMessageFromMsgsSystem($msg) {
  137. return $msg;
  138. }
  139. public function runByMessageId($id) {
  140. $msgRow = $this->getActiveMessage($id);
  141. $execNotes = '';
  142. if (!empty($msgRow->app_className)) {
  143. $route = Router::getRoute($msgRow->app_className);
  144. $route->runByMessageFromMsgsSystem($msgRow->msg, $execNotes);
  145. }
  146. //$this->finishMessage($id, $execNotes);
  147. }
  148. public function getMessage($id) {
  149. if (empty($id)) return;
  150. $id = intval($id);
  151. if ($id <= 0) return;
  152. $msg = null;
  153. $sql = "select * from `CRM_UI_MSGS` where `ID`='{$id}' ";
  154. $db = DB::getDB();
  155. $res = $db->query($sql);
  156. if ($r = $db->fetch($res)) {
  157. $msg = $r;
  158. }
  159. return $msg;
  160. }
  161. public function getActiveMessage($id) {
  162. if (empty($id)) return;
  163. $id = intval($id);
  164. if ($id <= 0) return;
  165. $msg = null;
  166. $sql = "select m.*
  167. from `CRM_UI_MSGS` m
  168. where m.`ID`='{$id}'
  169. and m.`A_STATUS`='WAITING'
  170. ";
  171. $db = DB::getDB();
  172. $res = $db->query($sql);
  173. if ($r = $db->fetch($res)) {
  174. $msg = $r;
  175. }
  176. if (!$msg) {
  177. throw new HttpException("Message not found", 404);
  178. }
  179. return $msg;
  180. }
  181. public function finishMessage($id, $execNotes) {
  182. if (empty($id)) return;
  183. $id = intval($id);
  184. if ($id <= 0) return;
  185. $usrLogin = User::getLogin();
  186. $db = DB::getDB();
  187. $execNotes = $db->_($execNotes);
  188. $sql = "update `CRM_UI_MSGS`
  189. set `A_STATUS`='OFF_HARD'
  190. , `actionExecutedTime`=NOW()
  191. , `actionNotes`='{$execNotes}'
  192. , `A_RECORD_UPDATE_DATE`=NOW()
  193. , `A_RECORD_UPDATE_AUTHOR`='{$usrLogin}'
  194. where `ID`='{$id}'
  195. ";
  196. $db->query($sql);
  197. return;
  198. }
  199. public function removeMessage($id) {
  200. if (empty($id)) return;
  201. $id = intval($id);
  202. if ($id <= 0) return;
  203. $sql = "update `CRM_UI_MSGS` set `A_STATUS`='DELETED' where `ID`='{$id}' ";
  204. $db = DB::getDB();
  205. $db->query($sql);
  206. }
  207. }