-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
check_notifications.php
120 lines (94 loc) · 4.62 KB
/
check_notifications.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
* Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
require '../../main.inc.php';
/*
* View
*/
top_httphead('text/html'); // TODO Use a json mime type
global $user, $db, $langs, $conf;
$time = (int) GETPOST('time'); // Use the time parameter that is always increased by time_update, even if call is late
//$time=dol_now();
$eventfound = array();
//Uncomment this to force a test
//$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
//dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']);
// TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when several tabs are opened.
if ($time >= $_SESSION['auto_check_events_not_before'])
{
$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
if (! empty($_SESSION['auto_check_events_not_before']))
{
// We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
// then we are not losing periods
$starttime = $_SESSION['auto_check_events_not_before'];
// Protection to avoid too long sessions
if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
{
dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
$starttime = $time;
}
}
else
{
$starttime = $time;
}
$_SESSION['auto_check_events_not_before'] = $time + $time_update;
// Force save of session change we did.
// WARNING: Any change in sessions after that will not be saved !
session_write_close();
require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.$_SESSION['auto_check_events_not_before']);
$sql = 'SELECT id';
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'actioncomm a, ' . MAIN_DB_PREFIX . 'actioncomm_resources ar';
$sql .= ' WHERE a.id = ar.fk_actioncomm';
// TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when several tabs are opened.
// This need to extend period to be sure to not miss and save in session what we notified to avoid duplicate (save is not done yet).
$sql .= " AND datep BETWEEN '" . $db->idate($starttime) . "' AND '" . $db->idate($time + $time_update - 1) . "'";
$sql .= ' AND a.code <> "AC_OTH_AUTO"';
$sql .= ' AND ar.element_type = "user"';
$sql .= ' AND ar.fk_element = ' . $user->id;
$sql .= ' LIMIT 10'; // Avoid too many notification at once
$resql = $db->query($sql);
if ($resql) {
$actionmod = new ActionComm($db);
while ($obj = $db->fetch_object($resql))
{
$langs->load("agenda");
$langs->load("commercial");
$actionmod->fetch($obj->id);
// Message must be formated and translated to be used with javascript directly
$event = array();
$event['type'] = 'agenda';
$event['id'] = $actionmod->id;
$event['tipo'] = $langs->transnoentities('Action' . $actionmod->code);
$event['titulo'] = $actionmod->label;
$event['location'] = $langs->transnoentities('Location').': '.$actionmod->location;
$eventfound[] = $event;
}
}
else
{
dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
}
}
print json_encode($eventfound);