浏览代码

rm DB::getDataSource dependency from Msgs route

Piotr Labudda 6 年之前
父节点
当前提交
35d3c96d97
共有 2 个文件被更改,包括 60 次插入92 次删除
  1. 34 69
      SE/se-lib/Route/Msgs.php
  2. 26 23
      SE/se-lib/Route/UserMsgs.php

+ 34 - 69
SE/se-lib/Route/Msgs.php

@@ -26,13 +26,13 @@ Lib::loadClass('RouteBase');
 */
 class Route_Msgs extends RouteBase {
 
-	public function handleAuth() {
+	function handleAuth() {
 		if (!User::logged()) {
 			throw new HttpException('Unauthorized', 401);
 		}
 	}
 
-	public function defaultAction() {
+	function defaultAction() {
 		SE_Layout::gora();
 		?>
 <div class="container">
@@ -43,17 +43,17 @@ class Route_Msgs extends RouteBase {
 		SE_Layout::dol();
 	}
 
-	public function reinstallAction() {
+	function reinstallAction() {
 		$this->reinstall();
 		die('OK');
 	}
 
-	public function reinstallFunctionsAction() {
+	function reinstallFunctionsAction() {
 		$this->reinstallFunctions();
 		die('OK');
 	}
 
-	public function runAction() {
+	function runAction() {
 		$msgId = V::get('_msgId', 0, $_REQUEST, 'int');
 		if ($msgId > 0) {
 			$this->runByMessageId($msgId);
@@ -65,15 +65,7 @@ class Route_Msgs extends RouteBase {
 		exit;
 	}
 
-	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 reinstallFunctions() {
+	function reinstallFunctions() {
 		$sqlList = array();
 		//$sqlList['RemoveTable_CRM_UI_MSGS__EXEC_LOG'] = "DROP TABLE IF EXISTS `CRM_UI_MSGS__EXEC_LOG`";
 		$sqlList['CreateTable_CRM_UI_MSGS__EXEC_LOG'] = "
@@ -226,19 +218,12 @@ SQL_QUERY;
 		END IF;
 
 SQL_QUERY;
-		$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()));
-			}
+			DB::getPDO()->execSql($sql);
 		}
 	}
 
-	public function reinstall() {
+	function reinstall() {
 		$sqlList = array();
 		//$sqlList['RemoveTable'] = "DROP TABLE IF EXISTS `CRM_UI_MSGS`";// TODO: update struct (add cells, rm old keys) - not drop/create
 		$sqlList['InstallTable'] = "
@@ -274,20 +259,13 @@ SQL_QUERY;
 				, 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()));
-			}
+			DB::getPDO()->execSql($sql);
 		}
 		$this->reinstallFunctions();
 	}
 
-	public function getActiveMessagesForTable($tblName) {
+	function getActiveMessagesForTable($tblName) {
 		if (empty($tblName)) return;
 		$sqlTableName = DB::getPDO()->quote($tblName);
 		$usrLogin = User::getLogin();
@@ -314,7 +292,7 @@ SQL_QUERY;
 		return $msgs;
 	}
 
-	public function getActiveMessagesForTableRecord($tblName, $id) {
+	function getActiveMessagesForTableRecord($tblName, $id) {
 		if (empty($tblName)) return;
 		$usrLogin = User::getLogin();
 		$msgs = [];
@@ -343,7 +321,7 @@ SQL_QUERY;
 		return $msgs;
 	}
 
-	public function parseMessage($r) {
+	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
@@ -363,11 +341,11 @@ SQL_QUERY;
 		return $msg;
 	}
 
-	public function parseMessageFromMsgsSystem($msg) {
+	function parseMessageFromMsgsSystem($msg) {
 		return $msg;
 	}
 
-	public function runByMessageId($id) {
+	function runByMessageId($id) {
 		$msgRow = $this->getActiveMessage($id);
 		$execNotes = '';
 		if (!empty($msgRow->app_className)) {
@@ -386,56 +364,43 @@ SQL_QUERY;
 		return ($msg) ? (object)$msg : null;
 	}
 
-	public function getActiveMessage($id) {
+	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);
-		}
+		$msg = DB::getPDO()->fetchFirst("
+			select m.*
+			from CRM_UI_MSGS m
+			where m.ID = :id
+				and m.A_STATUS = 'WAITING'
+		", [
+			':id' => $id,
+		]);
+		if (!$msg) throw new HttpException("Message not found", 404);
 		return $msg;
 	}
 
-	public function forceFinishMessage($id, $execNotes) {
+	function forceFinishMessage($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;
+		DB::getPDO()->update('CRM_UI_MSGS', 'ID', $id, [
+			'A_STATUS' => "OFF_HARD",
+			'actionExecutedTime' => "NOW()",
+			'actionNotes' => $execNotes,
+			'A_RECORD_UPDATE_DATE' => "NOW()",
+			'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
+		]);
 	}
 
-	public function removeMessage($id) {
+	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);
+		DB::getPDO()->execSql(" update `CRM_UI_MSGS` set `A_STATUS`='DELETED' where `ID` = :id ", [ ':id' => $id ]);
 	}
 
 	function removeTableRecordMsg($idMsg) {

+ 26 - 23
SE/se-lib/Route/UserMsgs.php

@@ -749,7 +749,6 @@ function tblMsgsLoadMoreRows(n) {
 	}
 
 	function printWidgetViewMsg($msg) {
-		$messageList = array();
 		$uiTargetName = $msg['_raw']->uiTargetName;
 		$uiTargetType = $msg['_raw']->uiTargetType;
 		$replyLink = "index.php?_route=UserMsgs&_task=reply&uiTargetName={$uiTargetName}&uiTargetType={$uiTargetType}";
@@ -757,20 +756,20 @@ function tblMsgsLoadMoreRows(n) {
 		$message = $this->_convertMessageToJson($msg['_raw']);
 		{//if ($message->idThread > 0) {
 			$sqlLimit = 100;
-			$ds = DB::getDataSource();
 			$sqlIdThread = ($message->idThread > 0)? $message->idThread : $message->id;
-			$sql = "
+			$messageList = array_map(function ($msg) {
+				return $this->_convertMessageToJson((object)$msg);
+			}, DB::getPDO()->fetchAll("
 				select m.*
-					from `CRM_UI_MSGS` m
-					where (m.`idThread` = {$sqlIdThread} or m.`ID` = {$sqlIdThread})
-				--		and m.`ID` < {$message->id}
-					order by m.`ID` asc
+					from CRM_UI_MSGS m
+					where ( m.idThread = :id_thread or m.ID = :id_thread )
+					order by m.ID asc
 					limit {$sqlLimit}
-			";
-			$moreMsgs = $ds->getListByQuery($sql);
-			foreach ($moreMsgs as $msg) $messageList[] = $this->_convertMessageToJson($msg);
+			", [
+				':id_thread' => $sqlIdThread
+			]));
+			// --		and m.`ID` < {$message->id}
 		}
-		//$messageList[] = $message;
 		?>
 <link rel="stylesheet" href="./stuff/widget-select.css">
 <style type="text/css">
@@ -1004,16 +1003,19 @@ function tblMsgsLoadMoreRows(n) {
 			if ($idThread <= 0) throw new Exception("Wrong param id!");
 
 			$sqlLimit = 10;// TODO: 100?
-			$ds = DB::getDataSource();
-			$sql = "
+			$moreMsgs = array_map(function ($item) {
+				return (object)$item;
+			}, DB::getPDO()->fetchAll("
 				select m.*
-					from `CRM_UI_MSGS` m
-					where m.`idThread` = {$idThread}
-						and m.`ID` > {$idLastMsg}
-					order by m.`ID` asc
-					limit {$sqlLimit}
-			";
-			$moreMsgs = $ds->getListByQuery($sql);
+				from CRM_UI_MSGS m
+				where m.idThread = :id_thread
+					and m.ID > :id_last_msg
+				order by m.ID asc
+				limit {$sqlLimit}
+			", [
+				':id_thread' => $idThread,
+				':id_last_msg' => $idLastMsg,
+			]));
 
 			$response = new stdClass();
 			$response->msg = "Nowe wiadomości";
@@ -1063,7 +1065,6 @@ function tblMsgsLoadMoreRows(n) {
 	}
 
 	function _reply($uiTargetType, $uiTargetName, $args) {
-		$ds = DB::getDataSource();
 		$newMsg = array();
 		$newMsg['idReplyTo'] = V::get('idReplyTo', '', $args, 'int');
 		$newMsg['msg'] = V::get('message', '', $args);
@@ -1083,9 +1084,11 @@ function tblMsgsLoadMoreRows(n) {
 		$newMsg['uiTargetType'] = $uiTargetType;// TODO:? $parentMsg->uiTargetType
 		$newMsg['uiTargetName'] = $uiTargetName;// TODO:? $parentMsg->uiTargetName
 
-		$insertedId = $ds->insert('CRM_UI_MSGS', $newMsg);
+		$insertedId = DB::getPDO()->insert('CRM_UI_MSGS', $newMsg);
 		if (!$insertedId) throw new Exception("Nie udało się utworzyć rekordu");
-		$msgAdded = $ds->getById('CRM_UI_MSGS', $insertedId);
+		$msgAdded = DB::getPDO()->fetchFirst(" select * from CRM_UI_MSGS where ID = :id ", [ ':id' => $insertedId ]);
+		if (!$msgAdded) throw new Exception("Nie naleziono rekordu nr '{$insertedId}'");
+		$msgAdded = (object)$msgAdded;
 
 		$response = new stdClass();
 		$response->msg = "Wysłano wiadomość";