2006-09-07 09:04:15 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare - Notifications
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package notifications
|
|
|
|
* @subpackage ajaxpopup
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Cornelius Weiss <nelius@cwtech.de>
|
2006-09-25 12:20:46 +02:00
|
|
|
* @version $Id$
|
2006-09-07 09:04:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
require_once('class.iface_notification.inc.php');
|
2007-11-22 09:29:16 +01:00
|
|
|
require_once(EGW_INCLUDE_ROOT.'/phpgwapi/inc/class.config.inc.php');
|
2006-09-07 09:04:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instant user notification with egroupware popup.
|
|
|
|
* egwpopup is a two stage notification. In the first stage
|
|
|
|
* notification is written into self::_notification_egwpopup
|
|
|
|
* table. In the second stage a request from the client reads
|
|
|
|
* out the table to look if there is a notificaton for this
|
|
|
|
* client. (multidisplay is supported)
|
|
|
|
*/
|
|
|
|
class notification_popup implements iface_notification {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification window {div|alert}
|
|
|
|
*/
|
|
|
|
const _window = 'div';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appname
|
|
|
|
*/
|
|
|
|
const _appname = 'notifications';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification table in SQL database
|
|
|
|
*/
|
|
|
|
const _notification_table = 'egw_notificationpopup';
|
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user who sends the message
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $sender;
|
|
|
|
|
2006-09-07 09:04:15 +02:00
|
|
|
/**
|
|
|
|
* holds account object for user to notify
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
2007-11-22 09:29:16 +01:00
|
|
|
private $recipient;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* holds config object (sitewide application config)
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $config;
|
2006-09-07 09:04:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* holds preferences object of user to notify
|
|
|
|
*
|
2006-09-25 09:00:51 +02:00
|
|
|
* @var object
|
2006-09-07 09:04:15 +02:00
|
|
|
*/
|
|
|
|
private $preferences;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* holds db object of SQL database
|
|
|
|
*
|
2006-09-25 09:00:51 +02:00
|
|
|
* @var egw_db
|
2006-09-07 09:04:15 +02:00
|
|
|
*/
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor of notification_egwpopup
|
|
|
|
*
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param object $_sender
|
|
|
|
* @param object $_recipient
|
2007-12-10 09:26:29 +01:00
|
|
|
* @param object $_config
|
2006-09-07 09:04:15 +02:00
|
|
|
* @param object $_preferences
|
|
|
|
*/
|
2007-11-22 09:29:16 +01:00
|
|
|
public function __construct( $_sender=false, $_recipient=false, $_config=false, $_preferences=false) {
|
|
|
|
// If we are called from class notification sender, recipient, config and prefs are objects.
|
2006-09-07 09:04:15 +02:00
|
|
|
// otherwise we have to fetch this objects for current user.
|
2007-11-22 09:29:16 +01:00
|
|
|
if (!is_object($_sender)) {
|
|
|
|
$this->sender = (object) $GLOBALS['egw']->accounts->read($_sender);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->sender = $_sender;
|
|
|
|
}
|
|
|
|
if (!is_object($_recipient)) {
|
|
|
|
$this->recipient = (object) $GLOBALS['egw']->accounts->read($_recipient);
|
2006-09-07 09:04:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2007-11-22 09:29:16 +01:00
|
|
|
$this->recipient = $_recipient;
|
|
|
|
}
|
|
|
|
if(!is_object($_config)) {
|
|
|
|
$config = new config(self::_appname);
|
|
|
|
$this->config = (object) $config->read_repository();
|
|
|
|
} else {
|
|
|
|
$this->config = $_config;
|
|
|
|
}
|
|
|
|
if(!is_object($_preferences)) {
|
2007-12-10 09:26:29 +01:00
|
|
|
$prefs = new preferences($this->recipient->account_id);
|
2007-11-22 09:29:16 +01:00
|
|
|
$preferences = $prefs->read();
|
|
|
|
$this->preferences = (object)$preferences[self::_appname ];
|
|
|
|
} else {
|
|
|
|
$this->preferences = $_preferences;
|
2006-09-07 09:04:15 +02:00
|
|
|
}
|
|
|
|
$this->db = &$GLOBALS['egw']->db;
|
|
|
|
$this->db->set_app( self::_appname );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sends notification if user is online
|
|
|
|
*
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param string $_subject
|
|
|
|
* @param array $_messages
|
|
|
|
* @param array $_attachments
|
2006-09-07 09:04:15 +02:00
|
|
|
*/
|
2007-11-22 09:29:16 +01:00
|
|
|
public function send( $_subject = false, $_messages, $_attachments = false) {
|
2007-12-10 09:26:29 +01:00
|
|
|
if(!is_object($this->sender)) {
|
|
|
|
throw new Exception("No sender given.");
|
|
|
|
}
|
2006-09-07 09:04:15 +02:00
|
|
|
$sessions = $GLOBALS['egw']->session->list_sessions(0, 'asc', 'session_dla', true);
|
|
|
|
$user_sessions = array();
|
|
|
|
foreach ($sessions as $session) {
|
2007-12-10 09:26:29 +01:00
|
|
|
if ($session['session_lid'] == $this->recipient->account_lid. '@'. $GLOBALS['egw_info']['user']['domain']) {
|
2006-09-07 09:04:15 +02:00
|
|
|
$user_sessions[] = $session['session_id'];
|
|
|
|
}
|
|
|
|
}
|
2007-12-10 09:26:29 +01:00
|
|
|
if ( empty($user_sessions) ) throw new Exception("User {$this->recipient->account_lid} isn't online. Can't send notification via popup");
|
|
|
|
$this->save( $_messages['html']['info_sender'].$_messages['html']['info_subject'].$_messages['html']['text'].$_messages['html']['link_jspopup'], $user_sessions );
|
2006-09-07 09:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all notification for current user.
|
|
|
|
* Requests and response is done via xajax
|
|
|
|
*
|
|
|
|
* @return xajax response
|
|
|
|
*/
|
|
|
|
public function ajax_get_notifications() {
|
|
|
|
$response =& new xajaxResponse();
|
|
|
|
$session_id = $GLOBALS['egw_info']['user']['sessionid'];
|
|
|
|
$message = '';
|
|
|
|
$this->db->select(self::_notification_table,
|
|
|
|
'*', array(
|
2007-12-10 09:26:29 +01:00
|
|
|
'account_id' => $this->recipient->account_id,
|
2006-09-07 09:04:15 +02:00
|
|
|
'session_id' => $session_id,
|
|
|
|
),
|
|
|
|
__LINE__,__FILE__);
|
|
|
|
if ($this->db->num_rows() != 0) {
|
|
|
|
while ($notification = $this->db->row(true)) {
|
|
|
|
switch (self::_window ) {
|
|
|
|
case 'div' :
|
2007-11-22 09:29:16 +01:00
|
|
|
$response->addScriptCall('append_notification_message',$notification['message']);
|
2006-09-07 09:04:15 +02:00
|
|
|
break;
|
|
|
|
case 'alert' :
|
2007-11-22 09:29:16 +01:00
|
|
|
$response->addAlert($notification['message']);
|
2006-09-07 09:04:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-11-22 09:29:16 +01:00
|
|
|
$myval=$this->db->delete(self::_notification_table,array(
|
2007-12-10 09:26:29 +01:00
|
|
|
'account_id' => $this->recipient->account_id,
|
2006-09-07 09:04:15 +02:00
|
|
|
'session_id' => $session_id,
|
|
|
|
),__LINE__,__FILE__);
|
|
|
|
|
|
|
|
switch (self::_window) {
|
|
|
|
case 'div' :
|
2007-11-22 09:29:16 +01:00
|
|
|
switch($this->preferences->egwpopup_verbosity) {
|
|
|
|
case 'low':
|
|
|
|
$response->addScript('notificationbell_switch("active");');
|
|
|
|
break;
|
|
|
|
case 'high':
|
|
|
|
$response->addAlert(lang('eGroupware has some notifications for you'));
|
|
|
|
$response->addScript('notificationwindow_display();');
|
|
|
|
break;
|
|
|
|
case 'medium':
|
|
|
|
default:
|
|
|
|
$response->addScript('notificationwindow_display();');
|
|
|
|
break;
|
|
|
|
}
|
2006-09-07 09:04:15 +02:00
|
|
|
break;
|
|
|
|
case 'alert' :
|
2007-11-22 09:29:16 +01:00
|
|
|
// nothing to do for now
|
2006-09-07 09:04:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $response->getXML();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* saves notification into database so that the client can fetch it from
|
|
|
|
* there via notification->get
|
|
|
|
*
|
|
|
|
* @param string $_message
|
|
|
|
* @param array $_user_sessions
|
|
|
|
*/
|
|
|
|
private function save( $_message, array $_user_sessions ) {
|
|
|
|
foreach ($_user_sessions as $user_session) {
|
|
|
|
$result =& $this->db->insert( self::_notification_table, array(
|
2007-12-10 09:26:29 +01:00
|
|
|
'account_id' => $this->recipient->account_id,
|
2006-09-07 09:04:15 +02:00
|
|
|
'session_id' => $user_session,
|
|
|
|
'message' => $_message
|
|
|
|
), false,__LINE__,__FILE__);
|
|
|
|
}
|
2007-11-22 09:29:16 +01:00
|
|
|
if ($result === false) throw new Exception("Can't save notification into SQL table");
|
2006-09-07 09:04:15 +02:00
|
|
|
}
|
|
|
|
}
|