dolibarr 19.0.3
check_notifications.php
1<?php
2/* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
3 * Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
5 * Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21if (!defined('NOTOKENRENEWAL')) {
22 define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
23}
24if (!defined('NOREQUIREMENU')) {
25 define('NOREQUIREMENU', '1');
26}
27if (!defined('NOREQUIREHTML')) {
28 define('NOREQUIREHTML', '1');
29}
30if (!defined('NOREQUIREAJAX')) {
31 define('NOREQUIREAJAX', '1');
32}
33if (!defined('NOREQUIRESOC')) {
34 define('NOREQUIRESOC', '1');
35}
36if (!defined('NOREQUIRETRAN')) {
37 define('NOREQUIRETRAN', '1');
38}
39
40// Load Dolibarr environment
41require '../../main.inc.php';
42
43//$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late
44$time = dol_now();
45$action = GETPOST('action', 'aZ09');
46$listofreminderids = GETPOST('listofreminderids', 'aZ09');
47
48// Security check
49// No permission check at top, but action later are all done with a test on $user->id.
50
51
52/*
53 * Actions
54 */
55
56if ($action == 'stopreminder') {
57 dol_syslog("Clear notification for listofreminderids=".$listofreminderids);
58 $listofreminderid = GETPOST('listofreminderids', 'intcomma');
59
60 // Set the reminder as done
61 $sql = 'UPDATE '.MAIN_DB_PREFIX.'actioncomm_reminder SET status = 1';
62 $sql .= ' WHERE status = 0 AND rowid IN ('.$db->sanitize($db->escape($listofreminderid)).')';
63 $sql .= ' AND fk_user = '.((int) $user->id).' AND entity = '.((int) $conf->entity);
64 $resql = $db->query($sql);
65 if (!$resql) {
66 dol_print_error($db);
67 }
68 //}
69
70 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
71
72 // Clean database
73 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'actioncomm_reminder';
74 $sql .= " WHERE dateremind < '".$db->idate(dol_time_plus_duree(dol_now(), -1, 'm'))."'";
75 $sql .= " AND fk_user = ".((int) $user->id).' AND entity = '.((int) $conf->entity);
76 $resql = $db->query($sql);
77 if (!$resql) {
78 dol_print_error($db);
79 }
80
81 exit;
82}
83
84
85/*
86 * View
87 */
88
89top_httphead('application/json');
90
91global $user, $db, $langs, $conf;
92
93$eventfound = array();
94//Uncomment this to force a test
95//$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
96
97// TODO Remove use of $_SESSION['auto_check_events_not_before']. Seems not used.
98if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow', 'int')) {
99 /*$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
100 if (!empty($_SESSION['auto_check_events_not_before']))
101 {
102 // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
103 // then we are not losing periods
104 $starttime = $_SESSION['auto_check_events_not_before'];
105 // Protection to avoid too long sessions
106 if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
107 {
108 dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
109 $starttime = $time;
110 }
111 } else {
112 $starttime = $time;
113 }
114
115 $_SESSION['auto_check_events_not_before'] = $time + $time_update;
116 */
117
118 // Force save of the session change we did.
119 // WARNING: Any change in sessions after that will not be saved !
120 session_write_close();
121
122 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
123
124
125 //dol_syslog('$_SESSION[auto_check_events_not_before]='.(empty($_SESSION['auto_check_events_not_before']) ? '' : $_SESSION['auto_check_events_not_before']));
126 dol_syslog('dolnotif_nb_test_for_page='.GETPOST('dolnotif_nb_test_for_page'));
127
128 $sql = 'SELECT a.id as id_agenda, a.code, a.datep, a.label, a.location, ar.rowid as id_reminder, ar.dateremind, ar.fk_user as id_user_reminder';
129 $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
130 $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
131 $sql .= " AND ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."'";
132 $sql .= " AND ar.status = 0";
133 $sql .= " AND ar.entity = ".((int) $conf->entity); // No sharing of entity for alerts
134 $sql .= $db->order('datep', 'ASC');
135 $sql .= $db->plimit(10); // Avoid too many notification at once
136
137 $resql = $db->query($sql);
138 if ($resql) {
139 while ($obj = $db->fetch_object($resql)) {
140 // Message must be formated and translated to be used with javascript directly
141 $event = array();
142 $event['type'] = 'agenda';
143 $event['id_reminder'] = $obj->id_reminder;
144 $event['id_agenda'] = $obj->id_agenda;
145 $event['id_user'] = $obj->id_user_reminder;
146 $event['code'] = $obj->code;
147 $event['label'] = $obj->label;
148 $event['location'] = $obj->location;
149 $event['reminder_date_formated_tzserver'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzserver');
150 $event['event_date_start_formated_tzserver'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzserver');
151 $event['reminder_date_formated'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzuser');
152 $event['event_date_start_formated'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzuser');
153
154 $eventfound[$obj->id_agenda] = $event;
155 }
156 } else {
157 dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
158 }
159}
160
161print json_encode(array('pastreminders'=>$eventfound, 'nextreminder'=>''));
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.