egroupware/notifications/inc/class.notifications_popup.inc.php

221 lines
6.7 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 backends
2006-09-07 09:04:15 +02:00
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>, Christian Binder <christian@jaytraxx.de>
2006-09-25 12:20:46 +02:00
* @version $Id$
2006-09-07 09:04:15 +02:00
*/
/**
* Instant user notification with egroupware popup.
*
* @abstract egwpopup is a two stage notification. In the first stage
* notification is written into self::_notification_table.
* In the second stage a request from the client reads
2006-09-07 09:04:15 +02:00
* out the table to look if there is a notificaton for this
* client. The second stage is done in class.ajaxnotifications.inc.php
*
* Todo:
* - save the messages by uid instead of sessionid into the notification table, this
* has several advantages (users poll the messages via ajax from multiple logins, and
* do not have to read one message twice, poll after re-login with different sessionid)
* - delete message from the table only if the user has really seen it
* - if the above things are done we should get rid of rendering the links here,
* instead it should be done by the ajax class, so sessionids in links could be possible then
*
* (multidisplay is supported)
*
2006-09-07 09:04:15 +02:00
*/
class notifications_popup implements notifications_iface {
2006-09-07 09:04:15 +02:00
/**
* 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;
/**
* holds html object to render elements
*
* @var object
*/
private $html;
2006-09-07 09:04:15 +02:00
/**
* constructor of notifications_egwpopup
2006-09-07 09:04:15 +02:00
*
* @param object $_sender
* @param object $_recipient
* @param object $_config
2006-09-07 09:04:15 +02:00
* @param object $_preferences
*/
public function __construct($_sender, $_recipient, $_config = null, $_preferences = null) {
if(!is_object($_sender)) { throw new Exception("no sender given."); }
if(!is_object($_recipient)) { throw new Exception("no recipient given."); }
$this->sender = $_sender;
$this->recipient = $_recipient;
$this->config = $_config;
$this->preferences = $_preferences;
2006-09-07 09:04:15 +02:00
$this->db = &$GLOBALS['egw']->db;
$this->db->set_app( self::_appname );
$this->html = html::singleton();
2006-09-07 09:04:15 +02:00
}
/**
* sends notification if user is online
*
* @param array $_messages
* @param string $_subject
* @param array $_links
* @param array $_attachments
2006-09-07 09:04:15 +02:00
*/
public function send(array $_messages, $_subject = false, $_links = false, $_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->account_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->account_lid} isn't online. Can't send notification via popup");
$message = $this->render_infos($_subject)
.$this->html->hr()
.$_messages['html']
.$this->html->hr()
.$this->render_links($_links);
$this->save( $message, $user_sessions );
2006-09-07 09:04:15 +02:00
}
2006-09-07 09:04:15 +02:00
/**
* saves notification into database so that the client can fetch it from there
2006-09-07 09:04:15 +02:00
*
* @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->account_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
}
/**
* renders plaintext/html links from given link array
* should be moved to the ajax class later - like mentioned in the Todo
*
* @param array $_links
* @return html rendered link(s) as complete string with jspopup or a new window
*/
private function render_links($_links = false) {
if(!is_array($_links) || count($_links) == 0) { return false; }
$newline = "<br />";
$rendered_links = array();
foreach($_links as $link) {
if(!$link->popup) { $link->view['no_popup'] = 1; }
$url = $this->html->link('/index.php', $link->view);
// do not expose sensitive data
$url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',$url);
// extract application-icon from menuaction
if($link->view['menuaction']) {
$menuaction_arr = explode('.',$link->view['menuaction']);
$application = $menuaction_arr[0];
$image = $application ? $this->html->image($application,'navbar',$link->text,'align="middle" style="width: 24px; margin-right: 0.5em;"') : '';
} else {
$image = '';
}
if($link->popup) {
$dimensions = explode('x', $link->popup);
$rendered_links[] = $this->html->div($image.$link->text,'onclick="'.$this->jspopup($url, '_blank', $dimensions[0], $dimensions[1]).'"','link');
} else {
$rendered_links[] = $this->html->div($this->html->a_href($image.$link->text, $url, false, 'target="_blank"'),'','link');
}
}
if(count($rendered_links) > 0) {
return $this->html->bold(lang('Linked entries:')).$newline.implode($newline,$rendered_links);
}
}
/**
* returns javascript to open a popup window: window.open(...)
*
* @param string $link link or this.href
* @param string $target='_blank' name of target or this.target
* @param int $width=750 width of the window
* @param int $height=400 height of the window
* @return string javascript (using single quotes)
*/
private function jspopup($link,$target='_blank',$width=750,$height=410)
{
return 'egw_openWindowCentered2('.($link == 'this.href' ? $link : "'".$link."'").','.
($target == 'this.target' ? $target : "'".$target."'").",$width,$height,'yes')";
}
/**
* renders additional infos from sender and subject
*
* @param string $_subject
* @return html rendered info as complete string
*/
private function render_infos($_subject = false) {
$infos = array();
$newline = "<br />";
$sender = $this->sender->account_fullname ? $this->sender->account_fullname : $this->sender_account_email;
$infos[] = lang('Message from').': '.$sender;
if(!empty($_subject)) { $infos[] = $this->html->bold($_subject); }
return implode($newline,$infos);
}
2006-09-07 09:04:15 +02:00
}