egroupware_official/notifications/inc/class.notification_popup.inc.php

210 lines
5.6 KiB
PHP
Raw Normal View History

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');
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';
/**
* 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
*/
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
*
* @var object
2006-09-07 09:04:15 +02:00
*/
private $preferences;
/**
* holds db object of SQL database
*
* @var egw_db
2006-09-07 09:04:15 +02:00
*/
private $db;
/**
* constructor of notification_egwpopup
*
* @param object $_sender
* @param object $_recipient
2006-09-07 09:04:15 +02:00
* @param object $_preferences
*/
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.
if (!is_object($_sender)) {
$this->sender = (object) $GLOBALS['egw']->accounts->read($_sender);
$this->sender->id =& $this->sender->account_id;
}
else {
$this->sender = $_sender;
}
if (!is_object($_recipient)) {
$this->recipient = (object) $GLOBALS['egw']->accounts->read($_recipient);
$this->recipient->id =& $this->recipient->account_id;
2006-09-07 09:04:15 +02:00
}
else {
$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)) {
$prefs = new preferences($this->recipient->id);
$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
*
* @param string $_subject
* @param array $_messages
* @param array $_attachments
2006-09-07 09:04:15 +02:00
*/
public function send( $_subject = false, $_messages, $_attachments = false) {
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) {
if ($session['session_lid'] == $this->recipient->lid. '@'. $GLOBALS['egw_info']['user']['domain']) {
2006-09-07 09:04:15 +02:00
$user_sessions[] = $session['session_id'];
}
}
if ( empty($user_sessions) ) throw new Exception("User {$this->recipient->lid} isn't online. Can't send notification via popup");
$this->save( $_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(
'account_id' => $this->recipient->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' :
$response->addScriptCall('append_notification_message',$notification['message']);
2006-09-07 09:04:15 +02:00
break;
case 'alert' :
$response->addAlert($notification['message']);
2006-09-07 09:04:15 +02:00
break;
}
}
$myval=$this->db->delete(self::_notification_table,array(
'account_id' => $this->recipient->id,
2006-09-07 09:04:15 +02:00
'session_id' => $session_id,
),__LINE__,__FILE__);
switch (self::_window) {
case 'div' :
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' :
// 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(
'account_id' => $this->recipient->id,
2006-09-07 09:04:15 +02:00
'session_id' => $user_session,
'message' => $_message
), false,__LINE__,__FILE__);
}
if ($result === false) throw new Exception("Can't save notification into SQL table");
2006-09-07 09:04:15 +02:00
}
}