From 5a1a3879ac93960b4baaed9f6175fd22ab0d6d24 Mon Sep 17 00:00:00 2001 From: Christian Binder Date: Wed, 11 Feb 2009 19:32:26 +0000 Subject: [PATCH] First draft of mail notifications, sponsored by kk-it.net. Checkout the forum for more information. IMPORTANT: You probably have to clear your browsers cache before using it. --- .../inc/class.ajaxnotifications.inc.php | 160 ++++++++++++++++-- notifications/js/notificationajaxpopup.js | 1 + 2 files changed, 146 insertions(+), 15 deletions(-) diff --git a/notifications/inc/class.ajaxnotifications.inc.php b/notifications/inc/class.ajaxnotifications.inc.php index f74912579b..091f1742e7 100644 --- a/notifications/inc/class.ajaxnotifications.inc.php +++ b/notifications/inc/class.ajaxnotifications.inc.php @@ -18,6 +18,11 @@ class ajaxnotifications { */ const _appname = 'notifications'; + /** + * Mailappname + */ + const _mailappname = 'felamimail'; + /** * Notification table in SQL database */ @@ -38,25 +43,48 @@ class ajaxnotifications { private $config; /** - * holds preferences object of user to notify + * holds preferences array of user to notify * - * @var object + * @var array */ private $preferences; /** * reference to global db object * - * @var egw_db + * @var db */ private $db; - - + + /** + * holds the users session data + * + * @var array + */ + var $session_data; + + /** + * holds the users session data defaults + * + * @var array + */ + var $session_data_defaults = array( + 'notified_mail_uids' => array(), + ); + + /** + * the xml response object + * + * @var response + */ + private $response; + /** * constructor of ajaxnotifications * */ public function __construct() { + $this->response = new xajaxResponse(); $this->recipient = (object)$GLOBALS['egw']->accounts->read(); $config = new config(self::_appname); @@ -64,11 +92,17 @@ class ajaxnotifications { $prefs = new preferences($this->recipient->account_id); $preferences = $prefs->read(); - $this->preferences = (object)$preferences[self::_appname]; + $this->preferences = $prefs->read(); $this->db = $GLOBALS['egw']->db; } + /** + * destructor of ajaxnotifications + * + */ + public function __destruct() {} + /** * Gets all egwpopup notification for calling user. * Requests and response is done via xajax @@ -76,7 +110,6 @@ class ajaxnotifications { * @return xajax response */ public function get_popup_notifications() { - $response =& new xajaxResponse(); $session_id = $GLOBALS['egw_info']['user']['sessionid']; $message = ''; $rs = $this->db->select(self::_notification_table, @@ -87,27 +120,124 @@ class ajaxnotifications { __LINE__,__FILE__,false,'',self::_appname); if ($rs->NumRows() > 0) { foreach ($rs as $notification) { - $response->addScriptCall('append_notification_message',$notification['message']); + $this->response->addScriptCall('append_notification_message',$notification['message']); } $myval=$this->db->delete(self::_notification_table,array( 'account_id' => $this->recipient->account_id, 'session_id' => $session_id, ),__LINE__,__FILE__,self::_appname); - switch($this->preferences->egwpopup_verbosity) { + switch($this->preferences[self::_appname]['egwpopup_verbosity']) { case 'low': - $response->addScript('notificationbell_switch("active");'); + $this->response->addScript('notificationbell_switch("active");'); break; case 'high': - $response->addAlert(lang('eGroupWare has notifications for you')); - $response->addScript('notificationwindow_display();'); + $this->response->addAlert(lang('eGroupWare has notifications for you')); + $this->response->addScript('notificationwindow_display();'); break; case 'medium': default: - $response->addScript('notificationwindow_display();'); + $this->response->addScript('notificationwindow_display();'); break; } } - return $response->getXML(); + return $this->response->getXML(); } -} \ No newline at end of file + + /** + * checks users mailbox and sends a notification if new mails have arrived + * + * @return xajax response + */ + public function check_mailbox() { + if(!isset($this->preferences[self::_mailappname]['notify_folders'])) { + return $this->response->getXML(); //no pref set for notifying - exit + } + $notify_folders = explode(',', $this->preferences[self::_mailappname]['notify_folders']); + if (count($notify_folders) == 0) { + return $this->response->getXML(); //no folders configured for notifying - exit + } + + $bofelamimail = new bofelamimail($GLOBALS['egw']->translation->charset()); + if(!$bofelamimail->openConnection()) { + // TODO: This is ugly. Log a bit nicer! + error_log(self::_appname.' (user: '.$this->recipient->account_lid.'): cannot connect to mailbox. Please check your prefs!'); + return $this->response->getXML(); // cannot connect to mailbox + } + + $this->restore_session_data(); + + $recent_messages = array(); + foreach($notify_folders as $id=>$notify_folder) { + $headers = $bofelamimail->getHeaders($notify_folder, 1, false, 0, true, array('status'=>'UNSEEN')); + if(is_array($headers['header']) && count($headers['header']) > 0) { + foreach($headers['header'] as $id=>$header) { + // check if unseen mail has already been notified + if(!in_array($header['uid'], $this->session_data['notified_mail_uids'])) { + // got a REAL recent message + $header['folder'] = $notify_folder; + $recent_messages[] = $header; + } + } + } + } + + if(count($recent_messages) > 0) { + // create notify messages and save notification status in user session + if(count($recent_messages) == 1) { + $notify_subject = lang("You've got a new mail"); + } else { + $notify_subject = lang("You've got new mails"); + } + $notify_message = ''; + foreach($recent_messages as $id=>$recent_message) { + $notify_message .= '' + .'' + .'' + .'' + .''; + // save notification status + $this->session_data['notified_mail_uids'][] = $recent_message['uid']; + } + $notify_message .= '
'.$recent_message['folder'].''.$recent_message['subject'].''.$recent_message['sender_address'].'
'; + + // send notification + $notification = new notifications(); + $notification->set_receivers(array($this->recipient->account_id)); + $notification->set_message($notify_message); + $notification->set_sender($this->recipient->account_id); + $notification->set_subject($notify_subject); + $notification->set_skip_backends(array('email')); + $notification->send(); + } + + $this->save_session_data(); + return $this->response->getXML(); + } + + /** + * restores the users session data for notifications + * + * @return boolean true + */ + private function restore_session_data() { + $session_data = $GLOBALS['egw']->session->appsession('session_data',self::_appname); + if(is_array($session_data)) { + $this->session_data = $session_data; + } else { + $this->session_data = $this->session_data_defaults; + } + + return true; + } + + /** + * saves the users session data for notifications + * + * @return boolean true + */ + private function save_session_data() { + $GLOBALS['egw']->session->appsession('session_data',self::_appname,$this->session_data); + return true; + } +} diff --git a/notifications/js/notificationajaxpopup.js b/notifications/js/notificationajaxpopup.js index a5129961c8..4437a6d0fc 100644 --- a/notifications/js/notificationajaxpopup.js +++ b/notifications/js/notificationajaxpopup.js @@ -18,6 +18,7 @@ function notificationwindow_setTimeout() { window.setTimeout("notificationwindow_refresh();", 60000); } function notificationwindow_refresh() { + xajax_doXMLHTTP("notifications.ajaxnotifications.check_mailbox"); xajax_doXMLHTTP("notifications.ajaxnotifications.get_popup_notifications"); notificationwindow_setTimeout(); }