dolibarr 21.0.0-beta
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-2024 Frédéric France <frederic.france@free.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
51//$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late
52$action = GETPOST('action', 'aZ09');
53
54$time = dol_now();
55$listofreminderids = GETPOST('listofreminderids', 'aZ09');
56
57// Security check
58// No permission check at top, but action later are all done with a test on $user->id.
59
60
61/*
62 * Actions
63 */
64
65if ($action == 'stopreminder') { // Test on permission not required here. Endpoint can be called
66 dol_syslog("Clear notification for listofreminderids=".$listofreminderids);
67 $listofreminderid = GETPOST('listofreminderids', 'intcomma');
68
69 // Set the reminder as done
70 $sql = 'UPDATE '.MAIN_DB_PREFIX.'actioncomm_reminder SET status = 1';
71 $sql .= ' WHERE status = 0 AND rowid IN ('.$db->sanitize($db->escape($listofreminderid)).')';
72 $sql .= ' AND fk_user = '.((int) $user->id).' AND entity = '.((int) $conf->entity);
73 $resql = $db->query($sql);
74 if (!$resql) {
75 dol_print_error($db);
76 }
77
78 include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
79
80 // Clean database
81 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'actioncomm_reminder';
82 $sql .= " WHERE dateremind < '".$db->idate(dol_time_plus_duree(dol_now(), -1, 'm'))."'";
83 $sql .= " AND fk_user = ".((int) $user->id).' AND entity = '.((int) $conf->entity);
84 $resql = $db->query($sql);
85 if (!$resql) {
86 dol_print_error($db);
87 }
88
89 exit;
90}
91
92
93/*
94 * View
95 */
96
97top_httphead('application/json');
98
99global $user, $db, $langs, $conf;
100
101$eventfound = array();
102//Uncomment this to force a test
103//$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
104
105// TODO Remove use of $_SESSION['auto_check_events_not_before']. Seems not used.
106if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto_check_events_not_before'] || GETPOSTINT('forcechecknow')) {
107 /*$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
108 if (!empty($_SESSION['auto_check_events_not_before']))
109 {
110 // We start scan from the not before so if two tabs were opened at different moments and we close one (so the js timer),
111 // then we are not losing periods
112 $starttime = $_SESSION['auto_check_events_not_before'];
113 // Protection to avoid too long sessions
114 if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
115 {
116 dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
117 $starttime = $time;
118 }
119 } else {
120 $starttime = $time;
121 }
122
123 $_SESSION['auto_check_events_not_before'] = $time + $time_update;
124 */
125
126 // Force save of the session change we did.
127 // WARNING: Any change in sessions after that will not be saved !
128 session_write_close();
129
130 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
131
132
133 //dol_syslog('$_SESSION[auto_check_events_not_before]='.(empty($_SESSION['auto_check_events_not_before']) ? '' : $_SESSION['auto_check_events_not_before']));
134 dol_syslog('dolnotif_nb_test_for_page='.GETPOST('dolnotif_nb_test_for_page'));
135
136 $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';
137 $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
138 $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
139 $sql .= " AND ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."'";
140 $sql .= " AND ar.status = 0";
141 $sql .= " AND ar.entity = ".((int) $conf->entity); // No sharing of entity for alerts
142 $sql .= $db->order('datep', 'ASC');
143 $sql .= $db->plimit(10); // Avoid too many notification at once
144
145 $resql = $db->query($sql);
146 if ($resql) {
147 while ($obj = $db->fetch_object($resql)) {
148 // Message must be formatted and translated to be used with javascript directly
149 $event = array();
150 $event['type'] = 'agenda';
151 $event['id_reminder'] = $obj->id_reminder;
152 $event['id_agenda'] = $obj->id_agenda;
153 $event['id_user'] = $obj->id_user_reminder;
154 $event['code'] = $obj->code;
155 $event['label'] = $obj->label;
156 $event['location'] = $obj->location;
157 $event['reminder_date_formated_tzserver'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzserver');
158 $event['event_date_start_formated_tzserver'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzserver');
159 $event['reminder_date_formated'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzuser');
160 $event['event_date_start_formated'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzuser');
161
162 $eventfound[$obj->id_agenda] = $event;
163 }
164 } else {
165 dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
166 }
167}
168
169print 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
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79