|
|
@@ -0,0 +1,232 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+Lib::loadClass('RouteBase');
|
|
|
+
|
|
|
+/*
|
|
|
+ ## Flash message system:
|
|
|
+
|
|
|
+ System for automatic and custom messages for users.
|
|
|
+
|
|
|
+ - `app_className` - for automatic msgs to search for msg text
|
|
|
+ - `msg` - msg to show or to parse by `app_className`
|
|
|
+ - `uiTargetType` - where to show this msg eg. 'default_db_table', 'after_login', 'everywhere'
|
|
|
+ - `uiTargetName` - eg. database table name (if from default_db)
|
|
|
+ - `userTargetType` - type of users allowed to see this msg eg. 'everyone', 'admin' (ADMIN_LEVEL=0?), 'user', 'group'
|
|
|
+ - `userTargetName` - login, group name
|
|
|
+ - `actionExecuted` - execution time if msg require user to run specific action (send msg.id in request)
|
|
|
+ - `actionNotes` - notes/msgs/dbg for user actions executed from this msg
|
|
|
+
|
|
|
+ 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.
|
|
|
+*/
|
|
|
+class Route_Msgs extends RouteBase {
|
|
|
+
|
|
|
+ public function handleAuth() {
|
|
|
+ if (!User::logged()) {
|
|
|
+ throw new HttpException('Unauthorized', 401);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function defaultAction() {
|
|
|
+ SE_Layout::gora();
|
|
|
+ ?>
|
|
|
+<div class="container">
|
|
|
+ <h1>Messages system</h1>
|
|
|
+ ...
|
|
|
+</div>
|
|
|
+ <?php
|
|
|
+ SE_Layout::dol();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function reinstallAction() {
|
|
|
+ $this->reinstall();
|
|
|
+ die('OK');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function runAction() {
|
|
|
+ $msgId = V::get('_msgId', 0, $_REQUEST, 'int');
|
|
|
+ if ($msgId > 0) {
|
|
|
+ $this->runByMessageId($msgId);
|
|
|
+ }
|
|
|
+ die('OK');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function addTestMsgAction() {
|
|
|
+ $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`)
|
|
|
+ VALUES (NULL, 'FixZasobPath', 'Update all paths', 'danger', 'default_db_table', 'CRM_LISTA_ZASOBOW', 'user', 'plabudda', NULL, '', NULL, 'plabudda', NULL, '')";
|
|
|
+ $db = DB::getDB();
|
|
|
+ $db->query($sql);
|
|
|
+ die('OK');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function reinstall() {
|
|
|
+ $sqlList = array();
|
|
|
+ $sqlList['RemoveTable'] = "DROP TABLE IF EXISTS `CRM_UI_MSGS`";
|
|
|
+ $sqlList['InstallTable'] = "
|
|
|
+ CREATE TABLE IF NOT EXISTS `CRM_UI_MSGS` (
|
|
|
+ `ID` int(11) NOT NULL AUTO_INCREMENT
|
|
|
+ -- app_className - for automatic msgs to search for msg text
|
|
|
+ , `app_className` varchar(255) DEFAULT NULL
|
|
|
+ -- msg - msg to show or to parse by app_className
|
|
|
+ , `msg` varchar(255) NOT NULL
|
|
|
+ , `msgType` enum('info','danger','warning','success') NOT NULL DEFAULT 'info'
|
|
|
+ -- uiTargetType - where to show this msg eg. 'default_db_table', 'after_login', 'everywhere'
|
|
|
+ , `uiTargetType` varchar(255) NOT NULL DEFAULT ''
|
|
|
+ -- uiTargetName - eg. database table name (if from default_db)
|
|
|
+ , `uiTargetName` varchar(255) NOT NULL DEFAULT ''
|
|
|
+ -- userTargetType - type of users allowed to see this msg eg. 'everyone', 'admin' (ADMIN_LEVEL=0?), 'user', 'group'
|
|
|
+ , `userTargetType` enum('none','everyone','admin','user','group') NOT NULL DEFAULT 'none'
|
|
|
+ -- userTargetName - login, group name
|
|
|
+ , `userTargetName` varchar(255) NOT NULL DEFAULT ''
|
|
|
+ -- actionExecutedTime - execution time if msg require user to run specific action (send msg.id in request)
|
|
|
+ , `actionExecutedTime` datetime DEFAULT NULL
|
|
|
+ -- actionNotes - notes/msgs/dbg for user actions executed from this msg
|
|
|
+ , `actionNotes` varchar(255) NOT NULL DEFAULT ''
|
|
|
+ , `A_STATUS` enum('WAITING','NORMAL','OFF_HARD','DELETED') NOT NULL DEFAULT 'WAITING'
|
|
|
+ , `A_RECORD_CREATE_DATE` datetime DEFAULT NULL
|
|
|
+ , `A_RECORD_CREATE_AUTHOR` varchar(40) NOT NULL DEFAULT ''
|
|
|
+ , `A_RECORD_UPDATE_DATE` datetime DEFAULT NULL
|
|
|
+ , `A_RECORD_UPDATE_AUTHOR` varchar(40) NOT NULL DEFAULT ''
|
|
|
+ , PRIMARY KEY (`ID`)
|
|
|
+ , KEY `app_className` (`app_className`)
|
|
|
+ ) ENGINE=MyISAM DEFAULT CHARSET=latin2
|
|
|
+ ";
|
|
|
+ $db = DB::getDB();
|
|
|
+ if ($db->has_errors()) {
|
|
|
+ throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
|
|
|
+ }
|
|
|
+ foreach ($sqlList as $sqlName => $sql) {
|
|
|
+ $res = $db->query($sql);
|
|
|
+ if ($db->has_errors()) {
|
|
|
+ throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getActiveMessagesForTable($tblName) {
|
|
|
+ if (empty($tblName)) return;
|
|
|
+ $db = DB::getDB();
|
|
|
+ $tblName = $db->_($tblName);
|
|
|
+
|
|
|
+ $usrLogin = User::getLogin();
|
|
|
+ $msgs = null;
|
|
|
+ $sql = "select m.*
|
|
|
+ from `CRM_UI_MSGS` m
|
|
|
+ where m.`uiTargetType`='default_db_table'
|
|
|
+ and m.`A_STATUS`='WAITING'
|
|
|
+ and m.`uiTargetName`='{$tblName}'
|
|
|
+ and (m.`userTargetType` in('everyone')
|
|
|
+ or (m.`userTargetType`='user' and m.`userTargetName`='{$usrLogin}')
|
|
|
+ -- TODO: use 'admin', 'group'
|
|
|
+ )
|
|
|
+ ";
|
|
|
+ $db = DB::getDB();
|
|
|
+ $res = $db->query($sql);
|
|
|
+ while ($r = $db->fetch($res)) {
|
|
|
+ if ($msg = $this->parseMessage($r)) {
|
|
|
+ $msg['link'] = 'index.php?_route=Msgs&_task=run&_msgId=' . $r->ID;
|
|
|
+ $msgs[$r->ID] = $msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $msgs;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function parseMessage($r) {
|
|
|
+ $msg = null;// ['type'=>'info', 'message'=>'...']
|
|
|
+ // $r->app_className - for automatic msgs to search for msg text
|
|
|
+ // $r->msg - for automatic msgs to search for msg text
|
|
|
+ // $r->msgType - 'info','danger','warning','success'
|
|
|
+ if (!empty($r->app_className)) {
|
|
|
+ $route = Router::getRoute($r->app_className);
|
|
|
+ $msg = array();
|
|
|
+ $msg['message'] = $route->parseMessageFromMsgsSystem($r->msg);
|
|
|
+ $msg['type'] = $r->msgType;
|
|
|
+ } else {
|
|
|
+ $msg = array();
|
|
|
+ $msg['message'] = $this->parseMessageFromMsgsSystem($r->msg);
|
|
|
+ $msg['type'] = $r->msgType;
|
|
|
+ }
|
|
|
+ return $msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function parseMessageFromMsgsSystem($msg) {
|
|
|
+ return $msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function runByMessageId($id) {
|
|
|
+ $msgRow = $this->getActiveMessage($id);
|
|
|
+ $execNotes = '';
|
|
|
+ if (!empty($msgRow->app_className)) {
|
|
|
+ $route = Router::getRoute($msgRow->app_className);
|
|
|
+ $route->runByMessageFromMsgsSystem($msgRow->msg, $execNotes);
|
|
|
+ }
|
|
|
+ //$this->finishMessage($id, $execNotes);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getMessage($id) {
|
|
|
+ if (empty($id)) return;
|
|
|
+ $id = intval($id);
|
|
|
+ if ($id <= 0) return;
|
|
|
+
|
|
|
+ $msg = null;
|
|
|
+ $sql = "select * from `CRM_UI_MSGS` where `ID`='{$id}' ";
|
|
|
+ $db = DB::getDB();
|
|
|
+ $res = $db->query($sql);
|
|
|
+ if ($r = $db->fetch($res)) {
|
|
|
+ $msg = $r;
|
|
|
+ }
|
|
|
+ return $msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getActiveMessage($id) {
|
|
|
+ if (empty($id)) return;
|
|
|
+ $id = intval($id);
|
|
|
+ if ($id <= 0) return;
|
|
|
+
|
|
|
+ $msg = null;
|
|
|
+ $sql = "select m.*
|
|
|
+ from `CRM_UI_MSGS` m
|
|
|
+ where m.`ID`='{$id}'
|
|
|
+ and m.`A_STATUS`='WAITING'
|
|
|
+ ";
|
|
|
+ $db = DB::getDB();
|
|
|
+ $res = $db->query($sql);
|
|
|
+ if ($r = $db->fetch($res)) {
|
|
|
+ $msg = $r;
|
|
|
+ }
|
|
|
+ if (!$msg) {
|
|
|
+ throw new HttpException("Message not found", 404);
|
|
|
+ }
|
|
|
+ return $msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function finishMessage($id, $execNotes) {
|
|
|
+ if (empty($id)) return;
|
|
|
+ $id = intval($id);
|
|
|
+ if ($id <= 0) return;
|
|
|
+
|
|
|
+ $usrLogin = User::getLogin();
|
|
|
+ $db = DB::getDB();
|
|
|
+ $execNotes = $db->_($execNotes);
|
|
|
+ $sql = "update `CRM_UI_MSGS`
|
|
|
+ set `A_STATUS`='OFF_HARD'
|
|
|
+ , `actionExecutedTime`=NOW()
|
|
|
+ , `actionNotes`='{$execNotes}'
|
|
|
+ , `A_RECORD_UPDATE_DATE`=NOW()
|
|
|
+ , `A_RECORD_UPDATE_AUTHOR`='{$usrLogin}'
|
|
|
+ where `ID`='{$id}'
|
|
|
+ ";
|
|
|
+ $db->query($sql);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function removeMessage($id) {
|
|
|
+ if (empty($id)) return;
|
|
|
+ $id = intval($id);
|
|
|
+ if ($id <= 0) return;
|
|
|
+
|
|
|
+ $sql = "update `CRM_UI_MSGS` set `A_STATUS`='DELETED' where `ID`='{$id}' ";
|
|
|
+ $db = DB::getDB();
|
|
|
+ $db->query($sql);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|