Просмотр исходного кода

added immediately reminder in Notify

Piotr Labudda 10 лет назад
Родитель
Сommit
c91062b2be
2 измененных файлов с 173 добавлено и 42 удалено
  1. 4 2
      SE/se-lib/Route/Cron.php
  2. 169 40
      SE/se-lib/Route/Notify.php

+ 4 - 2
SE/se-lib/Route/Cron.php

@@ -105,8 +105,10 @@ class Route_Cron extends RouteBase {
 				$timeNow = time();
 				$timeSendLimitFrom = mktime(8, 0, 0, date('n'), date('j'), date('Y'));
 				$timeSendLimitTo = mktime(20, 0, 0, date('n'), date('j'), date('Y'));
+				$dayNrNow = date('N');
+				$daysAllowed = array(1, 2, 3, 4, 5);// 1 = Poniedziałek, ... , 6 - Sobota, 7 - Niedziela
 				if ($timeNow > $timeSendLimitFrom && $timeNow < $timeSendLimitTo) {
-					$todoReminders = $notify->getTodoList(2);
+					$todoReminders = $notify->getTodoList(2, array('once_a_day', 'immediately'));
 				}
 			}
 			DBG::_('DBG_CRON', '>0', 'todoReminders', $todoReminders, __CLASS__, __FUNCTION__, __LINE__);
@@ -116,7 +118,7 @@ class Route_Cron extends RouteBase {
 					if (!empty($listWhat)) {
 						$reminders = array_keys($listWhat);
 						echo "<p>Sending to {$who} reminders [" . implode(",", $reminders) . "]</p>" . "\n";
-						$notify->send($who, $reminders, $forceMail = 'plabudda@biall-net.pl');
+						$notify->send($who, $listWhat, $when, $forceMail = 'plabudda@biall-net.pl');
 						$notify->markAsSent($who, $reminders);
 					}
 				}

+ 169 - 40
SE/se-lib/Route/Notify.php

@@ -183,9 +183,13 @@ function refreshPreview() {
 		echo json_encode($jsonData);
 	}
 
-	public function send($usrLogin, $notifyTypeList, $forceMail = null) {
+	/**
+	 * @param $reminders array(  )
+	 * @param $when 'once_a_day', 'immediately'
+	 */
+	public function send($usrLogin, $reminders, $when = null, $forceMail = null) {
 		DBG::_('DBG_NTF', '>1', "usrLogin", $usrLogin, __CLASS__, __FUNCTION__, __LINE__);
-		DBG::_('DBG_NTF', '>1', "reminders", $notifyTypeList, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG_NTF', '>1', "reminders(when: {$when})", $reminders, __CLASS__, __FUNCTION__, __LINE__);
 		$mainMail = $this->getUserMainMail($usrLogin);
 
 		$msgNote = '';
@@ -197,7 +201,7 @@ function refreshPreview() {
 		$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i";
 		if (!preg_match($pattern, $mainMail)) throw new Exception("Adres email ma błędny format");
 
-		$usrReminders = $this->getUserReminders($usrLogin, $notifyTypeList);
+		$usrReminders = $this->getUserReminders($usrLogin, $reminders);
 		if (empty($usrReminders)) {
 			DBG::_('DBG_NTF', '>0', "Skip usr({$usrLogin}) - no remindres", $usrReminders, __CLASS__, __FUNCTION__, __LINE__);
 			return;
@@ -207,10 +211,14 @@ function refreshPreview() {
 		$headers = "From: " . "noreply@{$_SERVER['SERVER_NAME']}" . "\r\n";
 		$headers .= "MIME-Version: 1.0\r\n";
 		$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
-		$subject = "Powiadomienia - {$_SERVER['SERVER_NAME']} - " . date("Y-m-d") . " - {$usrLogin}";
+		if (null == $when || 'once_a_day' == $when) {
+			$subject = "Powiadomienia dla {$usrLogin} - " . date("Y-m-d") . " - {$_SERVER['SERVER_NAME']}";
+		} else {
+			$subject = "Nowe powiadomienia dla {$usrLogin} - {$_SERVER['SERVER_NAME']}";
+		}
 		ob_start();
 		$this->viewUserReminders($usrReminders, $usrLogin);
-		echo "\n\n<br><br>\nWiadomość została wygenerowana autoamtycznie." . "\n\n";
+		echo "\n\n<br><br>\nWiadomość została wygenerowana automatycznie." . "\n\n";
 		{
 			$baseUrl = "https://{$_SERVER['SERVER_NAME']}/SE";
 			if ('biuro.biall-net.pl' == $_SERVER['SERVER_NAME']) $baseUrl = "https://{$_SERVER['SERVER_NAME']}/SE/version-git";
@@ -264,11 +272,14 @@ function refreshPreview() {
 		}
 
 		header('Content-Type: text/html; charset="UTF-8"');
-		$usrReminders = $this->getUserReminders($usrLogin, $notifyTypeList);
+		$usrReminders = $this->getAllUserReminders($usrLogin, $notifyTypeList);
 		$this->viewUserReminders($usrReminders, $usrLogin);
 	}
 
-	public function getUserReminders($usrLogin, $notifyTypeList) {
+	/**
+	 * @param $reminders - array( $notifyType )
+	 */
+	public function getAllUserReminders($usrLogin, $notifyTypeList) {
 		$usrReminders = array();
 		foreach ($notifyTypeList as $notifyType) {
 			$listTodo = $this->getTodoForUserReminder($usrLogin, $notifyType);
@@ -277,6 +288,18 @@ function refreshPreview() {
 		return $usrReminders;
 	}
 
+	/**
+	 * @param $reminders - array( $notifyType => $lastExecTime )
+	 */
+	public function getUserReminders($usrLogin, $reminders) {
+		$usrReminders = array();
+		foreach ($reminders as $notifyType => $lastExecTime) {
+			$listTodo = $this->getTodoForUserReminder($usrLogin, $notifyType, $lastExecTime);
+			if (!empty($listTodo)) $usrReminders[$notifyType] = $listTodo;
+		}
+		return $usrReminders;
+	}
+
 	public function viewUserReminders($usrReminders, $usrLogin) {
 		echo '<html>';
 		echo '<body>';
@@ -406,12 +429,12 @@ function refreshPreview() {
 		return $color;
 	}
 
-	public function getTodoForUserReminder($usrLogin, $reminderWhat) {
+	public function getTodoForUserReminder($usrLogin, $reminderWhat, $lastExecTime = null) {
 		$listTodo = array();
 		if ('msgs' == $reminderWhat) {
 			$routeMsgs = Router::getRoute('UserMsgs');
 			$routeMsgs->_listLimit = 50;
-			$msgs = $routeMsgs->getMsgs('unread', $usrLogin);
+			$msgs = $routeMsgs->getMsgs('unread', $usrLogin, $lastMsgId = null, $lastExecTime);
 			foreach ($msgs as $idMsg => $msg) {
 				$todo = array();
 				$todo['_task_type'] = 'msg';
@@ -442,6 +465,7 @@ function refreshPreview() {
 				if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
 				if ('projekt' != $task['_task_type']) continue;
 				//$task['DESC'] = $task['A_PROBLEM_DESC'];
+				if (!$this->filterTaskByTime($task, $lastExecTime)) continue;
 				$listTodo[] = $task;
 			}
 		}
@@ -451,6 +475,7 @@ function refreshPreview() {
 				if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
 				if ('koresp' != $task['_task_type']) continue;
 				//$task['DESC'] = $task['A_PROBLEM_DESC'];
+				if (!$this->filterTaskByTime($task, $lastExecTime)) continue;
 				$listTodo[] = $task;
 			}
 		}
@@ -460,6 +485,7 @@ function refreshPreview() {
 				if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
 				if ('task' != $task['_task_type']) continue;
 				//$task['DESC'] = $task['A_PROBLEM_DESC'];
+				if (!$this->filterTaskByTime($task, $lastExecTime)) continue;
 				$listTodo[] = $task;
 			}
 		}
@@ -468,6 +494,7 @@ function refreshPreview() {
 			foreach ($tasks as $task) {
 				if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
 				if ('proces' != $task['_task_type']) continue;
+				if (!$this->filterTaskByTime($task, $lastExecTime)) continue;
 				$listTodo[] = $task;
 			}
 		}
@@ -476,12 +503,27 @@ function refreshPreview() {
 			foreach ($tasks as $task) {
 				if (empty($task['_l_app_date']) || '0000-00-00' == $task['_l_app_date']) continue;
 				if ('zasob' != $task['_task_type']) continue;
+				if (!$this->filterTaskByTime($task, $lastExecTime)) continue;
 				$listTodo[] = $task;
 			}
 		}
 		return $listTodo;
 	}
 
+	public function filterTaskByTime($task, $lastExecTime) {
+		if (null == $lastExecTime) return true;
+		if (empty($task['A_RECORD_CREATE_DATE'])) return true;
+		if ($task['A_RECORD_CREATE_DATE'] >= $lastExecTime) return true;
+		if (empty($task['A_RECORD_UPDATE_DATE'])) {
+			DBG::_('DBG_NTF', '>1', "skip task by lastExecTime({$lastExecTime}) - empty update date", $task, __CLASS__, __FUNCTION__, __LINE__);
+			return false;
+		} else if ($task['A_RECORD_UPDATE_DATE'] < $lastExecTime) {
+			DBG::_('DBG_NTF', '>1', "skip task by lastExecTime({$lastExecTime}) - old update date", $task, __CLASS__, __FUNCTION__, __LINE__);
+			return false;
+		}
+		return true;
+	}
+
 	public function getPrzypomnijTasks($usrLogin) {
 		static $_tasks = array();
 		if (empty($_tasks)) {
@@ -567,6 +609,8 @@ function refreshPreview() {
 		}
 		$userNotifyList = $this->getUserNotifyList($usrLogin);
 		$userMail = $this->getUserMainMail($usrLogin);
+		$whatLabelsForOnceADay = $this->getFieldValueLabels('what', $userNotifyList, $when = 'once_a_day');
+		$whatLabelsForImmediately = $this->getFieldValueLabels('what', $userNotifyList, $when = 'immediately');
 		?>
 	<h1>Powiadomienia dla <code><?php echo $usrLogin; ?></code></h1>
 	<p><i>Będą wysyłane na adres: <?php echo $userMail; ?></i></p>
@@ -605,24 +649,39 @@ function refreshPreview() {
 		</table>
 	<?php endif; ?>
 
+	<?php if (!empty($whatLabelsForOnceADay)) : ?>
 	<hr>
-	<h4>Dodaj powiadomienia:</h4>
+	<h4>Dodaj powiadomienia wysyłane raz dziennie:</h4>
 	<form method="post" class="form-inline">
 		<input type="hidden" name="_subTask" value="_add_table_reminder">
+		<input type="hidden" name="when" value="once_a_day">
 		<label>rodzaj:</label>
 		<select type="select" name="what" class="form-control">
 			<option value="">[ Wybierz ]</option>
-			<?php foreach ($this->getFieldValueLabels('what') as $value => $label) : ?>
+			<?php foreach ($whatLabelsForOnceADay as $value => $label) : ?>
 				<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
 			<?php endforeach; ?>
 		</select>
-		<label>jak często:</label>
-		<select type="select" name="when" class="form-control">
+		<input type="submit" value="Dodaj" class="btn btn-primary">
+	</form>
+	<?php endif; ?>
+
+	<?php if (!empty($whatLabelsForImmediately)) : ?>
+	<hr>
+	<h4>Dodaj powiadomienia wysyłane na bieżąco:</h4>
+	<form method="post" class="form-inline">
+		<input type="hidden" name="_subTask" value="_add_table_reminder">
+		<input type="hidden" name="when" value="immediately">
+		<label>rodzaj:</label>
+		<select type="select" name="what" class="form-control">
 			<option value="">[ Wybierz ]</option>
-			<option value="once_a_day">Raz dziennie</option>
+			<?php foreach ($whatLabelsForImmediately as $value => $label) : ?>
+				<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
+			<?php endforeach; ?>
 		</select>
 		<input type="submit" value="Dodaj" class="btn btn-primary">
 	</form>
+	<?php endif; ?>
 		<?php
 		echo '</div>';// .container
 	}
@@ -646,18 +705,50 @@ function refreshPreview() {
 		return $allPrzypomnijTypes;
 	}
 
-	public function getFieldValueLabels($fldName) {
+	public function getFieldValueLabels($fldName, $userNotifyList = null, $when = null) {
 		$valueLabels = array();
 		if ('what' == $fldName) {
-			$valueLabels['msgs'] = "wiadomości";
-			$valueLabels['l_app_projekty'] = "projekty";
-			$valueLabels['l_app_koresp'] = "korespondencja";
-			$valueLabels['l_app_zadania'] = "zadania";
-			$valueLabels['l_app_procesy'] = "procesy";
-			$valueLabels['l_app_zasoby'] = "zasoby";
-			$valueLabels['l_app_all_przypomnij'] = "*wszystkie";
+			if (null == $userNotifyList) $userNotifyList = array();
+			if ('once_a_day' == $when) {
+				$valueLabels['msgs'] = "wiadomości";
+				$valueLabels['l_app_projekty'] = "projekty";
+				$valueLabels['l_app_koresp'] = "korespondencja";
+				$valueLabels['l_app_zadania'] = "zadania";
+				$valueLabels['l_app_procesy'] = "procesy";
+				$valueLabels['l_app_zasoby'] = "zasoby";
+				$valueLabels['l_app_all_przypomnij'] = "*wszystkie";
+				if (!empty($userNotifyList)) {// filter out existing notify
+					foreach ($userNotifyList as $notify) {
+						if ($when == $notify['when'] && array_key_exists($notify['what'], $valueLabels)) {
+							unset($valueLabels[$notify['what']]);
+						}
+					}
+					if (1 == count($valueLabels) && !empty($valueLabels['l_app_all_przypomnij'])) {
+						unset($valueLabels['l_app_all_przypomnij']);
+					}
+				}
+			} else if ('immediately' == $when) {
+				$valueLabels['msgs'] = "wiadomości";
+				$valueLabels['l_app_zadania'] = "zadania";
+				if (!empty($userNotifyList)) {// filter out existing notify
+					foreach ($userNotifyList as $notify) {
+						if ($when == $notify['when'] && array_key_exists($notify['what'], $valueLabels)) {
+							unset($valueLabels[$notify['what']]);
+						}
+					}
+				}
+			} else {
+				$valueLabels['msgs'] = "wiadomości";
+				$valueLabels['l_app_projekty'] = "projekty";
+				$valueLabels['l_app_koresp'] = "korespondencja";
+				$valueLabels['l_app_zadania'] = "zadania";
+				$valueLabels['l_app_procesy'] = "procesy";
+				$valueLabels['l_app_zasoby'] = "zasoby";
+				$valueLabels['l_app_all_przypomnij'] = "*wszystkie";
+			}
 		} else if ('when' == $fldName) {
 			$valueLabels['once_a_day'] = "Raz dziennie";
+			$valueLabels['immediately'] = "Na bieżąco";
 		}
 		return $valueLabels;
 	}
@@ -732,26 +823,35 @@ function refreshPreview() {
 		return $notifyTypeList;
 	}
 
-	public function getTodoList($limit = 10) {
-		// $sth = DB::getPDO()->prepare("
-		// 	select n.`who`, n.`when`, min(n.last_exec_time) as min_last_exec_time
-		// 	from `CRM_NOTIFY` n
-		// 	group by n.`who`, n.`when`
-		// 	having min_last_exec_time is null or min_last_exec_time < NOW()
-		// 	order by min_last_exec_time
-		// 	limit :limit
-		// ");
-		// $sth->bindValue(':limit', $limit, PDO::PARAM_INT);
+	public function getTodoList($limit = 10, $whenTypes = array()) {
 		$pdo = DB::getPDO();
-		$sqlWhenFltr = "
-			(
-				n.`when` = 'once_a_day' and (
+		if (empty($whenTypes)) throw new Exception("when types not set in getTodoList");
+
+		{
+			$sqlWhenFltr = array();
+			if (in_array('once_a_day', $whenTypes)) {
+				$sqlWhenFltr[] = "
+					n.`when` = 'once_a_day' and (
 						n.last_exec_time is null
 						or DATE(n.last_exec_time) < CURDATE()
 					)
-				-- TODO add `or` with another `when` values
-			)
-		";
+				";
+			}
+			if (in_array('immediately', $whenTypes)) {
+				$sqlWhenFltr[] = "
+					n.`when` = 'immediately'
+				";
+			}
+			if (empty($sqlWhenFltr)) throw new Exception("when types not allowed in getTodoList");
+
+			$sqlWhenFltr = "
+				(
+					(
+						" . implode(") or (", $sqlWhenFltr) . "
+					)
+				)
+			";
+		}
 		$sth = $pdo->prepare("
 			select u.ADM_ACCOUNT
 			from ADMIN_USERS u
@@ -769,7 +869,18 @@ function refreshPreview() {
 		$sqlUsers = array(); foreach ($rawUsers as $row) { $sqlUsers[] = $pdo->quote($row['ADM_ACCOUNT'], PDO::PARAM_STR); }
 		$sqlUsers = implode(", ", $sqlUsers);
 		$sth = $pdo->prepare("
-			select n.`who`, n.`when`, n.`what`, UNIX_TIMESTAMP(n.last_exec_time) as last_exec_time
+			select n.`who`, n.`when`, n.`what`
+				, UNIX_TIMESTAMP(n.last_exec_time) as last_exec_time
+				, IF(n.`when` = 'immediately'
+					, (select UNIX_TIMESTAMP(MAX(i.last_exec_time))
+							from CRM_NOTIFY i
+							where i.`who` = n.`who`
+								and i.`what` = n.`what`
+					--			and i.`when` = 'once_a_day'
+					--		limit 1
+						)
+					, null
+				) as _last_exec_time_fix_for_immediately
 			from CRM_NOTIFY n
 			where n.`who` in({$sqlUsers})
 				and {$sqlWhenFltr}
@@ -785,13 +896,16 @@ function refreshPreview() {
 			$when = $row['when'];
 			$what = $row['what'];
 			$last_exec_time = $row['last_exec_time'];
+			$last_exec_time_fix_for_immediately = $row['_last_exec_time_fix_for_immediately'];
 			if ('once_a_day' == $when) {
 				if ($last_exec_time >= $timeToday) {
-					DBG::_('DBG_NTF', '>1', "skip {$last_exec_time} for {$what} at {$when}", $row, __CLASS__, __FUNCTION__, __LINE__);
+					DBG::_('DBG_NTF', '>1', "skip {$last_exec_time} for '{$what}' at '{$when}'", $row, __CLASS__, __FUNCTION__, __LINE__);
 					continue;
 				} else {
 					//DBG::_(true, true, "TODO: else {$last_exec_time} >= {$timeToday}", $row, __CLASS__, __FUNCTION__, __LINE__);
 				}
+			} else if ('immediately' == $when) {
+				//DBG::_(true, true, "TODO: else {$last_exec_time} >= {$timeToday}", $row, __CLASS__, __FUNCTION__, __LINE__);
 			} else {
 				DBG::_('DBG_NTF', '>0', "TODO: unimplemented when = '{$when}'", $row, __CLASS__, __FUNCTION__, __LINE__);
 				continue;
@@ -800,6 +914,21 @@ function refreshPreview() {
 			if (!isset($reminders[$user])) $reminders[$user] = array();
 			if (!isset($reminders[$user][$when])) $reminders[$user][$when] = array();
 			$reminders[$user][$when][$what] = $last_date;
+			if ('immediately' == $when && null !== $last_exec_time_fix_for_immediately && $last_exec_time_fix_for_immediately > $last_exec_time) {
+				$reminders[$user][$when][$what] = date("Y-m-d H:i:s", $last_exec_time_fix_for_immediately);
+			}
+		}
+		// remove duplicates
+		foreach ($reminders as $user => $listWhen) {
+			if (!empty($listWhen['once_a_day']) && !empty($listWhen['immediately'])) {
+				foreach ($listWhen['once_a_day'] as $what => $last_exec) {
+					if (array_key_exists($what, $listWhen['immediately'])) {
+						DBG::_('DBG_NTF', '>2', "skip immediately '{$what}'", $listWhen['immediately'][$what], __CLASS__, __FUNCTION__, __LINE__);
+						unset($reminders[$user]['immediately'][$what]);
+					}
+				}
+				if (empty($reminders[$user]['immediately'])) unset($reminders[$user]['immediately']);
+			}
 		}
 		return $reminders;
 	}