2007-12-18 16:53:38 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2011-04-14 15:43:16 +02:00
|
|
|
* EGroupware - Notifications
|
2007-12-18 16:53:38 +01:00
|
|
|
*
|
|
|
|
* @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>, Christian Binder <christian@jaytraxx.de>
|
2013-07-22 15:29:20 +02:00
|
|
|
* @version $Id$
|
2007-12-18 16:53:38 +01:00
|
|
|
*/
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2016-05-06 11:19:36 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* Ajax methods for notifications
|
|
|
|
*/
|
2023-09-25 09:43:21 +02:00
|
|
|
class notifications_ajax
|
|
|
|
{
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* Appname
|
|
|
|
*/
|
|
|
|
const _appname = 'notifications';
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* Notification table in SQL database
|
|
|
|
*/
|
|
|
|
const _notification_table = 'egw_notificationpopup';
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2012-12-09 06:56:42 +01:00
|
|
|
/**
|
|
|
|
* Notification type
|
|
|
|
*/
|
|
|
|
const _type = 'base';
|
|
|
|
|
2023-09-25 09:43:21 +02:00
|
|
|
/**
|
|
|
|
* Do NOT consider notifications older than this
|
|
|
|
*/
|
2024-04-17 15:00:14 +02:00
|
|
|
static public $cut_off_date = '-30days';
|
2023-09-25 09:43:21 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user to notify
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $recipient;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
2009-02-11 20:32:26 +01:00
|
|
|
* holds preferences array of user to notify
|
2007-12-18 16:53:38 +01:00
|
|
|
*
|
2009-02-11 20:32:26 +01:00
|
|
|
* @var array
|
2007-12-18 16:53:38 +01:00
|
|
|
*/
|
|
|
|
private $preferences;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
2008-03-22 13:34:27 +01:00
|
|
|
* reference to global db object
|
2007-12-18 16:53:38 +01:00
|
|
|
*
|
2016-05-06 11:19:36 +02:00
|
|
|
* @var Api\Db
|
2007-12-18 16:53:38 +01:00
|
|
|
*/
|
|
|
|
private $db;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2009-02-11 20:32:26 +01:00
|
|
|
/**
|
|
|
|
* the xml response object
|
|
|
|
*
|
2016-05-06 11:19:36 +02:00
|
|
|
* @var Api\Json\Response
|
2009-02-11 20:32:26 +01:00
|
|
|
*/
|
|
|
|
private $response;
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2020-07-22 13:17:54 +02:00
|
|
|
/**
|
2020-07-24 09:07:13 +02:00
|
|
|
* Do we have a real push server, or only a fallback
|
2020-07-22 13:17:54 +02:00
|
|
|
*
|
2020-07-24 09:07:13 +02:00
|
|
|
* @var bool
|
2020-07-22 13:17:54 +02:00
|
|
|
*/
|
|
|
|
private $isPushServer;
|
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
2009-04-03 22:36:28 +02:00
|
|
|
* constructor
|
2007-12-18 16:53:38 +01:00
|
|
|
*
|
|
|
|
*/
|
2023-09-25 09:43:21 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-05-06 11:19:36 +02:00
|
|
|
$this->response = Api\Json\Response::get();
|
2024-04-17 15:00:14 +02:00
|
|
|
$this->recipient = (object)$GLOBALS['egw_info']['user'];
|
2007-12-18 16:53:38 +01:00
|
|
|
|
2008-03-22 13:34:27 +01:00
|
|
|
$this->db = $GLOBALS['egw']->db;
|
2020-07-22 13:17:54 +02:00
|
|
|
|
2020-07-24 09:07:13 +02:00
|
|
|
$this->isPushServer = Api\Cache::getInstance('notifications', 'isPushServer', function ()
|
2020-07-22 13:17:54 +02:00
|
|
|
{
|
2020-07-24 09:07:13 +02:00
|
|
|
return !Api\Json\Push::onlyFallback();
|
|
|
|
}, [], 900);
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
2009-04-03 22:17:22 +02:00
|
|
|
* public AJAX trigger function to be called by the JavaScript client
|
|
|
|
*
|
|
|
|
* this function calls all other recurring AJAX notifications methods
|
|
|
|
* to have ONE single recurring AJAX call per user
|
2009-04-28 22:24:05 +02:00
|
|
|
*
|
2007-12-18 16:53:38 +01:00
|
|
|
* @return xajax response
|
|
|
|
*/
|
2015-10-19 10:07:00 +02:00
|
|
|
public function get_notifications($browserNotify = false)
|
|
|
|
{
|
|
|
|
// close session now, to not block other user actions, as specially mail checks can be time consuming
|
|
|
|
$GLOBALS['egw']->session->commit_session();
|
|
|
|
|
2014-01-10 10:19:07 +01:00
|
|
|
// call a hook for notifications on new mail
|
2014-05-13 17:02:07 +02:00
|
|
|
//if ($GLOBALS['egw_info']['user']['apps']['mail']) $this->check_mailbox();
|
2016-05-06 11:19:36 +02:00
|
|
|
Api\Hooks::process('check_notify');
|
2011-04-14 11:50:35 +02:00
|
|
|
|
|
|
|
// update currentusers
|
|
|
|
if ($GLOBALS['egw_info']['user']['apps']['admin'] &&
|
|
|
|
$GLOBALS['egw_info']['user']['preferences']['common']['show_currentusers'])
|
|
|
|
{
|
2011-06-08 11:14:17 +02:00
|
|
|
$this->response->jquery('#currentusers', 'text', array((string)$GLOBALS['egw']->session->session_count()));
|
2011-04-14 11:50:35 +02:00
|
|
|
}
|
2020-07-22 13:17:54 +02:00
|
|
|
if ($this->isPushServer) $this->response->data(['isPushServer' => true]);
|
2011-11-24 00:48:58 +01:00
|
|
|
$this->get_egwpopup($browserNotify);
|
2009-02-11 20:32:26 +01:00
|
|
|
}
|
2009-04-28 22:24:05 +02:00
|
|
|
|
2011-04-13 19:51:02 +02:00
|
|
|
/**
|
2024-04-16 18:53:54 +02:00
|
|
|
* Remove given notification id(s) and app_ids from the table
|
2011-04-14 15:43:16 +02:00
|
|
|
*
|
2024-04-11 15:48:42 +02:00
|
|
|
* @param int[]|array[] $notifymessages one or multiple notify_id(s) or objects incl. id attribute
|
2024-04-16 18:53:54 +02:00
|
|
|
* @param array[] $app_ids app-name int[] pairs
|
2011-04-13 19:51:02 +02:00
|
|
|
*/
|
2024-04-16 18:53:54 +02:00
|
|
|
public function delete_message(array $notifymessages, array $app_ids=[])
|
2011-04-14 15:43:16 +02:00
|
|
|
{
|
2024-04-16 18:53:54 +02:00
|
|
|
$this->update($notifymessages, null, $app_ids ?? []); // null = delete
|
2024-04-11 15:48:42 +02:00
|
|
|
|
|
|
|
// if we delete all messages (we either delete one or all!), we return the next chunk of messages directly
|
|
|
|
if (count($notifymessages) > 1)
|
|
|
|
{
|
|
|
|
$this->get_notifications();
|
|
|
|
}
|
2017-05-16 18:03:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to update message(s) status
|
|
|
|
*
|
2021-03-05 15:22:14 +01:00
|
|
|
* @param array $notifymessages one or more notify_id(s)
|
2017-05-16 18:03:50 +02:00
|
|
|
* @param string $status = SEEN, status of message:
|
|
|
|
* - SEEN: message has been seen
|
|
|
|
* - UNSEEN: message has not been seen
|
|
|
|
* - DISPLAYED: message has been shown but no further status applied yet
|
|
|
|
* this status has been used more specifically for browser type
|
|
|
|
* of notifications.
|
|
|
|
*/
|
2023-09-25 09:43:21 +02:00
|
|
|
public function update_status(array $notifymessages, $status = "SEEN")
|
2017-05-16 18:03:50 +02:00
|
|
|
{
|
2023-09-25 09:43:21 +02:00
|
|
|
$this->update($notifymessages, $status);
|
2011-04-13 19:51:02 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 15:22:14 +01:00
|
|
|
/**
|
2023-09-25 09:43:21 +02:00
|
|
|
* Update or delete the given notification messages, incl. not explicitly mentioned ones with same app:id
|
|
|
|
*
|
|
|
|
* @param array $notifymessages
|
|
|
|
* @param string|null $status use null to delete
|
2024-04-16 18:53:54 +02:00
|
|
|
* @param array[] $app_ids app-name int[] pairs
|
2021-03-05 15:22:14 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2024-04-16 18:53:54 +02:00
|
|
|
protected function update(array $notifymessages, $status='SEEN', array $app_ids=[])
|
2021-03-05 15:22:14 +01:00
|
|
|
{
|
2024-04-16 18:53:54 +02:00
|
|
|
$notify_ids = [];
|
2021-03-05 15:22:14 +01:00
|
|
|
foreach ($notifymessages as $data)
|
|
|
|
{
|
2023-09-25 09:43:21 +02:00
|
|
|
if (is_array($data) && !empty($data['id']))
|
2021-03-05 15:22:14 +01:00
|
|
|
{
|
2023-09-25 09:43:21 +02:00
|
|
|
if (is_array($data['data'] ?? null) && !empty($data['data']['id']))
|
|
|
|
{
|
|
|
|
$app_ids[$data['data']['app']][$data['data']['id']] = $data['data']['id'];
|
|
|
|
}
|
|
|
|
$notify_ids[] = $data['id'];
|
2021-03-05 15:22:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-25 09:43:21 +02:00
|
|
|
$notify_ids[] = $data;
|
2021-03-05 15:22:14 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-17 15:00:14 +02:00
|
|
|
$cut_off = $this->db->quote(Api\DateTime::to(self::$cut_off_date, Api\DateTime::DATABASE));
|
2023-09-25 09:43:21 +02:00
|
|
|
try {
|
|
|
|
foreach($app_ids as $app => $ids)
|
|
|
|
{
|
|
|
|
$where = [
|
2021-03-05 15:22:14 +01:00
|
|
|
'account_id' => $this->recipient->account_id,
|
|
|
|
'notify_type' => self::_type,
|
2024-04-16 18:53:54 +02:00
|
|
|
'notify_app' => $app,
|
|
|
|
'notify_app_id' => array_unique($ids),
|
2023-09-25 09:43:21 +02:00
|
|
|
'notify_created > '.$cut_off,
|
|
|
|
];
|
|
|
|
if (isset($status))
|
|
|
|
{
|
|
|
|
$this->db->update(self::_notification_table, ['notify_status' => $status], $where, __LINE__, __FILE__, self::_appname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->db->delete(self::_notification_table, $where, __LINE__, __FILE__, self::_appname);
|
|
|
|
}
|
2021-03-05 15:22:14 +01:00
|
|
|
}
|
2023-09-25 09:43:21 +02:00
|
|
|
}
|
|
|
|
catch (Api\Db\Exception $e) {
|
2024-04-16 18:53:54 +02:00
|
|
|
// ignore, if DB is not yet updated with notify_app(_id) columns
|
2021-03-05 15:22:14 +01:00
|
|
|
}
|
2023-09-25 09:43:21 +02:00
|
|
|
$where = [
|
|
|
|
'notify_id' => array_unique($notify_ids),
|
|
|
|
'account_id' => $this->recipient->account_id,
|
|
|
|
'notify_type' => self::_type
|
|
|
|
];
|
|
|
|
if (isset($status))
|
|
|
|
{
|
|
|
|
$this->db->update(self::_notification_table, ['notify_status' => $status], $where, __LINE__, __FILE__, self::_appname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->db->delete(self::_notification_table, $where, __LINE__, __FILE__, self::_appname);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup messages older than our cut-off-date
|
|
|
|
$this->db->delete(self::_notification_table, [
|
2023-09-25 11:29:56 +02:00
|
|
|
'notify_created <= '.$cut_off,
|
2023-09-25 09:43:21 +02:00
|
|
|
'notify_type' => self::_type
|
|
|
|
], __LINE__, __FILE__, self::_appname);
|
2021-03-05 15:22:14 +01:00
|
|
|
}
|
2023-09-25 09:43:21 +02:00
|
|
|
|
2009-04-03 22:17:22 +02:00
|
|
|
/**
|
|
|
|
* gets all egwpopup notifications for calling user
|
2009-04-28 22:24:05 +02:00
|
|
|
*
|
2009-04-03 22:17:22 +02:00
|
|
|
* @return boolean true or false
|
|
|
|
*/
|
2021-03-08 12:12:54 +01:00
|
|
|
private function get_egwpopup($browserNotify = false)
|
|
|
|
{
|
|
|
|
$entries = notifications_popup::read($this->recipient->account_id);
|
2022-04-26 21:04:16 +02:00
|
|
|
$this->response->apply('app.notifications.append', array($entries['rows']??[], $browserNotify, $entries['total']??0));
|
2009-04-03 22:17:22 +02:00
|
|
|
return true;
|
2009-02-11 20:32:26 +01:00
|
|
|
}
|
2024-04-17 15:00:14 +02:00
|
|
|
|
|
|
|
public function initStatic()
|
|
|
|
{
|
|
|
|
$config = Api\Config::read(self::_appname);
|
|
|
|
|
|
|
|
if (!empty($config['popup_cut_off_days']) && $config['popup_cut_off_days'] > 0)
|
|
|
|
{
|
|
|
|
self::$cut_off_date = "-$config[popup_cut_off_days]days";
|
|
|
|
}
|
|
|
|
}
|
2022-04-26 21:04:16 +02:00
|
|
|
}
|