Przeglądaj źródła

fixed Notify: added markAsSent, fixed msg link, fixed all przypomnij type, added get todo task

Piotr Labudda 10 lat temu
rodzic
commit
4b4cb95628
1 zmienionych plików z 149 dodań i 37 usunięć
  1. 149 37
      SE/se-lib/Route/Notify.php

+ 149 - 37
SE/se-lib/Route/Notify.php

@@ -16,6 +16,11 @@ Sends mail by settings `CRM_NOTIFY`:
 - what - action type
 - last_exec_time
 
+TODO: add after_action for Msgs to user/group
+TODO: add after_action for Tasks with special params:
+  - Owner - notify after Worker change satus (100% done)
+	- Worker - notify after Owner set him as a Worker to task
+
 */
 class Route_Notify extends RouteBase {
 
@@ -55,6 +60,7 @@ class Route_Notify extends RouteBase {
 
 		if ('1' == V::get('send', 0, $_POST, 'int')) {
 			$this->send($usrLogin, $reminders);
+			$this->markAsSent($usrLogin, $reminders);
 		}
 
 		$urlParts = array();// index.php?_route=Notify&_task=generateUserReminders&usrLogin=magdalena.cichosz&reminder[]=msgs
@@ -175,12 +181,11 @@ function refreshPreview() {
 		echo json_encode($jsonData);
 	}
 
-	public function send($usrLogin, $reminders) {
-		DBG::_(true, true, "usrLogin", $usrLogin, __CLASS__, __FUNCTION__, __LINE__);
-		DBG::_(true, true, "reminders", $reminders, __CLASS__, __FUNCTION__, __LINE__);
+	public function send($usrLogin, $reminders, $forceMail = null) {
+		DBG::_('DBG_NTF', '>1', "usrLogin", $usrLogin, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG_NTF', '>1', "reminders", $reminders, __CLASS__, __FUNCTION__, __LINE__);
 		$user = null;
-		$pdo = DB::getPDO();
-		$sth = $pdo->prepare("
+		$sth = DB::getPDO()->prepare("
 			select u.ID, u.ADM_ACCOUNT as login, u.EMAIL_LOCAL_ACCOUNT_ADDRESS as aliasy
 			from ADMIN_USERS u
 			where u.ADM_ACCOUNT = :usr_login
@@ -189,9 +194,16 @@ function refreshPreview() {
 		$sth->execute();
 		$user = $sth->fetch();
 		if (!$user) throw new Exception("User not found");
-		if (empty($user['aliasy'])) throw new Exception("Brak lokalnego adresu email");
-		$aliasy = explode(' ', trim($user['aliasy']));
-		$mainMail = reset($aliasy);
+
+		$msgNote = '';
+		if (!empty($forceMail)) {
+			$mainMail = $forceMail;
+			if (empty($user['aliasy'])) $msgNote .= 'ERROR: Brak lokalnego adresu email' . "\n\n";
+		} else {
+			if (empty($user['aliasy'])) throw new Exception("Brak lokalnego adresu email");
+			$aliasy = explode(' ', trim($user['aliasy']));
+			$mainMail = reset($aliasy);
+		}
 
 		$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");
@@ -199,18 +211,29 @@ function refreshPreview() {
 		$to = $mainMail;
 		$headers = "From: " . "noreply@{$_SERVER['SERVER_NAME']}" . "\r\n";
 		$headers .= "MIME-Version: 1.0\r\n";
-		$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
-		$subject = "Powiadomienie - {$_SERVER['SERVER_NAME']}";
+		$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);
 		echo '<br><br>' . "\n";
-		echo "Wiadomość została wygenerowana autoamtycznie.";
-		echo "<br>\n------------------ TEST --------------------";// TODO: TEST
+		echo "Wiadomość została wygenerowana autoamtycznie." . "\n\n";
 		$message = ob_get_clean();
 
 		if (!mail($to, $subject, $message, $headers)) throw new Exception("Nie udało się wysłać powiadomienia");
 	}
 
+	public function markAsSent($usrLogin, $reminders) {
+		$sqlReminderTypes = "'" . implode("','", $reminders) . "'";
+		$sth = DB::getPDO()->prepare("
+			update CRM_NOTIFY
+			set `last_exec_time` = NOW()
+			where `who` = :login
+				and `what` in({$sqlReminderTypes})
+		");
+		$sth->bindValue(':login', $usrLogin, PDO::PARAM_STR);
+		$sth->execute();
+	}
+
 	public function generateUserRemindersAction() {
 		$usrLogin = User::getLogin();
 		$usrLogin = (isset($_GET['usrLogin']))? V::get('usrLogin', '', $_GET, 'login') : $usrLogin;
@@ -245,7 +268,7 @@ function refreshPreview() {
 				<?php if (V::get('ID', '', $todo) > 0) : ?>
 					<br><a href="<?php echo $this->editLink($todo); ?>" target="_blank" style="<?php echo $this->inlineCss('a'); ?>">Edytuj rekord <?php echo $todo['ID']; ?></a>
 				<?php elseif (V::get('idMsg', '', $todo) > 0) : ?>
-					<br><a href="<?php echo $this->readMsgLink($todo); ?>" target="_blank" style="<?php echo $this->inlineCss('a'); ?>">Przeczytaj</a>
+					<br><a href="<?php echo $this->readMsgLink($usrLogin, $todo); ?>" target="_blank" style="<?php echo $this->inlineCss('a'); ?>">Przeczytaj</a>
 				<?php endif; ?>
 			</td>
 			<td style="<?php echo $this->inlineCss('td.info'); ?>"><?php echo $todo['L_APPOITMENT_INFO']; ?></td>
@@ -289,7 +312,7 @@ function refreshPreview() {
 		return implode("", $urlParts);
 	}
 
-	public function readMsgLink($todo) {
+	public function readMsgLink($usrLogin, $todo) {
 		$idMsg = 0;
 		if ('msg' == $todo['_task_type']) {
 			$idMsg = $todo['idMsg'];
@@ -437,11 +460,19 @@ function refreshPreview() {
 		if (empty($_tasks)) {
 			$przypomnij = new Przypomnij();
 			$przypomnij->setRecurseLimit(3);
-			$przypomnij->fetchData();
+			$przypomnij->fetchData($usrLogin);
 			$przypomnij->setFltrUser($usrLogin);
 
 			$tasks = $przypomnij->getTasksByDate();
-			$usrGroupNames = User::getLdapGroupsNames();
+			{
+				$usrGroupNames = array();
+				//$usrGroupNames = User::getLdapGroupsNames();
+				Lib::loadClass('UsersLdapHelper');
+				$ldapGroups = UsersLdapHelper::getUserGroups($usrLogin, 3);
+				foreach ($ldapGroups as $kID => $vLDAPGroup) {
+					$usrGroupNames[$kID] = $vLDAPGroup->cn;
+				}
+			}
 			foreach ($tasks as $task) {
 				if (!$task->_show) continue;
 				if (
@@ -576,6 +607,17 @@ function refreshPreview() {
 		return $argValue;
 	}
 
+	public function getAllPrzypomnijTypes() {
+		$allPrzypomnijTypes = array();
+		$allPrzypomnijTypes[] = 'msgs';
+		$allPrzypomnijTypes[] = 'l_app_projekty';
+		$allPrzypomnijTypes[] = 'l_app_koresp';
+		$allPrzypomnijTypes[] = 'l_app_zadania';
+		$allPrzypomnijTypes[] = 'l_app_procesy';
+		$allPrzypomnijTypes[] = 'l_app_zasoby';
+		return $allPrzypomnijTypes;
+	}
+
 	public function getFieldValueLabels($fldName) {
 		$valueLabels = array();
 		if ('what' == $fldName) {
@@ -602,38 +644,58 @@ function refreshPreview() {
 		$what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$what_values));
 		$when_values = $this->getFieldValues('when');
 		$when = V::validate('when', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$when_values));
+
 		$pdo = DB::getPDO();
-		$sth = $pdo->prepare("
-			insert into `CRM_NOTIFY` (
-				`who`,
-				`what`,
-				`when`,
-				`_created`
-			) values (
-				:who,
-				:what,
-				:when,
-				NOW()
-			)
-		");
-		$bindValues = array();
-		$bindValues['who'] = array($usrLogin, PDO::PARAM_STR);
-		$bindValues['what'] = array($what, PDO::PARAM_STR);
-		$bindValues['when'] = array($when, PDO::PARAM_STR);
-		$pdo->bindValues($sth, $bindValues);
+		if ('l_app_all_przypomnij' == $what) {
+			$types = $this->getAllPrzypomnijTypes();
+			$sqlValues = array();
+			$sqlWho = $pdo->quote($usrLogin, PDO::PARAM_STR);
+			$sqlWhen = $pdo->quote($when, PDO::PARAM_STR);
+			foreach ($types as $type) {
+				$sqlWhat = $pdo->quote($type, PDO::PARAM_STR);
+				$sqlValues[] = "( {$sqlWho}, {$sqlWhat}, {$sqlWhen}, NOW() )";
+			}
+			$sqlValues = implode(", ", $sqlValues);
+			$pdo->exec("
+				insert ignore into `CRM_NOTIFY` (
+					`who`,
+					`what`,
+					`when`,
+					`_created`
+				) values {$sqlValues}
+			");
+		} else {
+			$sth = $pdo->prepare("
+				insert into `CRM_NOTIFY` (
+					`who`,
+					`what`,
+					`when`,
+					`_created`
+				) values (
+					:who,
+					:what,
+					:when,
+					NOW()
+				)
+			");
+			$bindValues = array();
+			$bindValues['who'] = array($usrLogin, PDO::PARAM_STR);
+			$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();
+				$sth->execute();
 //		} catch (PDOException $e) {
 //			throw new Exception("Błąd bazy danych: " . json_encode($e->errorInfo));
 //		} catch (Exception $e) {
 //			throw $e;
 //		}
+		}
 	}
 
 	public function getUserReminders($usrLogin) {
-		$pdo = DB::getPDO();
-		$sth = $pdo->prepare("
+		$sth = DB::getPDO()->prepare("
 			select n.*
 			from `CRM_NOTIFY` n
 			where n.`who` = '{$usrLogin}'
@@ -643,6 +705,56 @@ function refreshPreview() {
 		return $reminders;
 	}
 
+	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);
+		$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.last_exec_time is null or n.last_exec_time < NOW())
+				)
+			where u.A_STATUS = 'NORMAL'
+				and u.EMPLOYEE_TYPE = 'Pracownik'
+				and n.`who` is not null
+			limit :limit
+		");
+		$sth->bindValue(':limit', $limit, PDO::PARAM_INT);
+		$sth->execute();
+		$rawReminders = $sth->fetchAll();
+		$reminders = array();
+		$timeToday = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
+		//$timeTomorrow = mktime(0, 0, 0, date('n'), date('j') + 1, date('Y'));
+		DBG::_(true, true, "rawReminders (today:{$timeToday}. tomorrow:{$timeTomorrow})", $rawReminders, __CLASS__, __FUNCTION__, __LINE__);
+		foreach ($rawReminders as $row) {
+			$user = $row['who'];
+			$when = $row['when'];
+			$what = $row['what'];
+			$last_exec_time = $row['last_exec_time'];
+			if ('once_a_day' == $when) {
+				if ($last_exec_time >= $timeToday) {
+					DBG::_('DBG_CRON', '>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 {
+				DBG::_('DBG_CRON', '>0', "TODO: unimplemented when = '{$when}'", $row, __CLASS__, __FUNCTION__, __LINE__);
+				continue;
+			}
+			$last_date = ($last_exec_time)? date("Y-m-d H:i:s", $last_exec_time) : null;
+			$reminders[$user][$when][$what] = $last_date;
+		}
+		return $reminders;
+	}
+
 	public function reinstallAction() {
 		try {
 			$force = ('true' == V::get('_force', '', $_GET));