2006-09-07 09:04:15 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2011-04-14 15:43:16 +02:00
|
|
|
* EGroupware - Notifications
|
2006-09-07 09:04:15 +02:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package notifications
|
2007-12-18 16:53:38 +01:00
|
|
|
* @subpackage backends
|
2006-09-07 09:04:15 +02:00
|
|
|
* @link http://www.egroupware.org
|
2008-01-30 19:58:00 +01:00
|
|
|
* @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.
|
2007-12-18 16:53:38 +01:00
|
|
|
*
|
2008-08-13 22:14:44 +02:00
|
|
|
* @abstract egwpopup is a two stage notification. In the first stage
|
2008-01-30 19:58:00 +01:00
|
|
|
* notification is written into self::_notification_table.
|
|
|
|
* In the second stage a request from the client reads
|
2008-08-13 22:14:44 +02:00
|
|
|
* out the table to look if there is a notificaton for this
|
2009-04-03 22:36:28 +02:00
|
|
|
* client. The second stage is done in class.notifications_ajax.inc.php
|
2006-09-07 09:04:15 +02:00
|
|
|
*/
|
2008-01-30 19:58:00 +01:00
|
|
|
class notifications_popup implements notifications_iface {
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2006-09-07 09:04:15 +02:00
|
|
|
/**
|
|
|
|
* Appname
|
|
|
|
*/
|
|
|
|
const _appname = 'notifications';
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2006-09-07 09:04:15 +02:00
|
|
|
/**
|
|
|
|
* Notification table in SQL database
|
|
|
|
*/
|
|
|
|
const _notification_table = 'egw_notificationpopup';
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user who sends the message
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $sender;
|
2008-08-13 22:14:44 +02:00
|
|
|
|
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;
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds config object (sitewide application config)
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $config;
|
2008-08-13 22:14:44 +02:00
|
|
|
|
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;
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2006-09-07 09:04:15 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2006-09-07 09:04:15 +02:00
|
|
|
/**
|
2008-01-30 19:58:00 +01:00
|
|
|
* constructor of notifications_egwpopup
|
2006-09-07 09:04:15 +02:00
|
|
|
*
|
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-12-18 16:53:38 +01:00
|
|
|
public function __construct($_sender, $_recipient, $_config = null, $_preferences = null) {
|
2011-04-14 15:43:16 +02:00
|
|
|
//error_log(__METHOD__."(".array2string($_sender).', '.array2string($_recipient).', '.array2string($config).',...)');
|
2007-12-18 16:53:38 +01:00
|
|
|
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;
|
2011-04-14 15:43:16 +02:00
|
|
|
$this->db = $GLOBALS['egw']->db;
|
2006-09-07 09:04:15 +02:00
|
|
|
}
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2006-09-07 09:04:15 +02:00
|
|
|
/**
|
|
|
|
* sends notification if user is online
|
|
|
|
*
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param array $_messages
|
2007-12-18 16:53:38 +01:00
|
|
|
* @param string $_subject
|
|
|
|
* @param array $_links
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param array $_attachments
|
2006-09-07 09:04:15 +02:00
|
|
|
*/
|
2007-12-18 16:53:38 +01:00
|
|
|
public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false) {
|
2011-04-13 19:51:02 +02:00
|
|
|
// Check access log to see if user is still logged in
|
|
|
|
if ( !egw_session::notifications_active($this->recipient->account_id) )
|
|
|
|
{
|
|
|
|
throw new Exception("User {$this->recipient->account_lid} isn't online. Can't send notification via popup");
|
2006-09-07 09:04:15 +02:00
|
|
|
}
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
$message = $this->render_infos($_subject)
|
2008-03-09 15:35:48 +01:00
|
|
|
.html::hr()
|
2012-06-13 16:50:41 +02:00
|
|
|
.(isset($_messages['popup'])&&!empty($_messages['popup'])?$_messages['popup']:$_messages['html'])
|
2007-12-18 16:53:38 +01:00
|
|
|
.$this->render_links($_links);
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2011-04-13 19:51:02 +02:00
|
|
|
$this->save( $message );
|
2006-09-07 09:04:15 +02:00
|
|
|
}
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2006-09-07 09:04:15 +02:00
|
|
|
/**
|
2008-01-30 19:58:00 +01: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
|
|
|
|
*/
|
2011-04-13 19:51:02 +02:00
|
|
|
private function save( $_message ) {
|
|
|
|
$result = $this->db->insert( self::_notification_table, array(
|
2011-04-14 15:43:16 +02:00
|
|
|
'account_id' => $this->recipient->account_id,
|
|
|
|
'notify_message' => $_message
|
2011-04-13 19:51:02 +02:00
|
|
|
), false,__LINE__,__FILE__,self::_appname);
|
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
|
|
|
}
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* renders plaintext/html links from given link array
|
2008-01-30 19:58:00 +01:00
|
|
|
* should be moved to the ajax class later - like mentioned in the Todo
|
2007-12-18 16:53:38 +01:00
|
|
|
*
|
|
|
|
* @param array $_links
|
2008-01-30 19:58:00 +01:00
|
|
|
* @return html rendered link(s) as complete string with jspopup or a new window
|
2007-12-18 16:53:38 +01:00
|
|
|
*/
|
|
|
|
private function render_links($_links = false) {
|
|
|
|
if(!is_array($_links) || count($_links) == 0) { return false; }
|
2008-01-30 19:58:00 +01:00
|
|
|
$newline = "<br />";
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2008-01-30 19:58:00 +01:00
|
|
|
$rendered_links = array();
|
2007-12-18 16:53:38 +01:00
|
|
|
foreach($_links as $link) {
|
2008-01-30 19:58:00 +01:00
|
|
|
if(!$link->popup) { $link->view['no_popup'] = 1; }
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2008-03-09 15:35:48 +01:00
|
|
|
$url = html::link('/index.php', $link->view);
|
2011-04-14 15:43:16 +02:00
|
|
|
// do not expose sensitive data
|
2009-10-09 21:59:22 +02:00
|
|
|
$url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',$url);
|
2008-01-30 19:58:00 +01:00
|
|
|
// extract application-icon from menuaction
|
|
|
|
if($link->view['menuaction']) {
|
|
|
|
$menuaction_arr = explode('.',$link->view['menuaction']);
|
|
|
|
$application = $menuaction_arr[0];
|
2008-03-09 15:35:48 +01:00
|
|
|
$image = $application ? html::image($application,'navbar',$link->text,'align="middle" style="width: 24px; margin-right: 0.5em;"') : '';
|
2008-01-30 19:58:00 +01:00
|
|
|
} else {
|
|
|
|
$image = '';
|
|
|
|
}
|
2011-11-24 00:48:58 +01:00
|
|
|
if($link->popup && !$GLOBALS['egw_info']['user']['preferences']['notifications']['external_mailclient'])
|
|
|
|
{
|
2008-01-30 19:58:00 +01:00
|
|
|
$dimensions = explode('x', $link->popup);
|
2008-03-09 15:35:48 +01:00
|
|
|
$rendered_links[] = html::div($image.$link->text,'onclick="'.$this->jspopup($url, '_blank', $dimensions[0], $dimensions[1]).'"','link');
|
2008-01-30 19:58:00 +01:00
|
|
|
} else {
|
2008-10-16 07:30:31 +02:00
|
|
|
$rendered_links[] = html::div('<a href="'.$url.'" target="_blank">'.$image.$link->text.'</a>','','link');
|
2008-08-13 22:14:44 +02:00
|
|
|
}
|
|
|
|
|
2008-01-30 19:58:00 +01:00
|
|
|
}
|
|
|
|
if(count($rendered_links) > 0) {
|
2009-02-23 21:45:31 +01:00
|
|
|
return html::hr().html::bold(lang('Linked entries:')).$newline.implode($newline,$rendered_links);
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
|
|
|
}
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2011-11-24 00:48:58 +01:00
|
|
|
if($GLOBALS['egw_info']['user']['preferences']['notifications']['external_mailclient'])
|
|
|
|
{
|
|
|
|
return 'window.open('.($link == 'this.href' ? $link : "'".$link."'").','.
|
|
|
|
($target == 'this.target' ? $target : "'".$target."'").",$width,$height,'yes')";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 'egw_openWindowCentered2('.($link == 'this.href' ? $link : "'".$link."'").','.
|
|
|
|
($target == 'this.target' ? $target : "'".$target."'").",$width,$height,'yes')";
|
|
|
|
}
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
2008-08-13 22:14:44 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* 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();
|
2008-08-13 22:14:44 +02:00
|
|
|
$newline = "<br />";
|
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
$sender = $this->sender->account_fullname ? $this->sender->account_fullname : $this->sender_account_email;
|
|
|
|
$infos[] = lang('Message from').': '.$sender;
|
2008-03-09 15:35:48 +01:00
|
|
|
if(!empty($_subject)) { $infos[] = html::bold($_subject); }
|
2007-12-18 16:53:38 +01:00
|
|
|
return implode($newline,$infos);
|
|
|
|
}
|
2011-04-13 19:51:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Actions to take when deleting an account
|
|
|
|
*
|
|
|
|
* @param settings array with keys account_id and new_owner (new_owner is optional)
|
|
|
|
*/
|
2011-04-14 17:09:36 +02:00
|
|
|
public static function deleteaccount($settings) {
|
|
|
|
$GLOBALS['egw']->db->delete( self::_notification_table, array(
|
2011-04-14 15:43:16 +02:00
|
|
|
'account_id' => $settings['account_id']
|
|
|
|
),__LINE__,__FILE__,self::_appname);
|
2011-04-13 19:51:02 +02:00
|
|
|
}
|
2006-09-07 09:04:15 +02:00
|
|
|
}
|