فهرست منبع

Add Msgs system #113

Piotr Labudda 11 سال پیش
والد
کامیت
fe932a65f8
6فایلهای تغییر یافته به همراه315 افزوده شده و 0 حذف شده
  1. 50 0
      SE/se-lib/Route/FixZasobPath.php
  2. 232 0
      SE/se-lib/Route/Msgs.php
  3. 7 0
      SE/se-lib/RouteBase.php
  4. 10 0
      SE/se-lib/SE_Layout.php
  5. 4 0
      SE/se-lib/TableAjax.php
  6. 12 0
      SE/se-lib/tmpl/msgsForTable.php

+ 50 - 0
SE/se-lib/Route/FixZasobPath.php

@@ -170,6 +170,17 @@ jQuery(document).ready(function () {
 					set z.`path`=i.`idx_PATH_DESC`;
 				update `CRM_WSKAZNIK` wsk left join `_zasob_path_idx` i on(i.`ID`=wsk.`ID_ZASOB`)
 					set wsk.`path_CRM_LISTA_ZASOBOW`=coalesce(i.`idx_PATH_DESC`, '?');
+
+				update `CRM_UI_MSGS`
+				set `A_STATUS`='OFF_HARD'
+					, `actionExecutedTime`=NOW()
+					, `A_RECORD_UPDATE_DATE`=NOW()
+					, `A_RECORD_UPDATE_AUTHOR`='update_zasob_path_idx_rec'
+				where `A_STATUS`='WAITING'
+					and `app_className`='FixZasobPath'
+					and `msg`='Update all paths'
+					and `uiTargetType`='default_db_table'
+					and `uiTargetName`='CRM_LISTA_ZASOBOW';
 			END
 		";
 		$sqlList['RemoveTrigger_BeforeInsertZasob'] = "DROP TRIGGER IF EXISTS `_CRM_LISTA_ZASOBOW_tree_INSERT`";
@@ -290,6 +301,24 @@ jQuery(document).ready(function () {
 					IF NEW.`PARENT_ID`!=OLD.`PARENT_ID` or NEW.`DESC`!=OLD.`DESC` THEN
 						-- throw warning to update all pathes in `CRM_LISTA_ZASOBOW` with path under current?
 						IF (select count(1) from `CRM_LISTA_ZASOBOW` p where p.`PARENT_ID`=NEW.ID) > 0 THEN
+							IF (select count(1) from `CRM_UI_MSGS` where `app_className`='FixZasobPath'
+										and `msg`='Update all paths'
+										and `uiTargetType`='default_db_table'
+										and `uiTargetName`='CRM_LISTA_ZASOBOW'
+										and `A_STATUS`='WAITING'
+									) = 0 THEN
+								INSERT INTO `CRM_UI_MSGS` (`ID`
+										, `app_className`, `msg`, `msgType`
+										, `uiTargetType`, `uiTargetName`
+										, `userTargetType`
+										, `A_RECORD_CREATE_DATE`, `A_RECORD_CREATE_AUTHOR`)
+									VALUES (NULL
+										, 'FixZasobPath', 'Update all paths', 'danger'
+										, 'default_db_table', 'CRM_LISTA_ZASOBOW'
+										, 'everyone'
+										, NOW(), 'plabudda' -- TODO: NEW.A_RECORD_UPDATE_AUTHOR
+									);
+							END IF;
 							UPDATE `P5-MSG:Route_FixZasobPath:WARNING: Update all paths` SET x=1;
 						END IF;
 					END IF;
@@ -371,6 +400,27 @@ jQuery(document).ready(function () {
 		return $msg;
 	}
 
+	public function parseMessageFromMsgsSystem($msg) {
+		switch ($msg) {
+			case 'Update all paths': {
+				$msg = "Zaktualizuj ścieżki zasobów";
+				break;
+			}
+		}
+		return $msg;
+	}
+
+	public function runByMessageFromMsgsSystem($msg, &$execNotes) {
+		switch ($msg) {
+			case 'Update all paths': {
+				$execNotes .= 'call procedure... ';
+				$this->_callProcedure();
+				$execNotes .= ' done';
+				break;
+			}
+		}
+	}
+
 }
 
 /**

+ 232 - 0
SE/se-lib/Route/Msgs.php

@@ -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);
+	}
+
+}

+ 7 - 0
SE/se-lib/RouteBase.php

@@ -47,6 +47,13 @@ class RouteBase {
 		return $msg;
 	}
 
+	public function parseMessageFromMsgsSystem($msg) {
+		return $msg;
+	}
+
+	public function runByMessageFromMsgsSystem($msg, & $execNotes) {
+	}
+
 	public function handleAuth() {
 		User::authByRequest();
 	}

+ 10 - 0
SE/se-lib/SE_Layout.php

@@ -64,4 +64,14 @@ class SE_Layout {
 		}
 	}
 
+	public static function showMessagesForTable($tblName) {
+		if (empty($tblName)) return;
+
+		$msgsRoute = Router::getRoute('Msgs');
+		$msgs = $msgsRoute->getActiveMessagesForTable($tblName);
+		if (!empty($msgs)) {
+			self::loadTemplate('msgsForTable', array('msgs' => $msgs));
+		}
+	}
+
 }

+ 4 - 0
SE/se-lib/TableAjax.php

@@ -255,6 +255,10 @@ class TableAjax extends ViewAjax {
 		ob_start();
 		?>
 
+		<div class="container">
+			<?php SE_Layout::showMessagesForTable($this->_tbl); ?>
+		</div>
+
 		<?php if ($tblAjaxMap) $tblAjaxMap->printCSS(); ?>
 		<?php if ($tblAjaxMap) $tblAjaxMap->printJS(); ?>
 		<script src="stuff/jquery-ui-1.10.4.custom.min.js"></script>

+ 12 - 0
SE/se-lib/tmpl/msgsForTable.php

@@ -0,0 +1,12 @@
+<div>
+
+	<?php if (!empty($msgs)) : ?>
+		<?php foreach ($msgs as $msgId => $msg) : ?>
+			<div class="alert alert-<?php echo V::get('type', 'info', $msg); ?>">
+				<?php echo $msg['message']; ?>
+				 <a class="btn btn-primary btn-xs" target="_blank" href="<?php echo $msg['link']; ?>">uruchom</a>
+			</div>
+		<?php endforeach; ?>
+	<?php endif; ?>
+
+</div>