Browse Source

added send panel for Notify

Piotr Labudda 10 năm trước cách đây
mục cha
commit
8dd94240cb
1 tập tin đã thay đổi với 215 bổ sung21 xóa
  1. 215 21
      SE/se-lib/Route/Notify.php

+ 215 - 21
SE/se-lib/Route/Notify.php

@@ -1,7 +1,10 @@
 <?php
+// @requires $_SERVER['SERVER_NAME']
 
 Lib::loadClass('RouteBase');
 Lib::loadClass('Przypomnij');
+Lib::loadClass('Request');
+Lib::loadClass('Route_UserMsgs');
 
 /*
 # Nofitication system:
@@ -31,50 +34,190 @@ class Route_Notify extends RouteBase {
 		SE_Layout::dol();
 	}
 
+	public function remindersPanelAction() {
+		$usrLogin = User::getLogin();
+		SE_Layout::gora();
+		//SE_Layout::menu();
+
+		{// init args
+			$usrLogin = User::getLogin();
+			$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']; }
+			}
+		}
+
+		if ('1' == V::get('send', 0, $_POST, 'int')) {
+			$this->send($usrLogin, $reminders);
+		}
+
+		$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) {
+			$urlParts[] = "&reminder[]={$reminderType}";
+		}
+		$previewLink = implode('', $urlParts);
+
+		$lastSentDate = 'brak';
+		?>
+<div class="jumbotron">
+	<div class="container">
+		<form id="reminderPanelForm" class="form-horizontal" method="POST">
+			<div class="form-group">
+				<label class="col-sm-2 control-label">User</label>
+				<div class="col-sm-10">
+					<input class="form-control" type="text" name="usrLogin" value="<?php echo $usrLogin; ?>">
+				</div>
+			</div>
+
+			<?php foreach ($this->getFieldValueLabels('what') as $reminderType => $reminderLabel) : ?>
+				<?php if ('l_app_all_przypomnij' == $reminderType) continue; ?>
+				<div class="col-sm-3">
+					<div class="checkbox">
+						<label>
+							<input type="checkbox"
+										 name="reminder[]"
+										 <?php if (in_array($reminderType, $reminders)) : ?>
+											 checked="checked"
+										 <?php endif; ?>
+										 value="<?php echo $reminderType; ?>"> <?php echo $reminderLabel; ?>
+						</label>
+					</div>
+				</div>
+			<?php endforeach; ?>
+
+			<div class="col-sm-offset-2 col-sm-10" style="margin-top:10px;padding-top:10px;border-top:1px solid silver;">
+				<button class="btn btn-default" onclick="refreshPreview(); return false;">odśwież podgląd</button>
+				<button class="btn btn-primary" name="send" value="1">wyślij maila</button>
+				data wysłania ostatniej wiadomości do wybranego użytkownika: <?php echo $lastSentDate; ?>
+			</div>
+		</form>
+	</div>
+</div>
+<script>
+function refreshPreview() {
+//		$urlParts[] = Request::getPathUri();
+//		$urlParts[] = 'index.php?_route=Notify&_task=generateUserReminders';
+//		$urlParts[] = "&usrLogin={$usrLogin}";
+//		foreach ($reminders as $reminderType) {
+//			$urlParts[] = "&reminder[]={$reminderType}";
+//		}
+	var frm = document.getElementById('reminderPanelForm'),
+			baseUrl = '<?php echo Request::getPathUri(); ?>',
+			previewLink = '';
+
+	previewLink = baseUrl + 'index.php?_route=Notify&_task=generateUserReminders';
+	previewLink += '&usrLogin=' + frm['usrLogin'].value;
+	for (var i in frm['reminder[]']) {
+		console.log('frm[reminder[]]['+i+']', frm['reminder[]'][i].value, ' checked:', frm['reminder[]'][i].checked);
+		if (frm['reminder[]'][i].checked) {
+			previewLink += '&reminder[]=' + frm['reminder[]'][i].value;
+		}
+	}
+	console.log('frm[reminder[]].', frm['reminder[]']);
+	console.log('frm[reminder[]].value', frm['reminder[]'].value);
+	console.log('previewLink:', previewLink);
+	document.getElementById('reminderPreview').src = previewLink;
+}
+</script>
+<div>
+	<iframe id="reminderPreview" src="<?php echo $previewLink; ?>" style="width:100%;height:500px;max-height:500px;overflow:scroll;border:2px solid silver"></iframe>
+</div>
+<?php
+		SE_Layout::dol();
+	}
+
+	public function send($usrLogin, $reminders) {
+		DBG::_(true, true, "usrLogin", $usrLogin, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_(true, true, "reminders", $reminders, __CLASS__, __FUNCTION__, __LINE__);
+		$user = null;
+		$pdo = DB::getPDO();
+		$sth = $pdo->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
+		");
+		$sth->bindValue(':usr_login', $usrLogin, PDO::PARAM_STR);
+		$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);
+
+		$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");
+
+		$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']}";
+		ob_start();
+		$this->generateUserReminders($usrLogin, $reminders);
+		$message = ob_get_clean();
+
+		if (!mail($to, $subject, $message, $headers)) throw new Exception("Nie udało się wysłać powiadomienia");
+		echo 'Your message has been sent.';
+		throw new Exception(".EOF TODO");
+		DBG::_(true, true, "user", $user, __CLASS__, __FUNCTION__, __LINE__);
+	}
+
 	public function generateUserRemindersAction() {
 		$usrLogin = User::getLogin();
 		$usrLogin = (isset($_GET['usrLogin']))? V::get('usrLogin', '', $_GET, 'login') : $usrLogin;
 		if (!$usrLogin) die("Wrong user login!");
 
-		$userReminders = $this->getUserReminders($usrLogin);
-		if (empty($userReminders)) die("Brak zdefiniowanych powiadomień");
+		$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']; }
+		}
 
 		header('Content-Type: text/html; charset="UTF-8"');
-		$this->generateUserReminders($usrLogin, $userReminders);
+		$this->generateUserReminders($usrLogin, $reminders);
 	}
 
-	public function generateUserReminders($usrLogin, $userReminders) {
+	public function generateUserReminders($usrLogin, $reminders) {
 		echo '<html>';
 		echo '<body>';
 		echo '<div>';
 		?>
 <h1 style="<?php echo $this->inlineCss('h1'); ?>">Powiadomienia dla <code><?php echo $usrLogin; ?></code></h1>
-<table rules="all" cellspacing="0" style="border-color:#bbb;" cellpadding="8">
-<?php foreach ($userReminders as $reminder) : ?>
-	<tr>
-		<td colspan="4" style="<?php echo $this->inlineCss('td.header'); ?>"><strong><?php echo $this->printValue('what', $reminder['what']); ?></strong></td>
-	</tr>
-	<?php $lp = 0; foreach ($this->getTodoForUserReminder($usrLogin, $reminder['what']) as $todo) : ?>
+<?php foreach ($reminders as $reminderType) : ?>
+	<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) : ?>
 		<tr>
 			<td style="<?php echo $this->inlineCss('td.lp'); ?>"><?php echo ++$lp; ?></td>
 			<td style="<?php echo $this->inlineCss('td.' . $todo['_l_app_class']); ?>">
-				<?php echo $todo['_l_app_date']; ?>
-				<br><i style="color:#333;"><?php echo $todo['_l_app']; ?></i>
-				<?php if ($todo['ID'] > 0) : ?>
+				<span style="<?php echo $this->inlineCss('l_app_date.' . $todo['_l_app_class']); ?>"><?php echo $todo['_l_app_date']; ?></span>
+				<br><i style="<?php echo $this->inlineCss('l_app.target'); ?>"><?php echo $todo['_l_app']; ?></i>
+				<?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>
 				<?php endif; ?>
 			</td>
 			<td style="<?php echo $this->inlineCss('td.info'); ?>"><?php echo $todo['L_APPOITMENT_INFO']; ?></td>
 			<td style="<?php echo $this->inlineCss('td.record-info'); ?>">
-				<br><?php echo $todo['_title']; ?>
+				<?php echo $todo['_title']; ?>
 			</td>
 <?php if (V::get('DBG', '', $_GET)) : ?>
 			<td style="<?php echo $this->inlineCss('td.record-desc'); ?>"><pre style="border:1px solid red;max-height:80px;overflow:scroll"><?php print_r($todo); ?></pre></td>
 <?php endif; ?>
 		</tr>
 	<?php endforeach; ?>
+	</table>
 <?php endforeach; ?>
-</table>
 <?php
 		echo '</div>';// .container
 		echo '</body>';
@@ -84,7 +227,12 @@ class Route_Notify extends RouteBase {
 	public function editLink($todo) {
 		$idTblZasob = 0;
 		$idRecord = 0;
-		if ('zasob' == $todo['_task_type']) {
+		if ('zasob' == $todo['_task_type']
+				|| 'proces' == $todo['_task_type']
+				|| 'projekt' == $todo['_task_type']
+				|| 'koresp' == $todo['_task_type']
+				|| 'task' == $todo['_task_type']
+			 ) {
 			$idTblZasob = $todo['_idZasobTable'];
 			$idRecord = $todo['ID'];
 		}
@@ -100,6 +248,24 @@ class Route_Notify extends RouteBase {
 		return implode("", $urlParts);
 	}
 
+	public function readMsgLink($todo) {
+		$idMsg = 0;
+		if ('msg' == $todo['_task_type']) {
+			$idMsg = $todo['idMsg'];
+		}
+		if (!$idMsg) return '#' . json_encode($todo);
+		if (!$idMsg) return '#';
+
+		$urlParts = array();
+		$urlParts[] = Request::getPathUri();
+		$urlParts[] = 'index.php';
+		$urlParts[] = "?_route=UserMsgs";
+		$urlParts[] = "&_task=read";
+		$urlParts[] = "&usrLogin={$usrLogin}";
+		$urlParts[] = "&id={$idMsg}";
+		return implode("", $urlParts);
+	}
+
 	public function inlineCss($cssSelector) {
 		$baseCss = array();
 		$baseCss['font-family'] = 'Arial, Helvetica, sans-serif';
@@ -108,9 +274,11 @@ class Route_Notify extends RouteBase {
 
 		$customCss = array(
 			'h1' => array(),
-			'td.header' => array('background' => '#eee'),
+			'header' => array('background' => '#eee', 'padding' => '4px 10px', 'margin-top' => '6px'),
+			'l_app.target' => array('color' => '#999', 'white-space' => 'nowrap'),
 			'td.date-base' => array('white-space' => 'nowrap'),
 			'td.lp' => array('color' => 'silver'),
+			'l_app_date-base' => array('white-space' => 'nowrap'),
 			'a' => array('color' => '#337ab7', 'text-decoration' => 'none')
 		);
 
@@ -120,6 +288,10 @@ class Route_Notify extends RouteBase {
 			$css = V::extend($css, $customCss['td.date-base']);
 			$css = V::extend($css, array('color' => $this->getTodoDateCssColor(substr($cssSelector, 3))));
 		}
+		if ('l_app_date.date-' == substr($cssSelector, 0, 16)) {
+			$css = V::extend($css, $customCss['l_app_date-base']);
+			$css = V::extend($css, array('color' => $this->getTodoDateCssColor(substr($cssSelector, 11))));
+		}
 
 		$inlineCss = array();
 		foreach ($css as $cssPropName => $cssValue) {
@@ -146,10 +318,32 @@ class Route_Notify extends RouteBase {
 	public function getTodoForUserReminder($usrLogin, $reminderWhat) {
 		$listTodo = array();
 		if ('msgs' == $reminderWhat) {
-//			$todo = array();
-//			$todo['_title'] = 'test msgs id';
-//			$todo['L_APPOITMENT_INFO'] = 'test msgs id';
-//			$listTodo[] = $todo;
+			$routeMsgs = Router::getRoute('UserMsgs');
+			$routeMsgs->_listLimit = 50;
+			$msgs = $routeMsgs->getMsgs('unread', $usrLogin);
+			foreach ($msgs as $idMsg => $msg) {
+				$todo = array();
+				$todo['_task_type'] = 'msg';
+				// [userTargetType] => group
+				// [userTargetName] => 2948
+				$todo['_l_app'] = '';
+				if ('group' == $msg['_raw']->userTargetType) {
+					$todo['_l_app'] = "do grupy {$msg['_raw']->userTargetName}";
+				} else if ('user' == $msg['_raw']->userTargetType) {
+					$todo['_l_app'] = "do {$msg['_raw']->userTargetName}";
+				}
+				$todo['idMsg'] = $idMsg;
+				$todo['_l_app_date'] = substr($msg['_raw']->A_RECORD_CREATE_DATE, 0, 10);
+				$todo['_l_app_class'] = 'date-PO_TERMINIE';
+				$todo['L_APPOITMENT_INFO'] = $msg['message'];
+				// $msg['_raw']->A_STATUS == 'WAITING'  => Unread
+				// $msg['_raw']->A_STATUS == 'NORMAL'   => Read
+				// $msg['_raw']->A_STATUS == 'OFF_SOFT' => Trash
+				$msgInfo = '<i style="color:silver">' . "Wysłane {$msg['_raw']->A_RECORD_CREATE_DATE} przez {$msg['_raw']->A_RECORD_CREATE_AUTHOR}" . '</i>';
+				if ('NORMAL' == $msg['_raw']->A_STATUS) $msgInfo .= '<br><i style="color:silver">' . "Przeczytane {$msg['_raw']->A_RECORD_UPDATE_DATE} przez {$msg['_raw']->A_RECORD_UPDATE_AUTHOR}" . '</i>';
+				$todo['_title'] = $msgInfo;
+				$listTodo[] = $todo;
+			}
 		}
 		else if ('l_app_projekty' == $reminderWhat) {
 			$tasks = $this->getPrzypomnijTasks($usrLogin);