new notification module

This commit is contained in:
Cornelius Weiß 2006-09-07 07:04:15 +00:00
parent c4bb0dcf98
commit c57dadd8f7
15 changed files with 597 additions and 0 deletions

View File

@ -0,0 +1,31 @@
<?php
/**
* eGroupWare - Notifications
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package notifications
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>
* @version $Id: $
*/
/**
* Instant user notification
*/
interface iface_notification {
/**
* constructor
*
* @param object $_account
* @param object $_preferences
*/
public function __construct( $_account, $_preferences );
/**
* sends notification
*
* @param string $_message
*/
public function send( $_message );
}

View File

@ -0,0 +1,128 @@
<?php
/**
* eGroupWare - Notifications
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package notifications
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>
* @version $Id: $
*/
/**
* Notifies users according to their preferences.
*
* @abstract NOTE:Notifications are small messages. No subject and no attechments.
* If you need this kind of elements you probably want to send a mail, don't you :-)
* @abstract NOTE: This is for instant notifications. If you need time dependend notifications use the
* asyncservices wrapper!
*
* The classes doing the notifications are called notification_<method> and should only be
* called from this class.
* The <method> gets extractd out of the preferences labels.
*
*/
final class notification {
/**
* Appname
*/
const _appname = 'notifications';
/**
* array with objects of receivers
* @var array
*/
private $receivers = array();
/**
* holds notification message
* @var string
*/
private $message = '';
/**
* sets notification message
* @param string &$message
*/
public function set_message($_message) {
$this->message = $_message;
return true;
}
/**
* Set receivers for the current notification
*
* @param array $_receivers array with objects of accounts
* as long as the accounts class isn't a nice object, it's an array of account id's :-(
*/
public function set_receivers(array $_receivers) {
foreach ($_receivers as $receiver_id) {
$receiver = $GLOBALS['egw']->accounts->get_account_data($receiver_id);
$receiver[$receiver_id]['id'] = $receiver_id;
$this->receivers[$receiver_id] = (object)$receiver[$receiver_id];
}
}
/**
* sends notification
*/
public function send() {
if (empty($this->receivers) || !$this->message) {
throw new Exception('Error: Coud not send notification. No receiver or no message where supplied');
}
foreach ($this->receivers as $receiver) {
$prefs = new preferences($receiver->id);
$preferences = $prefs->read();
$preferences = (object)$preferences[self::_appname ];
if (!$preferences->disable_ajaxpopup) {
$notification_backends[] = 'notification_popup';
}
$send_succseed = 0;
foreach ((array)$notification_backends as $notification_backend) {
try {
require_once(EGW_INCLUDE_ROOT. SEP. self::_appname. SEP. 'inc'. SEP. 'class.'. $notification_backend. '.inc.php');
$obj = @new $notification_backend( $receiver, $preferences );
if ( !is_a( $obj, iface_notification )) {
unset ( $obj );
throw new Exception('Error: '.$notification_backend. ' is no implementation of iface_notification');
}
$obj->send( $this->message );
$send_succseed++;
}
catch (Exception $exception) {
$send_succseed--;
echo $exception->getMessage(), "\n";
}
}
if ($send_succseed == 0) {
throw new Exception('Error: Was not able to send Notification to user!');
}
}
}
/**
* gets message
*
* @return string
*/
public function get_message() {
return $this->message;
}
/**
* gets receivers
*
* @return array of account objects
*/
public function get_receivers() {
return $this->receivers;
}
}

View File

