Explorar el Código

fixed sql to fetch task, and generate reminders in Notify

Piotr Labudda hace 10 años
padre
commit
08b670a129
Se han modificado 1 ficheros con 78 adiciones y 48 borrados
  1. 78 48
      SE/se-lib/Route/Notify.php

+ 78 - 48
SE/se-lib/Route/Notify.php

@@ -52,24 +52,24 @@ class Route_Notify extends RouteBase {
 			$usrLogin = (isset($_REQUEST['usrLogin']))? V::get('usrLogin', '', $_REQUEST, 'login') : $usrLogin;
 			if (!$usrLogin) die("Wrong user login!");
 
-			$reminders = V::get('reminder', array(), $_REQUEST, 'array');
-			if (!$reminders) {
-				$userReminders = $this->getUserReminders($usrLogin);
-				if (empty($userReminders)) die("Brak zdefiniowanych powiadomień");
-				foreach ($userReminders as $reminder) { $reminders[] = $reminder['what']; }
+			$notifyTypeList = V::get('reminder', array(), $_REQUEST, 'array');
+			if (!$notifyTypeList) {
+				$userNotifyList = $this->getUserNotifyList($usrLogin);
+				if (empty($userNotifyList)) die("Brak zdefiniowanych powiadomień");
+				foreach ($userNotifyList as $notify) { $notifyTypeList[] = $notify['what']; }
 			}
 		}
 
 		if ('1' == V::get('send', 0, $_POST, 'int')) {
-			$this->send($usrLogin, $reminders);
-			$this->markAsSent($usrLogin, $reminders);
+			$this->send($usrLogin, $notifyTypeList);
+			$this->markAsSent($usrLogin, $notifyTypeList);
 		}
 
 		$urlParts = array();// index.php?_route=Notify&_task=generateUserReminders&usrLogin=magdalena.cichosz&reminder[]=msgs
 		$urlParts[] = Request::getPathUri();
 		$urlParts[] = 'index.php?_route=Notify&_task=generateUserReminders';
 		$urlParts[] = "&usrLogin={$usrLogin}";
-		foreach ($reminders as $reminderType) {
+		foreach ($notifyTypeList as $reminderType) {
 			$urlParts[] = "&reminder[]={$reminderType}";
 		}
 		$previewLink = implode('', $urlParts);
@@ -106,7 +106,7 @@ class Route_Notify extends RouteBase {
 						<label>
 							<input type="checkbox"
 										 name="reminder[]"
-										 <?php if (in_array($reminderType, $reminders)) : ?>
+										 <?php if (in_array($reminderType, $notifyTypeList)) : ?>
 											 checked="checked"
 										 <?php endif; ?>
 										 value="<?php echo $reminderType; ?>"> <?php echo $reminderLabel; ?>
@@ -128,7 +128,7 @@ function refreshPreview() {
 //		$urlParts[] = Request::getPathUri();
 //		$urlParts[] = 'index.php?_route=Notify&_task=generateUserReminders';
 //		$urlParts[] = "&usrLogin={$usrLogin}";
-//		foreach ($reminders as $reminderType) {
+//		foreach ($notifyTypeList as $reminderType) {
 //			$urlParts[] = "&reminder[]={$reminderType}";
 //		}
 	var frm = document.getElementById('reminderPanelForm'),
@@ -183,9 +183,9 @@ function refreshPreview() {
 		echo json_encode($jsonData);
 	}
 
-	public function send($usrLogin, $reminders, $forceMail = null) {
+	public function send($usrLogin, $notifyTypeList, $forceMail = null) {
 		DBG::_('DBG_NTF', '>1', "usrLogin", $usrLogin, __CLASS__, __FUNCTION__, __LINE__);
-		DBG::_('DBG_NTF', '>1', "reminders", $reminders, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG_NTF', '>1', "reminders", $notifyTypeList, __CLASS__, __FUNCTION__, __LINE__);
 		$mainMail = $this->getUserMainMail($usrLogin);
 
 		$msgNote = '';
@@ -197,13 +197,19 @@ 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);
+		if (empty($usrReminders)) {
+			DBG::_('DBG_NTF', '>0', "Skip usr({$usrLogin}) - no remindres", $usrReminders, __CLASS__, __FUNCTION__, __LINE__);
+			return;
+		}
+
 		$to = $mainMail;
 		$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}";
 		ob_start();
-		$this->generateUserReminders($usrLogin, $reminders);
+		$this->viewUserReminders($usrReminders, $usrLogin);
 		echo '<br><br>' . "\n";
 		echo "Wiadomość została wygenerowana autoamtycznie." . "\n\n";
 		$message = ob_get_clean();
@@ -228,8 +234,8 @@ function refreshPreview() {
 		return reset($aliasy);
 	}
 
-	public function markAsSent($usrLogin, $reminders) {
-		$sqlReminderTypes = "'" . implode("','", $reminders) . "'";
+	public function markAsSent($usrLogin, $notifyTypeList) {
+		$sqlReminderTypes = "'" . implode("','", $notifyTypeList) . "'";
 		$sth = DB::getPDO()->prepare("
 			update CRM_NOTIFY
 			set `last_exec_time` = NOW()
@@ -245,27 +251,37 @@ function refreshPreview() {
 		$usrLogin = (isset($_GET['usrLogin']))? V::get('usrLogin', '', $_GET, 'login') : $usrLogin;
 		if (!$usrLogin) die("Wrong user login!");
 
-		$reminders = V::get('reminder', array(), $_GET, 'array');
-		if (!$reminders) {
-			$userReminders = $this->getUserReminders($usrLogin);
-			if (empty($userReminders)) die("Brak zdefiniowanych powiadomień");
-			foreach ($userReminders as $reminder) { $reminders[] = $reminder['what']; }
+		$notifyTypeList = V::get('reminder', array(), $_GET, 'array');
+		if (!$notifyTypeList) {
+			$userNotifyList = $this->getUserNotifyList($usrLogin);
+			if (empty($userNotifyList)) die("Brak zdefiniowanych powiadomień");
+			foreach ($userNotifyList as $notify) { $notifyTypeList[] = $notify['what']; }
 		}
 
 		header('Content-Type: text/html; charset="UTF-8"');
-		$this->generateUserReminders($usrLogin, $reminders);
+		$usrReminders = $this->getUserReminders($usrLogin, $notifyTypeList);
+		$this->viewUserReminders($usrReminders, $usrLogin);
+	}
+
+	public function getUserReminders($usrLogin, $notifyTypeList) {
+		$usrReminders = array();
+		foreach ($notifyTypeList as $notifyType) {
+			$listTodo = $this->getTodoForUserReminder($usrLogin, $notifyType);
+			if (!empty($listTodo)) $usrReminders[$notifyType] = $listTodo;
+		}
+		return $usrReminders;
 	}
 
-	public function generateUserReminders($usrLogin, $reminders) {
+	public function viewUserReminders($usrReminders, $usrLogin) {
 		echo '<html>';
 		echo '<body>';
 		echo '<div>';
 		?>
 <h1 style="<?php echo $this->inlineCss('h1'); ?>">Powiadomienia dla <code><?php echo $usrLogin; ?></code></h1>
-<?php foreach ($reminders as $reminderType) : ?>
+<?php foreach ($usrReminders as $reminderType => $listTodo) : ?>
 	<div style="<?php echo $this->inlineCss('header'); ?>"><strong><?php echo $this->printValue('what', $reminderType); ?></strong></div>
 	<table rules="all" cellspacing="0" style="border-color:#bbb;" cellpadding="8">
-	<?php $lp = 0; foreach ($this->getTodoForUserReminder($usrLogin, $reminderType) as $todo) : ?>
+	<?php $lp = 0; foreach ($listTodo as $todo) : ?>
 		<tr>
 			<td style="<?php echo $this->inlineCss('td.lp'); ?>"><?php echo ++$lp; ?></td>
 			<td style="<?php echo $this->inlineCss('td.' . $todo['_l_app_class']); ?>">
@@ -544,12 +560,12 @@ function refreshPreview() {
 				SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 			}
 		}
-		$userReminders = $this->getUserReminders($usrLogin);
+		$userNotifyList = $this->getUserNotifyList($usrLogin);
 		$userMail = $this->getUserMainMail($usrLogin);
 		?>
 	<h1>Powiadomienia dla <code><?php echo $usrLogin; ?></code></h1>
 	<p><i>Będą wysyłane na adres: <?php echo $userMail; ?></i></p>
-	<?php if (empty($userReminders)) : ?>
+	<?php if (empty($userNotifyList)) : ?>
 		<?php SE_Layout::alert('warning', "Brak zdefiniowanych powiadomień"); ?>
 	<?php else : ?>
 		<table class="table table-hovered">
@@ -563,18 +579,18 @@ function refreshPreview() {
 				</tr>
 			</thead>
 			<tbody>
-				<?php foreach ($userReminders as $reminder) : ?>
+				<?php foreach ($userNotifyList as $notify) : ?>
 					<tr>
-						<td><?php echo $this->printValue('what', $reminder['what']); ?></td>
-						<td><?php echo $this->printValue('when', $reminder['when']); ?></td>
-						<td><?php echo $reminder['_created']; ?></td>
-						<td><?php echo ($reminder['last_exec_time'])? $reminder['last_exec_time'] : '<i>brak danych</i>'; ?></td>
+						<td><?php echo $this->printValue('what', $notify['what']); ?></td>
+						<td><?php echo $this->printValue('when', $notify['when']); ?></td>
+						<td><?php echo $notify['_created']; ?></td>
+						<td><?php echo ($notify['last_exec_time'])? $notify['last_exec_time'] : '<i>brak danych</i>'; ?></td>
 						<td>
 							<form method="post" class="form-inline">
 								<input type="hidden" name="_subTask" value="_rm_table_reminder">
 								<input type="hidden" name="who" value="<?php echo $usrLogin; ?>">
-								<input type="hidden" name="what" value="<?php echo $reminder['what']; ?>">
-								<input type="hidden" name="when" value="<?php echo $reminder['when']; ?>">
+								<input type="hidden" name="what" value="<?php echo $notify['what']; ?>">
+								<input type="hidden" name="when" value="<?php echo $notify['when']; ?>">
 								<button class="btn btn-link btn-xs" onclick="return confirm('Czy usunąć powiadomienie?')" type="submit"><i class="glyphicon glyphicon-remove" style="color:red;opacity:0.4;"></i></button>
 							</form>
 						</td>
@@ -690,7 +706,6 @@ function refreshPreview() {
 			$bindValues['what'] = array($what, PDO::PARAM_STR);
 			$bindValues['when'] = array($when, PDO::PARAM_STR);
 			$pdo->bindValues($sth, $bindValues);
-		//DBG::_(true, true, "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
 //		try {
 				$sth->execute();
 //		} catch (PDOException $e) {
@@ -701,15 +716,15 @@ function refreshPreview() {
 		}
 	}
 
-	public function getUserReminders($usrLogin) {
+	public function getUserNotifyList($usrLogin) {
 		$sth = DB::getPDO()->prepare("
 			select n.*
 			from `CRM_NOTIFY` n
 			where n.`who` = '{$usrLogin}'
 		");
 		$sth->execute();
-		$reminders = $sth->fetchAll();
-		return $reminders;
+		$notifyTypeList = $sth->fetchAll();
+		return $notifyTypeList;
 	}
 
 	public function getTodoList($limit = 10) {
@@ -722,25 +737,38 @@ function refreshPreview() {
 		// 	limit :limit
 		// ");
 		// $sth->bindValue(':limit', $limit, PDO::PARAM_INT);
-		$sth = DB::getPDO()->prepare("
-			select n.`who`, n.`when`, n.`what`, UNIX_TIMESTAMP(n.last_exec_time) as last_exec_time
-			from ADMIN_USERS u
-				left join CRM_NOTIFY n on(n.`who` = u.ADM_ACCOUNT
-					and (
-						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
+		$pdo = DB::getPDO();
+		$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
+			)
+		";
+		$sth = $pdo->prepare("
+			select u.ADM_ACCOUNT
+			from ADMIN_USERS u
+				join CRM_NOTIFY n on(n.`who` = u.ADM_ACCOUNT and {$sqlWhenFltr})
 			where u.A_STATUS = 'NORMAL'
 				and u.EMPLOYEE_TYPE = 'Pracownik'
 				and n.`who` is not null
+			group by u.ADM_ACCOUNT
 			limit :limit
 		");
 		$sth->bindValue(':limit', $limit, PDO::PARAM_INT);
 		$sth->execute();
+		$rawUsers = $sth->fetchAll();
+		$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
+			from CRM_NOTIFY n
+			where n.`who` in({$sqlUsers})
+				and {$sqlWhenFltr}
+		");
+		$sth->execute();
 		$rawReminders = $sth->fetchAll();
 		$reminders = array();
 		$timeToday = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
@@ -763,6 +791,8 @@ function refreshPreview() {
 				continue;
 			}
 			$last_date = ($last_exec_time)? date("Y-m-d H:i:s", $last_exec_time) : null;
+			if (!isset($reminders[$user])) $reminders[$user] = array();
+			if (!isset($reminders[$user][$when])) $reminders[$user][$when] = array();
 			$reminders[$user][$when][$what] = $last_date;
 		}
 		return $reminders;