egroupware_official/notifications/inc/class.notifications_popup.inc.php

316 lines
9.4 KiB
PHP
Raw Normal View History

2006-09-07 09:04:15 +02:00
<?php
/**
* 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
* @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
*/
2016-05-06 11:19:36 +02:00
use EGroupware\Api;
2006-09-07 09:04:15 +02:00
/**
* Instant user notification with egroupware popup.
*
2008-08-13 22:14:44 +02:00
* @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
2008-08-13 22:14:44 +02:00
* out the table to look if there is a notificaton for this
* client. The second stage is done in class.notifications_ajax.inc.php
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';
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
/**
* Notification type
*/
const _type = 'base';
/**
* 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
*/
private $recipient;
2008-08-13 22:14:44 +02: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
*
* @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
*
2016-05-06 11:19:36 +02:00
* @var Api\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
/**
* 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) {
//error_log(__METHOD__."(".array2string($_sender).', '.array2string($_recipient).', '.array2string($config).',...)');
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;
$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
*
* @param array $_messages
* @param string $_subject
* @param array $_links
* @param array $_attachments
* @param array $_data
2006-09-07 09:04:15 +02:00
*/
public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false)
2016-05-06 11:19:36 +02:00
{
unset($_attachments); // not used
2008-08-13 22:14:44 +02:00
$message = $this->render_infos($_subject)
2016-05-06 11:19:36 +02:00
.Api\Html::hr()
.(isset($_messages['popup'])&&!empty($_messages['popup'])?$_messages['popup']:$_messages['html'])
.$this->render_links($_links);
2008-08-13 22:14:44 +02:00
$this->save($message, $_data);
2006-09-07 09:04:15 +02:00
}
2008-08-13 22:14:44 +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
* @param array $_data
2006-09-07 09:04:15 +02:00
*/
private function save($_message, $_data)
{
$result = $this->db->insert( self::_notification_table, array(
'account_id' => $this->recipient->account_id,
'notify_message' => $_message,
'notify_type' => self::_type,
'notify_data' => $_data && is_array($_data) ? json_encode($_data) : NULL,
'notify_created' => new Api\DateTime(),
), false,__LINE__,__FILE__,self::_appname);
if ($result === false) throw new Exception("Can't save notification into SQL table");
$push = new Api\Json\Push($this->recipient->account_id);
$entries = self::read($this->recipient->account_id);
$push->call('app.notifications.append', $entries['rows'], null, $entries['total']);
}
/**
* Read the 100 most recent notification messages for given recipient
*
* We use a cut-off-date of 30day, not returning anything older!
*
* @param $_account_id
* @param int $num_rows
* @return array
*/
public static function read($_account_id, int $num_rows=100)
{
if (!$_account_id) return [];
/** @var Api\Db $db */
$db = $GLOBALS['egw']->db;
$result = [];
if (($total = $db->select(self::_notification_table, 'COUNT(*)', [
'account_id' => $_account_id,
'notify_type' => self::_type,
'notify_created > '.($cut_off=$db->quote(Api\DateTime::to(notifications_ajax::CUT_OFF_DATE, Api\DateTime::DATABASE))),
], __LINE__, __FILE__, false, '', self::_appname)->fetchColumn()))
{
$n = 0;
$chunk_size = 150;
do
{
$notification = null;
foreach ($rs=$db->select(self::_notification_table, '*', [
'account_id' => $_account_id,
'notify_type' => self::_type,
'notify_created > ' . $cut_off,
], __LINE__, __FILE__, $n, 'ORDER BY notify_id DESC', self::_appname, $chunk_size) as $notification)
{
$actions = null;
$data = json_decode($notification['notify_data'], true);
if (!empty($data['appname']) && !empty($data['data']))
{
$_actions = Api\Hooks::process(array(
'location' => 'notifications_actions',
'data' => $data['data']
), $data['appname'], true);
$actions = $_actions[$data['appname']];
}
$data = [
'id' => $notification['notify_id'],
'message' => $notification['notify_message'],
'status' => $notification['notify_status'],
'created' => Api\DateTime::server2user($notification['notify_created']),
'current' => new Api\DateTime('now'),
'actions' => is_array($actions) ? $actions : NULL,
'extra_data' => $data['data'] ?? [],
];
// aggregate by app:id reporting only the newest entry
if (!empty($data['extra_data']['id']))
{
if (!isset($result[$id = $data['extra_data']['app'] . ':' . $data['extra_data']['id']]))
{
$result[$id] = $data;
}
else
{
$total--;
/* in case we want to show all
$result['id']['others'][] = $data;
*/
}
}
else
{
$result[] = $data;
}
}
$n += $chunk_size;
}
while(!$notification || count($result) < min($num_rows, $total));
return ['rows' => array_values($result), 'total'=> $total];
}
2006-09-07 09:04:15 +02:00
}
2008-08-13 22:14:44 +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
2016-05-06 11:19:36 +02:00
* @return string 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 />";
2008-08-13 22:14:44 +02:00
$rendered_links = array();
foreach($_links as $link) {
if(!$link->popup) { $link->view['no_popup'] = 1; }
2008-08-13 22:14:44 +02:00
// do not expose sensitive data
2016-05-06 11:19:36 +02:00
$url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',
Api\Html::link('/index.php', $link->view));
// extract application-icon from menuaction
if($link->view['menuaction']) {
$menuaction_arr = explode('.',$link->view['menuaction']);
$application = $menuaction_arr[0];
2016-05-06 11:19:36 +02:00
$image = $application ? Api\Html::image($application,'navbar',$link->text,'align="middle" style="width: 24px; margin-right: 0.5em;"') : '';
} else {
$image = '';
}
2024-03-06 19:34:13 +01:00
if($link->popup && empty($GLOBALS['egw_info']['user']['preferences']['notifications']['external_mailclient']))
{
$data = array(
"data-app = '{$link->app}'",
"data-id = '{$link->id}'",
"data-url = '$url'",
"data-popup = '{$link->popup}'"
);
2016-05-06 11:19:36 +02:00
$rendered_links[] = Api\Html::div($image.$link->text,implode(' ',$data),'link');
} else {
2016-05-06 11:19:36 +02:00
$rendered_links[] = Api\Html::div('<a href="'.$url.'" target="_blank">'.$image.$link->text.'</a>','','link');
2008-08-13 22:14:44 +02:00
}
}
if(count($rendered_links) > 0) {
return Api\Html::hr().Api\Html::bold(Api\Translation::translate_as($this->recipient->account_id,'Linked entries:')).$newline.implode($newline,$rendered_links);
}
}
2008-08-13 22:14:44 +02:00
/**
* returns javascript to open a popup window: window.open(...)
*
* @param string $link link or this.href
2016-05-06 11:19:36 +02:00
* @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)
{
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')";
}
}
2008-08-13 22:14:44 +02:00
/**
* renders additional infos from sender and subject
*
* @param string $_subject
2016-05-06 11:19:36 +02:00
* @return string html rendered info as complete string
*/
private function render_infos($_subject = false) {
$infos = array();
2008-08-13 22:14:44 +02:00
$newline = "<br />";
$sender = $this->sender->account_fullname ? $this->sender->account_fullname : $this->sender_account_email;
$infos[] = Api\Translation::translate_as($this->recipient->account_id, 'Message from').': '.$sender;
2016-05-06 11:19:36 +02:00
if(!empty($_subject)) { $infos[] = Api\Html::bold($_subject); }
return implode($newline,$infos);
}
/**
* Actions to take when deleting an account
*
* @param settings array with keys account_id and new_owner (new_owner is optional)
*/
public static function deleteaccount($settings)
{
$GLOBALS['egw']->db->delete( self::_notification_table, array(
'account_id' => $settings['account_id']
),__LINE__,__FILE__,self::_appname);
}
}