@ -0,0 +1,163 @@
<?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>
* @version $Id: $
*/
require_once('class.iface_notification.inc.php');
/**
* 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 to notify
*
* @var object
*/
private $account;
/**
* holds preferences object of user to notify
*
* @var unknown_type
*/
private $preferences;
/**
* holds db object of SQL database
*
* @var unknown_type
*/
private $db;
/**
* constructor of notification_egwpopup
*
* @param object $_account
* @param object $_preferences
*/
public function __construct( $_account=false, $_preferences=false) {
// If we are called from class notification account and prefs are objects.
// otherwise we have to fetch this objects for current user.
if (!is_object($_account)) {
$account_id = $GLOBALS['egw_info']['user']['account_id'];
$this->account = $GLOBALS['egw']->accounts->get_account_data($account_id);
$this->account[$account_id]['id'] = $account_id;
$this->account = (object)$this->account[$account_id];
}
else {
$this->account = $_account;
}
$this->preferences = is_object($_preferences) ? $_preferences : $GLOBALS['egw']->preferences;
$this->db = &$GLOBALS['egw']->db;
$this->db->set_app( self::_appname );
}
/**
* sends notification if user is online
*
* @param string $_message
*/
public function send( $_message ) {
$sessions = $GLOBALS['egw']->session->list_sessions(0, 'asc', 'session_dla', true);
$user_sessions = array();
foreach ($sessions as $session) {
if ($session['session_lid'] == $this->account->lid. '@'. $GLOBALS['egw_info']['user']['domain']) {
$user_sessions[] = $session['session_id'];
}
}
if ( empty($user_sessions) ) throw new Exception("Notice: User isn't online. Can't send notification via popup");
$this->save( $_message, $user_sessions );
}
/**
* 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->account->id,
'session_id' => $session_id,
),
__LINE__,__FILE__);
if ($this->db->num_rows() != 0) {
while ($notification = $this->db->row(true)) {
switch (self::_window ) {
case 'div' :
$message .= nl2br($notification['message']). '<br>';
break;
case 'alert' :
$message .= $notification['message']. "\n";
break;
}
}
$this->db->delete(self::_notification_table,array(
'account_id' =>$this->account->id,
'session_id' => $session_id,
),__LINE__,__FILE__);
switch (self::_window) {
case 'div' :
$response->addAppend('notificationwindow_message','innerHTML',$message);
$response->addScript('notificationwindow_display();');
break;
case 'alert' :
$response->addAlert($message);
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->account->id,
'session_id' => $user_session,
'message' => $_message
), false,__LINE__,__FILE__);
}
if ($result === false) throw new Exception("Error: Can't save notification into SQL table");
}
}

View File

@ -0,0 +1,83 @@
<?php
/**
* eGroupWare - Notifications
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package notifications
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>
* @version $Id: $
*/
require_once(EGW_INCLUDE_ROOT. SEP. 'etemplate'. SEP. 'inc'. SEP. 'class.etemplate.inc.php');
class uinotificationprefs {
const _appname = 'notifications';
public $public_functions = array(
'index' => true,
);
/**
* This are the preferences for notifications
*
* @var array
*/
private $notification_preferences = array(
'disable_ajaxpopup' => '', // bool: true / false
);
/**
* Holds preferences object for current user
*
* @var object
*/
private $preferences;
public function __construct($_account_id = 0, $_referer = false) {
$account_id = $_account_id > 0 ? $_account_id : $GLOBALS['egw_info']['user']['account_id'];
$this->preferences = new preferences($account_id);
$GLOBALS['egw_info']['flags']['app_header'] = lang('Preferences for notification');
}
public function index($_content = false) {
$et = new etemplate(self::_appname. '.prefsindex');
$content = array();
$sel_options = array();
$readonlys = array();
$preserv = array();
if (is_array($_content)) {
if (is_array($_content['button'])) {
$preferences = array_intersect_key($_content, $this->notification_preferences);
list($button) = each($_content['button']);
switch ($button) {
case 'save' :
$this->save($preferences);
$GLOBALS['egw']->redirect_link($_content['referer']);
case 'apply' :
$this->save($preferences);
break;
case 'cancel' :
default :
$GLOBALS['egw']->redirect_link($_content['referer']);
}
}
}
else {
$preferences = $this->preferences->read();
$preferences = $preferences[self::_appname];
$preserv['referer'] = $GLOBALS['egw']->common->get_referer();
}
$content = array_merge($content,(array)$preferences);
return $et->exec(self::_appname. '.uinotificationprefs.index',$content,$sel_options,$readonlys,$preserv);
}
private function save($_preferences) {
$this->preferences->read();
$this->preferences->user[self::_appname] = $_preferences;
$this->preferences->save_repository();
return;
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* eGroupWare - Notifications
* serves the hook "after_navbar" to create the notificationwindow
*
* @abstract notificatonwindow is an empty and non displayed 1px div which gets rezised
* and populated if a notification is about to be displayed.
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package notifications
* @subpackage ajaxpoup
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>
* @version $Id: $
* @todo check if user wants notifications via egw popup!
*/
$GLOBALS['egw']->translation->add_app('notifications');
if (!$GLOBALS['egw_info']['user']['preferences']['notifications']['disable_ajaxpopup']) {
echo '<script src="'. $GLOBALS['egw_info']['server']['webserver_url']. SEP. 'notifications'. SEP. 'js'. SEP. 'notificationajaxpopup.js'. '" type="text/javascript"></script>';
echo '<script type="text/javascript">notificationwindow_init()</script>';
echo '
<div id="notificationwindow" style="display: none; z-index: 999;">
<div id="divAppboxHeader">'. lang('Notification'). '</div>
<div id="divAppbox">
<div id="notificationwindow_message"</div>
<center><input type="submit" value="'. lang('ok'). '" onClick="notificationwindow_button_ok();"></center>
</div>
</div>
';
}

View File

@ -0,0 +1,23 @@
<?php
/**
* eGroupWare - Notifications
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package notifications
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>
* @version $Id: $
*/
$title = $appname;
$file = array(
'Preferences' => $GLOBALS['egw']->link(
'/index.php',
'menuaction=notifications.uinotificationprefs.index',
'_account_id=0',
'_referer='
),
);
display_section($appname,$title,$file);
?>

View File

@ -0,0 +1,41 @@
/**
* eGroupWare - Notifications
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package notifications
* @subpackage ajaxpoup
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>
* @version $Id: $
*/
function notificationwindow_init() {
window.setTimeout("notificationwindow_refresh();", 1000);
}
function notificationwindow_setTimeout() {
window.setTimeout("notificationwindow_refresh();", 10000);
}
function notificationwindow_refresh() {
xajax_doXMLHTTP("notifications.notification_popup.ajax_get_notifications");
notificationwindow_setTimeout();
}
function notificationwindow_display() {
var notificationwindow;
notificationwindow = document.getElementById("notificationwindow");
notificationwindow.style.display = "inline";
notificationwindow.style.position = "absolute";
notificationwindow.style.width = "500px";
notificationwindow.style.left = screen.availWidth/2 - 250 + "px";
notificationwindow.style.top = screen.availHeight/4 + "px";
notificationwindow.style.height = "100%";
}
function notificationwindow_button_ok() {
var notificationwindow;
var notificationwindow_message;
notificationwindow = document.getElementById("notificationwindow");
notificationwindow_message = document.getElementById("notificationwindow_message");
notificationwindow.style.display = "none";
notificationwindow_message.innerHTML = "";
}

View File

@ -0,0 +1,9 @@
<?php
// eTemplates for Application 'notifications', generated by soetemplate::dump4setup() 2006-09-06 19:42
/* $Id$ */
$templ_version=1;
$templ_data[] = array('name' => 'notifications.prefsindex','template' => '','lang' => '','group' => '0','version' => '','data' => 'a:1:{i:0;a:4:{s:4:"type";s:4:"grid";s:4:"data";a:6:{i:0;a:0:{}i:1;a:2:{s:1:"A";a:1:{s:4:"type";s:5:"label";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:2;a:2:{s:1:"A";a:2:{s:4:"type";s:8:"checkbox";s:4:"name";s:17:"disable_ajaxpopup";}s:1:"B";a:2:{s:4:"type";s:5:"label";s:5:"label";s:40:"Don\'t notify me via popups in eGroupWare";}}i:3;a:2:{s:1:"A";a:1:{s:4:"type";s:5:"label";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:4;a:2:{s:1:"A";a:1:{s:4:"type";s:5:"label";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:5;a:2:{s:1:"A";a:5:{s:4:"type";s:4:"hbox";s:4:"size";s:1:"2";s:4:"span";s:3:"all";i:1;a:4:{s:4:"type";s:4:"hbox";s:4:"size";s:1:"2";i:1;a:3:{s:4:"type";s:6:"button";s:5:"label";s:4:"save";s:4:"name";s:12:"button[save]";}i:2;a:3:{s:4:"type";s:6:"button";s:5:"label";s:5:"apply";s:4:"name";s:13:"button[apply]";}}i:2;a:4:{s:4:"type";s:6:"button";s:5:"label";s:6:"cancel";s:5:"align";s:5:"right";s:4:"name";s:14:"button[cancel]";}}s:1:"B";a:1:{s:4:"type";s:5:"label";}}}s:4:"rows";i:5;s:4:"cols";i:2;}}','size' => '','style' => '','modified' => '1157394610',);

View File

@ -0,0 +1,9 @@
<?php
// eTemplates for Application 'notifications', generated by soetemplate::dump4setup() 2006-09-04 21:59
/* $Id$ */
$templ_version=1;
$templ_data[] = array('name' => 'notifications.prefsindex','template' => '','lang' => '','group' => '0','version' => '','data' => 'a:1:{i:0;a:4:{s:4:"type";s:4:"grid";s:4:"data";a:6:{i:0;a:0:{}i:1;a:2:{s:1:"A";a:1:{s:4:"type";s:5:"label";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:2;a:2:{s:1:"A";a:2:{s:4:"type";s:8:"checkbox";s:4:"name";s:17:"disable_ajaxpopup";}s:1:"B";a:2:{s:4:"type";s:5:"label";s:5:"label";s:40:"Don\'t notify me via popups in eGroupWare";}}i:3;a:2:{s:1:"A";a:1:{s:4:"type";s:5:"label";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:4;a:2:{s:1:"A";a:1:{s:4:"type";s:5:"label";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:5;a:2:{s:1:"A";a:5:{s:4:"type";s:4:"hbox";s:4:"size";s:1:"2";s:4:"span";s:3:"all";i:1;a:4:{s:4:"type";s:4:"hbox";s:4:"size";s:1:"2";i:1;a:3:{s:4:"type";s:6:"button";s:5:"label";s:4:"save";s:4:"name";s:12:"button[save]";}i:2;a:3:{s:4:"type";s:6:"button";s:5:"label";s:5:"apply";s:4:"name";s:13:"button[apply]";}}i:2;a:4:{s:4:"type";s:6:"button";s:5:"label";s:6:"cancel";s:5:"align";s:5:"right";s:4:"name";s:14:"button[cancel]";}}s:1:"B";a:1:{s:4:"type";s:5:"label";}}}s:4:"rows";i:5;s:4:"cols";i:2;}}','size' => '','style' => '','modified' => '1157394610',);

View File

@ -0,0 +1,4 @@
don't notify me via popups in egroupware notifications de Benachrichtigungen durch Popups unterdrücken.
notification notifications de Benachrichtigung
ok notifications de ok
preferences for notification notifications de Benachrichtigungs-Einstellungen

View File

@ -0,0 +1,4 @@
don't notify me via popups in egroupware notifications en Don't notify me via popups in eGroupWare
notification notifications en Notification
ok notifications en ok
preferences for notification notifications en Preferences for notification

View File

@ -0,0 +1 @@
don't notify me via popups in egroupware notifications en Don't notify me via popups in eGroupWare

View File

@ -0,0 +1,48 @@
<?php
/**
* eGroupWare - Notifications
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package notifications
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>
* @version $Id: $
*/
if (!defined('NOTIFICATION_APP'))
{
define('NOTIFICATION_APP','notifications');
}
$setup_info[NOTIFICATION_APP]['name'] = NOTIFICATION_APP;
$setup_info[NOTIFICATION_APP]['version'] = '0.5';
$setup_info[NOTIFICATION_APP]['app_order'] = 1;
$setup_info[NOTIFICATION_APP]['tables'] = array('egw_notificationpopup');
$setup_info[NOTIFICATION_APP]['enable'] = 2;
$setup_info[NOTIFICATION_APP]['author'] =
$setup_info[NOTIFICATION_APP]['maintainer'] = array(
'name' => 'Cornelius Weiss',
'email' => 'nelius@cwtech.de'
);
$setup_info[NOTIFICATION_APP]['license'] = 'GPL';
$setup_info[NOTIFICATION_APP]['description'] =
'Instant norification of users via various channels.';
/* The hooks this app includes, needed for hooks registration */
$setup_info[NOTIFICATION_APP]['hooks'][] = 'after_navbar';
$setup_info[NOTIFICATION_APP]['hooks'][] = 'preferences';
//$setup_info[NOTIFICATION_APP]['hooks']['settings'] = NOTIFICATION_APP.'.ts_admin_prefs_sidebox_hooks.settings';
//$setup_info[NOTIFICATION_APP]['hooks']['admin'] = NOTIFICATION_APP.'.ts_admin_prefs_sidebox_hooks.all_hooks';
//$setup_info[NOTIFICATION_APP]['hooks']['sidebox_menu'] = NOTIFICATION_APP.'.ts_admin_prefs_sidebox_hooks.all_hooks';
//$setup_info[NOTIFICATION_APP]['hooks']['search_link'] = NOTIFICATION_APP.'.bonotification.search_link';
/* Dependencies for this app to work */
$setup_info[NOTIFICATION_APP]['depends'][] = array(
'appname' => 'phpgwapi',
'versions' => Array('1.2','1.3')
);
$setup_info[NOTIFICATION_APP]['depends'][] = array(
'appname' => 'etemplate',
'versions' => Array('1.2','1.3')
);

View File

@ -0,0 +1,24 @@
<?php
/**
* eGroupWare - Notifications
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package notifications
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>
* @version $Id: $
*/
$phpgw_baseline = array(
'egw_notificationpopup' => array(
'fd' => array(
'account_id' => array('type' => 'varchar','precision' => '255','nullable' => False),
'session_id' => array('type' => 'varchar','precision' => '255','nullable' => False),
'message' => array('type' => 'longtext')
),
'pk' => array(),
'fk' => array(),
'ix' => array('account_id','session_id'),
'uc' => array()
)
);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB